queryparser-python3 0.6.1__py3-none-any.whl → 0.7.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,6 +1,6 @@
1
- # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.11.1
1
+ # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.13.1
2
2
  from antlr4 import *
3
- if __name__ is not None and "." in __name__:
3
+ if "." in __name__:
4
4
  from .ADQLParser import ADQLParser
5
5
  else:
6
6
  from ADQLParser import ADQLParser
@@ -143,15 +143,6 @@ class ADQLParserListener(ParseTreeListener):
143
143
  pass
144
144
 
145
145
 
146
- # Enter a parse tree produced by ADQLParser#centroid.
147
- def enterCentroid(self, ctx:ADQLParser.CentroidContext):
148
- pass
149
-
150
- # Exit a parse tree produced by ADQLParser#centroid.
151
- def exitCentroid(self, ctx:ADQLParser.CentroidContext):
152
- pass
153
-
154
-
155
146
  # Enter a parse tree produced by ADQLParser#char_function.
156
147
  def enterChar_function(self, ctx:ADQLParser.Char_functionContext):
157
148
  pass
@@ -188,6 +179,15 @@ class ADQLParserListener(ParseTreeListener):
188
179
  pass
189
180
 
190
181
 
182
+ # Enter a parse tree produced by ADQLParser#circle_center.
183
+ def enterCircle_center(self, ctx:ADQLParser.Circle_centerContext):
184
+ pass
185
+
186
+ # Exit a parse tree produced by ADQLParser#circle_center.
187
+ def exitCircle_center(self, ctx:ADQLParser.Circle_centerContext):
188
+ pass
189
+
190
+
191
191
  # Enter a parse tree produced by ADQLParser#column_name.
192
192
  def enterColumn_name(self, ctx:ADQLParser.Column_nameContext):
193
193
  pass
@@ -251,6 +251,15 @@ class ADQLParserListener(ParseTreeListener):
251
251
  pass
252
252
 
253
253
 
254
+ # Enter a parse tree produced by ADQLParser#contains_predicate.
255
+ def enterContains_predicate(self, ctx:ADQLParser.Contains_predicateContext):
256
+ pass
257
+
258
+ # Exit a parse tree produced by ADQLParser#contains_predicate.
259
+ def exitContains_predicate(self, ctx:ADQLParser.Contains_predicateContext):
260
+ pass
261
+
262
+
254
263
  # Enter a parse tree produced by ADQLParser#coord_sys.
255
264
  def enterCoord_sys(self, ctx:ADQLParser.Coord_sysContext):
256
265
  pass
@@ -269,24 +278,6 @@ class ADQLParserListener(ParseTreeListener):
269
278
  pass
270
279
 
271
280
 
272
- # Enter a parse tree produced by ADQLParser#coord1.
273
- def enterCoord1(self, ctx:ADQLParser.Coord1Context):
274
- pass
275
-
276
- # Exit a parse tree produced by ADQLParser#coord1.
277
- def exitCoord1(self, ctx:ADQLParser.Coord1Context):
278
- pass
279
-
280
-
281
- # Enter a parse tree produced by ADQLParser#coord2.
282
- def enterCoord2(self, ctx:ADQLParser.Coord2Context):
283
- pass
284
-
285
- # Exit a parse tree produced by ADQLParser#coord2.
286
- def exitCoord2(self, ctx:ADQLParser.Coord2Context):
287
- pass
288
-
289
-
290
281
  # Enter a parse tree produced by ADQLParser#coordinate1.
291
282
  def enterCoordinate1(self, ctx:ADQLParser.Coordinate1Context):
292
283
  pass
@@ -386,15 +377,6 @@ class ADQLParserListener(ParseTreeListener):
386
377
  pass
387
378
 
388
379
 
