Skip to content
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

Move ascii function to datafusion_functions #9740

Merged
merged 6 commits into from
Mar 22, 2024
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
8 changes: 1 addition & 7 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ pub enum BuiltinScalarFunction {
Cot,

// string functions
/// ascii
Ascii,
/// bit_length
BitLength,
/// character_length
Expand Down Expand Up @@ -240,7 +238,6 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Cbrt => Volatility::Immutable,
BuiltinScalarFunction::Cot => Volatility::Immutable,
BuiltinScalarFunction::Trunc => Volatility::Immutable,
BuiltinScalarFunction::Ascii => Volatility::Immutable,
BuiltinScalarFunction::BitLength => Volatility::Immutable,
BuiltinScalarFunction::CharacterLength => Volatility::Immutable,
BuiltinScalarFunction::Chr => Volatility::Immutable,
Expand Down Expand Up @@ -290,7 +287,6 @@ impl BuiltinScalarFunction {
// the return type of the built in function.
// Some built-in functions' return type depends on the incoming type.
match self {
BuiltinScalarFunction::Ascii => Ok(Int32),
BuiltinScalarFunction::BitLength => {
utf8_to_int_type(&input_expr_types[0], "bit_length")
}
Expand Down Expand Up @@ -429,8 +425,7 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Coalesce => {
Signature::variadic_equal(self.volatility())
}
BuiltinScalarFunction::Ascii
| BuiltinScalarFunction::BitLength
BuiltinScalarFunction::BitLength
| BuiltinScalarFunction::CharacterLength
| BuiltinScalarFunction::InitCap
| BuiltinScalarFunction::Lower
Expand Down Expand Up @@ -677,7 +672,6 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Coalesce => &["coalesce"],

// string functions
BuiltinScalarFunction::Ascii => &["ascii"],
BuiltinScalarFunction::BitLength => &["bit_length"],
BuiltinScalarFunction::CharacterLength => {
&["character_length", "char_length", "length"]
Expand Down
2 changes: 0 additions & 2 deletions datafusion/expr/src/expr_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ scalar_expr!(Uuid, uuid, , "returns uuid v4 as a string value");
scalar_expr!(Log, log, base x, "logarithm of a `x` for a particular `base`");

// string functions
scalar_expr!(Ascii, ascii, chr, "ASCII code value of the character");
scalar_expr!(
BitLength,
bit_length,
Expand Down Expand Up @@ -1063,7 +1062,6 @@ mod test {
test_scalar_expr!(Nanvl, nanvl, x, y);
test_scalar_expr!(Iszero, iszero, input);

test_scalar_expr!(Ascii, ascii, input);
test_scalar_expr!(BitLength, bit_length, string);
test_scalar_expr!(CharacterLength, character_length, string);
test_scalar_expr!(Chr, chr, string);
Expand Down
91 changes: 91 additions & 0 deletions datafusion/functions/src/string/ascii.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 crate::string::common::make_scalar_function;
use arrow::array::Int32Array;
use arrow::array::{ArrayRef, OffsetSizeTrait};
use arrow::datatypes::DataType;
use datafusion_common::{cast::as_generic_string_array, internal_err, Result};
use datafusion_expr::ColumnarValue;
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
use std::any::Any;
use std::sync::Arc;

/// Returns the numeric code of the first character of the argument.
/// ascii('x') = 120
pub fn ascii<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
let string_array = as_generic_string_array::<T>(&args[0])?;

let result = string_array
.iter()
.map(|string| {
string.map(|string: &str| {
let mut chars = string.chars();
chars.next().map_or(0, |v| v as i32)
})
})
.collect::<Int32Array>();

Ok(Arc::new(result) as ArrayRef)
}

#[derive(Debug)]
pub(super) struct AsciiFunc {
signature: Signature,
}
impl AsciiFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::uniform(
1,
vec![Utf8, LargeUtf8],
Volatility::Immutable,
),
}
}
}

impl ScalarUDFImpl for AsciiFunc {
fn as_any(&self) -> &dyn Any {
self
}

fn name(&self) -> &str {
"ascii"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
use DataType::*;

Ok(Int32)
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
match args[0].data_type() {
DataType::Utf8 => make_scalar_function(ascii::<i32>, vec![])(args),
DataType::LargeUtf8 => {
return make_scalar_function(ascii::<i64>, vec![])(args);
}
_ => internal_err!("Unsupported data type"),
}
}
}
17 changes: 16 additions & 1 deletion datafusion/functions/src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::Arc;

use datafusion_expr::ScalarUDF;

mod ascii;
mod btrim;
mod common;
mod ltrim;
Expand All @@ -30,6 +31,7 @@ mod to_hex;
mod upper;

// create UDFs
make_udf_function!(ascii::AsciiFunc, ASCII, ascii);
make_udf_function!(btrim::BTrimFunc, BTRIM, btrim);
make_udf_function!(ltrim::LtrimFunc, LTRIM, ltrim);
make_udf_function!(rtrim::RtrimFunc, RTRIM, rtrim);
Expand All @@ -40,6 +42,11 @@ make_udf_function!(upper::UpperFunc, UPPER, upper);
pub mod expr_fn {
use datafusion_expr::Expr;

#[doc = "Returns the numeric code of the first character of the argument."]
pub fn ascii(arg1: Expr) -> Expr {
super::ascii().call(vec![arg1])
}

#[doc = "Removes all characters, spaces by default, from both sides of a string"]
pub fn btrim(args: Vec<Expr>) -> Expr {
super::btrim().call(args)
Expand Down Expand Up @@ -73,5 +80,13 @@ pub mod expr_fn {

/// Return a list of all functions in this package
pub fn functions() -> Vec<Arc<ScalarUDF>> {
vec![btrim(), ltrim(), rtrim(), starts_with(), to_hex(), upper()]
vec![
ascii(),
btrim(),
ltrim(),
rtrim(),
starts_with(),
to_hex(),
upper(),
]
}
36 changes: 0 additions & 36 deletions datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,6 @@ pub fn create_physical_fun(
Arc::new(|args| make_scalar_function_inner(math_expressions::cot)(args))
}
// string functions
BuiltinScalarFunction::Ascii => Arc::new(|args| match args[0].data_type() {
DataType::Utf8 => {
make_scalar_function_inner(string_expressions::ascii::<i32>)(args)
}
DataType::LargeUtf8 => {
make_scalar_function_inner(string_expressions::ascii::<i64>)(args)
}
other => exec_err!("Unsupported data type {other:?} for function ascii"),
}),
BuiltinScalarFunction::BitLength => Arc::new(|args| match &args[0] {
ColumnarValue::Array(v) => Ok(ColumnarValue::Array(bit_length(v.as_ref())?)),
ColumnarValue::Scalar(v) => match v {
Expand Down Expand Up @@ -681,33 +672,6 @@ mod tests {

#[test]
fn test_functions() -> Result<()> {
test_function!(Ascii, &[lit("x")], Ok(Some(120)), i32, Int32, Int32Array);
test_function!(Ascii, &[lit("ésoj")], Ok(Some(233)), i32, Int32, Int32Array);
test_function!(
Ascii,
&[lit("💯")],
Ok(Some(128175)),
i32,
Int32,
Int32Array
);
test_function!(
Ascii,
&[lit("💯a")],
Ok(Some(128175)),
i32,
Int32,
Int32Array
);
test_function!(Ascii, &[lit("")], Ok(Some(0)), i32, Int32, Int32Array);
test_function!(
Ascii,
&[lit(ScalarValue::Utf8(None))],
Ok(None),
i32,
Int32,
Int32Array
);
test_function!(
BitLength,
&[lit("chars")],
Expand Down
18 changes: 0 additions & 18 deletions datafusion/physical-expr/src/string_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,6 @@ where
}
}

/// Returns the numeric code of the first character of the argument.
/// ascii('x') = 120
pub fn ascii<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
let string_array = as_generic_string_array::<T>(&args[0])?;

let result = string_array
.iter()
.map(|string| {
string.map(|string: &str| {
let mut chars = string.chars();
chars.next().map_or(0, |v| v as i32)
})
})
.collect::<Int32Array>();

Ok(Arc::new(result) as ArrayRef)
}

/// Returns the character with the given code. chr(0) is disallowed because text data types cannot store that character.
/// chr(65) = 'A'
pub fn chr(args: &[ArrayRef]) -> Result<ArrayRef> {
Expand Down
2 changes: 1 addition & 1 deletion datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ enum ScalarFunction {
// 1 was Acos
// 2 was Asin
Atan = 3;
Ascii = 4;
// 4 was Ascii
Ceil = 5;
Cos = 6;
// 7 was Digest
Expand Down
3 changes: 0 additions & 3 deletions datafusion/proto/src/generated/pbjson.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions datafusion/proto/src/generated/prost.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions datafusion/proto/src/logical_plan/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ use datafusion_expr::expr::Unnest;
use datafusion_expr::expr::{Alias, Placeholder};
use datafusion_expr::window_frame::{check_window_frame, regularize_window_order_by};
use datafusion_expr::{
acosh, ascii, asinh, atan, atan2, atanh, bit_length, cbrt, ceil, character_length,
chr, coalesce, concat_expr, concat_ws_expr, cos, cosh, cot, degrees, ends_with, exp,
acosh, asinh, atan, atan2, atanh, bit_length, cbrt, ceil, character_length, chr,
coalesce, concat_expr, concat_ws_expr, cos, cosh, cot, degrees, ends_with, exp,
expr::{self, InList, Sort, WindowFunction},
factorial, find_in_set, floor, gcd, initcap, iszero, lcm, left, levenshtein, ln, log,
log10, log2,
Expand Down Expand Up @@ -462,7 +462,6 @@ impl From<&protobuf::ScalarFunction> for BuiltinScalarFunction {
ScalarFunction::Lower => Self::Lower,
ScalarFunction::Log2 => Self::Log2,
ScalarFunction::Signum => Self::Signum,
ScalarFunction::Ascii => Self::Ascii,
ScalarFunction::BitLength => Self::BitLength,
ScalarFunction::CharacterLength => Self::CharacterLength,
ScalarFunction::Chr => Self::Chr,
Expand Down Expand Up @@ -1435,9 +1434,6 @@ pub fn parse_expr(
ScalarFunction::Lower => {
Ok(lower(parse_expr(&args[0], registry, codec)?))
}
ScalarFunction::Ascii => {
Ok(ascii(parse_expr(&args[0], registry, codec)?))
}
ScalarFunction::BitLength => {
Ok(bit_length(parse_expr(&args[0], registry, codec)?))
}
Expand Down
1 change: 0 additions & 1 deletion datafusion/proto/src/logical_plan/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,6 @@ impl TryFrom<&BuiltinScalarFunction> for protobuf::ScalarFunction {
BuiltinScalarFunction::Lower => Self::Lower,
BuiltinScalarFunction::Log2 => Self::Log2,
BuiltinScalarFunction::Signum => Self::Signum,
BuiltinScalarFunction::Ascii => Self::Ascii,
BuiltinScalarFunction::BitLength => Self::BitLength,
BuiltinScalarFunction::CharacterLength => Self::CharacterLength,
BuiltinScalarFunction::Chr => Self::Chr,
Expand Down
15 changes: 15 additions & 0 deletions datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,21 @@ SELECT ascii(NULL)
----
NULL

query I
SELECT ascii('ésoj')
----
233

query I
SELECT ascii('💯')
----
128175

query I
SELECT ascii('💯a')
----
128175

query I
SELECT bit_length('')
----
Expand Down
Loading