Skip to content

Commit 538c2bb

Browse files
committed
Fix adql translator for nested log functions
1 parent 2c11d80 commit 538c2bb

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/queryparser/adql/adqltranslator.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import antlr4
66
from antlr4.error.ErrorListener import ErrorListener
7+
import re
78

89
from .ADQLLexer import ADQLLexer
910
from .ADQLParser import ADQLParser
@@ -470,15 +471,6 @@ def visitIntersects(self, ctx):
470471
_remove_children(ctx)
471472
self.contexts[ctx] = ctx_text
472473

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-
482474

483475
class SelectQueryListener(ADQLParserListener):
484476
def __init__(self):
@@ -691,4 +683,9 @@ def to_postgresql(self):
691683
translator_visitor.visit(self.tree)
692684

693685
translated_query = self.translate(translator_visitor)
686+
687+
# Translate LOG10 to LOG and LOG to LN. It's not the most elegant solution but it works.
688+
translated_query = re.sub(r'(?<=[\+\-\*/\(\s,])log\(', 'LN(', translated_query, flags=re.IGNORECASE)
689+
translated_query = re.sub(r'(?<=[\+\-\*/\(\s,])log10\(', 'LOG(', translated_query, flags=re.IGNORECASE)
690+
694691
return translated_query

src/queryparser/testing/tests.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,14 @@ adql_mysql_tests:
889889
# coordsys will ensure the backward compatibility for adql2.0 but can be removed
890890
# later.
891891
adql_postgresql_tests:
892+
-
893+
- SELECT TOP 10 LOG(a) as a FROM db.tab
894+
- SELECT LN(a) AS a FROM db.tab LIMIT 10;
895+
896+
-
897+
- SELECT TOP 10 LOG10(a) as a FROM db.tab
898+
- SELECT LOG(a) AS a FROM db.tab LIMIT 10;
899+
892900
-
893901
- SELECT TOP 10 AREA(CIRCLE('ICRS', "tab".RA, -2.23, 176.98)) FROM db.tab
894902
- SELECT square_degrees(area(scircle(spoint(RADIANS("tab".RA), RADIANS(-2.23)), RADIANS(176.98)))) AS adql_area FROM db.tab LIMIT 10;

0 commit comments

Comments
 (0)