Repository navigation
SNOW-4468388: Accept a Column or literal str format in to_timestamp, to_char, to_binary and try_to_binary - #4497
Merged
Conversation
sfc-gh-yixie
requested review from
sfc-gh-aling,
sfc-gh-mayliu and
sfc-gh-yuwang
November 11, 2026 00:01
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4497 +/- ##
=======================================
Coverage 95.47% 95.47%
=======================================
Files 179 179
Lines 46927 46927
Branches 8110 8106 -4
=======================================
+ Hits 44803 44805 +2
+ Misses 1332 1331 -1
+ Partials 792 791 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
sfc-gh-yixie
requested review from
sfc-gh-jzeng
and removed request for
sfc-gh-aling
November 11, 2026 07:11
| _ast=ast, | ||
| _emit_ast=_emit_ast, | ||
| ) | ||
| if fmt is not None |
Collaborator
There was a problem hiding this comment.
can we add tests to test when fmt is ""?
Collaborator
Author
There was a problem hiding this comment.
Added in 131152a. Checked against Snowflake first:
to_binary(e, "")raisesInvalid binary format string ''. The test asserts this, and also covers an unknown format ("FOO").try_to_binary(e, "")returns NULL for every row, including valid hex input. The test asserts this. It runs live only, becausetry_to_binaryhas no local-testing mock.
This also turned up two bugs in the local-testing to_binary mock, fixed in the same commit:
- It treated
""as HEX, so it silently decoded valid input where Snowflake errors. - Its invalid-format check tested the wrong variable, so an unknown format failed with an unrelated error.
Both now raise Invalid binary format string, matching Snowflake. The fix is listed under the local-testing bug fixes in the CHANGELOG.
🤖 Posted by an AI agent (Claude Code · skill none) on behalf of @sfc-gh-yixie.
sfc-gh-yuwang
approved these changes
Nov 12, 2026
…to_char, to_binary and try_to_binary - to_timestamp, to_char/to_varchar: widen the fmt/format type hints to ColumnOrLiteralStr (plus an int scale for to_timestamp); build the to_timestamp AST explicitly. Generated SQL is unchanged. - to_binary, try_to_binary: a Column fmt raised TypeError because of `if fmt:`; use `fmt is not None` and _to_col_if_lit, and build the AST explicitly. An empty-string fmt is now passed to Snowflake. - Local testing: to_binary treats a format column with one repeated value as a constant, returns NULL for an all-NULL format, and raises NotImplementedError for per-row formats. - Tests: Column fmt coverage live and in local testing, plus AST test data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Tests: to_binary(e, "") raises an invalid binary format error, and try_to_binary(e, "") returns NULL for every row, matching Snowflake. - Local testing: to_binary no longer treats an empty-string format as HEX, and an unknown format now raises "Invalid binary format string" instead of failing with an unrelated error (the check tested the wrong variable). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sfc-gh-yixie
force-pushed
the
yixie-SNOW-4468388-align-fmt-column-support
branch
from
November 13, 2026 00:10
131152a to
b7133a8
Compare
sfc-gh-yixie
enabled auto-merge (squash)
November 13, 2026 00:15
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.
Which Jira issue is this PR addressing? Make sure that there is an accompanying issue to your PR.
Fixes SNOW-4468388
Fill out the following pre-review checklist:
snowflakedb/local-testingPlease describe how your code solves the related issue.
This is a follow-up to SNOW-4467757: Add TRY_TO_* conversion functions #4496. The new
try_to_*functions accept the format as a Column or a literalstr, and astris always a literal format, never a column name. Most existingto_*functions already do the same. This PR aligns the four existing functions that didn't. Snowflake accepts a non-constant format for all of them; I verified this live.to_timestampOptional[Column]ColumnOrLiteralStror an int scale; AST built explicitly. Generated SQL is unchanged.to_char/to_varcharOptional[str]to_binary,try_to_binaryfmtraisedTypeError: Cannot convert a Column object into bool(if fmt:)fmt is not Noneand_to_col_if_lit, build the AST explicitly, widen the type hintsBehavior change:
to_binary/try_to_binarywithfmt=""used to drop the format silently. It is now passed to Snowflake. This is noted in the CHANGELOG.Local testing: the mock layer decides how to pass arguments from the public type hints, so after widening them a literal format reaches
mock_to_binaryas a column holding one repeated value.mock_to_binarynow handles that:NotImplementedErrorwhen the format differs from row to row.The
to_charandto_timestampmocks already handled per-row formats.try_to_binaryhas no local-testing mock onmain, so its new test is skipped in local testing.Testing:
New integration tests in
tests/integ/test_function.py:test_to_binary_fmt_literal_and_columnandtest_try_to_binary_fmt_literal_and_column: HEX, BASE64 and UTF-8 formats in different rows; invalid values give NULL fortry_to_binary.test_to_char_column_format(for bothto_charandto_varchar): two date formats in different rows.test_to_timestamp_fmt_variants: a str format, a Column format and int scales.All pass live and with
--local_testing_mode, except thetry_to_binarytest, which is skipped in local testing as noted above.AST test data in
tests/ast/data/functions_fmt_column.test. The existingfunctions1andfunctions2expectations did not change.The doctests,
tests/mock,tests/unitand the local-testing runs oftest_function.py/test_column.pypass.🤖 Generated with Claude Code