389
- # Enter a parse tree produced by ADQLParser#extract_coordsys.
390
- def enterExtract_coordsys(self, ctx:ADQLParser.Extract_coordsysContext):
391
- pass
392
-
393
- # Exit a parse tree produced by ADQLParser#extract_coordsys.
394
- def exitExtract_coordsys(self, ctx:ADQLParser.Extract_coordsysContext):
395
- pass
396
-
397
-
398
380
  # Enter a parse tree produced by ADQLParser#factor.
399
381
  def enterFactor(self, ctx:ADQLParser.FactorContext):
400
382
  pass
@@ -521,6 +503,15 @@ class ADQLParserListener(ParseTreeListener):
521
503
  pass
522
504
 
523
505
 
506
+ # Enter a parse tree produced by ADQLParser#intersects_predicate.
507
+ def enterIntersects_predicate(self, ctx:ADQLParser.Intersects_predicateContext):
508
+ pass
509
+
510
+ # Exit a parse tree produced by ADQLParser#intersects_predicate.
511
+ def exitIntersects_predicate(self, ctx:ADQLParser.Intersects_predicateContext):
512
+ pass
513
+
514
+
524
515
  # Enter a parse tree produced by ADQLParser#join_column_list.
525
516
  def enterJoin_column_list(self, ctx:ADQLParser.Join_column_listContext):
526
517
  pass
@@ -737,6 +728,15 @@ class ADQLParserListener(ParseTreeListener):
737
728
  pass
738
729
 
739
730
 
731
+ # Enter a parse tree produced by ADQLParser#point_value.
732
+ def enterPoint_value(self, ctx:ADQLParser.Point_valueContext):
733
+ pass
734
+
735
+ # Exit a parse tree produced by ADQLParser#point_value.
736
+ def exitPoint_value(self, ctx:ADQLParser.Point_valueContext):
737
+ pass
738
+
739
+
740
740
  # Enter a parse tree produced by ADQLParser#polygon.
741
741
  def enterPolygon(self, ctx:ADQLParser.PolygonContext):
742
742
  pass
@@ -827,15 +827,6 @@ class ADQLParserListener(ParseTreeListener):
827
827
  pass
828
828
 
829
829
 
830
- # Enter a parse tree produced by ADQLParser#region.
831
- def enterRegion(self, ctx:ADQLParser.RegionContext):
832
- pass
833
-
834
- # Exit a parse tree produced by ADQLParser#region.
835
- def exitRegion(self, ctx:ADQLParser.RegionContext):
836
- pass
837
-
838
-
839
830
  # Enter a parse tree produced by ADQLParser#regular_identifier.
840
831
  def enterRegular_identifier(self, ctx:ADQLParser.Regular_identifierContext):
841
832
  pass
@@ -971,15 +962,6 @@ class ADQLParserListener(ParseTreeListener):
971
962
  pass
972
963
 
973
964
 
974
- # Enter a parse tree produced by ADQLParser#string_geometry_function.
975
- def enterString_geometry_function(self, ctx:ADQLParser.String_geometry_functionContext):
976
- pass
977
-
978
- # Exit a parse tree produced by ADQLParser#string_geometry_function.
979
- def exitString_geometry_function(self, ctx:ADQLParser.String_geometry_functionContext):
980
- pass
981
-
982
-
983
965
  # Enter a parse tree produced by ADQLParser#string_value_expression.
984
966
  def enterString_value_expression(self, ctx:ADQLParser.String_value_expressionContext):
985
967
  pass
@@ -1,6 +1,6 @@
1
- # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.11.1
1
+ # Generated from src/queryparser/adql/ADQLParser.g4 by ANTLR 4.13.1
2
2
  from antlr4 import *
3
- if __name__ is not None and "." in __name__:
3
+ if "." in __name__:
4
4
  from .ADQLParser import ADQLParser
5
5
  else:
6
6
  from ADQLParser import ADQLParser
@@ -84,11 +84,6 @@ class ADQLParserVisitor(ParseTreeVisitor):
84
84
  return self.visitChildren(ctx)
85
85
 
86
86
 
