Skip to content

Commit

Permalink
removed SpecialConstant
Browse files Browse the repository at this point in the history
  • Loading branch information
ea-rus committed Jan 10, 2024
1 parent 71ea50a commit 921b610
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mindsdb_sql/parser/ast/select/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 4 additions & 17 deletions mindsdb_sql/parser/ast/select/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions mindsdb_sql/parser/dialects/mindsdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions mindsdb_sql/parser/dialects/mysql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_parser/test_base_sql/test_misc_sql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down

0 comments on commit 921b610

Please sign in to comment.