add quotes to field if numeric in safe_cast macro #892
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.
resolves #891
Problem
The
safe_cast
macro currently uses the try_cast function in snowflake, which has the known limitation:This means that this does not work:
select try_cast(1 as NUMBER);
Even though all of these do:
Other warehouses support
safe_cast
for all data types (select try_cast(1 as NUMBER);
works in BigQuery and postgres). Forsafe_cast
to be an effective cross-db macro, it should have consistent behavior regardless of the warehouse.This PR extends
safe_cast
to allow forfields
that are type numeric by wrapping thefield
in quotes if it is anumber
:select try_cast(1 as NUMBER);
->select try_cast('1' as NUMBER);
Testing
I have tested this by adding this macro to my example unit testing project. With the updated macro this unit test:
creates the expected SQL:
Note: Ideally, this should be resolved by snowflake accepting
select try_cast(1 as NUMBER);
, but this is a work-around in the meantime.Checklist
Note: I would like to add an integration test for this change in functionality, but don't know the best place for that.