87
- # Visit a parse tree produced by ADQLParser#centroid.
88
- def visitCentroid(self, ctx:ADQLParser.CentroidContext):
89
- return self.visitChildren(ctx)
90
-
91
-
92
87
  # Visit a parse tree produced by ADQLParser#char_function.
93
88
  def visitChar_function(self, ctx:ADQLParser.Char_functionContext):
94
89
  return self.visitChildren(ctx)
@@ -109,6 +104,11 @@ class ADQLParserVisitor(ParseTreeVisitor):
109
104
  return self.visitChildren(ctx)
110
105
 
111
106
 
107
+ # Visit a parse tree produced by ADQLParser#circle_center.
108
+ def visitCircle_center(self, ctx:ADQLParser.Circle_centerContext):
109
+ return self.visitChildren(ctx)
110
+
111
+
112
112
  # Visit a parse tree produced by ADQLParser#column_name.
113
113
  def visitColumn_name(self, ctx:ADQLParser.Column_nameContext):
114
114
  return self.visitChildren(ctx)
@@ -144,6 +144,11 @@ class ADQLParserVisitor(ParseTreeVisitor):
144
144
  return self.visitChildren(ctx)
145
145
 
146
146
 
147
+ # Visit a parse tree produced by ADQLParser#contains_predicate.
148
+ def visitContains_predicate(self, ctx:ADQLParser.Contains_predicateContext):
149
+ return self.visitChildren(ctx)
150
+
151
+
147
152
  # Visit a parse tree produced by ADQLParser#coord_sys.
148
153
  def visitCoord_sys(self, ctx:ADQLParser.Coord_sysContext):
149
154
  return self.visitChildren(ctx)
@@ -154,16 +159,6 @@ class ADQLParserVisitor(ParseTreeVisitor):
154
159
  return self.visitChildren(ctx)
155
160
 
156
161
 
157
- # Visit a parse tree produced by ADQLParser#coord1.
158
- def visitCoord1(self, ctx:ADQLParser.Coord1Context):
159
- return self.visitChildren(ctx)
160
-
161
-
162
- # Visit a parse tree produced by ADQLParser#coord2.
163
- def visitCoord2(self, ctx:ADQLParser.Coord2Context):
164
- return self.visitChildren(ctx)
165
-
166
-
167
162
  # Visit a parse tree produced by ADQLParser#coordinate1.
168
163
  def visitCoordinate1(self, ctx:ADQLParser.Coordinate1Context):
169
164
  return self.visitChildren(ctx)
@@ -219,11 +214,6 @@ class ADQLParserVisitor(ParseTreeVisitor):
219
214
  return self.visitChildren(ctx)
220
215
 
221
216
 
222
- # Visit a parse tree produced by ADQLParser#extract_coordsys.
223
- def visitExtract_coordsys(self, ctx:ADQLParser.Extract_coordsysContext):
224
- return self.visitChildren(ctx)
225
-
226
-
227
217
  # Visit a parse tree produced by ADQLParser#factor.
228
218
  def visitFactor(self, ctx:ADQLParser.FactorContext):
229
219
  return self.visitChildren(ctx)
@@ -294,6 +284,11 @@ class ADQLParserVisitor(ParseTreeVisitor):
294
284
  return self.visitChildren(ctx)
295
285
 
296
286
 
287
+ # Visit a parse tree produced by ADQLParser#intersects_predicate.
288
+ def visitIntersects_predicate(self, ctx:ADQLParser.Intersects_predicateContext):
289
+ return self.visitChildren(ctx)
290
+
291
+
297
292
  # Visit a parse tree produced by ADQLParser#join_column_list.
298
293
  def visitJoin_column_list(self, ctx:ADQLParser.Join_column_listContext):
299
294
  return self.visitChildren(ctx)
@@ -414,6 +409,11 @@ class ADQLParserVisitor(ParseTreeVisitor):
414
409
  return self.visitChildren(ctx)
415
410
 
416
411
 
