""" Infrastructure for obtaining metadata about on tables and columns controlled by DaCHS. This has the command line interface for dachs info, and the annotation machinery is also used by dachs limits; the common functionality should probably move to rsc at some point (I think it should live with rsc.dbtable). The core here is annotateDBTable. This will gather various pieces of table metadata and fetch column metadata through _annotateColumns. That, in turn, constructs a query fetching the metadata from the database. Since writing this query is a bit involved, it is done in terms of a sequence of AnnotationMakers. These combine SQL making (through OutputFields) and then pulling the results out of the database result (in their annotate methods). The end result is that keys in the columns' annotations dictionaries are added. """ #c Copyright 2008-2025, the GAVO project #c #c This program is free software, covered by the GNU GPL. See the #c COPYING file in the source distribution. import datetime import math import time from gavo import api from gavo import base from gavo import stc from gavo import svcs from gavo import utils from gavo.protocols import scs def getDefaultSamplePercent(tableSize): """returns a hopefully sensible value for samplePercent depending on the tableSize. This is based on the gut feeling that up to 1e6 rows, we can just scan all, whereas for 1e7 50% is fine, and then: 1e8: 20%, 1e9: 10%, 1e10: 5%. I *think* there might be a theory for that. """ if not tableSize: return 100 sf = (math.log10(tableSize)-6) if sf<=0: return 100 return max(int(100*(1/(1+sf**1.5))), 1) class AnnotationMaker: """A class for producing column annotations. An annotation simply is a dictionary with some well-known keys. They are generated from DB queries, and this class orchestrates that by collecting the DB query's result columns pertaining to a column and producing the annotation dictionary from them. To make this happen, it is constructed with the column; then, for each property queried, addPropertyKey is called. Finally, addAnnotation is called with the DB result row (see annotateDBTable) to actually make and attach the dictionary. colRef is where to find the values of the column to annotate. This will generally be the column's name, but you can pass SQL expressions there; this is currently done for timestamps, which are turned into MJDs. """ def __init__(self, column, colRef): self.column = column self.colRef = colRef if not hasattr(self.column, "annotations"): self.column.annotations = {} self.propDests = {} def doesWork(self): """returns a true value if the annotator will contribute a query. """ return self.propDests def getOutputFieldFor(self, propName, propFunc, nameMaker, extractor=None): """returns an OutputField that will generate a propName annotation from the propFunc function. propFunc needs to have {colRef} where the column name (or whatever was passed in as colRef) must be inserted. nameMaker is something like a base.VOTNameMaker. extractor can be a callable receiving the result of propFunc and the annotation dictionary; it the must modify the annotation dictionary to reflect the result (the default is to just add the result under propName). """ destCol = nameMaker.makeName(propName+"_"+self.column.key) self.propDests[destCol] = extractor or propName ofield = api.makeStruct(svcs.OutputField, name=destCol, select=propFunc.format(colRef=self.colRef), type=self.column.type) return ofield def annotate(self, resultRow): """builds an annotation of the column from resultRow. resultRow is a dictionary containing values for all keys registered through addPropertyKey. If the column already has an annotation, only the new keys will be overwritten. """ for srcKey, destKey in self.propDests.items(): if resultRow[srcKey] is None: continue if isinstance(destKey, str): self.column.annotations[destKey] = resultRow[srcKey] else: destKey(self.column.annotations, resultRow[srcKey]) def makeScalarExtractor(td, col, destName): """returns an extractor for a single statistics gatherer. This is an extractor callback for getOutputFieldFor; over just entering the result in the right field in annotations, this does refuses insane stuff. """ if col.type not in ("real", "double precision"): def _(annotations, dbResult): annotations[destName] = dbResult else: def _(annotations, dbResult): res = float(dbResult) if not math.isfinite(res): base.ui.notifyWarning( f"Non-finite stat {dbResult}" f" in {destName} statistics on {td}.{col.name}") return annotations[destName] = dbResult return _ def getAnnotators(td): """returns a pair of output fields and annotators to gather column statistcs for td. The annotators are AnnotationMaker instances that will add the relevant annotations to td's columns. The rules applying are given in annotateDBTable. """ outputFields, annotators = [], [] nameMaker = base.VOTNameMaker() for col in td: if col.getProperty("statistics", None)=="no": continue # TODO: Logic on pre-stat transformations should # really be in per-dbtype type objects that we ought to have one day. if col.type=="timestamp": # PG chose the inverse JD noon convention than MJD, hence the extra 1. colRef=f"(EXTRACT(JULIAN FROM {col.name})-2400001)" else: colRef = col.name annotator = AnnotationMaker(col, colRef) if col.type in base.ORDERED_TYPES or col.type.startswith("char"): outputFields.append( annotator.getOutputFieldFor( "max_value", "MAX({colRef})", nameMaker, extractor=makeScalarExtractor(td, col, "max_value"))) outputFields.append( annotator.getOutputFieldFor( "min_value", "MIN({colRef})", nameMaker, extractor=makeScalarExtractor(td, col, "min_value"))) if col.type in base.NUMERIC_TYPES or col.type=="timestamp": outputFields.append(annotator.getOutputFieldFor("percentiles", "percentile_cont(ARRAY[0.03, 0.5, 0.97]) WITHIN GROUP" " (ORDER BY {colRef})", nameMaker, extractor=lambda annotations, res: annotations.update(dict(zip( ["percentile03", "median", "percentile97"], res))))) # for now, we only do discrete-value stats for strings, not for # integers. If we want to cover integers, too, we'll have to think # about a null literal for them. if (col.type=='text' and ( col.isEnumerated() or col.getProperty("statistics", None)=="enumerate")): outputFields.append(annotator.getOutputFieldFor("discrete_values", "(SELECT jsonb_object_agg(COALESCE(val, 'NULL'), ct) FROM (" " SELECT {colRef} AS val, count(*) AS ct" " FROM {tableName} GROUP BY {colRef}) AS {subqueryName})".format( tableName=td.getQName(), subqueryName="subquery_"+col.name, colRef=colRef), nameMaker, extractor=_normalizeDist)) outputFields.append(annotator.getOutputFieldFor("fill_factor", "AVG(CASE WHEN {colRef} IS NULL THEN 0 ELSE 1 END)", nameMaker)) if annotator.doesWork(): annotators.append(annotator) return outputFields, annotators def _normalizeDist(annotations, dist): """stores dist normalized to sum(dist.values())=1 in annotations[discrete_values] This is probably only useful as the extractor for discrete_values output fields. """ normalizer = sum(dist.values(), 0) if not normalizer: # no metadata detected, which might be benign. return annotations["discrete_values"] = dict((key, val/normalizer) for key, val in dist.items()) def _estimateStatRuntime(rowsInspected): """returns some text guessing at how long a stat run will take based on now many rows will have to be inspected. This assumes not too unreasonable hardware of ~2020; adjust secondsScale over time """ secondsScale = 5e5 if rowsInspected (mincol, maxcol, transformer) expected to raise a NotFoundError if no appropriate columns can be found. This is either getTimeLimitsExprs or getSpectralLimitsExprs at this point. transformer here is a function val -> val turning what's coming back from the database to what's expected by the coverage machinery (e.g., MJD -> jYear). It's conceivable that at some point we'll give multiple intervals, and hence this is an iterator (that can very well yield nothing for a variety of reasons). """ try: minExpr, maxExpr, transformer = columnsDeterminer(td) except base.NotFoundError: return query = "SELECT %s, %s FROM %s"%( str(minExpr), str(maxExpr), td.getQName()) with base.getTableConn() as conn: if not base.UnmanagedQuerier(conn).getTableType(td.getQName()): return for res in conn.query(query): try: if res[0] is not None and res[1] is not None: yield [transformer(res[0]), transformer(res[1])] except ZeroDivisionError: raise base.ReportableError(f"Invalid limits in {td}: {res}") def _format_val(val): """returns a string representation of val for inclusion in our info table. """ return utils.makeEllipsis(utils.safe_str(val), 30) def _format_percent(val): """formats the float val as a percentage. """ return "%.0f%%"%(val*100) _PROP_SEQ = ( ("min_value", _format_val), ("median", _format_val), ("max_value", _format_val), ("fill_factor", _format_percent)) def printTableInfo(td, samplePercent=None): """tries to obtain various information on the properties of the database table described by td. """ annotateDBTable(td, samplePercent=samplePercent) propTable = [("col",)+tuple(p[0] for p in _PROP_SEQ)] for col in td: row = [col.name] for prop, formatter in _PROP_SEQ: if prop in col.annotations: row.append(formatter(col.annotations[prop])) else: row.append("-") propTable.append(tuple(row)) print(utils.formatSimpleTable(propTable)) def estimateTableSize(tableName, connection): """returns an estimate for the size of the table tableName. This is precise for tables postgres guesses are small (currently, 1e6 rows); for larger tables, we round up postgres' estimate. The function will return None for non-existing tables. """ try: estsize = base.UnmanagedQuerier(connection).getRowEstimate(tableName) except KeyError: return None if estsize<1e6: # for small tables, we can be precise; but this will also kick in # for views (which have estsize=0), where this may run forever. # I don't think this can be helped. res = list(connection.query("SELECT COUNT(*) FROM %s"%tableName)) if not res: return None return res[0][0] else: return utils.roundO2M(estsize) def parseCmdline(): from argparse import ArgumentParser parser = ArgumentParser( description="Displays various stats about the table referred to in" " the argument.") parser.add_argument("tableId", help="Table id (of the form rdId#tableId)") parser.add_argument("-m", "--moc-order", type=int, default=None, dest="mocOrder", help="Also print a MOC giving the coverage at MOC_ORDER (use MOC_ORDER=6" " for about 1 deg resolution).", metavar="MOC_ORDER") parser.add_argument("-s", "--sample-percent", type=float, default=None, dest="samplePercent", metavar="P", help="Only look at P percent of the table to determine min/max/mean.") return parser.parse_args() def main(): args = parseCmdline() td = api.getReferencedElement(args.tableId, api.TableDef) printTableInfo(td, args.samplePercent) if args.mocOrder: print("Computing MOC at order %s..."%args.mocOrder) print(getMOCForStdTable(td, args.mocOrder))