queryparser-python3 0.6.0__py3-none-any.whl → 0.7.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.
@@ -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)
@@ -23,10 +23,43 @@ adql_function_names = ('ABS', 'ACOS', 'ASIN', 'ATAN', 'ATAN2', 'CEILING',
23
23
 
24
24
 
25
25
  def _remove_children(ctx):
26
- for i in range(ctx.getChildCount() - 1):
26
+ for _ in range(ctx.getChildCount() - 1):
27
27
  ctx.removeLastChild()
28
28
 
29
29
 
30
+ def _convert_values(ctx, cidx, output_sql):
31
+ """
32
+ Values inside the ADQL functions can be floats, expressions, or
33
+ strings. Strings need to be treated differently because
34
+ mysql-sphere syntax differs slightly if we pass a column name
35
+ instead of a value.
36
+
37
+ :param ctx:
38
+ antlr context.
39
+
40
+ :param cidx:
41
+ Context child index.
42
+
43
+ """
44
+ vals = []
45
+ for i in ctx.children[cidx].getText().split(','):
46
+ try:
47
+ val = float(i)
48
+ except ValueError:
49
+ try:
50
+ val = float(eval(i))
51
+ except (AttributeError, ValueError, NameError, SyntaxError):
52
+ if output_sql == 'mysql':
53
+ val = '.'.join('`{0}`'.format(v.rstrip("'").
54
+ lstrip("'").
55
+ rstrip('"').lstrip('"'))
56
+ for v in i.split('.'))
57
+ elif output_sql == 'postgresql':
58
+ val = '.'.join('{0}'.format(v)
59
+ for v in i.split('.'))
60
+ vals.append(val)
61
+ return vals
62
+
30
63
  def _process_regular_identifier(ctx_text, sql_output):
31
64
  if sql_output == 'mysql':
32
65
  ri = ctx_text.rstrip("'").lstrip("'").rstrip('"').lstrip('"')
@@ -36,6 +69,20 @@ def _process_regular_identifier(ctx_text, sql_output):
36
69
  else:
37
70
  return ri
38
71
 
72
+ def _get_ancestor_class_node(ctx, ancestor_class, depth=1):
73
+ """ Returns the ancestor node at 'depth' level above the current node if
74
+ the node is of type 'ancestor_class'. Otherwise, returns None
75
+ """
76
+ if not hasattr(ctx, 'parentCtx'):
77
+ return None
78
+
79
+ if depth > 1:
80
+ return _get_ancestor_class_node(ctx.parentCtx, ancestor_class, depth-1)
81
+ elif depth == 1:
82
+ if isinstance(ctx.parentCtx, ancestor_class):
83
+ return ctx.parentCtx
84
+ return None
85
+
39
86
 
40
87
  class SyntaxErrorListener(ErrorListener):
41
88
  def __init__(self):
@@ -97,9 +144,7 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
97
144
  elif self.output_sql == 'postgresql':
98
145
  val = '.'.join('{0}'.format(v)
99
146
  for v in i.split('.'))
100
-
101
147
  vals.append(val)
102
-
103
148
  return vals
104
149
 
105
150
  def visitRegular_identifier(self, ctx):
@@ -133,8 +178,12 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
133
178
 
134
179
  def visitPoint(self, ctx):
135
180
  coords = []
136
- for j in (2, 4):
137
- coords.extend(self._convert_values(ctx, j))
181
+ if len(ctx.children) > 4:
182
+ for j in (2, 4):
183
+ coords.extend(self._convert_values(ctx, j))
184
+ else:
185
+ coords.extend(self._convert_values(ctx, 2))
186
+
138
187
  if len(coords) == 3:
139
188
  coords = coords[1:]
140
189
 
@@ -144,6 +193,11 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
144
193
  elif self.output_sql == 'postgresql':
145
194
  ctx_text = "spoint( %s(%s), %s(%s) )" % (self.conunits, coords[0],
146
195
  self.conunits, coords[1])
196
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
197
+ if derived_column is not None:
198
+ ctx_text = f"spoint_to_array_deg({ctx_text})"
199
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
200
+ ctx_text = f"{ctx_text} AS point"
147
201
  else:
148
202
  ctx_text = ''
149
203
 
@@ -151,22 +205,27 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
151
205
  self.contexts[ctx] = ctx_text
152
206
 
153
207
  def visitBox(self, ctx):
154
- s = 4
155
208
  pars = []
156
- for j in range(0, 5, 2):
157
- pars.extend(self._convert_values(ctx, s + j))
209
+ s = 4 if len(ctx.children) > 8 else 2
210
+ pars.extend(self._convert_values(ctx, s))
211
+ pars.extend(self._convert_values(ctx, s+2))
212
+ pars.extend(self._convert_values(ctx, s+4))
158
213
 
159
214
  try:
160
- topright_x = float(pars[0]) + float(pars[2])
161
- topright_y = float(pars[1]) + float(pars[3])
215
+ pos_cent_ra = float(pars[0])
216
+ pos_cent_dec = float(pars[1])
217
+ dra = float(pars[2])/2
218
+ ddec = float(pars[3])/2
162
219
  except ValueError:
163
220
  raise QueryError('sbox values incorrect')
164
221
 
165
222
  if self.output_sql in ('mysql', 'postgresql'):
166
223
  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)