412
+ # Visit a parse tree produced by ADQLParser#point_value.
413
+ def visitPoint_value(self, ctx:ADQLParser.Point_valueContext):
414
+ return self.visitChildren(ctx)
415
+
416
+
417
417
  # Visit a parse tree produced by ADQLParser#polygon.
418
418
  def visitPolygon(self, ctx:ADQLParser.PolygonContext):
419
419
  return self.visitChildren(ctx)
@@ -464,11 +464,6 @@ class ADQLParserVisitor(ParseTreeVisitor):
464
464
  return self.visitChildren(ctx)
465
465
 
466
466
 
467
- # Visit a parse tree produced by ADQLParser#region.
468
- def visitRegion(self, ctx:ADQLParser.RegionContext):
469
- return self.visitChildren(ctx)
470
-
471
-
472
467
  # Visit a parse tree produced by ADQLParser#regular_identifier.
473
468
  def visitRegular_identifier(self, ctx:ADQLParser.Regular_identifierContext):
474
469
  return self.visitChildren(ctx)
@@ -544,11 +539,6 @@ class ADQLParserVisitor(ParseTreeVisitor):
544
539
  return self.visitChildren(ctx)
545
540
 
546
541
 
547
- # Visit a parse tree produced by ADQLParser#string_geometry_function.
548
- def visitString_geometry_function(self, ctx:ADQLParser.String_geometry_functionContext):
549
- return self.visitChildren(ctx)
550
-
551
-
552
542
  # Visit a parse tree produced by ADQLParser#string_value_expression.
553
543
  def visitString_value_expression(self, ctx:ADQLParser.String_value_expressionContext):
554
544
  return self.visitChildren(ctx)
@@ -22,11 +22,52 @@ adql_function_names = ('ABS', 'ACOS', 'ASIN', 'ATAN', 'ATAN2', 'CEILING',
22
22
  'SQRT', 'TAN', 'TRUNCATE')
23
23
 
24
24
 
25
- def _remove_children(ctx):
26
- for i in range(ctx.getChildCount() - 1):
27
- ctx.removeLastChild()
25
+ def _removeFirstChild(ctx):
26
+ if ctx.children is not None:
27
+ del ctx.children[0]
28
28
 
29
29
 
30
+ def _remove_children(ctx, reverse=False):
31
+ for _ in range(ctx.getChildCount() - 1):
32
+ if reverse:
33
+ _removeFirstChild(ctx)
34
+ else:
35
+ ctx.removeLastChild()
36
+
37
+
38
+ def _convert_values(ctx, cidx, output_sql):
39
+ """
40
+ Values inside the ADQL functions can be floats, expressions, or
41
+ strings. Strings need to be treated differently because
42
+ mysql-sphere syntax differs slightly if we pass a column name
43
+ instead of a value.
44
+
45
+ :param ctx:
46
+ antlr context.
47
+
48
+ :param cidx:
49
+ Context child index.
50
+
51
+ """
52
+ vals = []
53
+ for i in ctx.children[cidx].getText().split(','):
54
+ try:
55
+ val = float(i)
56
+ except ValueError:
57
+ try:
58
+ val = float(eval(i))
59
+ except (AttributeError, ValueError, NameError, SyntaxError):
60
+ if output_sql == 'mysql':
61
+ val = '.'.join('`{0}`'.format(v.rstrip("'").
62
+ lstrip("'").
63
+ rstrip('"').lstrip('"'))
64
+ for v in i.split('.'))
65
+ elif output_sql == 'postgresql':
66
+ val = '.'.join('{0}'.format(v)
67
+ for v in i.split('.'))
68
+ vals.append(val)
69
+ return vals
70
+
30
71
  def _process_regular_identifier(ctx_text, sql_output):
31
72
  if sql_output == 'mysql':
32
73
  ri = ctx_text.rstrip("'").lstrip("'").rstrip('"').lstrip('"')
@@ -36,6 +77,20 @@ def _process_regular_identifier(ctx_text, sql_output):
36
77
  else:
37
78
  return ri
