Skip to content

Commit 163f008

Browse files
Parsed ALTER DATABASE query (#45)
* Parsed ALTER DATABASE query
1 parent e55ad35 commit 163f008

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

sqlglot/dialects/singlestore.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ class Parser(parser.Parser):
444444
TokenType.DPIPE: exp.Or,
445445
}
446446

447+
ALTERABLES = parser.Parser.ALTERABLES | {TokenType.DATABASE}
448+
449+
ALTER_PARSERS = {
450+
**parser.Parser.ALTER_PARSERS,
451+
"WITH": lambda self: self._parse_alter_table_set(),
452+
}
453+
447454
def _parse_match_against(self) -> exp.MatchAgainst:
448455
if self._match_text_seq("TABLE"):
449456
expressions = []
@@ -461,6 +468,17 @@ def _parse_match_against(self) -> exp.MatchAgainst:
461468
exp.MatchAgainst, this=this, expressions=expressions
462469
)
463470

471+
def _parse_alter_table_set(self) -> exp.AlterSet:
472+
alter_set = self.expression(exp.AlterSet)
473+
474+
if self._match_text_seq("SYNC", "REPLICATION"):
475+
alter_set.set("expressions", [exp.Var(this=f"SYNC REPLICATION")])
476+
elif self._match_text_seq("ASYNC", "REPLICATION"):
477+
alter_set.set("expressions", [exp.Var(
478+
this=f"ASYNC REPLICATION")])
479+
480+
return alter_set
481+
464482
class Generator(generator.Generator):
465483
LOCKING_READS_SUPPORTED = True
466484
EXCEPT_INTERSECT_SUPPORT_ALL_CLAUSE = False
@@ -2959,10 +2977,6 @@ def indexconstraintoption_sql(self,
29592977
self.unsupported("Unsupported index constraint option.")
29602978
return ""
29612979

2962-
def alterset_sql(self, expression: exp.AlterSet) -> str:
2963-
self.unsupported("ALTER SET query is not supported in SingleStore")
2964-
return super().alterset_sql(expression)
2965-
29662980
def periodforsystemtimeconstraint_sql(self,
29672981
expression: exp.PeriodForSystemTimeConstraint) -> str:
29682982
self.unsupported(

tests/dialects/test_singlestore.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,12 +1957,10 @@ def test_expressions_generation(self):
19571957
sql="CREATE TABLE ColumnConstraint (id INT AUTO_INCREMENT PRIMARY KEY)",
19581958
exp_type=exp.ColumnConstraint)
19591959
self.validate_generation(
1960-
sql="ALTER TABLE orders SET DATA_RETENTION_TIME_IN_DAYS = 1",
1961-
expected_sql="ALTER TABLE orders SET",
1962-
error_message="ALTER SET query is not supported in SingleStore",
1960+
sql="ALTER DATABASE db SET SYNC REPLICATION",
1961+
expected_sql="ALTER DATABASE db SET SYNC REPLICATION",
19631962
exp_type=exp.AlterSet,
1964-
from_dialect="snowflake",
1965-
run=False
1963+
from_dialect="singlestore",
19661964
)
19671965
self.validate_generation(
19681966
sql="CREATE TABLE ConstraintTable (a INT, CONSTRAINT id PRIMARY KEY (a))",
@@ -6282,3 +6280,53 @@ def test_operators_parsing(self):
62826280
),
62836281
run=False
62846282
)
6283+
6284+
def test_alter_database_parsing(self):
6285+
self.validate_parsing(
6286+
"ALTER DATABASE db WITH SYNC REPLICATION",
6287+
exp.Alter(
6288+
this=exp.Table(this=exp.Identifier(this="db", kind="SCHEMA")),
6289+
kind="DATABASE",
6290+
actions=[
6291+
exp.AlterSet(
6292+
expressions=[exp.Var(this="SYNC REPLICATION")]
6293+
)
6294+
]
6295+
)
6296+
)
6297+
self.validate_parsing(
6298+
"ALTER DATABASE db WITH ASYNC REPLICATION",
6299+
exp.Alter(
6300+
this=exp.Table(this=exp.Identifier(this="db", kind="SCHEMA")),
6301+
kind="DATABASE",
6302+
actions=[
6303+
exp.AlterSet(
6304+
expressions=[exp.Var(this="ASYNC REPLICATION")]
6305+
)
6306+
]
6307+
)
6308+
)
6309+
self.validate_parsing(
6310+
"ALTER DATABASE db SET SYNC REPLICATION",
6311+
exp.Alter(
6312+
this=exp.Table(this=exp.Identifier(this="db", kind="SCHEMA")),
6313+
kind="DATABASE",
6314+
actions=[
6315+
exp.AlterSet(
6316+
expressions=[exp.Var(this="SYNC REPLICATION")]
6317+
)
6318+
]
6319+
)
6320+
)
6321+
self.validate_parsing(
6322+
"ALTER DATABASE db SET ASYNC REPLICATION",
6323+
exp.Alter(
6324+
this=exp.Table(this=exp.Identifier(this="db", kind="SCHEMA")),
6325+
kind="DATABASE",
6326+
actions=[
6327+
exp.AlterSet(
6328+
expressions=[exp.Var(this="ASYNC REPLICATION")]
6329+
)
6330+
]
6331+
)
6332+
)

0 commit comments

Comments
 (0)