224
+ (self.conunits, '%.12f' % (pos_cent_ra - dra),
225
+ self.conunits, '%.12f' % (pos_cent_dec - ddec),
226
+ self.conunits, '%.12f' % (pos_cent_ra + dra),
227
+ self.conunits, '%.12f' % (pos_cent_dec + ddec))
228
+
170
229
  else:
171
230
  ctx_text = ''
172
231
 
@@ -174,32 +233,49 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
174
233
  self.contexts[ctx] = ctx_text
175
234
 
176
235
  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
-
236
+ ctx_text = ''
182
237
  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
-
238
+ s = 4 if isinstance(ctx.children[2], ADQLParser.Coord_sysContext) else 2
239
+ radius = self._convert_values(ctx, s+2)[0]
240
+ circle_center = ctx.children[s]
241
+ if isinstance(circle_center.children[0], ADQLParser.CoordinatesContext):
242
+ point_parameters = self._convert_values(circle_center, 0)
243
+ point_ctx_text = "spoint(%s(%s), %s(%s))" %\
244
+ (self.conunits, point_parameters[0],
245
+ self.conunits, point_parameters[1])
246
+ else:
247
+ point_ctx = circle_center.children[0].children[0].children[0]
248
+ if isinstance(point_ctx, ADQLParser.PointContext):
249
+ self.visitPoint(point_ctx)
250
+ point_ctx_text = self.contexts[point_ctx]
251
+ else:
252
+ raise QueryError('In the current implementation, circle ' +
253
+ 'allows only explicitly defined point as ' +
254
+ 'the circle center. For instance, ' +
255
+ 'CIRCLE(POINT(t.ra, r.dec), 0.1)')
256
+
257
+ ctx_text = "scircle( %s, %s(%s) )" %\
258
+ (point_ctx_text, self.conunits, radius)
259
+ if self.output_sql == 'postgresql':
260
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
261
+ if derived_column is not None:
262
+ ctx_text = f"scircle_to_array_deg({ctx_text})"
263
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
264
+ ctx_text = f"{ctx_text} AS circle"
189
265
  _remove_children(ctx)
190
266
  self.contexts[ctx] = ctx_text
191
267
 
192
268
  def visitPolygon(self, ctx):
193
- s = 4
194
-
195
- j = 0
196
269
  pars = []
