-
Notifications
You must be signed in to change notification settings - Fork 321
feat: Support Spark Expression Decode #4284
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
abf3a20
test: add encode sql
YutaLin aed0c24
docs: support expression decode
YutaLin 56eb321
docs: support spark expression decode
YutaLin 210b254
Merge branch 'main' into 3184_support_expression_decode
YutaLin a06ada9
Merge branch 'main' into 3184_support_expression_decode
YutaLin e900e7e
Apply suggestion from @comphead
comphead 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
105 changes: 105 additions & 0 deletions
105
spark/src/test/resources/sql-tests/expressions/string/decode.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,105 @@ | ||
| -- 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. | ||
|
|
||
| -- Tests for the SQL `decode` function. | ||
| -- | ||
| -- Spark's `decode` is overloaded: | ||
| -- * decode(bin, charset) -> StringDecode (charset binary->string) | ||
| -- * decode(expr, search, result, ..., [default]) -> CaseWhen with EqualNullSafe branches | ||
| -- | ||
| -- The Oracle-style form is implemented in Spark via the RuntimeReplaceable trait, so by the | ||
| -- time Comet sees the plan the wrapper has already been replaced with CaseWhen and Comet | ||
| -- handles it through its existing CaseWhen + EqualNullSafe serde. | ||
| -- | ||
| -- The 2-arg charset form lowers to a cast(binary, string) inside Comet's stringDecode | ||
| -- handler, but only when the charset is 'utf-8' (case-insensitive). Other charsets fall | ||
| -- back to Spark JVM execution. | ||
|
|
||
| -- =========================================================================== | ||
| -- Charset form: decode(bin, charset) for UTF-8 (the supported native path) | ||
| -- =========================================================================== | ||
|
|
||
| statement | ||
| CREATE TABLE test_decode_utf8(b binary) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_decode_utf8 VALUES (CAST('hello' AS BINARY)), (CAST('world' AS BINARY)), (CAST('' AS BINARY)), | ||
| (CAST('café' AS BINARY)), (NULL) | ||
|
|
||
| query | ||
| SELECT decode(b, 'utf-8') FROM test_decode_utf8 | ||
|
|
||
| query | ||
| SELECT decode(b, 'UTF-8') FROM test_decode_utf8 | ||
|
|
||
| query | ||
| SELECT decode(CAST('hello' AS BINARY), 'utf-8'), decode(CAST('' AS BINARY), 'utf-8'), decode(NULL, 'utf-8') | ||
|
|
||
| -- Charset form: non-UTF-8 | ||
|
|
||
| statement | ||
| CREATE TABLE test_decode_charset_safe(b binary) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_decode_charset_safe VALUES (CAST('ab' AS BINARY)), (CAST('abcd' AS BINARY)), (CAST('' AS BINARY)), (NULL) | ||
|
|
||
| query expect_fallback(Comet only supports decoding with 'utf-8'.) | ||
| SELECT decode(b, 'UTF-16BE') FROM test_decode_charset_safe | ||
|
|
||
| query expect_fallback(Comet only supports decoding with 'utf-8'.) | ||
| SELECT decode(b, 'US-ASCII') FROM test_decode_charset_safe | ||
|
|
||
| query expect_fallback(Comet only supports decoding with 'utf-8'.) | ||
| SELECT decode(b, 'ISO-8859-1') FROM test_decode_utf8 | ||
|
|
||
| -- Oracle-style form: decode(expr, search1, result1, ..., [default]) | ||
|
comphead marked this conversation as resolved.
Outdated
|
||
|
|
||
| statement | ||
| CREATE TABLE test_decode_oracle(status string, code int) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_decode_oracle VALUES ('A', 1), ('I', 2), ('X', 3), (NULL, 4), ('A', NULL) | ||
|
|
||
| query | ||
| SELECT decode(status, 'A', 'Active', 'I', 'Inactive', 'Other') FROM test_decode_oracle | ||
|
|
||
| query | ||
| SELECT decode(status, 'A', 'Active', 'I', 'Inactive') FROM test_decode_oracle | ||
|
|
||
| query | ||
| SELECT decode(code, 1, 'one', 2, 'two', 3, 'three', 'unknown') FROM test_decode_oracle | ||
|
|
||
| query | ||
| SELECT decode(code, 1, 'one', 2, 'two') FROM test_decode_oracle | ||
|
|
||
| query | ||
| SELECT decode(status, 'A', 'has-A', NULL, 'is-null', 'other') FROM test_decode_oracle | ||
|
|
||
| query | ||
| SELECT decode(status, 'A', 'Active') FROM test_decode_oracle | ||
|
|
||
| query | ||
| SELECT decode(2, 1, 'Southlake', 2, 'San Francisco', 3, 'New Jersey', 'Other') | ||
|
|
||
| query | ||
| SELECT decode(6, 1, 'Southlake', 2, 'San Francisco', 'Other') | ||
|
|
||
| query | ||
| SELECT decode(6, 1, 'Southlake', 2, 'San Francisco') | ||
|
|
||
| query | ||
| SELECT decode(NULL, 6, 'Spark', NULL, 'SQL', 4, 'rocks') | ||
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.
Thanks @YutaLin. I'm not sure why we need oracle style? it might be confusing, other than that the PR looks good to go
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.
Hi @comphead, thanks for the review!
This is the legacy sql function originally created by Oracle. Before standard sql introduced
case whenstatement, Oracle createdDecodeas a quick way to writeIf-Then-ELSElogic inside a query. Spark supports it purely for backward compatibility.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.
Ic, lets just keep the test case without mentioning Oracle