queryparser-python3 0.7.4__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.
@@ -204,7 +204,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
204
204
  self.conunits, coords[1])
205
205
  derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
206
206
  if derived_column is not None:
207
- ctx_text = f"spoint_to_array_deg({ctx_text})"
208
207
  if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
209
208
  ctx_text = f"{ctx_text} AS adql_point"
210
209
  else:
@@ -268,7 +267,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
268
267
  if self.output_sql == 'postgresql':
269
268
  derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
270
269
  if derived_column is not None:
271
- ctx_text = f"scircle_to_array_deg({ctx_text})"
272
270
  if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
273
271
  ctx_text = f"{ctx_text} AS circle"
274
272
  _remove_children(ctx)
@@ -296,7 +294,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
296
294
  if self.output_sql == 'postgresql':
297
295
  derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
298
296
  if derived_column is not None:
299
- ctx_text = f"spoly_to_array_deg({ctx_text})"
300
297
  if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
301
298
  ctx_text = f"{ctx_text} AS adql_polygon"
302
299
  else:
@@ -320,8 +317,12 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
320
317
  self.output_sql = output_sql
321
318
  self.conunits = conunits
322
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
+
323
324
  def visitArea(self, ctx):
324
- arg = self.contexts[ctx.children[2].children[0]]
325
+ arg = self._get_geometry_value(ctx, 2)
325
326
  if self.output_sql == 'mysql':
326
327
  ctx_text = 'sarea(%s)' % arg
327
328
  elif self.output_sql == 'postgresql':
@@ -383,8 +384,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
383
384
 
384
385
 
385
386
  def visitContains(self, ctx):
386
- arg = (self.contexts[ctx.children[2].children[0]],
387
- self.contexts[ctx.children[4].children[0]])
387
+ arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
388
388
 
389
389
  if self.output_sql == 'mysql':
390
390
  ctx_text = 'srcontainsl(%s, %s)' % arg
@@ -396,20 +396,47 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
396
396
  _remove_children(ctx)
397
397
  self.contexts[ctx] = ctx_text
398
398
 
399
- def visitDistance(self, ctx):
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):
400
427
  arg = ('', '')
401
- if isinstance(ctx.children[2],ADQLParser.Coord_valueContext):
402
- if isinstance(ctx.children[2].children[0].children[0], ADQLParser.PointContext):
403
- point_ctx1 = ctx.children[2].children[0].children[0]
404
- point_ctx2 = ctx.children[4].children[0].children[0]
405
- arg = (self.contexts[point_ctx1],
406
- self.contexts[point_ctx2])
407
- 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):
408
432
  raise QueryError('Distance in the current implementation is ' +
409
433
  'possible only between two explicitly ' +
410
434
  'defined points. For instance, ' +
411
435
  'DISTANCE(POINT(t.ra, r.dec), POINT(0.0, 0.0)) ' +
412
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)
413
440
  else:
414
441
  arg = (f"spoint(RADIANS({_convert_values(ctx, 2, self.output_sql)[0]}), " +
415
442
  f"RADIANS({_convert_values(ctx, 4, self.output_sql)[0]}))",
@@ -424,6 +451,8 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
424
451
  if derived_column is not None:
425
452
  if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
426
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, ))
427
456
  else:
428
457
  ctx_text = ''
429
458
 
@@ -458,8 +487,7 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
458
487
 
459
488
 
460
489
  def visitIntersects(self, ctx):
461
- arg = (self.contexts[ctx.children[2].children[0]],
462
- self.contexts[ctx.children[4].children[0]])
490
+ arg = (self._get_geometry_value(ctx, 2), self._get_geometry_value(ctx, 4))
463
491
 
464
492
  if self.output_sql == 'mysql':
465
493
  ctx_text = 'soverlaps(%s, %s)' % arg