38
79
 
80
+ def _get_ancestor_class_node(ctx, ancestor_class, depth=1):
81
+ """ Returns the ancestor node at 'depth' level above the current node if
82
+ the node is of type 'ancestor_class'. Otherwise, returns None
83
+ """
84
+ if not hasattr(ctx, 'parentCtx'):
85
+ return None
86
+
87
+ if depth > 1:
88
+ return _get_ancestor_class_node(ctx.parentCtx, ancestor_class, depth-1)
89
+ elif depth == 1:
90
+ if isinstance(ctx.parentCtx, ancestor_class):
91
+ return ctx.parentCtx
92
+ return None
93
+
39
94
 
40
95
  class SyntaxErrorListener(ErrorListener):
41
96
  def __init__(self):
@@ -97,9 +152,7 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
97
152
  elif self.output_sql == 'postgresql':
98
153
  val = '.'.join('{0}'.format(v)
99
154
  for v in i.split('.'))
100
-
101
155
  vals.append(val)
102
-
103
156
  return vals
104
157
 
105
158
  def visitRegular_identifier(self, ctx):
@@ -133,8 +186,12 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
133
186
 
134
187
  def visitPoint(self, ctx):
135
188
  coords = []
136
- for j in (2, 4):
137
- coords.extend(self._convert_values(ctx, j))
189
+ if len(ctx.children) > 4:
190
+ for j in (2, 4):
191
+ coords.extend(self._convert_values(ctx, j))
192
+ else:
193
+ coords.extend(self._convert_values(ctx, 2))
194
+
138
195
  if len(coords) == 3:
139
196
  coords = coords[1:]
140
197
 
@@ -144,6 +201,11 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
144
201
  elif self.output_sql == 'postgresql':
145
202
  ctx_text = "spoint( %s(%s), %s(%s) )" % (self.conunits, coords[0],
146
203
  self.conunits, coords[1])
204
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
205
+ if derived_column is not None:
206
+ ctx_text = f"spoint_to_array_deg({ctx_text})"
207
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
208
+ ctx_text = f"{ctx_text} AS point"
147
209
  else:
148
210
  ctx_text = ''
149
211
 
@@ -151,22 +213,27 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
151
213
  self.contexts[ctx] = ctx_text
152
214
 
153
215
  def visitBox(self, ctx):
154
- s = 4
155
216
  pars = []
156
- for j in range(0, 5, 2):
157
- pars.extend(self._convert_values(ctx, s + j))
217
+ s = 4 if len(ctx.children) > 8 else 2
218
+ pars.extend(self._convert_values(ctx, s))
219
+ pars.extend(self._convert_values(ctx, s+2))
220
+ pars.extend(self._convert_values(ctx, s+4))
158
221
 
159
222
  try:
160
- topright_x = float(pars[0]) + float(pars[2])
161
- topright_y = float(pars[1]) + float(pars[3])
223
+ pos_cent_ra = float(pars[0])
224
+ pos_cent_dec = float(pars[1])
225
+ dra = float(pars[2])/2
226
+ ddec = float(pars[3])/2
162
227
  except ValueError:
163
228
  raise QueryError('sbox values incorrect')
164
229
 
165
230
  if self.output_sql in ('mysql', 'postgresql'):
166
231
  ctx_text = "sbox( spoint(%s(%s),%s(%s)),spoint(%s(%s),%s(%s)) )" %\
167
- (self.conunits, pars[0], self.conunits, pars[1],
168
- self.conunits, '%.12f' % topright_x, self.conunits,
169
- '%.12f' % topright_y)
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))
236
+
170
237
  else:
171
238
  ctx_text = ''
172
239
 
@@ -174,32 +241,47 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
174
241
  self.contexts[ctx] = ctx_text
175
242
 
176
243
  def visitCircle(self, ctx):
177
- s = 4
178
- pars = []
179
- for j in range(0, 3, 2):
180
- pars.extend(self._convert_values(ctx, s + j))
181
-
244
+ ctx_text = ''
182
245
  if self.output_sql in ('mysql', 'postgresql'):
