Skip to content

Commit

Permalink
Fix(parser)!: refactor exp.Chr
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Sep 6, 2024
1 parent dbb1dde commit 95c00ba
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ class Generator(generator.Generator):
exp.Variance: rename_func("varSamp"),
exp.SchemaCommentProperty: lambda self, e: self.naked_property(e),
exp.Stddev: rename_func("stddevSamp"),
exp.Chr: lambda self, e: self.func("char", e.this),
exp.Chr: rename_func("CHAR"),
exp.Lag: lambda self, e: self.func(
"lagInFrame", e.this, e.args.get("offset"), e.args.get("default")
),
Expand Down
18 changes: 5 additions & 13 deletions sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ class Parser(parser.Parser):

FUNCTION_PARSERS = {
**parser.Parser.FUNCTION_PARSERS,
"CHAR": lambda self: self._parse_chr(),
"CHAR": lambda self: self.expression(
exp.Chr,
expressions=self._parse_csv(self._parse_assignment),
charset=self._match(TokenType.USING) and self._parse_var(),
),
"GROUP_CONCAT": lambda self: self._parse_group_concat(),
# https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
"VALUES": lambda self: self.expression(
Expand Down Expand Up @@ -620,18 +624,6 @@ def _parse_type(
parse_interval=parse_interval, fallback_to_identifier=fallback_to_identifier
)

def _parse_chr(self) -> t.Optional[exp.Expression]:
expressions = self._parse_csv(self._parse_assignment)
kwargs: t.Dict[str, t.Any] = {"this": seq_get(expressions, 0)}

if len(expressions) > 1:
kwargs["expressions"] = expressions[1:]

if self._match(TokenType.USING):
kwargs["charset"] = self._parse_var()

return self.expression(exp.Chr, **kwargs)

def _parse_group_concat(self) -> t.Optional[exp.Expression]:
def concat_exprs(
node: t.Optional[exp.Expression], exprs: t.List[exp.Expression]
Expand Down
2 changes: 1 addition & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5172,7 +5172,7 @@ class Coalesce(Func):


class Chr(Func):
arg_types = {"this": True, "charset": False, "expressions": False}
arg_types = {"expressions": True, "charset": False}
is_var_len_args = True
_sql_names = ["CHR", "CHAR"]

Expand Down
2 changes: 2 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class Parser(metaclass=_Parser):
"ARRAY_AGG": lambda args, dialect: exp.ArrayAgg(
this=seq_get(args, 0), nulls_excluded=dialect.ARRAY_AGG_INCLUDES_NULLS is None or None
),
"CHAR": lambda args: exp.Chr(expressions=args),
"CHR": lambda args: exp.Chr(expressions=args),
"COUNT": lambda args: exp.Count(this=seq_get(args, 0), expressions=args[1:], big_int=True),
"CONCAT": lambda args, dialect: exp.Concat(
expressions=args,
Expand Down
7 changes: 4 additions & 3 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_clickhouse(self):
self.assertEqual(expr.sql(dialect="clickhouse"), "COUNT(x)")
self.assertIsNone(expr._meta)

self.validate_identity("SELECT toString(CHAR(104.1, 101, 108.9, 108.9, 111, 32))")
self.validate_identity("@macro").assert_is(exp.Parameter).this.assert_is(exp.Var)
self.validate_identity("SELECT toFloat(like)")
self.validate_identity("SELECT like")
Expand Down Expand Up @@ -160,10 +161,10 @@ def test_clickhouse(self):
)

self.validate_all(
"char(67) || char(65) || char(84)",
"CHAR(67) || CHAR(65) || CHAR(84)",
read={
"clickhouse": "char(67) || char(65) || char(84)",
"oracle": "chr(67) || chr(65) || chr(84)",
"clickhouse": "CHAR(67) || CHAR(65) || CHAR(84)",
"oracle": "CHR(67) || CHR(65) || CHR(84)",
},
)
self.validate_all(
Expand Down

0 comments on commit 95c00ba

Please sign in to comment.