Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ class Generator(generator.Generator):
),
exp.GenerateSeries: rename_func("GENERATE_ARRAY"),
exp.GroupConcat: lambda self, e: groupconcat_sql(
self, e, func_name="STRING_AGG", within_group=False
self, e, func_name="STRING_AGG", within_group=False, sep=None
),
exp.Hex: lambda self, e: self.func("UPPER", self.func("TO_HEX", self.sql(e, "this"))),
exp.HexString: lambda self, e: self.hexstring_sql(e, binary_function_repr="FROM_HEX"),
Expand Down
11 changes: 8 additions & 3 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,12 +1863,14 @@ def groupconcat_sql(
self: Generator,
expression: exp.GroupConcat,
func_name="LISTAGG",
sep: str = ",",
sep: t.Optional[str] = ",",
within_group: bool = True,
on_overflow: bool = False,
) -> str:
this = expression.this
separator = self.sql(expression.args.get("separator") or exp.Literal.string(sep))
separator = self.sql(
expression.args.get("separator") or (exp.Literal.string(sep) if sep else None)
)

on_overflow_sql = self.sql(expression, "on_overflow")
on_overflow_sql = f" ON OVERFLOW {on_overflow_sql}" if (on_overflow and on_overflow_sql) else ""
Expand All @@ -1884,7 +1886,10 @@ def groupconcat_sql(
if order and order.this:
this = order.this.pop()

args = self.format_args(this, f"{separator}{on_overflow_sql}")
args = self.format_args(
this, f"{separator}{on_overflow_sql}" if separator or on_overflow_sql else None
)

listagg: exp.Expression = exp.Anonymous(this=func_name, expressions=[args])

modifiers = self.sql(limit)
Expand Down
9 changes: 3 additions & 6 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2915,15 +2915,12 @@ def test_string_agg(self):
self.validate_identity("STRING_AGG(DISTINCT a, ' & ')")
self.validate_identity("STRING_AGG(a, ' & ' ORDER BY LENGTH(a))")
self.validate_identity("STRING_AGG(foo, b'|' ORDER BY bar)")
self.validate_identity("STRING_AGG(a)", "STRING_AGG(a, ',')")
self.validate_identity("STRING_AGG(a)")
self.validate_identity("STRING_AGG(DISTINCT v, sep LIMIT 3)")
self.validate_identity(
"STRING_AGG(DISTINCT a ORDER BY b DESC, c DESC LIMIT 10)",
"STRING_AGG(DISTINCT a, ',' ORDER BY b DESC, c DESC LIMIT 10)",
)
self.validate_identity("STRING_AGG(DISTINCT a ORDER BY b DESC, c DESC LIMIT 10)")
self.validate_identity(
"SELECT a, GROUP_CONCAT(b) FROM table GROUP BY a",
"SELECT a, STRING_AGG(b, ',') FROM table GROUP BY a",
"SELECT a, STRING_AGG(b) FROM table GROUP BY a",
)

def test_annotate_timestamps(self):
Expand Down