Skip to content
Open
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
32 changes: 32 additions & 0 deletions sqlglot/dialects/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,38 @@ def objectinsert_sql(self, expression: exp.ObjectInsert) -> str:

return self.func("STRUCT_INSERT", this, kv_sql)

def startswith_sql(self, expression: exp.StartsWith) -> str:
this = expression.this
expr = expression.expression

if not this.type:
from sqlglot.optimizer.annotate_types import annotate_types

this = annotate_types(this, dialect=self.dialect)

if not expr.type:
from sqlglot.optimizer.annotate_types import annotate_types

expr = annotate_types(expr, dialect=self.dialect)

if isinstance(expression.this, exp.ByteString):
expression.this.replace(exp.Literal.string(expression.this.this))
elif this.type and not this.is_type(
exp.DataType.Type.VARCHAR, exp.DataType.Type.UNKNOWN
):
expression.this.replace(exp.cast(expression.this, exp.DataType.Type.VARCHAR))

if isinstance(expression.expression, exp.ByteString):
expression.expression.replace(exp.Literal.string(expression.expression.this))
elif expr.type and not expr.is_type(
exp.DataType.Type.VARCHAR, exp.DataType.Type.UNKNOWN
):
expression.expression.replace(
exp.cast(expression.expression, exp.DataType.Type.VARCHAR)
)

return self.func("STARTS_WITH", expression.this, expression.expression)

def unnest_sql(self, expression: exp.Unnest) -> str:
explode_array = expression.args.get("explode_array")
if explode_array:
Expand Down
15 changes: 15 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,21 @@ def test_bigquery(self):
"spark": "CAST(a AS BINARY)",
},
)
# Test STARTS_WITH with BYTES/BLOB handling from BigQuery to DuckDB
self.validate_all(
"STARTS_WITH(CAST('foo' AS BYTES), CAST('f' AS BYTES))",
write={
"bigquery": "STARTS_WITH(CAST('foo' AS BYTES), CAST('f' AS BYTES))",
"duckdb": "STARTS_WITH(CAST(CAST('foo' AS BLOB) AS TEXT), CAST(CAST('f' AS BLOB) AS TEXT))",
},
)
self.validate_all(
"STARTS_WITH(CAST('foo' AS BYTES), b'f')",
write={
"bigquery": "STARTS_WITH(CAST('foo' AS BYTES), b'f')",
"duckdb": "STARTS_WITH(CAST(CAST('foo' AS BLOB) AS TEXT), 'f')",
},
)
self.validate_all(
"CAST(a AS NUMERIC)",
write={
Expand Down