197
- while True:
198
- try:
199
- pars.append(self._convert_values(ctx, s + j))
200
- s += 2
201
- except IndexError:
202
- break
270
+
271
+ for ch in ctx.children:
272
+ print(ch.getText())
273
+ for j in range(2, len(ctx.children), 2):
274
+ par = self._convert_values(ctx, j)
275
+ # only append coordinates
276
+ # (ctx.children[2] can still return coord_sys which is deprecated)
277
+ if len(par) > 1:
278
+ pars.append(par)
203
279
 
204
280
  ustr = ''
205
281
  if self.conunits == "RADIANS":
@@ -210,6 +286,12 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
210
286
  for p in pars:
211
287
  ctx_text += '(%s%s,%s%s),' % (str(p[0]), ustr, str(p[1]), ustr)
212
288
  ctx_text = ctx_text[:-1] + "}')"
289
+ if self.output_sql == 'postgresql':
290
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=3)
291
+ if derived_column is not None:
292
+ ctx_text = f"spoly_to_array_deg({ctx_text})"
293
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
294
+ ctx_text = f"{ctx_text} AS polygon"
213
295
  else:
214
296
  ctx_text = ''
215
297
 
@@ -217,29 +299,6 @@ class ADQLGeometryTranslationVisitor(ADQLParserVisitor):
217
299
  self.contexts[ctx] = ctx_text
218
300
 
219
301
 
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
302
  class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
244
303
  """
245
304
  Run this visitor after the geometry has already been processed.
@@ -255,20 +314,23 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
255
314
  self.conunits = conunits
256
315
 
257
316
  def visitArea(self, ctx):
258
- # arg = self.contexts[ctx.children[2].children[0].children[0]]
259
317
  arg = self.contexts[ctx.children[2].children[0]]
260
-
261
318
  if self.output_sql == 'mysql':
262
319
  ctx_text = 'sarea(%s)' % arg
263
320
  elif self.output_sql == 'postgresql':
264
- ctx_text = 'area(%s)' % arg
321
+ ctx_text = 'square_degrees(area(%s))' % arg
265
322
  else:
266
323
  ctx_text = ''
324
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=9)
325
+ if derived_column is not None:
326
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
327
+ ctx_text = f"{ctx_text} AS adql_area"
267
328
 
268
- for i in range(ctx.getChildCount() - 1):
329
+ for _ in range(ctx.getChildCount() - 1):
269
330
  ctx.removeLastChild()
270
331
  self.contexts[ctx] = ctx_text
271
332
 
333
+ '''
272
334
  def visitCentroid(self, ctx):
273
335
  """
274
336
  Works only for circles.
@@ -280,12 +342,29 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
280
342
  if self.output_sql == 'mysql':
281
343
  ctx_text = 'scenter(%s)' % arg
282
344
  elif self.output_sql == 'postgresql':
283
- ctx_text = 'center(%s)' % arg
345
+ ctx_text = '@@ %s' % arg
284
346
  else:
285
347
  ctx_text = ''
286
348
 
287
349
  _remove_children(ctx)
288
350
  self.contexts[ctx] = ctx_text
