andygrove commented on code in PR #398: URL: https://github.com/apache/datafusion-spark/pull/398#discussion_r4669572360
########## crates/sqllogictest/test_files/string/lower.slt: ########## @@ -8,7 +8,556 @@ # For more information, please see: # https://github.com/apache/datafusion/issues/15914 -## Original Query: SELECT lower('SparkSql'); -## PySpark 3.5.5 Result: {'lower(SparkSql)': 'sparksql', 'typeof(lower(SparkSql))': 'string', 'typeof(SparkSql)': 'string'} -#query -#SELECT lower('SparkSql'::string); +# Expected results verified against Spark 4.2.0 with default ICU case mappings. + +query T +SELECT lower('SparkSql'); +---- +sparksql + +query T +SELECT lower('ÄBĆΔE'); +---- +äbćδe + +query T +SELECT lower('ΣΑΛΑΤΑ'); +---- +σαλατα + +query T +SELECT lower('ΘΑΛΑΣΣΙΝΟΣ'); +---- +θαλασσινος + +query T +SELECT lower('Σ'); +---- +σ + +query T +SELECT lower('ΣΑΣ X'); +---- +σας x + +query T +SELECT lower('ΣΑΣ!'); +---- +σας! + +query T +SELECT lower('ΑΣ́'); +---- +ας́ + +query T +SELECT lower('ΑΣ́Α'); +---- +ασ́α + +query T +SELECT lower('ΑΣ’'); +---- +ας’ + +query T +SELECT lower('ΑΣ’Α'); +---- +ασ’α + +query T +SELECT lower('I'); +---- +i + +query T +SELECT lower('İ'); +---- +i̇ + +query T +SELECT lower('FIDELİO'); +---- +fideli̇o + +query T +SELECT lower('Iİ'); +---- +ii̇ + +query T +SELECT lower('ß fi ffi ff st ῗ'); +---- +ß fi ffi ff st ῗ + +query T +SELECT lower('𐐅'); +---- +𐐭 + +query T +SELECT lower('𝔸'); +---- +𝔸 + +query T +SELECT lower('a🙃B🙃c'); +---- +a🙃b🙃c + +query T +SELECT lower('ςαλατα'); +---- +ςαλατα + +query T +SELECT lower('θαλασσινοσ'); +---- +θαλασσινοσ + +query T +SELECT lower('ASCII ONLY 123'); +---- +ascii only 123 + +query T +SELECT lower('123!?'); +---- +123!? + +query T +SELECT lower(''); +---- +(empty) + +query T +SELECT lower(NULL::STRING); +---- +NULL + +query T +SELECT lower('AAAAAAAAAAİ'); +---- +aaaaaaaaaai̇ + +query T +SELECT lower('İİİİİİ'); +---- +i̇i̇i̇i̇i̇i̇ + +query T +SELECT lower('HELLO WORLD LONG ASCII'); +---- +hello world long ascii + +query T +SELECT lower('ΣΣ'); +---- +σς + +query T +SELECT lower('́Σ'); +---- +́σ + +query T +SELECT lower(arrow_cast(s, 'Utf8')) FROM (VALUES + ('SparkSql'), + ('ÄBĆΔE'), + ('ΣΑΛΑΤΑ'), + ('ΘΑΛΑΣΣΙΝΟΣ'), + ('Σ'), + ('ΣΑΣ X'), + ('ΣΑΣ!'), + ('ΑΣ́'), + ('ΑΣ́Α'), + ('ΑΣ’'), + ('ΑΣ’Α'), + ('I'), + ('İ'), + ('FIDELİO'), + ('Iİ'), + ('ß fi ffi ff st ῗ'), + ('𐐅'), + ('𝔸'), + ('a🙃B🙃c'), + ('ςαλατα'), + ('θαλασσινοσ'), + ('ASCII ONLY 123'), + ('123!?'), + (''), + (NULL::STRING), + ('AAAAAAAAAAİ'), + ('İİİİİİ'), + ('HELLO WORLD LONG ASCII'), + ('ΣΣ'), + ('́Σ')) AS t(s); +---- +sparksql +äbćδe +σαλατα +θαλασσινος +σ +σας x +σας! +ας́ +ασ́α +ας’ +ασ’α +i +i̇ +fideli̇o +ii̇ +ß fi ffi ff st ῗ +𐐭 +𝔸 +a🙃b🙃c +ςαλατα +θαλασσινοσ +ascii only 123 +123!? +(empty) +NULL +aaaaaaaaaai̇ +i̇i̇i̇i̇i̇i̇ +hello world long ascii +σς +́σ + +query T +SELECT arrow_typeof(lower(arrow_cast('İ', 'Utf8'))); +---- +Utf8 + +query T +SELECT lower(arrow_cast(s, 'Dictionary(Int32, Utf8)')) FROM (VALUES ('ΘΑΛΑΣΣΙΝΟΣ'), ('İ'), (NULL::STRING), ('')) AS t(s); +---- +θαλασσινος +i̇ +NULL +(empty) + +query T +SELECT lower(arrow_cast(s, 'LargeUtf8')) FROM (VALUES + ('SparkSql'), + ('ÄBĆΔE'), + ('ΣΑΛΑΤΑ'), + ('ΘΑΛΑΣΣΙΝΟΣ'), + ('Σ'), + ('ΣΑΣ X'), + ('ΣΑΣ!'), + ('ΑΣ́'), + ('ΑΣ́Α'), + ('ΑΣ’'), + ('ΑΣ’Α'), + ('I'), + ('İ'), + ('FIDELİO'), + ('Iİ'), + ('ß fi ffi ff st ῗ'), + ('𐐅'), + ('𝔸'), + ('a🙃B🙃c'), + ('ςαλατα'), + ('θαλασσινοσ'), + ('ASCII ONLY 123'), + ('123!?'), + (''), + (NULL::STRING), + ('AAAAAAAAAAİ'), + ('İİİİİİ'), + ('HELLO WORLD LONG ASCII'), + ('ΣΣ'), + ('́Σ')) AS t(s); +---- +sparksql +äbćδe +σαλατα +θαλασσινος +σ +σας x +σας! +ας́ +ασ́α +ας’ +ασ’α +i +i̇ +fideli̇o +ii̇ +ß fi ffi ff st ῗ +𐐭 +𝔸 +a🙃b🙃c +ςαλατα +θαλασσινοσ +ascii only 123 +123!? +(empty) +NULL +aaaaaaaaaai̇ +i̇i̇i̇i̇i̇i̇ +hello world long ascii +σς +́σ + +query T +SELECT arrow_typeof(lower(arrow_cast('İ', 'LargeUtf8'))); +---- +LargeUtf8 + +query T +SELECT lower(arrow_cast(s, 'Dictionary(Int32, LargeUtf8)')) FROM (VALUES ('ΘΑΛΑΣΣΙΝΟΣ'), ('İ'), (NULL::STRING), ('')) AS t(s); +---- +θαλασσινος +i̇ +NULL +(empty) + +query T +SELECT lower(arrow_cast(s, 'Utf8View')) FROM (VALUES + ('SparkSql'), + ('ÄBĆΔE'), + ('ΣΑΛΑΤΑ'), + ('ΘΑΛΑΣΣΙΝΟΣ'), + ('Σ'), + ('ΣΑΣ X'), + ('ΣΑΣ!'), + ('ΑΣ́'), + ('ΑΣ́Α'), + ('ΑΣ’'), + ('ΑΣ’Α'), + ('I'), + ('İ'), + ('FIDELİO'), + ('Iİ'), + ('ß fi ffi ff st ῗ'), + ('𐐅'), + ('𝔸'), + ('a🙃B🙃c'), + ('ςαλατα'), + ('θαλασσινοσ'), + ('ASCII ONLY 123'), + ('123!?'), + (''), + (NULL::STRING), + ('AAAAAAAAAAİ'), + ('İİİİİİ'), + ('HELLO WORLD LONG ASCII'), + ('ΣΣ'), + ('́Σ')) AS t(s); +---- +sparksql +äbćδe +σαλατα +θαλασσινος +σ +σας x +σας! +ας́ +ασ́α +ας’ +ασ’α +i +i̇ +fideli̇o +ii̇ +ß fi ffi ff st ῗ +𐐭 +𝔸 +a🙃b🙃c +ςαλατα +θαλασσινοσ +ascii only 123 +123!? +(empty) +NULL +aaaaaaaaaai̇ +i̇i̇i̇i̇i̇i̇ +hello world long ascii +σς +́σ + +query T +SELECT arrow_typeof(lower(arrow_cast('İ', 'Utf8View'))); +---- +Utf8View + +query T +SELECT lower(arrow_cast(s, 'Dictionary(Int32, Utf8View)')) FROM (VALUES ('ΘΑΛΑΣΣΙΝΟΣ'), ('İ'), (NULL::STRING), ('')) AS t(s); +---- +θαλασσινος +i̇ +NULL +(empty) + +# Unicode mapping and replacement do not depend on ANSI mode. +statement ok +SET datafusion.execution.enable_ansi_mode = false; + +query T +SELECT lower(s) FROM (VALUES ('Iİ'), ('ΘΑΛΑΣΣΙΝΟΣ'), ('𐐅'), (NULL::STRING), ('')) AS t(s); +---- +ii̇ +θαλασσινος +𐐭 +NULL +(empty) + +query T +SELECT lower(b) FROM (VALUES (X'535041524B'), (X'41FF42'), (X'EDA0BDEDB2A9'), (X'C0AF'), (X'F0808080'), (X'F0908041'), (X'E282'), (X'C4B0'), (X'F0909085'), (X''), (NULL::BINARY)) AS t(b); +---- +spark +a�b +������ +�� +���� +�a +� +i̇ +𐐭 +(empty) +NULL + +query T +SELECT lower(123::INT); +---- +123 + +query T +SELECT lower((-123)::BIGINT); +---- +-123 + +query T +SELECT lower(1.25::DOUBLE); Review Comment: Since `lower` is the first function on `SparkInputType::String`, the Arrow cast that runs after coercion now produces visible results. A few of them are not Spark's: ``` lower(1e20::DOUBLE) -- DataFusion 1e20, Spark 1.0e20 lower(1.0E-5::DOUBLE) -- DataFusion 0.00001, Spark 1.0e-5 lower('2020-01-01 00:00:00'::TIMESTAMP) -- DataFusion 2020-01-01t00:00:00, Spark 2020-01-01 00:00:00 lower('2020-01-01 12:34:56.5'::TIMESTAMP) -- DataFusion 2020-01-01t12:34:56.500, Spark 2020-01-01 12:34:56.5 ``` On `main` these were planning errors, so a user who gets one of these strings now would previously have been told the query isn't supported. I'd rather keep them as errors until the casts are ported. Could you leave FLOAT, DOUBLE and TIMESTAMP out of the accepted input types for now? `1.25` happens to format the same way in both engines, so it would also help to add the queries above as commented-out records carrying Spark's result and a link to #88. Then uncommenting them later verifies the cast port. ########## crates/sqllogictest/test_files/string/lower.slt: ########## @@ -8,7 +8,556 @@ # For more information, please see: # https://github.com/apache/datafusion/issues/15914 -## Original Query: SELECT lower('SparkSql'); -## PySpark 3.5.5 Result: {'lower(SparkSql)': 'sparksql', 'typeof(lower(SparkSql))': 'string', 'typeof(SparkSql)': 'string'} -#query -#SELECT lower('SparkSql'::string); +# Expected results verified against Spark 4.2.0 with default ICU case mappings. + +query T +SELECT lower('SparkSql'); +---- +sparksql + +query T +SELECT lower('ÄBĆΔE'); +---- +äbćδe + +query T +SELECT lower('ΣΑΛΑΤΑ'); +---- +σαλατα + +query T +SELECT lower('ΘΑΛΑΣΣΙΝΟΣ'); +---- +θαλασσινος + +query T +SELECT lower('Σ'); +---- +σ + +query T +SELECT lower('ΣΑΣ X'); +---- +σας x + +query T +SELECT lower('ΣΑΣ!'); +---- +σας! + +query T +SELECT lower('ΑΣ́'); +---- +ας́ + +query T +SELECT lower('ΑΣ́Α'); +---- +ασ́α + +query T +SELECT lower('ΑΣ’'); +---- +ας’ + +query T +SELECT lower('ΑΣ’Α'); +---- +ασ’α + +query T +SELECT lower('I'); +---- +i + +query T +SELECT lower('İ'); +---- +i̇ + +query T +SELECT lower('FIDELİO'); +---- +fideli̇o + +query T +SELECT lower('Iİ'); +---- +ii̇ + +query T +SELECT lower('ß fi ffi ff st ῗ'); +---- +ß fi ffi ff st ῗ + +query T +SELECT lower('𐐅'); +---- +𐐭 + +query T +SELECT lower('𝔸'); +---- +𝔸 + +query T +SELECT lower('a🙃B🙃c'); +---- +a🙃b🙃c + +query T +SELECT lower('ςαλατα'); +---- +ςαλατα + +query T +SELECT lower('θαλασσινοσ'); +---- +θαλασσινοσ + +query T +SELECT lower('ASCII ONLY 123'); +---- +ascii only 123 + +query T +SELECT lower('123!?'); +---- +123!? + +query T +SELECT lower(''); +---- +(empty) + +query T +SELECT lower(NULL::STRING); +---- +NULL + +query T +SELECT lower('AAAAAAAAAAİ'); +---- +aaaaaaaaaai̇ + +query T +SELECT lower('İİİİİİ'); +---- +i̇i̇i̇i̇i̇i̇ + +query T +SELECT lower('HELLO WORLD LONG ASCII'); +---- +hello world long ascii + +query T +SELECT lower('ΣΣ'); +---- +σς + +query T +SELECT lower('́Σ'); +---- +́σ + +query T +SELECT lower(arrow_cast(s, 'Utf8')) FROM (VALUES + ('SparkSql'), + ('ÄBĆΔE'), + ('ΣΑΛΑΤΑ'), + ('ΘΑΛΑΣΣΙΝΟΣ'), + ('Σ'), + ('ΣΑΣ X'), + ('ΣΑΣ!'), + ('ΑΣ́'), + ('ΑΣ́Α'), + ('ΑΣ’'), + ('ΑΣ’Α'), + ('I'), + ('İ'), + ('FIDELİO'), + ('Iİ'), + ('ß fi ffi ff st ῗ'), + ('𐐅'), + ('𝔸'), + ('a🙃B🙃c'), + ('ςαλατα'), + ('θαλασσινοσ'), + ('ASCII ONLY 123'), + ('123!?'), + (''), + (NULL::STRING), + ('AAAAAAAAAAİ'), + ('İİİİİİ'), + ('HELLO WORLD LONG ASCII'), + ('ΣΣ'), + ('́Σ')) AS t(s); +---- +sparksql +äbćδe +σαλατα +θαλασσινος +σ +σας x +σας! +ας́ +ασ́α +ας’ +ασ’α +i +i̇ +fideli̇o +ii̇ +ß fi ffi ff st ῗ +𐐭 +𝔸 +a🙃b🙃c +ςαλατα +θαλασσινοσ +ascii only 123 +123!? +(empty) +NULL +aaaaaaaaaai̇ +i̇i̇i̇i̇i̇i̇ +hello world long ascii +σς +́σ + +query T +SELECT arrow_typeof(lower(arrow_cast('İ', 'Utf8'))); +---- +Utf8 + +query T +SELECT lower(arrow_cast(s, 'Dictionary(Int32, Utf8)')) FROM (VALUES ('ΘΑΛΑΣΣΙΝΟΣ'), ('İ'), (NULL::STRING), ('')) AS t(s); +---- +θαλασσινος +i̇ +NULL +(empty) + +query T +SELECT lower(arrow_cast(s, 'LargeUtf8')) FROM (VALUES + ('SparkSql'), + ('ÄBĆΔE'), + ('ΣΑΛΑΤΑ'), + ('ΘΑΛΑΣΣΙΝΟΣ'), + ('Σ'), + ('ΣΑΣ X'), + ('ΣΑΣ!'), + ('ΑΣ́'), + ('ΑΣ́Α'), + ('ΑΣ’'), + ('ΑΣ’Α'), + ('I'), + ('İ'), + ('FIDELİO'), + ('Iİ'), + ('ß fi ffi ff st ῗ'), + ('𐐅'), + ('𝔸'), + ('a🙃B🙃c'), + ('ςαλατα'), + ('θαλασσινοσ'), + ('ASCII ONLY 123'), + ('123!?'), + (''), + (NULL::STRING), + ('AAAAAAAAAAİ'), + ('İİİİİİ'), + ('HELLO WORLD LONG ASCII'), + ('ΣΣ'), + ('́Σ')) AS t(s); +---- +sparksql +äbćδe +σαλατα +θαλασσινος +σ +σας x +σας! +ας́ +ασ́α +ας’ +ασ’α +i +i̇ +fideli̇o +ii̇ +ß fi ffi ff st ῗ +𐐭 +𝔸 +a🙃b🙃c +ςαλατα +θαλασσινοσ +ascii only 123 +123!? +(empty) +NULL +aaaaaaaaaai̇ +i̇i̇i̇i̇i̇i̇ +hello world long ascii +σς +́σ + +query T +SELECT arrow_typeof(lower(arrow_cast('İ', 'LargeUtf8'))); +---- +LargeUtf8 + +query T +SELECT lower(arrow_cast(s, 'Dictionary(Int32, LargeUtf8)')) FROM (VALUES ('ΘΑΛΑΣΣΙΝΟΣ'), ('İ'), (NULL::STRING), ('')) AS t(s); +---- +θαλασσινος +i̇ +NULL +(empty) + +query T +SELECT lower(arrow_cast(s, 'Utf8View')) FROM (VALUES Review Comment: #76 just merged and switched `arrow_cast(x, 'Utf8View')` to `x::STRING` and `arrow_cast(x, 'Binary')` to `x::BINARY` across the tree. Could you rebase and use those here (and at line 520, and in `lcase.slt`) so the new files match? The large, view, fixed-size and dictionary casts stay as `arrow_cast`. ########## crates/sqllogictest/test_files/string/lower.slt: ########## @@ -8,7 +8,556 @@ # For more information, please see: # https://github.com/apache/datafusion/issues/15914 -## Original Query: SELECT lower('SparkSql'); -## PySpark 3.5.5 Result: {'lower(SparkSql)': 'sparksql', 'typeof(lower(SparkSql))': 'string', 'typeof(SparkSql)': 'string'} -#query -#SELECT lower('SparkSql'::string); +# Expected results verified against Spark 4.2.0 with default ICU case mappings. + +query T +SELECT lower('SparkSql'); +---- +sparksql + +query T +SELECT lower('ÄBĆΔE'); +---- +äbćδe + +query T +SELECT lower('ΣΑΛΑΤΑ'); +---- +σαλατα + +query T +SELECT lower('ΘΑΛΑΣΣΙΝΟΣ'); +---- +θαλασσινος + +query T +SELECT lower('Σ'); +---- +σ + +query T +SELECT lower('ΣΑΣ X'); +---- +σας x + +query T +SELECT lower('ΣΑΣ!'); +---- +σας! + +query T +SELECT lower('ΑΣ́'); +---- +ας́ + +query T +SELECT lower('ΑΣ́Α'); +---- +ασ́α + +query T +SELECT lower('ΑΣ’'); +---- +ας’ + +query T +SELECT lower('ΑΣ’Α'); Review Comment: Spark 3.5 differs from 4.2 on two of these cases, and engines such as Comet still target 3.5. Could you add a short comment at the query site so it is recorded? - `lower('ΑΣ’Α')` returns `ας’α` on 3.5 (JDK case mapping) and `ασ’α` on 4.2 (ICU treats U+2019 as case-ignorable). - Invalid UTF-8 in binary input (line 408) is kept as raw bytes on 3.5. `hex(lower(X'61FF62'))` is `61FF62` on 3.5 and `61EFBFBD62` on 4.2. Once #391 lands, the Spark 3.5 runner will report these records, so they will need `# xfail spark<4.0:` markers. That goes in whichever PR merges second. ########## crates/sqllogictest/test_files/string/lcase.slt: ########## @@ -8,7 +8,441 @@ # For more information, please see: Review Comment: Since `lcase` is a pure alias of `lower`, could this file be trimmed to a few records that show the alias resolves? For example one scalar, one column over `VALUES` with a NULL, one binary and one non-ASCII case. Right now it mirrors `lower.slt` almost line for line, so every future expectation change (for example when #88 changes cast output) has to be made twice. `len.slt` is a two-record precedent for an alias. ########## crates/spark/src/function/string/lower.rs: ########## @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +use std::sync::Arc; + +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::config::ConfigOptions; +use datafusion_common::utils::take_function_args; +use datafusion_common::{Result, internal_err}; +use datafusion_expr::{ + ColumnarValue, Documentation, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDF, + ScalarUDFImpl, Signature, Volatility, +}; +use datafusion_functions::string::lower::LowerFunc; +use datafusion_functions::utils::make_scalar_function; +use datafusion_macros::user_doc; + +use super::make_valid_utf8::spark_make_valid_utf8_inner; +use crate::function::implicit_cast::{SparkInputType, SparkInputTypes}; + +/// Spark-compatible `lower` / `lcase` with UTF8_BINARY mapping. +/// +/// <https://spark.apache.org/docs/latest/api/sql/index.html#lower> +#[user_doc( + doc_section(label = "String Functions"), + description = "Converts a string to lowercase using full, locale-independent Unicode \ Review Comment: Small accuracy point on the doc. `str::to_lowercase` uses the Unicode tables of the compiler that builds the crate, while Spark 4.2 uses ICU4J 78 (Unicode 17). With a current toolchain (Unicode 18) I found 20 code points that differ from Spark 4.2, for example U+A7DD and U+1DF40. Could the description say the mapping follows the Rust standard library's Unicode version, rather than saying it matches Spark 4.2? Also, "the source string layout ... is preserved" is not quite true for dictionary input. `lower(arrow_cast('A', 'Dictionary(Int32, Utf8)'))` returns `Utf8`. Maybe narrow that to `Utf8`, `LargeUtf8` and `Utf8View`. -- 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]