183
- ctx_text = "scircle( spoint(%s(%s), %s(%s)), %s(%s) )" %\
184
- (self.conunits, pars[0], self.conunits, pars[1],
185
- self.conunits, pars[2])
186
- else:
187
- ctx_text = ''
188
-
246
+ s = 4 if isinstance(ctx.children[2], ADQLParser.Coord_sysContext) else 2
247
+ radius = self._convert_values(ctx, s+2)[0]
248
+ circle_center = ctx.children[s]
249
+ if isinstance(circle_center.children[0], ADQLParser.CoordinatesContext):
250
+ 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
+ else:
255
+ point_ctx = circle_center.children[0].children[0].children[0]
256
+ if isinstance(point_ctx, ADQLParser.PointContext):
257
+ self.visitPoint(point_ctx)
258
+ point_ctx_text = self.contexts[point_ctx]
259
+ else:
260
+ raise QueryError('In the current implementation, circle ' +
261
+ 'allows only explicitly defined point as ' +
262
+ 'the circle center. For instance, ' +
263
+ 'CIRCLE(POINT(t.ra, r.dec), 0.1)')
264
+
265
+ ctx_text = "scircle( %s, %s(%s) )" %\
266
+ (point_ctx_text, self.conunits, radius)
267
+ if self.output_sql == 'postgresql':
268
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
269
+ if derived_column is not None:
270
+ ctx_text = f"scircle_to_array_deg({ctx_text})"
271
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
272
+ ctx_text = f"{ctx_text} AS circle"
189
273
  _remove_children(ctx)
190
274
  self.contexts[ctx] = ctx_text
191
275
 
192
276
  def visitPolygon(self, ctx):
193
- s = 4
194
-
195
- j = 0
196
277
  pars = []
197
- while True:
198
- try:
199
- pars.append(self._convert_values(ctx, s + j))
200
- s += 2
201
- except IndexError:
202
- break
278
+
279
+ for j in range(2, len(ctx.children), 2):
280
+ par = self._convert_values(ctx, j)
281
+ # only append coordinates
282
+ # (ctx.children[2] can still return coord_sys which is deprecated)
283
+ if len(par) > 1:
284
+ pars.append(par)
203
285
 
204
286
  ustr = ''
205
287
  if self.conunits == "RADIANS":
@@ -210,6 +292,12 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
210
292
  for p in pars:
211
293
  ctx_text += '(%s%s,%s%s),' % (str(p[0]), ustr, str(p[1]), ustr)
212
294
  ctx_text = ctx_text[:-1] + "}')"
295
+ if self.output_sql == 'postgresql':
296
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
297
+ if derived_column is not None:
298
+ ctx_text = f"spoly_to_array_deg({ctx_text})"
299
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
300
+ ctx_text = f"{ctx_text} AS polygon"
213
301
  else:
214
302
  ctx_text = ''
215
303
 
@@ -217,29 +305,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
217
305
  self.contexts[ctx] = ctx_text
218
306
 
219
307
 
220
- class ADQLContainsVisitor(ADQLParserVisitor):
221
- def __init__(self):
222
- self.contains = []
223
-
224
- def visitContains(self, ctx):
225
- if ctx.getText().lower()[:8] == 'contains':
226
- self.contains.append(ctx)
227
-
228
-
229
- class ADQLComparisonPredicateVisitor(ADQLParserVisitor):
230
- # Get rid of "1=" in 1=CONTAINS() statement
231
- def visitComparison_predicate(self, ctx):
232
- contains_visitor = ADQLContainsVisitor()
233
- contains_visitor.visit(ctx)
234
- if len(contains_visitor.contains):
235
- if ctx.children[0].getText().lower()[:8] == 'contains':
236
- ctx.children[1].removeLastChild()
237
- ctx.children[2].removeLastChild()
238
- elif ctx.children[2].getText().lower()[:8] == 'contains':
239
- ctx.children[0].removeLastChild()
240
- ctx.children[1].removeLastChild()
241
-
242
-
243
308
  class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
