queryparser-python3 0.7.3__py3-none-any.whl → 0.8.0.dev0__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/ADQLParser.py +873 -860
- queryparser/adql/adqltranslator.py +50 -25
- queryparser/postgresql/PostgreSQLLexer.py +908 -901
- queryparser/postgresql/PostgreSQLParser.py +1496 -2096
- queryparser/postgresql/PostgreSQLParserListener.py +0 -81
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dev0.dist-info}/METADATA +7 -8
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dev0.dist-info}/RECORD +11 -11
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dev0.dist-info}/WHEEL +1 -1
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dev0.dist-info}/licenses/LICENSE +0 -0
- {queryparser_python3-0.7.3.dist-info → queryparser_python3-0.8.0.dev0.dist-info}/top_level.txt +0 -0
|
@@ -4,6 +4,7 @@ from __future__ import absolute_import
|
|
|
4
4
|
|
|
5
5
|
import antlr4
|
|
6
6
|
from antlr4.error.ErrorListener import ErrorListener
|
|
7
|
+
import re
|
|
7
8
|
|
|
8
9
|
from .ADQLLexer import ADQLLexer
|
|
9
10
|
from .ADQLParser import ADQLParser
|
|
@@ -203,7 +204,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
203
204
|
self.conunits, coords[1])
|
|
204
205
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
205
206
|
if derived_column is not None:
|
|
206
|
-
ctx_text = f"spoint_to_array_deg({ctx_text})"
|
|
207
207
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
208
208
|
ctx_text = f"{ctx_text} AS adql_point"
|
|
209
209
|
else:
|
|
@@ -267,7 +267,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
267
267
|
if self.output_sql == 'postgresql':
|
|
268
268
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
269
269
|
if derived_column is not None:
|
|
270
|
-
ctx_text = f"scircle_to_array_deg({ctx_text})"
|
|
271
270
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
272
271
|
ctx_text = f"{ctx_text} AS circle"
|
|
273
272
|
_remove_children(ctx)
|
|
@@ -295,7 +294,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
|
|
|
295
294
|
if self.output_sql == 'postgresql':
|
|
296
295
|
derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
|
|
297
296
|
if derived_column is not None:
|
|
298
|
-
ctx_text = f"spoly_to_array_deg({ctx_text})"
|
|
299
297
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
300
298
|
ctx_text = f"{ctx_text} AS adql_polygon"
|
|
301
299
|
else:
|
|
@@ -319,8 +317,12 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
319
317
|
self.output_sql = output_sql
|
|
320
318
|
self.conunits = conunits
|
|
321
319
|
|
|
320
|
+
def _get_geometry_value(self, ctx, child_index):
|
|
321
|
+
value = ctx.children[child_index].children[0]
|
|
322
|
+
return self.contexts.get(value, value.getText())
|
|
323
|
+
|
|
322
324
|
def visitArea(self, ctx):
|
|
323
|
-
arg = self.
|
|
325
|
+
arg = self._get_geometry_value(ctx, 2)
|
|
324
326
|
if self.output_sql == 'mysql':
|
|
325
327
|
ctx_text = 'sarea(%s)' % arg
|
|
326
328
|
elif self.output_sql == 'postgresql':
|
|
@@ -382,8 +384,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
382
384
|
|
|
383
385
|
|
|
384
386
|
def visitContains(self, ctx):
|
|
385
|
-
arg = (self.
|
|
386
|
-
self.contexts[ctx.children[4].children[0]])
|
|
387
|
+
arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
|
|
387
388
|
|
|
388
389
|
if self.output_sql == 'mysql':
|
|
389
390
|
ctx_text = 'srcontainsl(%s, %s)' % arg
|
|
@@ -395,20 +396,47 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
395
396
|
_remove_children(ctx)
|
|
396
397
|
self.contexts[ctx] = ctx_text
|
|
397
398
|
|
|
398
|
-
def
|
|
399
|
+
def visitComparison_predicate(self, ctx):
|
|
400
|
+
if self.output_sql != 'postgresql' or ctx.getChildCount() != 3:
|
|
401
|
+
return self.visitChildren(ctx)
|
|
402
|
+
|
|
403
|
+
left, operator, right = ctx.children
|
|
404
|
+
if operator.getText() not in ('<', '<='):
|
|
405
|
+
return self.visitChildren(ctx)
|
|
406
|
+
|
|
407
|
+
distance = left
|
|
408
|
+
while distance.getChildCount() == 1:
|
|
409
|
+
distance = distance.children[0]
|
|
410
|
+
if not isinstance(distance, ADQLParser.DistanceContext):
|
|
411
|
+
return self.visitChildren(ctx)
|
|
412
|
+
if 'DISTANCE' in right.getText().upper():
|
|
413
|
+
return self.visitChildren(ctx)
|
|
414
|
+
|
|
415
|
+
ancestor = ctx.parentCtx
|
|
416
|
+
while ancestor is not None and not isinstance(
|
|
417
|
+
ancestor, (ADQLParser.Where_clauseContext,
|
|
418
|
+
ADQLParser.Join_conditionContext)):
|
|
419
|
+
ancestor = ancestor.parentCtx
|
|
420
|
+
if ancestor is None:
|
|
421
|
+
return self.visitChildren(ctx)
|
|
422
|
+
|
|
423
|
+
_remove_children(ctx)
|
|
424
|
+
self.visitDistance(distance, right.getText())
|
|
425
|
+
|
|
426
|
+
def visitDistance(self, ctx, dwithin_radius=None):
|
|
399
427
|
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:
|
|
428
|
+
if isinstance(ctx.children[2], ADQLParser.Coord_valueContext):
|
|
429
|
+
values = (ctx.children[2].children[0], ctx.children[4].children[0])
|
|
430
|
+
if self.output_sql != 'postgresql' and not all(
|
|
431
|
+
isinstance(value, ADQLParser.Point_valueContext) for value in values):
|
|
407
432
|
raise QueryError('Distance in the current implementation is ' +
|
|
408
433
|
'possible only between two explicitly ' +
|
|
409
434
|
'defined points. For instance, ' +
|
|
410
435
|
'DISTANCE(POINT(t.ra, r.dec), POINT(0.0, 0.0)) ' +
|
|
411
436
|
'or DISTANCE(t.ra, r.dec, 0.0, 0.0)')
|
|
437
|
+
arg = tuple(self.contexts.get(
|
|
438
|
+
value.children[0] if isinstance(value, ADQLParser.Point_valueContext) else value,
|
|
439
|
+
value.getText()) for value in values)
|
|
412
440
|
else:
|
|
413
441
|
arg = (f"spoint(RADIANS({_convert_values(ctx, 2, self.output_sql)[0]}), " +
|
|
414
442
|
f"RADIANS({_convert_values(ctx, 4, self.output_sql)[0]}))",
|
|
@@ -423,6 +451,8 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
423
451
|
if derived_column is not None:
|
|
424
452
|
if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
|
|
425
453
|
ctx_text = f"{ctx_text} AS distance"
|
|
454
|
+
if dwithin_radius is not None:
|
|
455
|
+
ctx_text = 'spoint_dwithin(%s, %s, RADIANS(%s))' % (arg + (dwithin_radius, ))
|
|
426
456
|
else:
|
|
427
457
|
ctx_text = ''
|
|
428
458
|
|
|
@@ -457,8 +487,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
457
487
|
|
|
458
488
|
|
|
459
489
|
def visitIntersects(self, ctx):
|
|
460
|
-
arg = (self.
|
|
461
|
-
self.contexts[ctx.children[4].children[0]])
|
|
490
|
+
arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
|
|
462
491
|
|
|
463
492
|
if self.output_sql == 'mysql':
|
|
464
493
|
ctx_text = 'soverlaps(%s, %s)' % arg
|
|
@@ -470,15 +499,6 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
|
|
|
470
499
|
_remove_children(ctx)
|
|
471
500
|
self.contexts[ctx] = ctx_text
|
|
472
501
|
|
|
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
502
|
|
|
483
503
|
class SelectQueryListener(ADQLParserListener):
|
|
484
504
|
def __init__(self):
|
|
@@ -691,4 +711,9 @@ class ADQLQueryTranslator(object):
|
|
|
691
711
|
translator_visitor.visit(self.tree)
|
|
692
712
|
|
|
693
713
|
translated_query = self.translate(translator_visitor)
|
|
714
|
+
|
|
715
|
+
# Translate LOG10 to LOG and LOG to LN. It's not the most elegant solution but it works.
|
|
716
|
+
translated_query = re.sub(r'(?<=[\+\-\*/\(\s,])log\(', 'LN(', translated_query, flags=re.IGNORECASE)
|
|
717
|
+
translated_query = re.sub(r'(?<=[\+\-\*/\(\s,])log10\(', 'LOG(', translated_query, flags=re.IGNORECASE)
|
|
718
|
+
|
|
694
719
|
return translated_query
|