From 921b610eb38bca3295c66a87fa05bd2fb26187f9 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 10 Jan 2024 19:39:12 +0300 Subject: [PATCH] removed SpecialConstant --- mindsdb_sql/parser/ast/select/__init__.py | 2 +- mindsdb_sql/parser/ast/select/constant.py | 21 ++++--------------- mindsdb_sql/parser/dialects/mindsdb/parser.py | 6 +++--- mindsdb_sql/parser/dialects/mysql/parser.py | 4 ++-- .../test_base_sql/test_misc_sql_queries.py | 6 +++--- 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/mindsdb_sql/parser/ast/select/__init__.py b/mindsdb_sql/parser/ast/select/__init__.py index e30a0add..8ba33604 100644 --- a/mindsdb_sql/parser/ast/select/__init__.py +++ b/mindsdb_sql/parser/ast/select/__init__.py @@ -1,7 +1,7 @@ from .select import Select from .common_table_expression import CommonTableExpression from .union import Union -from .constant import Constant, NullConstant, SpecialConstant, Last +from .constant import Constant, NullConstant, Last from .star import Star from .identifier import Identifier from .join import Join diff --git a/mindsdb_sql/parser/ast/select/constant.py b/mindsdb_sql/parser/ast/select/constant.py index eef0b463..0b31af1e 100644 --- a/mindsdb_sql/parser/ast/select/constant.py +++ b/mindsdb_sql/parser/ast/select/constant.py @@ -4,16 +4,17 @@ class Constant(ASTNode): - def __init__(self, value, *args, **kwargs): + def __init__(self, value, with_quotes=True, *args, **kwargs): super().__init__(*args, **kwargs) self.value = value + self.with_quotes = with_quotes def to_tree(self, *args, level=0, **kwargs): alias_str = f', alias={self.alias.to_tree()}' if self.alias else '' return indent(level) + f'Constant(value={repr(self.value)}{alias_str})' def get_string(self, *args, **kwargs): - if isinstance(self.value, str): + if isinstance(self.value, str) and self.with_quotes: out_str = f"\'{self.value}\'" elif isinstance(self.value, bool): out_str = 'TRUE' if self.value else 'FALSE' @@ -29,26 +30,12 @@ def __init__(self, *args, **kwargs): super().__init__(value=None, *args, **kwargs) def to_tree(self, *args, level=0, **kwargs): - return '\t'*level + 'NullConstant()' + return '\t'*level + 'NullConstant()' def get_string(self, *args, **kwargs): return 'NULL' -# TODO replace it to just Constant? -# DEFAULT -class SpecialConstant(ASTNode): - def __init__(self, name, *args, **kwargs): - super().__init__(*args, **kwargs) - self.name = name - - def to_tree(self, *args, level=0, **kwargs): - return indent(level) + f'SpecialConstant(name={self.name})' - - def get_string(self, *args, **kwargs): - return self.name - - class Last(Constant): def __init__(self, *args, **kwargs): self.value = 'last' diff --git a/mindsdb_sql/parser/dialects/mindsdb/parser.py b/mindsdb_sql/parser/dialects/mindsdb/parser.py index 213c010a..72777809 100644 --- a/mindsdb_sql/parser/dialects/mindsdb/parser.py +++ b/mindsdb_sql/parser/dialects/mindsdb/parser.py @@ -356,7 +356,7 @@ def set(self, p): if isinstance(p[4], Constant): val = p[4] else: - val = SpecialConstant('DEFAULT') + val = Constant(p[4], with_quotes=False) params['COLLATE'] = val return Set(category=p.id.lower(), arg=arg, params=params) @@ -365,8 +365,8 @@ def set(self, p): @_('SET charset constant', 'SET charset DEFAULT') def set(self, p): - if hasattr(p, 'DEFAULT'): - arg = SpecialConstant('DEFAULT') + if hasattr(p, 'id'): + arg = Constant(p.id, with_quotes=False) else: arg = p.constant return Set(category='CHARSET', arg=arg) diff --git a/mindsdb_sql/parser/dialects/mysql/parser.py b/mindsdb_sql/parser/dialects/mysql/parser.py index 3ad56d29..f5f9966b 100644 --- a/mindsdb_sql/parser/dialects/mysql/parser.py +++ b/mindsdb_sql/parser/dialects/mysql/parser.py @@ -124,7 +124,7 @@ def set(self, p): if isinstance(p[4], Constant): val = p[4] else: - val = SpecialConstant('DEFAULT') + val = Constant('DEFAULT', with_quotes=False) params['COLLATE'] = val return Set(category=p.id.lower(), arg=arg, params=params) @@ -134,7 +134,7 @@ def set(self, p): @_('SET charset DEFAULT') def set(self, p): if hasattr(p, 'DEFAULT'): - arg = SpecialConstant('DEFAULT') + arg = Constant('DEFAULT', with_quotes=False) else: arg = p.constant return Set(category='CHARSET', arg=arg) diff --git a/tests/test_parser/test_base_sql/test_misc_sql_queries.py b/tests/test_parser/test_base_sql/test_misc_sql_queries.py index bc8df714..53f09879 100644 --- a/tests/test_parser/test_base_sql/test_misc_sql_queries.py +++ b/tests/test_parser/test_base_sql/test_misc_sql_queries.py @@ -103,7 +103,7 @@ def test_set(self, dialect): assert str(ast) == str(expected_ast) - sql = "SET NAMES some_name collate default" + sql = "SET NAMES some_name collate DEFAULT" ast = parse_sql(sql, dialect=dialect) expected_ast = Set(category="names", @@ -126,14 +126,14 @@ def test_set_charset(self, dialect): sql = "SET CHARACTER SET DEFAULT" ast = parse_sql(sql, dialect=dialect) - expected_ast = Set(category='CHARSET', arg=SpecialConstant('DEFAULT')) + expected_ast = Set(category='CHARSET', arg=Constant('DEFAULT', with_quotes=False)) assert ast.to_tree() == expected_ast.to_tree() sql = "SET CHARSET DEFAULT" ast = parse_sql(sql, dialect=dialect) - expected_ast = Set(category='CHARSET', arg=SpecialConstant('DEFAULT')) + expected_ast = Set(category='CHARSET', arg=Constant('DEFAULT', with_quotes=False)) assert ast.to_tree() == expected_ast.to_tree() assert str(ast) == str(expected_ast)