244
309
  """
245
310
  Run this visitor after the geometry has already been processed.
@@ -255,20 +320,23 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
255
320
  self.conunits = conunits
256
321
 
257
322
  def visitArea(self, ctx):
258
- # arg = self.contexts[ctx.children[2].children[0].children[0]]
259
323
  arg = self.contexts[ctx.children[2].children[0]]
260
-
261
324
  if self.output_sql == 'mysql':
262
325
  ctx_text = 'sarea(%s)' % arg
263
326
  elif self.output_sql == 'postgresql':
264
- ctx_text = 'area(%s)' % arg
327
+ ctx_text = 'square_degrees(area(%s))' % arg
265
328
  else:
266
329
  ctx_text = ''
330
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=9)
331
+ if derived_column is not None:
332
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
333
+ ctx_text = f"{ctx_text} AS adql_area"
267
334
 
268
- for i in range(ctx.getChildCount() - 1):
335
+ for _ in range(ctx.getChildCount() - 1):
269
336
  ctx.removeLastChild()
270
337
  self.contexts[ctx] = ctx_text
271
338
 
339
+ '''
272
340
  def visitCentroid(self, ctx):
273
341
  """
274
342
  Works only for circles.
@@ -280,12 +348,38 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
280
348
  if self.output_sql == 'mysql':
281
349
  ctx_text = 'scenter(%s)' % arg
282
350
  elif self.output_sql == 'postgresql':
283
- ctx_text = 'center(%s)' % arg
351
+ ctx_text = '@@ %s' % arg
284
352
  else:
285
353
  ctx_text = ''
286
354
 
287
355
  _remove_children(ctx)
288
356
  self.contexts[ctx] = ctx_text
