-
Notifications
You must be signed in to change notification settings - Fork 342
feat: remove native cast from boolean to decimal #5185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andygrove
wants to merge
1
commit into
apache:main
Choose a base branch
from
andygrove:remove-native-bool-to-decimal-cast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−64
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
72 changes: 72 additions & 0 deletions
72
spark/src/test/resources/sql-tests/expressions/cast/cast_boolean_to_decimal.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| -- Licensed to the Apache Software Foundation (ASF) under one | ||
| -- or more contributor license agreements. See the NOTICE file | ||
| -- distributed with this work for additional information | ||
| -- regarding copyright ownership. The ASF licenses this file | ||
| -- to you under the Apache License, Version 2.0 (the | ||
| -- "License"); you may not use this file except in compliance | ||
| -- with the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, | ||
| -- software distributed under the License is distributed on an | ||
| -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| -- KIND, either express or implied. See the License for the | ||
| -- specific language governing permissions and limitations | ||
| -- under the License. | ||
|
|
||
| -- Boolean -> Decimal has no native path in Comet. `CometCast` reports it as unsupported so the | ||
| -- `CodegenDispatchFallback` mixin routes it through Spark's own generated code inside the Comet | ||
| -- pipeline. The default `query` mode therefore still asserts fully native execution. | ||
|
|
||
| statement | ||
| CREATE TABLE test_cast_bool_to_decimal(id int, b boolean) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_cast_bool_to_decimal VALUES (1, true), (2, false), (3, NULL) | ||
|
|
||
| -- basic precision/scale combinations, including the long fast path (precision <= 18) and the | ||
| -- BigDecimal path (precision > 18) | ||
| query | ||
| SELECT id, cast(b as decimal(10,2)), cast(b as decimal(14,4)), cast(b as decimal(30,0)) | ||
| FROM test_cast_bool_to_decimal ORDER BY id | ||
|
|
||
| -- smallest precision/scale that can represent 1 | ||
| query | ||
| SELECT id, cast(b as decimal(1,0)), cast(b as decimal(2,1)) | ||
| FROM test_cast_bool_to_decimal ORDER BY id | ||
|
|
||
| -- maximum precision, with and without scale | ||
| query | ||
| SELECT id, cast(b as decimal(38,0)), cast(b as decimal(38,37)) | ||
| FROM test_cast_bool_to_decimal ORDER BY id | ||
|
|
||
| -- overflow: decimal(1,1) holds at most 0.9, so true does not fit and returns NULL in non-ANSI | ||
| -- mode while false and NULL are unaffected | ||
| query | ||
| SELECT id, cast(b as decimal(1,1)), cast(b as decimal(2,2)), cast(b as decimal(38,38)) | ||
| FROM test_cast_bool_to_decimal ORDER BY id | ||
|
|
||
| -- literal arguments | ||
| query | ||
| SELECT cast(true as decimal(10,2)), cast(false as decimal(10,2)), | ||
| cast(cast(NULL as boolean) as decimal(10,2)) | ||
|
|
||
| -- literal overflow | ||
| query | ||
| SELECT cast(true as decimal(1,1)), cast(false as decimal(1,1)) | ||
|
|
||
| -- try_cast returns NULL on overflow rather than throwing | ||
| query | ||
| SELECT id, try_cast(b as decimal(10,2)), try_cast(b as decimal(1,1)) | ||
| FROM test_cast_bool_to_decimal ORDER BY id | ||
|
|
||
| -- comparison predicates on the result of the cast | ||
| query | ||
| SELECT id FROM test_cast_bool_to_decimal | ||
| WHERE cast(b as decimal(10,2)) > 0.5 ORDER BY id | ||
|
|
||
| -- the cast feeding an aggregate | ||
| query | ||
| SELECT sum(cast(b as decimal(10,2))), count(cast(b as decimal(1,1))) | ||
| FROM test_cast_bool_to_decimal |
79 changes: 79 additions & 0 deletions
79
spark/src/test/resources/sql-tests/expressions/cast/cast_boolean_to_decimal_ansi.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| -- Licensed to the Apache Software Foundation (ASF) under one | ||
| -- or more contributor license agreements. See the NOTICE file | ||
| -- distributed with this work for additional information | ||
| -- regarding copyright ownership. The ASF licenses this file | ||
| -- to you under the Apache License, Version 2.0 (the | ||
| -- "License"); you may not use this file except in compliance | ||
| -- with the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, | ||
| -- software distributed under the License is distributed on an | ||
| -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| -- KIND, either express or implied. See the License for the | ||
| -- specific language governing permissions and limitations | ||
| -- under the License. | ||
|
|
||
| -- ANSI edge cases for Boolean -> Decimal. Comet has no native path for this cast; the | ||
| -- `CodegenDispatchFallback` mixin runs Spark's own generated code inside the Comet pipeline, so | ||
| -- the ANSI overflow errors must match Spark exactly. The non-error queries act as sentinels | ||
| -- proving the cast really executed natively rather than falling the whole plan back to Spark. | ||
|
|
||
| -- Config: spark.sql.ansi.enabled=true | ||
|
|
||
| statement | ||
| CREATE TABLE test_cast_bool_to_decimal_ansi(id int, b boolean) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_cast_bool_to_decimal_ansi VALUES (1, true), (2, false), (3, NULL) | ||
|
|
||
| -- sentinel: a cast that always fits must run natively under ANSI mode | ||
| query | ||
| SELECT id, cast(b as decimal(10,2)), cast(b as decimal(38,0)) | ||
| FROM test_cast_bool_to_decimal_ansi ORDER BY id | ||
|
|
||
| -- sentinel: decimal(1,0) is the tightest type that can hold 1 | ||
| query | ||
| SELECT id, cast(b as decimal(1,0)), cast(b as decimal(2,1)) | ||
| FROM test_cast_bool_to_decimal_ansi ORDER BY id | ||
|
|
||
| -- decimal(1,1) holds at most 0.9, so casting true overflows and must throw under ANSI mode | ||
| query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) | ||
| SELECT cast(b as decimal(1,1)) FROM test_cast_bool_to_decimal_ansi WHERE id = 1 | ||
|
|
||
| -- decimal(2,2) holds at most 0.99: same overflow, larger precision | ||
| query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) | ||
| SELECT cast(b as decimal(2,2)) FROM test_cast_bool_to_decimal_ansi WHERE id = 1 | ||
|
|
||
| -- all-scale decimal at maximum precision still cannot represent 1 | ||
| query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) | ||
| SELECT cast(b as decimal(38,38)) FROM test_cast_bool_to_decimal_ansi WHERE id = 1 | ||
|
|
||
| -- literal true overflows the same way | ||
| query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) | ||
| SELECT cast(true as decimal(1,1)) | ||
|
|
||
| -- overflow is value dependent: false scales to 0, which fits every decimal type, so rows that | ||
| -- only contain false must not throw | ||
| query | ||
| SELECT cast(b as decimal(1,1)), cast(b as decimal(38,38)) | ||
| FROM test_cast_bool_to_decimal_ansi WHERE id = 2 | ||
|
|
||
| -- NULL input short-circuits before the precision check, so it must not throw either | ||
| query | ||
| SELECT cast(b as decimal(1,1)), cast(b as decimal(38,38)) | ||
| FROM test_cast_bool_to_decimal_ansi WHERE id = 3 | ||
|
|
||
| -- try_cast suppresses the ANSI overflow and yields NULL | ||
| query | ||
| SELECT id, try_cast(b as decimal(1,1)), try_cast(b as decimal(38,38)) | ||
| FROM test_cast_bool_to_decimal_ansi ORDER BY id | ||
|
|
||
| -- overflow raised from inside an aggregate input | ||
| query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) | ||
| SELECT sum(cast(b as decimal(1,1))) FROM test_cast_bool_to_decimal_ansi | ||
|
|
||
| -- overflow raised from inside a filter predicate | ||
| query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) | ||
| SELECT id FROM test_cast_bool_to_decimal_ansi WHERE cast(b as decimal(1,1)) > 0.5 |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this ignored test? are we planning to switch it back after future changes?