queryparser-python3 0.7.3__py3-none-any.whl → 0.8.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- queryparser/__init__.py +1 -1
- queryparser/adql/ADQLLexer.py +1006 -1003
- queryparser/adql/ADQLParser.py +1125 -1105
- queryparser/adql/ADQLParserListener.py +1 -1
- queryparser/adql/ADQLParserVisitor.py +1 -1
- queryparser/adql/adqltranslator.py +117 -66
- queryparser/mysql/MySQLLexer.py +2 -2
- queryparser/mysql/MySQLParser.py +2 -2
- queryparser/mysql/MySQLParserListener.py +1 -1
- queryparser/postgresql/PostgreSQLLexer.py +959 -946
- queryparser/postgresql/PostgreSQLParser.py +1575 -2170
- queryparser/postgresql/PostgreSQLParserListener.py +1 -82
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dist-info}/METADATA +12 -11
- queryparser_python3-0.8.0.dist-info/RECORD +27 -0
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dist-info}/WHEEL +1 -1
- queryparser_python3-0.7.3.dist-info/RECORD +0 -27
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dist-info}/licenses/LICENSE +0 -0
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dist-info}/top_level.txt +0 -0
|
@@ -17,9 +17,9 @@ from ..exceptions import QueryError, QuerySyntaxError
|
|
|
17
17
|
# between the name and left parenthesis is not allowed and needs to be
|
|
18
18
|
# deleted.
|
|
19
19
|
adql_function_names = ('ABS', 'ACOS', 'ASIN', 'ATAN', 'ATAN2', 'CEILING',
|
|
20
|
-
'COS', 'DEGREES', 'EXP', 'FLOOR', 'LOG', 'LOG10',
|
|
21
|
-
'MOD', 'PI', 'POWER', 'RADIANS', 'RAND',
|
|
22
|
-
'SQRT', 'TAN', 'TRUNCATE')
|
|
20
|
+
'COS', 'COT', 'DEGREES', 'EXP', 'FLOOR', 'LOG', 'LOG10',
|
|
21
|
+
'LN', 'MOD', 'PI', 'POWER', 'RADIANS', 'RAND',
|
|
22
|
+
'SIN', 'SQRT', 'TAN', 'TRUNCATE', 'TRUNC')
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
def _removeFirstChild(ctx):
|
|
@@ -77,6 +77,7 @@ def _process_regular_identifier(ctx_text, sql_output):
|
|
|
77
77
|
else:
|
|
78
78
|
return ri
|
|
79
79
|
|
|
80
|
+
|
|
80
81
|
def _get_ancestor_class_node(ctx, ancestor_class, depth=1):
|
|
81
82
|
""" Returns the ancestor node at 'depth' level above the current node if
|
|
82
83
|
the node is of type 'ancestor_class'. Otherwise, returns None
|
|
@@ -112,15 +113,15 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
112
113
|
as a hash. This way we can return the hashed string instead the
|
|
113
114
|
token so we effectively translate the rule.
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
ADQL geometry coordinates and radii are specified in degrees. Native
|
|
117
|
+
pgSphere constructors use radians, so numeric geometry values are
|
|
118
|
+
converted when they cross into PostgreSQL. Geometry-valued expressions
|
|
119
|
+
are passed through unchanged.
|
|
118
120
|
|
|
119
121
|
"""
|
|
120
|
-
def __init__(self, output_sql
|
|
122
|
+
def __init__(self, output_sql):
|
|
121
123
|
self.contexts = {}
|
|
122
124
|
self.output_sql = output_sql
|
|
123
|
-
self.conunits = conunits
|
|
124
125
|
|
|
125
126
|
def _convert_values(self, ctx, cidx):
|
|
126
127
|
"""
|
|
@@ -162,6 +163,10 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
162
163
|
ri = _process_regular_identifier(ctx.getText(), self.output_sql)
|
|
163
164
|
self.contexts[ctx] = ri
|
|
164
165
|
|
|
166
|
+
def visitDelimited_identifier(self, ctx):
|
|
167
|
+
ri = _process_regular_identifier(ctx.getText(), self.output_sql)
|
|
168
|
+
self.contexts[ctx] = ri
|
|
169
|
+
|
|
165
170
|
def visitSchema_name(self, ctx):
|
|
166
171
|
ri = _process_regular_identifier(ctx.getText(), self.output_sql)
|
|
167
172
|
self.contexts[ctx] = ri
|
|
@@ -196,14 +201,13 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
196
201
|
coords = coords[1:]
|
|
197
202
|
|
|
198
203
|
if self.output_sql == 'mysql':
|
|
199
|
-
ctx_text = "spoint(
|
|
200
|
-
|
|
204
|
+
ctx_text = "spoint( RADIANS(%s), RADIANS(%s) )" %\
|
|
205
|
+
(coords[0], coords[1])
|
|
201
206
|
elif self.output_sql == 'postgresql':
|
|
202
|
-
ctx_text = "spoint(
|
|
203
|
-
|
|
207
|
+
ctx_text = "spoint( RADIANS(%s), RADIANS(%s) )" %\
|
|
208
|
+
(coords[0], coords[1])
|
|
204
209
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
205
210
|
if derived_column is not None:
|
|
206
|
-
ctx_text = f"spoint_to_array_deg({ctx_text})"
|
|
207
211
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
208
212
|
ctx_text = f"{ctx_text} AS adql_point"
|
|
209
213
|
else:
|
|
@@ -228,11 +232,10 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
228
232
|
raise QueryError('sbox values incorrect')
|
|
229
233
|
|
|
230
234
|
if self.output_sql in ('mysql', 'postgresql'):
|
|
231
|
-
ctx_text = "sbox( spoint(
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
self.conunits, '%.12f' % (pos_cent_dec + ddec))
|
|
235
|
+
ctx_text = "sbox( spoint(RADIANS(%r),RADIANS(%r))," \
|
|
236
|
+
"spoint(RADIANS(%r),RADIANS(%r)) )" %\
|
|
237
|
+
(pos_cent_ra - dra, pos_cent_dec - ddec,
|
|
238
|
+
pos_cent_ra + dra, pos_cent_dec + ddec)
|
|
236
239
|
|
|
237
240
|
else:
|
|
238
241
|
ctx_text = ''
|
|
@@ -248,9 +251,8 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
248
251
|
circle_center = ctx.children[s]
|
|
249
252
|
if isinstance(circle_center.children[0], ADQLParser.CoordinatesContext):
|
|
250
253
|
point_parameters = self._convert_values(circle_center, 0)
|
|
251
|
-
point_ctx_text = "spoint(
|
|
252
|
-
(
|
|
253
|
-
self.conunits, point_parameters[1])
|
|
254
|
+
point_ctx_text = "spoint(RADIANS(%s), RADIANS(%s))" %\
|
|
255
|
+
(point_parameters[0], point_parameters[1])
|
|
254
256
|
else:
|
|
255
257
|
point_ctx = circle_center.children[0].children[0].children[0]
|
|
256
258
|
if isinstance(point_ctx, ADQLParser.PointContext):
|
|
@@ -262,12 +264,11 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
262
264
|
'the circle center. For instance, ' +
|
|
263
265
|
'CIRCLE(POINT(t.ra, r.dec), 0.1)')
|
|
264
266
|
|
|
265
|
-
ctx_text = "scircle( %s,
|
|
266
|
-
(point_ctx_text,
|
|
267
|
+
ctx_text = "scircle( %s, RADIANS(%s) )" %\
|
|
268
|
+
(point_ctx_text, radius)
|
|
267
269
|
if self.output_sql == 'postgresql':
|
|
268
270
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
269
271
|
if derived_column is not None:
|
|
270
|
-
ctx_text = f"scircle_to_array_deg({ctx_text})"
|
|
271
272
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
272
273
|
ctx_text = f"{ctx_text} AS circle"
|
|
273
274
|
_remove_children(ctx)
|
|
@@ -283,19 +284,15 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
283
284
|
if len(par) > 1:
|
|
284
285
|
pars.append(par)
|
|
285
286
|
|
|
286
|
-
ustr = ''
|
|
287
|
-
if self.conunits == "RADIANS":
|
|
288
|
-
ustr = 'd'
|
|
289
|
-
|
|
290
287
|
if self.output_sql in ('mysql', 'postgresql'):
|
|
291
288
|
ctx_text = "spoly('{"
|
|
292
289
|
for p in pars:
|
|
293
|
-
|
|
290
|
+
# The standard spoly input accepts degree-suffixed values.
|
|
291
|
+
ctx_text += '(%sd,%sd),' % (str(p[0]), str(p[1]))
|
|
294
292
|
ctx_text = ctx_text[:-1] + "}')"
|
|
295
293
|
if self.output_sql == 'postgresql':
|
|
296
294
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
297
295
|
if derived_column is not None:
|
|
298
|
-
ctx_text = f"spoly_to_array_deg({ctx_text})"
|
|
299
296
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
300
297
|
ctx_text = f"{ctx_text} AS adql_polygon"
|
|
301
298
|
else:
|
|
@@ -314,13 +311,35 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
314
311
|
the replaced geometry chunks.
|
|
315
312
|
|
|
316
313
|
"""
|
|
317
|
-
def __init__(self, contexts, output_sql
|
|
314
|
+
def __init__(self, contexts, output_sql):
|
|
318
315
|
self.contexts = contexts
|
|
319
316
|
self.output_sql = output_sql
|
|
320
|
-
|
|
317
|
+
|
|
318
|
+
def _get_geometry_value(self, ctx, child_index):
|
|
319
|
+
value = ctx.children[child_index].children[0]
|
|
320
|
+
return self.contexts.get(value, value.getText())
|
|
321
|
+
|
|
322
|
+
def visitMath_function(self, ctx):
|
|
323
|
+
self.visitChildren(ctx)
|
|
324
|
+
|
|
325
|
+
if self.output_sql != 'postgresql':
|
|
326
|
+
return
|
|
327
|
+
|
|
328
|
+
function_name = ctx.children[0].getText().upper()
|
|
329
|
+
replacement = {
|
|
330
|
+
'LOG': 'LN',
|
|
331
|
+
'LOG10': 'LOG',
|
|
332
|
+
'TRUNCATE': 'trunc',
|
|
333
|
+
}.get(function_name)
|
|
334
|
+
|
|
335
|
+
if replacement is not None:
|
|
336
|
+
self.contexts[ctx.children[0]] = replacement
|
|
337
|
+
elif function_name == 'RAND':
|
|
338
|
+
self.contexts[ctx] = 'random()'
|
|
339
|
+
_remove_children(ctx)
|
|
321
340
|
|
|
322
341
|
def visitArea(self, ctx):
|
|
323
|
-
arg = self.
|
|
342
|
+
arg = self._get_geometry_value(ctx, 2)
|
|
324
343
|
if self.output_sql == 'mysql':
|
|
325
344
|
ctx_text = 'sarea(%s)' % arg
|
|
326
345
|
elif self.output_sql == 'postgresql':
|
|
@@ -382,8 +401,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
382
401
|
|
|
383
402
|
|
|
384
403
|
def visitContains(self, ctx):
|
|
385
|
-
arg = (self.
|
|
386
|
-
self.contexts[ctx.children[4].children[0]])
|
|
404
|
+
arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
|
|
387
405
|
|
|
388
406
|
if self.output_sql == 'mysql':
|
|
389
407
|
ctx_text = 'srcontainsl(%s, %s)' % arg
|
|
@@ -395,34 +413,73 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
395
413
|
_remove_children(ctx)
|
|
396
414
|
self.contexts[ctx] = ctx_text
|
|
397
415
|
|
|
398
|
-
def
|
|
416
|
+
def visitComparison_predicate(self, ctx):
|
|
417
|
+
"""Rewrite DISTANCE thresholds, which require the comparison's radius.
|
|
418
|
+
|
|
419
|
+
visitDistance handles the distance function itself; this method owns
|
|
420
|
+
the surrounding operator and radius needed for spoint_dwithin.
|
|
421
|
+
"""
|
|
422
|
+
if self.output_sql != 'postgresql' or ctx.getChildCount() != 3:
|
|
423
|
+
return self.visitChildren(ctx)
|
|
424
|
+
|
|
425
|
+
left, operator, right = ctx.children
|
|
426
|
+
if operator.getText() not in ('<', '<='):
|
|
427
|
+
return self.visitChildren(ctx)
|
|
428
|
+
|
|
429
|
+
distance = left
|
|
430
|
+
while distance.getChildCount() == 1:
|
|
431
|
+
distance = distance.children[0]
|
|
432
|
+
if not isinstance(distance, ADQLParser.DistanceContext):
|
|
433
|
+
return self.visitChildren(ctx)
|
|
434
|
+
if 'DISTANCE' in right.getText().upper():
|
|
435
|
+
return self.visitChildren(ctx)
|
|
436
|
+
|
|
437
|
+
ancestor = ctx.parentCtx
|
|
438
|
+
while ancestor is not None and not isinstance(
|
|
439
|
+
ancestor, (ADQLParser.Where_clauseContext,
|
|
440
|
+
ADQLParser.Join_conditionContext)):
|
|
441
|
+
ancestor = ancestor.parentCtx
|
|
442
|
+
if ancestor is None:
|
|
443
|
+
return self.visitChildren(ctx)
|
|
444
|
+
|
|
445
|
+
_remove_children(ctx)
|
|
446
|
+
self.visitDistance(distance, right.getText())
|
|
447
|
+
|
|
448
|
+
def visitDistance(self, ctx, dwithin_radius=None):
|
|
399
449
|
arg = ('', '')
|
|
400
|
-
if isinstance(ctx.children[2],ADQLParser.Coord_valueContext):
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
arg = (self.contexts[point_ctx1],
|
|
405
|
-
self.contexts[point_ctx2])
|
|
406
|
-
else:
|
|
450
|
+
if isinstance(ctx.children[2], ADQLParser.Coord_valueContext):
|
|
451
|
+
values = (ctx.children[2].children[0], ctx.children[4].children[0])
|
|
452
|
+
if self.output_sql != 'postgresql' and not all(
|
|
453
|
+
isinstance(value, ADQLParser.Point_valueContext) for value in values):
|
|
407
454
|
raise QueryError('Distance in the current implementation is ' +
|
|
408
455
|
'possible only between two explicitly ' +
|
|
409
456
|
'defined points. For instance, ' +
|
|
410
457
|
'DISTANCE(POINT(t.ra, r.dec), POINT(0.0, 0.0)) ' +
|
|
411
458
|
'or DISTANCE(t.ra, r.dec, 0.0, 0.0)')
|
|
459
|
+
arg = tuple(self.contexts.get(
|
|
460
|
+
value.children[0] if isinstance(value, ADQLParser.Point_valueContext) else value,
|
|
461
|
+
value.getText()) for value in values)
|
|
412
462
|
else:
|
|
413
|
-
arg = (
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
463
|
+
arg = (
|
|
464
|
+
"spoint(RADIANS(%s), RADIANS(%s))" %
|
|
465
|
+
(_convert_values(ctx, 2, self.output_sql)[0],
|
|
466
|
+
_convert_values(ctx, 4, self.output_sql)[0]),
|
|
467
|
+
"spoint(RADIANS(%s), RADIANS(%s))" %
|
|
468
|
+
(_convert_values(ctx, 6, self.output_sql)[0],
|
|
469
|
+
_convert_values(ctx, 8, self.output_sql)[0]),
|
|
470
|
+
)
|
|
417
471
|
|
|
418
472
|
if self.output_sql == 'mysql':
|
|
419
|
-
ctx_text = '
|
|
473
|
+
ctx_text = 'DEGREES(sdist(%s, %s))' % arg
|
|
420
474
|
elif self.output_sql == 'postgresql':
|
|
421
|
-
ctx_text = '
|
|
475
|
+
ctx_text = 'DEGREES(%s <-> %s)' % arg
|
|
422
476
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=9)
|
|
423
477
|
if derived_column is not None:
|
|
424
478
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
425
479
|
ctx_text = f"{ctx_text} AS distance"
|
|
480
|
+
if dwithin_radius is not None:
|
|
481
|
+
ctx_text = 'spoint_dwithin(%s, %s, %s)' %\
|
|
482
|
+
(arg + ('RADIANS(%s)' % dwithin_radius, ))
|
|
426
483
|
else:
|
|
427
484
|
ctx_text = ''
|
|
428
485
|
|
|
@@ -457,8 +514,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
457
514
|
|
|
458
515
|
|
|
459
516
|
def visitIntersects(self, ctx):
|
|
460
|
-
arg = (self.
|
|
461
|
-
self.contexts[ctx.children[4].children[0]])
|
|
517
|
+
arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
|
|
462
518
|
|
|
463
519
|
if self.output_sql == 'mysql':
|
|
464
520
|
ctx_text = 'soverlaps(%s, %s)' % arg
|
|
@@ -470,15 +526,6 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
470
526
|
_remove_children(ctx)
|
|
471
527
|
self.contexts[ctx] = ctx_text
|
|
472
528
|
|
|
473
|
-
def visitMath_function(self, ctx):
|
|
474
|
-
ctx_text = ctx.getText()
|
|
475
|
-
if self.output_sql == 'postgresql' and ctx_text[:5].lower() == 'log10':
|
|
476
|
-
_remove_children(ctx)
|
|
477
|
-
self.contexts[ctx] = 'LOG' + ctx_text[5:]
|
|
478
|
-
elif self.output_sql == 'postgresql' and ctx_text[:3].lower() == 'log':
|
|
479
|
-
_remove_children(ctx)
|
|
480
|
-
self.contexts[ctx] = 'LN' + ctx_text[3:]
|
|
481
|
-
|
|
482
529
|
|
|
483
530
|
class SelectQueryListener(ADQLParserListener):
|
|
484
531
|
def __init__(self):
|
|
@@ -550,13 +597,16 @@ class FormatListener(ADQLParserListener):
|
|
|
550
597
|
pass
|
|
551
598
|
|
|
552
599
|
try:
|
|
553
|
-
nd = self.contexts[node
|
|
600
|
+
nd = self.contexts[node]
|
|
554
601
|
except KeyError:
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
602
|
+
try:
|
|
603
|
+
nd = self.contexts[node.parentCtx]
|
|
604
|
+
except KeyError:
|
|
605
|
+
nd = node.getText()
|
|
606
|
+
if isinstance(node.parentCtx,
|
|
607
|
+
ADQLParser.Character_string_literalContext):
|
|
608
|
+
if nd == "'":
|
|
609
|
+
nd = None
|
|
560
610
|
|
|
561
611
|
if nd is not None:
|
|
562
612
|
if isinstance(node.parentCtx, ADQLParser.Set_function_typeContext)\
|
|
@@ -691,4 +741,5 @@ class ADQLQueryTranslator(object):
|
|
|
691
741
|
translator_visitor.visit(self.tree)
|
|
692
742
|
|
|
693
743
|
translated_query = self.translate(translator_visitor)
|
|
744
|
+
|
|
694
745
|
return translated_query
|
queryparser/mysql/MySQLLexer.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Generated from src/queryparser/mysql/MySQLLexer.g4 by ANTLR 4.13.
|
|
1
|
+
# Generated from src/queryparser/mysql/MySQLLexer.g4 by ANTLR 4.13.2
|
|
2
2
|
from antlr4 import *
|
|
3
3
|
from io import StringIO
|
|
4
4
|
import sys
|
|
@@ -4984,7 +4984,7 @@ class MySQLLexer(Lexer):
|
|
|
4984
4984
|
|
|
4985
4985
|
def __init__(self, input=None, output:TextIO = sys.stdout):
|
|
4986
4986
|
super().__init__(input, output)
|
|
4987
|
-
self.checkVersion("4.13.
|
|
4987
|
+
self.checkVersion("4.13.2")
|
|
4988
4988
|
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
|
|
4989
4989
|
self._actions = None
|
|
4990
4990
|
self._predicates = None
|
queryparser/mysql/MySQLParser.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Generated from src/queryparser/mysql/MySQLParser.g4 by ANTLR 4.13.
|
|
1
|
+
# Generated from src/queryparser/mysql/MySQLParser.g4 by ANTLR 4.13.2
|
|
2
2
|
# encoding: utf-8
|
|
3
3
|
from antlr4 import *
|
|
4
4
|
from io import StringIO
|
|
@@ -1465,7 +1465,7 @@ class MySQLParser ( Parser ):
|
|
|
1465
1465
|
|
|
1466
1466
|
def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
|
|
1467
1467
|
super().__init__(input, output)
|
|
1468
|
-
self.checkVersion("4.13.
|
|
1468
|
+
self.checkVersion("4.13.2")
|
|
1469
1469
|
self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache)
|
|
1470
1470
|
self._predicates = None
|
|
1471
1471
|
|