queryparser-python3 0.7.4__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 +116 -62
- 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.4.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.4.dist-info → queryparser_python3-0.8.0.dist-info}/WHEEL +1 -1
- queryparser/adql/adqlprocessor.py +0 -23
- queryparser_python3-0.7.4.dist-info/RECORD +0 -28
- {queryparser_python3-0.7.4.dist-info → queryparser_python3-0.8.0.dist-info}/licenses/LICENSE +0 -0
- {queryparser_python3-0.7.4.dist-info → queryparser_python3-0.8.0.dist-info}/top_level.txt +0 -0
|
@@ -4,7 +4,6 @@ from __future__ import absolute_import
|
|
|
4
4
|
|
|
5
5
|
import antlr4
|
|
6
6
|
from antlr4.error.ErrorListener import ErrorListener
|
|
7
|
-
import re
|
|
8
7
|
|
|
9
8
|
from .ADQLLexer import ADQLLexer
|
|
10
9
|
from .ADQLParser import ADQLParser
|
|
@@ -18,9 +17,9 @@ from ..exceptions import QueryError, QuerySyntaxError
|
|
|
18
17
|
# between the name and left parenthesis is not allowed and needs to be
|
|
19
18
|
# deleted.
|
|
20
19
|
adql_function_names = ('ABS', 'ACOS', 'ASIN', 'ATAN', 'ATAN2', 'CEILING',
|
|
21
|
-
'COS', 'DEGREES', 'EXP', 'FLOOR', 'LOG', 'LOG10',
|
|
22
|
-
'MOD', 'PI', 'POWER', 'RADIANS', 'RAND',
|
|
23
|
-
'SQRT', 'TAN', 'TRUNCATE')
|
|
20
|
+
'COS', 'COT', 'DEGREES', 'EXP', 'FLOOR', 'LOG', 'LOG10',
|
|
21
|
+
'LN', 'MOD', 'PI', 'POWER', 'RADIANS', 'RAND',
|
|
22
|
+
'SIN', 'SQRT', 'TAN', 'TRUNCATE', 'TRUNC')
|
|
24
23
|
|
|
25
24
|
|
|
26
25
|
def _removeFirstChild(ctx):
|
|
@@ -78,6 +77,7 @@ def _process_regular_identifier(ctx_text, sql_output):
|
|
|
78
77
|
else:
|
|
79
78
|
return ri
|
|
80
79
|
|
|
80
|
+
|
|
81
81
|
def _get_ancestor_class_node(ctx, ancestor_class, depth=1):
|
|
82
82
|
""" Returns the ancestor node at 'depth' level above the current node if
|
|
83
83
|
the node is of type 'ancestor_class'. Otherwise, returns None
|
|
@@ -113,15 +113,15 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
113
113
|
as a hash. This way we can return the hashed string instead the
|
|
114
114
|
token so we effectively translate the rule.
|
|
115
115
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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.
|
|
119
120
|
|
|
120
121
|
"""
|
|
121
|
-
def __init__(self, output_sql
|
|
122
|
+
def __init__(self, output_sql):
|
|
122
123
|
self.contexts = {}
|
|
123
124
|
self.output_sql = output_sql
|
|
124
|
-
self.conunits = conunits
|
|
125
125
|
|
|
126
126
|
def _convert_values(self, ctx, cidx):
|
|
127
127
|
"""
|
|
@@ -163,6 +163,10 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
163
163
|
ri = _process_regular_identifier(ctx.getText(), self.output_sql)
|
|
164
164
|
self.contexts[ctx] = ri
|
|
165
165
|
|
|
166
|
+
def visitDelimited_identifier(self, ctx):
|
|
167
|
+
ri = _process_regular_identifier(ctx.getText(), self.output_sql)
|
|
168
|
+
self.contexts[ctx] = ri
|
|
169
|
+
|
|
166
170
|
def visitSchema_name(self, ctx):
|
|
167
171
|
ri = _process_regular_identifier(ctx.getText(), self.output_sql)
|
|
168
172
|
self.contexts[ctx] = ri
|
|
@@ -197,14 +201,13 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
197
201
|
coords = coords[1:]
|
|
198
202
|
|
|
199
203
|
if self.output_sql == 'mysql':
|
|
200
|
-
ctx_text = "spoint(
|
|
201
|
-
|
|
204
|
+
ctx_text = "spoint( RADIANS(%s), RADIANS(%s) )" %\
|
|
205
|
+
(coords[0], coords[1])
|
|
202
206
|
elif self.output_sql == 'postgresql':
|
|
203
|
-
ctx_text = "spoint(
|
|
204
|
-
|
|
207
|
+
ctx_text = "spoint( RADIANS(%s), RADIANS(%s) )" %\
|
|
208
|
+
(coords[0], coords[1])
|
|
205
209
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
206
210
|
if derived_column is not None:
|
|
207
|
-
ctx_text = f"spoint_to_array_deg({ctx_text})"
|
|
208
211
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
209
212
|
ctx_text = f"{ctx_text} AS adql_point"
|
|
210
213
|
else:
|
|
@@ -229,11 +232,10 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
229
232
|
raise QueryError('sbox values incorrect')
|
|
230
233
|
|
|
231
234
|
if self.output_sql in ('mysql', 'postgresql'):
|
|
232
|
-
ctx_text = "sbox( spoint(
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
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)
|
|
237
239
|
|
|
238
240
|
else:
|
|
239
241
|
ctx_text = ''
|
|
@@ -249,9 +251,8 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
249
251
|
circle_center = ctx.children[s]
|
|
250
252
|
if isinstance(circle_center.children[0], ADQLParser.CoordinatesContext):
|
|
251
253
|
point_parameters = self._convert_values(circle_center, 0)
|
|
252
|
-
point_ctx_text = "spoint(
|
|
253
|
-
(
|
|
254
|
-
self.conunits, point_parameters[1])
|
|
254
|
+
point_ctx_text = "spoint(RADIANS(%s), RADIANS(%s))" %\
|
|
255
|
+
(point_parameters[0], point_parameters[1])
|
|
255
256
|
else:
|
|
256
257
|
point_ctx = circle_center.children[0].children[0].children[0]
|
|
257
258
|
if isinstance(point_ctx, ADQLParser.PointContext):
|
|
@@ -263,12 +264,11 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
263
264
|
'the circle center. For instance, ' +
|
|
264
265
|
'CIRCLE(POINT(t.ra, r.dec), 0.1)')
|
|
265
266
|
|
|
266
|
-
ctx_text = "scircle( %s,
|
|
267
|
-
(point_ctx_text,
|
|
267
|
+
ctx_text = "scircle( %s, RADIANS(%s) )" %\
|
|
268
|
+
(point_ctx_text, radius)
|
|
268
269
|
if self.output_sql == 'postgresql':
|
|
269
270
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
270
271
|
if derived_column is not None:
|
|
271
|
-
ctx_text = f"scircle_to_array_deg({ctx_text})"
|
|
272
272
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
273
273
|
ctx_text = f"{ctx_text} AS circle"
|
|
274
274
|
_remove_children(ctx)
|
|
@@ -284,19 +284,15 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
284
284
|
if len(par) > 1:
|
|
285
285
|
pars.append(par)
|
|
286
286
|
|
|
287
|
-
ustr = ''
|
|
288
|
-
if self.conunits == "RADIANS":
|
|
289
|
-
ustr = 'd'
|
|
290
|
-
|
|
291
287
|
if self.output_sql in ('mysql', 'postgresql'):
|
|
292
288
|
ctx_text = "spoly('{"
|
|
293
289
|
for p in pars:
|
|
294
|
-
|
|
290
|
+
# The standard spoly input accepts degree-suffixed values.
|
|
291
|
+
ctx_text += '(%sd,%sd),' % (str(p[0]), str(p[1]))
|
|
295
292
|
ctx_text = ctx_text[:-1] + "}')"
|
|
296
293
|
if self.output_sql == 'postgresql':
|
|
297
294
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
298
295
|
if derived_column is not None:
|
|
299
|
-
ctx_text = f"spoly_to_array_deg({ctx_text})"
|
|
300
296
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
301
297
|
ctx_text = f"{ctx_text} AS adql_polygon"
|
|
302
298
|
else:
|
|
@@ -315,13 +311,35 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
315
311
|
the replaced geometry chunks.
|
|
316
312
|
|
|
317
313
|
"""
|
|
318
|
-
def __init__(self, contexts, output_sql
|
|
314
|
+
def __init__(self, contexts, output_sql):
|
|
319
315
|
self.contexts = contexts
|
|
320
316
|
self.output_sql = output_sql
|
|
321
|
-
|
|
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)
|
|
322
340
|
|
|
323
341
|
def visitArea(self, ctx):
|
|
324
|
-
arg = self.
|
|
342
|
+
arg = self._get_geometry_value(ctx, 2)
|
|
325
343
|
if self.output_sql == 'mysql':
|
|
326
344
|
ctx_text = 'sarea(%s)' % arg
|
|
327
345
|
elif self.output_sql == 'postgresql':
|
|
@@ -383,8 +401,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
383
401
|
|
|
384
402
|
|
|
385
403
|
def visitContains(self, ctx):
|
|
386
|
-
arg = (self.
|
|
387
|
-
self.contexts[ctx.children[4].children[0]])
|
|
404
|
+
arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
|
|
388
405
|
|
|
389
406
|
if self.output_sql == 'mysql':
|
|
390
407
|
ctx_text = 'srcontainsl(%s, %s)' % arg
|
|
@@ -396,34 +413,73 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
396
413
|
_remove_children(ctx)
|
|
397
414
|
self.contexts[ctx] = ctx_text
|
|
398
415
|
|
|
399
|
-
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):
|
|
400
449
|
arg = ('', '')
|
|
401
|
-
if isinstance(ctx.children[2],ADQLParser.Coord_valueContext):
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
arg = (self.contexts[point_ctx1],
|
|
406
|
-
self.contexts[point_ctx2])
|
|
407
|
-
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):
|
|
408
454
|
raise QueryError('Distance in the current implementation is ' +
|
|
409
455
|
'possible only between two explicitly ' +
|
|
410
456
|
'defined points. For instance, ' +
|
|
411
457
|
'DISTANCE(POINT(t.ra, r.dec), POINT(0.0, 0.0)) ' +
|
|
412
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)
|
|
413
462
|
else:
|
|
414
|
-
arg = (
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
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
|
+
)
|
|
418
471
|
|
|
419
472
|
if self.output_sql == 'mysql':
|
|
420
|
-
ctx_text = '
|
|
473
|
+
ctx_text = 'DEGREES(sdist(%s, %s))' % arg
|
|
421
474
|
elif self.output_sql == 'postgresql':
|
|
422
|
-
ctx_text = '
|
|
475
|
+
ctx_text = 'DEGREES(%s <-> %s)' % arg
|
|
423
476
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=9)
|
|
424
477
|
if derived_column is not None:
|
|
425
478
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
426
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, ))
|
|
427
483
|
else:
|
|
428
484
|
ctx_text = ''
|
|
429
485
|
|
|
@@ -458,8 +514,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
458
514
|
|
|
459
515
|
|
|
460
516
|
def visitIntersects(self, ctx):
|
|
461
|
-
arg = (self.
|
|
462
|
-
self.contexts[ctx.children[4].children[0]])
|
|
517
|
+
arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
|
|
463
518
|
|
|
464
519
|
if self.output_sql == 'mysql':
|
|
465
520
|
ctx_text = 'soverlaps(%s, %s)' % arg
|
|
@@ -542,13 +597,16 @@ class FormatListener(ADQLParserListener):
|
|
|
542
597
|
pass
|
|
543
598
|
|
|
544
599
|
try:
|
|
545
|
-
nd = self.contexts[node
|
|
600
|
+
nd = self.contexts[node]
|
|
546
601
|
except KeyError:
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
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
|
|
552
610
|
|
|
553
611
|
if nd is not None:
|
|
554
612
|
if isinstance(node.parentCtx, ADQLParser.Set_function_typeContext)\
|
|
@@ -684,8 +742,4 @@ class ADQLQueryTranslator(object):
|
|
|
684
742
|
|
|
685
743
|
translated_query = self.translate(translator_visitor)
|
|
686
744
|
|
|
687
|
-
# Translate LOG10 to LOG and LOG to LN. It's not the most elegant solution but it works.
|
|
688
|
-
translated_query = re.sub(r'(?<=[\+\-\*/\(\s,])log\(', 'LN(', translated_query, flags=re.IGNORECASE)
|
|
689
|
-
translated_query = re.sub(r'(?<=[\+\-\*/\(\s,])log10\(', 'LOG(', translated_query, flags=re.IGNORECASE)
|
|
690
|
-
|
|
691
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
|
|