Skip to content

Commit

Permalink
Clean up starrocks properties
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Dec 18, 2024
1 parent ee7dc96 commit 392f99b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
7 changes: 2 additions & 5 deletions sqlglot/dialects/starrocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Parser(MySQL.Parser):

PROPERTY_PARSERS = {
**MySQL.Parser.PROPERTY_PARSERS,
"UNIQUE": lambda self: self._parse_unique_property(),
"UNIQUE": lambda self: self._parse_composite_key_property(exp.UniqueKeyProperty),
"PROPERTIES": lambda self: self._parse_wrapped_properties(),
"PARTITION BY": lambda self: self._parse_partition_by_opt_range(),
}
Expand Down Expand Up @@ -79,16 +79,13 @@ def _parse_unnest(self, with_alias: bool = True) -> t.Optional[exp.Unnest]:

return unnest

def _parse_interval_or_number(self) -> t.Optional[exp.Expression]:
return self._parse_interval(keep_number=True) or self._parse_number()

def _parse_partitioning_granularity_dynamic(self) -> exp.PartitionByRangePropertyDynamic:
self._match_text_seq("START")
start = self._parse_wrapped(self._parse_string)
self._match_text_seq("END")
end = self._parse_wrapped(self._parse_string)
self._match_text_seq("EVERY")
every = self._parse_wrapped(self._parse_interval_or_number)
every = self._parse_wrapped(lambda: self._parse_interval() or self._parse_number())
return self.expression(
exp.PartitionByRangePropertyDynamic, start=start, end=end, every=every
)
Expand Down
16 changes: 10 additions & 6 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4622,14 +4622,18 @@ def xmlelement_sql(self, expression: exp.XMLElement) -> str:
return self.func("XMLELEMENT", name, *expression.expressions)

def partitionbyrangeproperty_sql(self, expression: exp.PartitionByRangeProperty) -> str:
partitions = self.expressions(expression, key="partition_expressions")
create = self.expressions(expression, key="create_expressions")
partitions = self.expressions(expression, "partition_expressions")
create = self.expressions(expression, "create_expressions")
return f"PARTITION BY RANGE {self.wrap(partitions)} {self.wrap(create)}"

def partitionbyrangepropertydynamic_sql(
self, expression: exp.PartitionByRangePropertyDynamic
) -> str:
start = self.sql(expression, key="start")
end = self.sql(expression, key="end")
every = self.sql(expression, key="every")
return f"START {self.wrap(start)} END {self.wrap(end)} EVERY {self.wrap(every)}"
start = self.sql(expression, "start")
end = self.sql(expression, "end")

every = expression.args["every"]
if isinstance(every, exp.Interval) and every.this.is_string:
every.this.replace(exp.Literal.number(every.name))

return f"START {self.wrap(start)} END {self.wrap(end)} EVERY {self.wrap(self.sql(every))}"
14 changes: 3 additions & 11 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ class Parser(metaclass=_Parser):
exp.StabilityProperty, this=exp.Literal.string("IMMUTABLE")
),
"DISTRIBUTED": lambda self: self._parse_distributed_property(),
"DUPLICATE": lambda self: self._parse_duplicate(),
"DUPLICATE": lambda self: self._parse_composite_key_property(exp.DuplicateKeyProperty),
"DYNAMIC": lambda self: self.expression(exp.DynamicProperty),
"DISTKEY": lambda self: self._parse_distkey(),
"DISTSTYLE": lambda self: self._parse_property_assignment(exp.DistStyleProperty),
Expand Down Expand Up @@ -2213,12 +2213,6 @@ def _parse_composite_key_property(self, expr_type: t.Type[E]) -> E:
expressions = self._parse_wrapped_id_vars()
return self.expression(expr_type, expressions=expressions)

def _parse_duplicate(self) -> exp.DuplicateKeyProperty:
return self._parse_composite_key_property(exp.DuplicateKeyProperty)

def _parse_unique_property(self) -> exp.UniqueKeyProperty:
return self._parse_composite_key_property(exp.UniqueKeyProperty)

def _parse_with_property(self) -> t.Optional[exp.Expression] | t.List[exp.Expression]:
if self._match_text_seq("(", "SYSTEM_VERSIONING"):
prop = self._parse_system_versioning_property(with_=True)
Expand Down Expand Up @@ -4595,9 +4589,7 @@ def _parse_escape(self, this: t.Optional[exp.Expression]) -> t.Optional[exp.Expr
return this
return self.expression(exp.Escape, this=this, expression=self._parse_string())

def _parse_interval(
self, match_interval: bool = True, keep_number: bool = False
) -> t.Optional[exp.Add | exp.Interval]:
def _parse_interval(self, match_interval: bool = True) -> t.Optional[exp.Add | exp.Interval]:
index = self._index

if not self._match(TokenType.INTERVAL) and match_interval:
Expand All @@ -4624,7 +4616,7 @@ def _parse_interval(

# Most dialects support, e.g., the form INTERVAL '5' day, thus we try to parse
# each INTERVAL expression into this canonical form so it's easy to transpile
if this and this.is_number and not keep_number:
if this and this.is_number:
this = exp.Literal.string(this.to_py())
elif this and this.is_string:
parts = exp.INTERVAL_STRING_RE.findall(this.name)
Expand Down

0 comments on commit 392f99b

Please sign in to comment.