queryparser-python3 0.8.0.dev0__py3-none-any.whl → 0.8.1__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.
@@ -1,4 +1,4 @@
1
- # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.13.1
1
+ # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.13.2
2
2
  from antlr4 import *
3
3
  if "." in __name__:
4
4
  from .ADQLParser import ADQLParser
@@ -1,4 +1,4 @@
1
- # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.13.1
1
+ # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.13.2
2
2
  from antlr4 import *
3
3
  if "." in __name__:
4
4
  from .ADQLParser import ADQLParser
@@ -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', 'SIN',
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
- :param conunits:
117
- What should we be converting the units to. If no conversion is
118
- necessary, just pass an empty string.
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, conunits="RADIANS"):
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,11 +201,11 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
197
201
  coords = coords[1:]
198
202
 
199
203
  if self.output_sql == 'mysql':
200
- ctx_text = "spoint( %s(%s), %s(%s) )" % (self.conunits, coords[0],
201
- self.conunits, coords[1])
204
+ ctx_text = "spoint( RADIANS(%s), RADIANS(%s) )" %\
205
+ (coords[0], coords[1])
202
206
  elif self.output_sql == 'postgresql':
203
- ctx_text = "spoint( %s(%s), %s(%s) )" % (self.conunits, coords[0],
204
- self.conunits, coords[1])
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
211
  if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
