feat(duckDB): Cast inputs (BLOB → VARCHAR) for duckDB STARTS_WITH#6240
Merged
georgesittas merged 7 commits intomainfrom Nov 12, 2025
Merged
Conversation
georgesittas
reviewed
Nov 3, 2025
a99ba1f to
93b7efa
Compare
Collaborator
georgesittas
left a comment
There was a problem hiding this comment.
We should mimic the implementation for Lower and Upper. Take a look at _case_conversion_sql.
82a535d to
fd278a0
Compare
Collaborator
georgesittas
left a comment
There was a problem hiding this comment.
Just one comment, looks good otherwise. Thanks!
sqlglot/dialects/duckdb.py
Outdated
Comment on lines
+1218
to
+1228
| # Annotate types if needed for type-based casting | ||
| if not arg.type: | ||
| from sqlglot.optimizer.annotate_types import annotate_types | ||
|
|
||
| annotate_types(arg, dialect=self.dialect) | ||
|
|
||
| # Convert ByteString to String literal before generation | ||
| # ByteStrings get typed as UNKNOWN and would be wrapped in CAST(...AS BLOB) by generator | ||
| if isinstance(arg, exp.ByteString): | ||
| arg.replace(exp.Literal.string(arg.this)) | ||
| # Cast non-VARCHAR types to VARCHAR |
Collaborator
There was a problem hiding this comment.
I don't think this logic is necessary?
- We don't have to annotate types here, since transpiling this successfully from bigquery to duckdb requires that we run type inference first, and hence we should have the types available. For duckdb -> duckdb, we don't need types– the varchar/unknown branch should suffice since the type will always be unknown and we won't cast, hence preserving the sql @ roundtrip
- Doesn't look like we should deal with bytestring as a special case. Despite a bit verbose, doing a double-cast is probably the safer way to go.
georgesittas
approved these changes
Nov 12, 2025
Collaborator
georgesittas
left a comment
There was a problem hiding this comment.
Probably worth inlining _prepare_startswith_arg but I'll just get this in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
STARTS_WITHis used withBLOB/BYTEStypes that are not literals (e.g., from CAST, table columns, function results), the transpiled DuckDB query fails because DuckDB's starts_with only acceptsVARCHAR.Before:
After: