Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion native-engine/auron-serde/proto/auron.proto
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ enum ScalarFunction {
ConcatWithSeparator=27;
DatePart=28;
DateTrunc=29;
InitCap=30;
// InitCap=30;
Left=31;
Lpad=32;
Lower=33;
Expand Down
2 changes: 1 addition & 1 deletion native-engine/auron-serde/src/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ impl From<protobuf::ScalarFunction> for Arc<ScalarUDF> {
ScalarFunction::CharacterLength => f::unicode::character_length(),
ScalarFunction::Chr => f::string::chr(),
ScalarFunction::ConcatWithSeparator => f::string::concat_ws(),
ScalarFunction::InitCap => f::unicode::initcap(),
// ScalarFunction::InitCap => f::unicode::initcap(),
ScalarFunction::Left => f::unicode::left(),
ScalarFunction::Lpad => f::unicode::lpad(),
ScalarFunction::Random => f::math::random(),
Expand Down
2 changes: 2 additions & 0 deletions native-engine/datafusion-ext-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod spark_crypto;
mod spark_dates;
pub mod spark_get_json_object;
mod spark_hash;
mod spark_initcap;
mod spark_isnan;
mod spark_make_array;
mod spark_make_decimal;
Expand Down Expand Up @@ -64,6 +65,7 @@ pub fn create_auron_ext_function(name: &str) -> Result<ScalarFunctionImplementat
"Spark_StringConcatWs" => Arc::new(spark_strings::string_concat_ws),
"Spark_StringLower" => Arc::new(spark_strings::string_lower),
"Spark_StringUpper" => Arc::new(spark_strings::string_upper),
"Spark_InitCap" => Arc::new(spark_initcap::string_initcap),
"Spark_Year" => Arc::new(spark_dates::spark_year),
"Spark_Month" => Arc::new(spark_dates::spark_month),
"Spark_Day" => Arc::new(spark_dates::spark_day),
Expand Down
119 changes: 119 additions & 0 deletions native-engine/datafusion-ext-functions/src/spark_initcap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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.

use std::sync::Arc;

use arrow::array::{ArrayRef, StringArray};
use datafusion::{
common::{Result, ScalarValue, cast::as_string_array},
logical_expr::ColumnarValue,
};
use datafusion_ext_commons::df_execution_err;

pub fn string_initcap(args: &[ColumnarValue]) -> Result<ColumnarValue> {
match &args[0] {
ColumnarValue::Array(array) => {
let input_array = as_string_array(array)?;
let output_array =
StringArray::from_iter(input_array.into_iter().map(|s| s.map(|s| initcap(s))));
Ok(ColumnarValue::Array(Arc::new(output_array) as ArrayRef))
}
ColumnarValue::Scalar(ScalarValue::Utf8(Some(str))) => {
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(initcap(str)))))
}
_ => df_execution_err!("Unsupported args {args:?} for `string_initcap`"),
}
}

fn initcap(input: &str) -> String {
let mut out = String::with_capacity(input.len());
let mut prev_is_space = true; // i == 0 or chars[i-1] == ' '

if input.is_ascii() {
// ASCII
for ch in input.chars() {
if prev_is_space && ch.is_ascii_alphanumeric() {
out.push(ch.to_ascii_uppercase());
} else {
out.push(ch.to_ascii_lowercase());
};
prev_is_space = ch == ' ';
}
} else {
// Non-ASCII
for ch in input.chars() {
if prev_is_space && ch.is_alphabetic() {
out.extend(ch.to_uppercase());
} else {
out.extend(ch.to_lowercase());
}
prev_is_space = ch == ' ';
}
}
out
}