357
+ '''
358
+
359
+ def visitContains_predicate(self, ctx):
360
+ comp_value_l = ctx.children[0].getText()
361
+ comp_value_r = ctx.children[2].getText()
362
+ if comp_value_l == '1' or comp_value_l == '0':
363
+ self.visitContains(ctx.children[2])
364
+ ctx_text = self.contexts[ctx.children[2]]
365
+ if self.output_sql == 'mysql':
366
+ ctx_text = f"{comp_value_l} = {ctx_text}"
367
+ elif self.output_sql == 'postgresql' and comp_value_l == '0':
368
+ ctx_text = ctx_text.replace('@', '!@')
369
+ _remove_children(ctx)
370
+ elif comp_value_r == '1' or comp_value_r == '0':
371
+ self.visitContains(ctx.children[0])
372
+ ctx_text = self.contexts[ctx.children[0]]
373
+ if self.output_sql == 'mysql':
374
+ ctx_text = f"{comp_value_r} = {ctx_text}"
375
+ elif self.output_sql == 'postgresql' and comp_value_r == '0':
376
+ ctx_text = ctx_text.replace('@', '!@')
377
+ _remove_children(ctx, reverse=True)
378
+ else:
379
+ raise QueryError('The function CONTAINS allows comparison to 1 or 0 only.')
380
+
381
+ self.contexts[ctx] = ctx_text
382
+
289
383
 
290
384
  def visitContains(self, ctx):
291
385
  arg = (self.contexts[ctx.children[2].children[0]],
@@ -302,25 +396,66 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
302
396
  self.contexts[ctx] = ctx_text
303
397
 
304
398
  def visitDistance(self, ctx):
305
- try:
306
- arg = (self.contexts[ctx.children[2].children[0]],
307
- self.contexts[ctx.children[4].children[0]])
308
- except KeyError:
309
- raise QueryError('Distance in the current implementation is ' +
310
- 'possible only between to explicitly defined' +
311
- 'points.')
399
+ arg = ('', '')
400
+ if isinstance(ctx.children[2],ADQLParser.Coord_valueContext):
401
+ if isinstance(ctx.children[2].children[0].children[0], ADQLParser.PointContext):
402
+ point_ctx1 = ctx.children[2].children[0].children[0]
403
+ point_ctx2 = ctx.children[4].children[0].children[0]
404
+ arg = (self.contexts[point_ctx1],
405
+ self.contexts[point_ctx2])
406
+ else:
407
+ raise QueryError('Distance in the current implementation is ' +
408
+ 'possible only between two explicitly ' +
409
+ 'defined points. For instance, ' +
410
+ 'DISTANCE(POINT(t.ra, r.dec), POINT(0.0, 0.0)) ' +
411
+ 'or DISTANCE(t.ra, r.dec, 0.0, 0.0)')
412
+ else:
413
+ arg = (f"spoint(RADIANS({_convert_values(ctx, 2, self.output_sql)[0]}), " +
414
+ f"RADIANS({_convert_values(ctx, 4, self.output_sql)[0]}))",
415
+ f"spoint(RADIANS({_convert_values(ctx, 6, self.output_sql)[0]}), " +
416
+ f"RADIANS({_convert_values(ctx, 8, self.output_sql)[0]}))")
312
417
 
313
418
  if self.output_sql == 'mysql':
314
419
  ctx_text = '%s(sdist(%s, %s))' % ((self.conunits, ) + arg)
315
420
  elif self.output_sql == 'postgresql':
316
421
  ctx_text = '%s(%s <-> %s)' % ((self.conunits, ) + arg)
422
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=9)
423
+ if derived_column is not None:
424
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
425
+ ctx_text = f"{ctx_text} AS distance"
317
426
  else:
318
427
  ctx_text = ''
319
428
 
320
- for i in range(ctx.getChildCount() - 1):
429
+ for _ in range(ctx.getChildCount() - 1):
321
430
  ctx.removeLastChild()
322
431
  self.contexts[ctx] = ctx_text
323
432
 
433
+
434
+ def visitIntersects_predicate(self, ctx):
435
+ comp_value_l = ctx.children[0].getText()
436
+ comp_value_r = ctx.children[2].getText()
437
+ if comp_value_l == '1' or comp_value_l == '0':
438
+ self.visitIntersects(ctx.children[2])
439
+ ctx_text = self.contexts[ctx.children[2]]
440
+ if self.output_sql == 'mysql':
441
+ ctx_text = f"{comp_value_l} = {ctx_text}"
442
+ elif self.output_sql == 'postgresql' and comp_value_l == '0':
443
+ ctx_text = ctx_text.replace('&&', '!&&')
444
+ _remove_children(ctx)
445
+ elif comp_value_r == '1' or comp_value_r == '0':
446
+ self.visitIntersects(ctx.children[0])
447
+ ctx_text = self.contexts[ctx.children[0]]
448
+ if self.output_sql == 'mysql':
449
+ ctx_text = f"{comp_value_r} = {ctx_text}"
450
+ elif self.output_sql == 'postgresql' and comp_value_r == '0':
451
+ ctx_text = ctx_text.replace('&&', '!&&')
452
+ _remove_children(ctx, reverse=True)
453
+ else:
454
+ raise QueryError('The function INTERSECTS allows comparison to 1 or 0 only.')
455
+
456
+ self.contexts[ctx] = ctx_text
457
+
458
+
324
459
  def visitIntersects(self, ctx):
325
460
  arg = (self.contexts[ctx.children[2].children[0]],
326
461
  self.contexts[ctx.children[4].children[0]])
@@ -547,9 +682,6 @@ class ADQLQueryTranslator(object):
547
682
 
548
683
  self.parse()
549
684
 
550
- comp_visitor = ADQLComparisonPredicateVisitor()
551
- comp_visitor.visit(self.tree)
552
-
553
685
  translator_visitor = ADQLGeometryTranslationVisitor(
554
686
  output_sql='postgresql')
555
687
  translator_visitor.visit(self.tree)