| 1 |
""" |
| 2 |
Exceptions and helper functions for ADQL processing. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
from gavo import utils |
| 12 |
from functools import reduce |
| 13 |
|
| 14 |
class Error(utils.Error): |
| 15 |
"""A base class for the exceptions from this module. |
| 16 |
""" |
| 17 |
# XXX todo: We should wrap pyparsing ParseExceptions as well. |
| 18 |
pass |
| 19 |
|
| 20 |
class NotImplementedError(Error): |
| 21 |
"""is raised for features we don't (yet) support. |
| 22 |
""" |
| 23 |
|
| 24 |
class ColumnNotFound(Error, utils.NotFoundError): |
| 25 |
"""is raised if a column name cannot be resolved. |
| 26 |
""" |
| 27 |
def __init__(self, colName, hint=None): |
| 28 |
utils.NotFoundError.__init__(self, colName, "column", "table metadata", |
| 29 |
hint=hint) |
| 30 |
|
| 31 |
class TableNotFound(Error, utils.NotFoundError): |
| 32 |
"""is raised when a table name cannot be resolved. |
| 33 |
""" |
| 34 |
def __init__(self, tableName, hint=None): |
| 35 |
utils.NotFoundError.__init__(self, tableName, "table", "table metadata", |
| 36 |
hint=hint) |
| 37 |
|
| 38 |
class MorphError(Error): |
| 39 |
"""is raised when the expectations of the to-ADQL morphers are violated. |
| 40 |
""" |
| 41 |
pass |
| 42 |
|
| 43 |
class AmbiguousColumn(Error): |
| 44 |
"""is raised if a column name matches more than one column in a |
| 45 |
compound query. |
| 46 |
""" |
| 47 |
|
| 48 |
class NoChild(Error): |
| 49 |
"""is raised if a node is asked for a non-existing child. |
| 50 |
""" |
| 51 |
def __init__(self, searchedType, toks): |
| 52 |
self.searchedType, self.toks = searchedType, toks |
| 53 |
|
| 54 |
def __str__(self): |
| 55 |
return "No %s child found in %s"%(self.searchedType, self.toks) |
| 56 |
|
| 57 |
class MoreThanOneChild(NoChild): |
| 58 |
"""is raised if a node is asked for a unique child but has more than |
| 59 |
one. |
| 60 |
""" |
| 61 |
def __str__(self): |
| 62 |
return "Multiple %s children found in %s"%(self.searchedType, |
| 63 |
self.toks) |
| 64 |
|
| 65 |
class BadKeywords(Error): # pragma: no cover |
| 66 |
"""is raised when an ADQL node is constructed with bad keywords. |
| 67 |
|
| 68 |
This is a development help and should not occur in production code. |
| 69 |
""" |
| 70 |
def __str__(self): |
| 71 |
return "Bad keywords: "+utils.safe_str(self.args) |
| 72 |
|
| 73 |
class UfuncError(Error): |
| 74 |
"""is raised if something is wrong with a call to a user defined |
| 75 |
function. |
| 76 |
""" |
| 77 |
|
| 78 |
class GeometryError(Error): |
| 79 |
"""is raised if something is wrong with a geometry. |
| 80 |
""" |
| 81 |
|
| 82 |
class RegionError(GeometryError): |
| 83 |
"""is raised if a region specification is in some way bad. |
| 84 |
""" |
| 85 |
|
| 86 |
class FlattenError(Error): |
| 87 |
"""is raised when something cannot be flattened. |
| 88 |
""" |
| 89 |
|
| 90 |
class IncompatibleTables(Error): |
| 91 |
"""is raised when the operands of a set operation are not deemed |
| 92 |
compatible. |
| 93 |
""" |
| 94 |
|
| 95 |
class Absent(object): |
| 96 |
"""is a sentinel to pass as default to nodes.getChildOfType. |
| 97 |
""" |
| 98 |
|
| 99 |
|
| 100 |
def getUniqueMatch(matches, colName): |
| 101 |
"""returns the only item of matches if there is exactly one, raises an |
| 102 |
appropriate exception if not. |
| 103 |
""" |
| 104 |
if len(matches)==1: |
| 105 |
return matches[0] |
| 106 |
elif not matches: |
| 107 |
raise ColumnNotFound(colName) |
| 108 |
else: |
| 109 |
# Todo: This kind-of, but not quite, compares whether the references |
| 110 |
# actually end up at the same column |
| 111 |
matches = set(matches) |
| 112 |
if len(matches)!=1: |
| 113 |
raise AmbiguousColumn(colName) |
| 114 |
else: |
| 115 |
return matches.pop() |
| 116 |
|
| 117 |
|
| 118 |
def computeCommonColumns(tableNode): |
| 119 |
"""returns a set of column names that only occur once in the result |
| 120 |
table. |
| 121 |
|
| 122 |
For a natural join, that's all column names occurring in all tables, |
| 123 |
for a USING join, that's all names occurring in USING, else it's |
| 124 |
an empty set. |
| 125 |
|
| 126 |
""" |
| 127 |
joinType = getattr(tableNode, "getJoinType", lambda: "CROSS")() |
| 128 |
if joinType=="NATURAL": |
| 129 |
# NATURAL JOIN, collect common names |
| 130 |
return reduce(lambda a,b: a&b, |
| 131 |
[set(t.fieldInfos.columns) for t in tableNode.joinedTables]) |
| 132 |
elif joinType=="USING": |
| 133 |
return set(tableNode.joinSpecification.usingColumns) |
| 134 |
else: # CROSS join, comma, etc. |
| 135 |
return set() |
| 136 |
|
| 137 |
|
| 138 |
class FieldInfoGetter: |
| 139 |
"""An abstract class to retrieve table metadata. |
| 140 |
|
| 141 |
A subclass of this must be passed into adql.parseAnnotating. |
| 142 |
Implementations must fill out the getInfosFor(tableName) method, |
| 143 |
which must return a sequence of (column name, adql.FieldInfo) pairs |
| 144 |
for the named table. |
| 145 |
|
| 146 |
plain strings for table names will be normalised (lowercased). |
| 147 |
""" |
| 148 |
def __init__(self): |
| 149 |
self.extraFieldInfos = {} |
| 150 |
self.cache = {} |
| 151 |
|
| 152 |
def normalizeName(self, tableName): |
| 153 |
if isinstance(tableName, str): |
| 154 |
return tableName.lower() |
| 155 |
elif hasattr(tableName, "getNormalized"): |
| 156 |
# a nodes.TableName, presumably |
| 157 |
return tableName.getNormalized() |
| 158 |
else: |
| 159 |
return tableName |
| 160 |
|
| 161 |
def __call__(self, tableName): |
| 162 |
normalized = self.normalizeName(tableName) |
| 163 |
if normalized in self.extraFieldInfos: |
| 164 |
return self.extraFieldInfos[normalized] |
| 165 |
|
| 166 |
if normalized not in self.cache: |
| 167 |
self.cache[normalized] = self.getInfosFor(normalized) |
| 168 |
if self.cache[normalized] is None: |
| 169 |
raise utils.NotFoundError(str(normalized), "table", |
| 170 |
"system and uploaded tables") |
| 171 |
return self.cache[normalized] |
| 172 |
|
| 173 |
def addExtraFieldInfos(self, tableName, fieldInfos): |
| 174 |
"""adds field infos for tableName. |
| 175 |
|
| 176 |
fieldInfos must be a sequence of (columnName, adql.FieldInfo) pairs. |
| 177 |
|
| 178 |
Note that tableName is normalised to lowercase here. |
| 179 |
""" |
| 180 |
self.extraFieldInfos[self.normalizeName(tableName)] = fieldInfos |
| 181 |
|
| 182 |
def getInfosFor(self, tableName): # pragma: no cover |
| 183 |
raise NotImplementedError("Abstract FieldInfoGetter used!") |