Skip to content

Commit 4558bb7

Browse files
authored
Fix(bigquery): always infer concat type as either bytes or string (#5085)
1 parent ba7b5a8 commit 4558bb7

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

sqlglot/dialects/bigquery.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ def _json_extract_sql(self: BigQuery.Generator, expression: JSON_EXTRACT_TYPE) -
353353
return sql
354354

355355

356+
def _annotate_concat(self: TypeAnnotator, expression: exp.Concat) -> exp.Concat:
357+
annotated = self._annotate_by_args(expression, "expressions")
358+
359+
# Args must be BYTES or types that can be cast to STRING, return type is either BYTES or STRING
360+
# https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#concat
361+
if not annotated.is_type(exp.DataType.Type.BINARY, exp.DataType.Type.UNKNOWN):
362+
annotated.type = exp.DataType.Type.VARCHAR
363+
364+
return annotated
365+
366+
356367
class BigQuery(Dialect):
357368
WEEK_OFFSET = -1
358369
UNNEST_COLUMN_ONLY = True
@@ -433,7 +444,7 @@ class BigQuery(Dialect):
433444
exp.Substring,
434445
)
435446
},
436-
exp.Concat: lambda self, e: self._annotate_by_args(e, "expressions"),
447+
exp.Concat: _annotate_concat,
437448
exp.Sign: lambda self, e: self._annotate_by_args(e, "this"),
438449
exp.Split: lambda self, e: self._annotate_by_args(e, "this", array=True),
439450
}

tests/fixtures/optimizer/annotate_functions.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ STRING;
205205
CONCAT(tbl.bin_col, tbl.bin_col);
206206
BINARY;
207207

208+
# dialect: bigquery
209+
CONCAT(0, tbl.str_col);
210+
STRING;
211+
212+
# dialect: bigquery
213+
CONCAT(tbl.str_col, 0);
214+
STRING;
215+
208216
# dialect: bigquery
209217
LEFT(tbl.str_col, 1);
210218
STRING;

0 commit comments

Comments
 (0)