#[cfg(test)]
mod test {
use std::sync::Arc;

use arrow::array::{ArrayRef, StringArray};
use datafusion::{
common::{Result, ScalarValue},
physical_plan::ColumnarValue,
};

use crate::spark_initcap::string_initcap;

#[test]
fn test_initcap_array() -> Result<()> {
let input_data = vec![
None,
Some(""),
Some("hI THOmAS"),
Some("James-Smith"),
Some("michael rose"),
Some("a1b2 c3D4"),
Some(" ---abc--- ABC --ABC-- a-b A B eB Ac c d"),
Some(" 世 界 世界 "),
];
let input_columnar_value = ColumnarValue::Array(Arc::new(StringArray::from(input_data)));

let result = string_initcap(&vec![input_columnar_value])?.into_array(6)?;

let expected_data = vec![
None,
Some(""),
Some("Hi Thomas"),
Some("James-smith"),
Some("Michael Rose"),
Some("A1b2 C3d4"),
Some(" ---abc--- Abc --abc-- A-b A B Eb Ac C D"),
Some(" 世 界 世界 "),
];
let expected: ArrayRef = Arc::new(StringArray::from(expected_data));
assert_eq!(&result, &expected);
Ok(())
}

#[test]
fn test_initcap_scalar() -> Result<()> {
let input_columnar_value = ColumnarValue::Scalar(ScalarValue::from("abC c3D4"));
let result = string_initcap(&vec![input_columnar_value])?.into_array(1)?;
let expected: ArrayRef = Arc::new(StringArray::from(vec![Some("Abc C3d4")]));
assert_eq!(&result, &expected);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,37 +312,53 @@ class AuronQuerySuite extends AuronQueryTest with BaseAuronSQLSuite with AuronSQ
}

test("initcap basic") {
Seq(
("select initcap('spark sql')", Row("Spark Sql")),
("select initcap('SPARK')", Row("Spark")),
("select initcap('sPaRk')", Row("Spark")),
("select initcap('')", Row("")),
("select initcap(null)", Row(null))).foreach { case (q, expected) =>
checkAnswer(sql(q), Seq(expected))
withTable("initcap_basic_tbl") {
sql(s"CREATE TABLE initcap_basic_tbl(id INT, txt STRING) USING parquet")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would better not to use parquet table to do testing, the test would fail on second run because it would create a directory on local disk, on second run, the directory name is already taken.

Use temp view or something similar would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion. We'll fix this in a follow-up PR.

sql(s"""
|INSERT INTO initcap_basic_tbl VALUES
| (1, 'spark sql'),
| (2, 'SPARK'),
| (3, 'sPaRk'),
| (4, ''),
| (5, NULL)
""".stripMargin)
checkSparkAnswerAndOperator("select id, initcap(txt) from initcap_basic_tbl")
}
}

test("initcap: word boundaries and punctuation") {
Seq(
("select initcap('hello world')", Row("Hello World")),
("select initcap('hello_world')", Row("Hello_world")),
("select initcap('über-alles')", Row("Über-alles")),
("select initcap('foo.bar/baz')", Row("Foo.bar/baz")),
("select initcap('v2Ray is COOL')", Row("V2ray Is Cool")),
("select initcap('rock''n''roll')", Row("Rocknroll")),
("select initcap('hi\\tthere')", Row("Hi\tthere")),
("select initcap('hi\\nthere')", Row("Hi\nthere"))).foreach { case (q, expected) =>
checkAnswer(sql(q), Seq(expected))
withTable("initcap_bound_tbl") {
sql(s"CREATE TABLE initcap_bound_tbl(id INT, txt STRING) USING parquet")
sql(s"""
|INSERT INTO initcap_bound_tbl VALUES
| (1, 'hello world'),
| (2, 'hello_world'),
| (3, 'über-alles'),
| (4, 'foo.bar/baz'),
| (5, 'v2Ray is COOL'),
| (6, 'rock''n''roll'),
| (7, 'hi\tthere'),
| (8, 'hi\nthere')
""".stripMargin)
checkSparkAnswerAndOperator("select id, initcap(txt) from initcap_bound_tbl")
}
}

test("initcap: mixed cases and edge cases") {
Seq(
("select initcap('a1b2 c3D4')", Row("A1b2 C3d4")),
("select initcap('---abc---')", Row("---abc---")),
("select initcap(' multiple spaces ')", Row(" Multiple Spaces "))).foreach {
case (q, expected) =>
checkAnswer(sql(q), Seq(expected))
withTable("initcap_mixed_tbl") {
sql(s"CREATE TABLE initcap_mixed_tbl(id INT, txt STRING) USING parquet")
sql(s"""
|INSERT INTO initcap_mixed_tbl VALUES
| (1, 'a1b2 c3D4'),
| (2, '---abc--- ABC --ABC-- 世界 世 界 '),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that! I think it would be better to remove the Chinese test cases here and use English instead. This way, other team members can more easily understand and verify the code during review. Thanks for understanding!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is ok to use chinese (or any other non-ascii characters) because we have to test with real unicode strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to @richox for the feedback. From the perspective of Unicode testing, I believe this is acceptable.

| (3, ' multiple spaces '),
| (4, 'AbCdE aBcDe'),
| (5, ' A B A b '),
| (6, 'aBćDe ab世De AbĆdE aB世De ÄBĆΔE'),
| (7, 'i\u0307onic FIDELİO'),
| (8, 'a🙃B🙃c 😄 😆')
""".stripMargin)
checkSparkAnswerAndOperator("select id, initcap(txt) from initcap_mixed_tbl")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,8 @@ object NativeConverters extends Logging {
buildExtScalarFunction("Spark_MD5", Seq(unpackBinaryTypeCast(_1)), StringType)
case Reverse(_1) =>
buildScalarFunction(pb.ScalarFunction.Reverse, Seq(unpackBinaryTypeCast(_1)), StringType)
case InitCap(_1) =>
buildScalarFunction(pb.ScalarFunction.InitCap, Seq(unpackBinaryTypeCast(_1)), StringType)
case e: InitCap =>
buildExtScalarFunction("Spark_InitCap", e.children, e.dataType)
case Sha2(_1, Literal(224, _)) =>
buildExtScalarFunction("Spark_Sha224", Seq(unpackBinaryTypeCast(_1)), StringType)
case Sha2(_1, Literal(0, _)) =>
Expand Down
Loading