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
2 changes: 1 addition & 1 deletion sqlglot-integration-tests
20 changes: 20 additions & 0 deletions sqlglot/generators/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4326,6 +4326,26 @@ def round_sql(self, expression: exp.Round) -> str:

return self.func(func, this, decimals, truncate)

def trycast_sql(self, expression: exp.TryCast) -> str:
if not expression.args.get("requires_string"):
return super().trycast_sql(expression)

Comment on lines +4330 to +4332
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary and irrelevant to DuckDB, it's only used for Snowflake roundtrip.

src, to, to_type = expression.this, expression.to, expression.to.this
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (style): let's split these across three lines, we don't use the multi-assignment syntax as much.


if to_type == exp.DType.BOOLEAN:
return _to_boolean_sql(self, exp.ToBoolean(this=src.copy(), safe=True))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we copying?

elif to_type in exp.DataType.TEXT_TYPES and to.expressions:
return self.sql(
exp.case()
.when(
exp.LTE(this=exp.func("LENGTH", src.copy()), expression=to.expressions[0].this),
exp.cast(src.copy(), "TEXT"),
)
.else_(exp.Null())
)
Comment on lines +4337 to +4345
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This messes up DuckDB's roundtrip, right? This behavior needs to be controlled with a new arg in TryCast.


return super().trycast_sql(expression)

def strtok_sql(self, expression: exp.Strtok) -> str:
string_arg = expression.this
delimiter_arg = expression.args.get("delimiter")
Expand Down
Loading