351
+ '''
352
+
353
+ def visitContains_predicate(self, ctx):
354
+ comp_value = ctx.children[0].getText()
355
+ if comp_value == '1' or comp_value == '0':
356
+ self.visitContains(ctx.children[2])
357
+ ctx_text = self.contexts[ctx.children[2]]
358
+ if self.output_sql == 'mysql':
359
+ ctx_text = f"{comp_value} = {ctx_text}"
360
+ elif self.output_sql == 'postgresql' and comp_value == '0':
361
+ ctx_text = ctx_text.replace('@', '!@')
362
+ else:
363
+ raise QueryError('The function CONTAINS allows comparison to 1 or 0 only.')
364
+
365
+ _remove_children(ctx)
366
+ self.contexts[ctx] = ctx_text
367
+
289
368
 
290
369
  def visitContains(self, ctx):
291
370
  arg = (self.contexts[ctx.children[2].children[0]],
@@ -302,25 +381,55 @@ class ADQLFunctionsTranslationVisitor(ADQLParserVisitor):
302
381
  self.contexts[ctx] = ctx_text
303
382
 
304
383
  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.')
384
+ arg = ('', '')
385
+ if isinstance(ctx.children[2],ADQLParser.Coord_valueContext):
386
+ if isinstance(ctx.children[2].children[0].children[0], ADQLParser.PointContext):
387
+ point_ctx1 = ctx.children[2].children[0].children[0]
388
+ point_ctx2 = ctx.children[4].children[0].children[0]
389
+ arg = (self.contexts[point_ctx1],
390
+ self.contexts[point_ctx2])
391
+ else:
392
+ raise QueryError('Distance in the current implementation is ' +
393
+ 'possible only between two explicitly ' +
394
+ 'defined points. For instance, ' +
395
+ 'DISTANCE(POINT(t.ra, r.dec), POINT(0.0, 0.0)) ' +
396
+ 'or DISTANCE(t.ra, r.dec, 0.0, 0.0)')
397
+ else:
398
+ arg = (f"spoint(RADIANS({_convert_values(ctx, 2, self.output_sql)[0]}), " +
399
+ f"RADIANS({_convert_values(ctx, 4, self.output_sql)[0]}))",
400
+ f"spoint(RADIANS({_convert_values(ctx, 6, self.output_sql)[0]}), " +
401
+ f"RADIANS({_convert_values(ctx, 8, self.output_sql)[0]}))")
312
402
 
313
403
  if self.output_sql == 'mysql':
314
404
  ctx_text = '%s(sdist(%s, %s))' % ((self.conunits, ) + arg)
315
405
  elif self.output_sql == 'postgresql':
316
406
  ctx_text = '%s(%s <-> %s)' % ((self.conunits, ) + arg)
407
+ derived_column = _get_ancestor_class_node(ctx, ADQLParser.Derived_columnContext, depth=9)
408
+ if derived_column is not None:
409
+ if not (any([isinstance(child, ADQLParser.As_clauseContext) for child in derived_column.children])):
410
+ ctx_text = f"{ctx_text} AS distance"
317
411
  else:
318
412
  ctx_text = ''
319
413
 
320
- for i in range(ctx.getChildCount() - 1):
414
+ for _ in range(ctx.getChildCount() - 1):
321
415
  ctx.removeLastChild()
322
416
  self.contexts[ctx] = ctx_text
323
417
 
418
+ def visitIntersects_predicate(self, ctx):
419
+ comp_value = ctx.children[0].getText()
420
+ if comp_value == '1' or comp_value == '0':
421
+ self.visitIntersects(ctx.children[2])
422
+ ctx_text = self.contexts[ctx.children[2]]
423
+ if self.output_sql == 'mysql':
424
+ ctx_text = f"{comp_value} = {ctx_text}"
425
+ elif self.output_sql == 'postgresql' and comp_value == '0':
426
+ ctx_text = ctx_text.replace('&&', '!&&')
427
+ else:
428
+ raise QueryError('The function INTERSECTS allows comparison to 1 or 0 only.')
429
+
430
+ _remove_children(ctx)
431
+ self.contexts[ctx] = ctx_text
432
+
324
433
  def visitIntersects(self, ctx):
325
434
  arg = (self.contexts[ctx.children[2].children[0]],
326
435
  self.contexts[ctx.children[4].children[0]])
@@ -547,9 +656,6 @@ class ADQLQueryTranslator(object):
547
656
 
548
657
  self.parse()
549
658
 
550
- comp_visitor = ADQLComparisonPredicateVisitor()
551
- comp_visitor.visit(self.tree)
552
-
553
659
  translator_visitor = ADQLGeometryTranslationVisitor(
554
660
  output_sql='postgresql')
555
661
  translator_visitor.visit(self.tree)