YutaLin commented on code in PR #4284:
URL: https://github.com/apache/datafusion-comet/pull/4284#discussion_r3242954118


##########
spark/src/test/resources/sql-tests/expressions/string/decode.sql:
##########
@@ -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])

Review Comment:
   Hi @comphead, thanks for the review!
   This is the legacy sql function originally created by Oracle. Before 
standard sql introduced `case when` statement, Oracle created `Decode` as a 
quick way to write `If-Then-ELSE` logic inside a query. Spark supports it 
purely for backward compatibility.
   
   ```sql
   -- Oracle-style DECODE
   SELECT DECODE(status_code, 1, 'Active', 2, 'Suspended', 'Unknown') FROM 
users;
   
   -- Standard SQL Equivalent
   SELECT 
     CASE status_code 
       WHEN 1 THEN 'Active'
       WHEN 2 THEN 'Suspended'
       ELSE 'Unknown'
     END
   FROM users;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to