@@ -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(%s(%s),%s(%s)),spoint(%s(%s),%s(%s)) )" %\
232
- (self.conunits, '%.12f' % (pos_cent_ra - dra),
233
- self.conunits, '%.12f' % (pos_cent_dec - ddec),
234
- self.conunits, '%.12f' % (pos_cent_ra + dra),
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(%s(%s), %s(%s))" %\
252
- (self.conunits, point_parameters[0],
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,8 +264,8 @@ 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, %s(%s) )" %\
266
- (point_ctx_text, self.conunits, radius)
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:
@@ -282,14 +284,11 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
282
284
  if len(par) > 1:
283
285
  pars.append(par)
284
286
 
285
- ustr = ''
286
- if self.conunits == "RADIANS":
287
- ustr = 'd'
288
-
289
287
  if self.output_sql in ('mysql', 'postgresql'):
290
288
  ctx_text = "spoly('{"
291
289
  for p in pars:
292
- ctx_text += '(%s%s,%s%s),' % (str(p[0]), ustr, str(p[1]), ustr)
290
+ # The standard spoly input accepts degree-suffixed values.
291
+ ctx_text += '(%sd,%sd),' % (str(p[0]), str(p[1]))
293
292
  ctx_text = ctx_text[:-1] + "}')"
294
293
  if self.output_sql == 'postgresql':
295
294
  derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
@@ -312,15 +311,33 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
312
311
  the replaced geometry chunks.
313
312
 
314
313
  """
315
- def __init__(self, contexts, output_sql, conunits="DEGREES"):
314
+ def __init__(self, contexts, output_sql):
316
315
  self.contexts = contexts
317
316
  self.output_sql = output_sql
318
- self.conunits = conunits
319
317
 
320
318
  def _get_geometry_value(self, ctx, child_index):
321
319
  value = ctx.children[child_index].children[0]
322
320
  return self.contexts.get(value, value.getText())
323
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)
340
+
324
341
  def visitArea(self, ctx):
325
342
  arg = self._get_geometry_value(ctx, 2)
326
343
  if self.output_sql == 'mysql':
@@ -397,19 +414,28 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
397
414
  self.contexts[ctx] = ctx_text
398
415
 
399
416
  def visitComparison_predicate(self, ctx):
417
+ """Rewrite equivalent DISTANCE thresholds, which require the radius.
418
+
419
+ visitDistance handles the distance function itself; this method owns
420
+ the surrounding operator and radius needed for spoint_dwithin.
421
+ """
400
422
  if self.output_sql != 'postgresql' or ctx.getChildCount() != 3:
401
423
  return self.visitChildren(ctx)
402
424
 
403
425
  left, operator, right = ctx.children
404
- if operator.getText() not in ('<', '<='):
426
+ distance_on_left = operator.getText() in ('<', '<=')
427
+ if distance_on_left:
428
+ distance, radius = left, right
429
+ elif operator.getText() in ('>', '>='):
430
+ distance, radius = right, left
431
+ else:
405
432
  return self.visitChildren(ctx)
406
433
 
407
- distance = left
408
434
  while distance.getChildCount() == 1:
409
435
  distance = distance.children[0]
410
436
  if not isinstance(distance, ADQLParser.DistanceContext):
411
437
  return self.visitChildren(ctx)
412
- if 'DISTANCE' in right.getText().upper():
438
+ if 'DISTANCE' in radius.getText().upper():
413
439
  return self.visitChildren(ctx)
414
440
 
415
441
  ancestor = ctx.parentCtx
@@ -420,8 +446,8 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
420
446
  if ancestor is None:
421
447
  return self.visitChildren(ctx)
422
448
 
423
- _remove_children(ctx)
424
- self.visitDistance(distance, right.getText())
449
+ _remove_children(ctx, reverse=not distance_on_left)
450
+ self.visitDistance(distance, radius.getText())
425
451
 
426
452
  def visitDistance(self, ctx, dwithin_radius=None):
427
453
  arg = ('', '')
@@ -438,21 +464,26 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
438
464
  value.children[0] if isinstance(value, ADQLParser.Point_valueContext) else value,
439
465
  value.getText()) for value in values)
440
466
  else:
441
- arg = (f"spoint(RADIANS({_convert_values(ctx, 2, self.output_sql)[0]}), " +
442
- f"RADIANS({_convert_values(ctx, 4, self.output_sql)[0]}))",
443
- f"spoint(RADIANS({_convert_values(ctx, 6, self.output_sql)[0]}), " +
444
- f"RADIANS({_convert_values(ctx, 8, self.output_sql)[0]}))")
467
+ arg = (
468
+ "spoint(RADIANS(%s), RADIANS(%s))" %
469
+ (_convert_values(ctx, 2, self.output_sql)[0],
470
+ _convert_values(ctx, 4, self.output_sql)[0]),
471
+ "spoint(RADIANS(%s), RADIANS(%s))" %
472
+ (_convert_values(ctx, 6, self.output_sql)[0],
473
+ _convert_values(ctx, 8, self.output_sql)[0]),
474
+ )
445
475
 
446
476
  if self.output_sql == 'mysql':
447
- ctx_text = '%s(sdist(%s, %s))' % ((self.conunits, ) + arg)
477
+ ctx_text = 'DEGREES(sdist(%s, %s))' % arg
448
478
  elif self.output_sql == 'postgresql':
449
- ctx_text = '%s(%s <-> %s)' % ((self.conunits, ) + arg)
479
+ ctx_text = 'DEGREES(%s <-> %s)' % arg
450
480
  derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=9)
451
481
  if derived_column is not None:
452
482
  if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
453
483
  ctx_text = f"{ctx_text} AS distance"
454
484
  if dwithin_radius is not None:
455
- ctx_text = 'spoint_dwithin(%s, %s, RADIANS(%s))' % (arg + (dwithin_radius, ))
485
+ ctx_text = 'spoint_dwithin(%s, %s, %s)' %\
486
+ (arg + ('RADIANS(%s)' % dwithin_radius, ))
456
487
  else:
457
488
  ctx_text = ''
458
489
 
@@ -570,13 +601,16 @@ class FormatListener(ADQLParserListener):
570
601
  pass
571
602
 
572
603
  try:
573
- nd = self.contexts[node.parentCtx]
604
+ nd = self.contexts[node]
574
605
  except KeyError:
575
- nd = node.getText()
576
- if isinstance(node.parentCtx,
577
- ADQLParser.Character_string_literalContext):
578
- if nd == "'":
579
- nd = None
606
+ try:
607
+ nd = self.contexts[node.parentCtx]
608
+ except KeyError:
609
+ nd = node.getText()
610
+ if isinstance(node.parentCtx,
611
+ ADQLParser.Character_string_literalContext):
612
+ if nd == "'":
613
+ nd = None
580
614
 
581
615
  if nd is not None:
582
616
  if isinstance(node.parentCtx, ADQLParser.Set_function_typeContext)\
@@ -712,8 +746,4 @@ class ADQLQueryTranslator(object):
712
746
 
713
747
  translated_query = self.translate(translator_visitor)
714
748
 
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
-
719
749
  return translated_query
@@ -1,4 +1,4 @@
1
- # Generated from src/queryparser/mysql/MySQLLexer.g4 by ANTLR 4.13.1
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.1")
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
@@ -1,4 +1,4 @@
1
- # Generated from src/queryparser/mysql/MySQLParser.g4 by ANTLR 4.13.1
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.1")
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
 
@@ -1,4 +1,4 @@
1
- # Generated from src/queryparser/mysql/MySQLParser.g4 by ANTLR 4.13.1
1
+ # Generated from src/queryparser/mysql/MySQLParser.g4 by ANTLR 4.13.2
2
2
  from antlr4 import *
3
3
  if "." in __name__:
4
4
  from .MySQLParser import MySQLParser