Skip to content

Commit

Permalink
[task apache#9539] Move starts_with, to_hex, trim, upper to datafusio…
Browse files Browse the repository at this point in the history
…n-functions

Signed-off-by: tangruilin <tang.ruilin@foxmail.com>
  • Loading branch information
Tangruilin committed Mar 10, 2024
1 parent 88187d4 commit f14bb76
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
11 changes: 10 additions & 1 deletion datafusion/functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ authors = { workspace = true }
rust-version = { workspace = true }

[features]
# enable string functions
string_expressions = []
# enable core functions
core_expressions = []
# enable datetime functions
datetime_expressions = []
# Enable encoding by default so the doctests work. In general don't automatically enable all packages.
default = ["core_expressions", "datetime_expressions", "encoding_expressions", "math_expressions", "regex_expressions"]
default = [
"core_expressions",
"datetime_expressions",
"encoding_expressions",
"math_expressions",
"regex_expressions",
"string_expressions",
]
# enable encode/decode functions
encoding_expressions = ["base64", "hex"]
# enable math functions
Expand Down
7 changes: 6 additions & 1 deletion datafusion/functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ use log::debug;
#[macro_use]
pub mod macros;

#[cfg(feature = "string_expressions")]
pub mod string;
make_stub_package!(string, "string_expressions");

/// Core datafusion expressions
/// Enabled via feature flag `core_expressions`
#[cfg(feature = "core_expressions")]
Expand Down Expand Up @@ -137,7 +141,8 @@ pub fn register_all(registry: &mut dyn FunctionRegistry) -> Result<()> {
.chain(datetime::functions())
.chain(encoding::functions())
.chain(math::functions())
.chain(regex::functions());
.chain(regex::functions())
.chain(string::functions());

all_functions.try_for_each(|udf| {
let existing_udf = registry.register_udf(udf)?;
Expand Down
22 changes: 22 additions & 0 deletions datafusion/functions/src/string/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

//! "regx" DataFusion functions
pub mod starts_with;
// create UDFs
export_functions!();
79 changes: 79 additions & 0 deletions datafusion/functions/src/string/starts_with.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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 arrow::array::{Array, ArrayRef, OffsetSizeTrait};
use arrow::compute::kernels::regexp;
use arrow::datatypes::DataType;
use datafusion_common::exec_err;
use datafusion_common::ScalarValue;
use datafusion_common::{arrow_datafusion_err, plan_err};
use datafusion_common::{
cast::as_generic_string_array, internal_err, DataFusionError, Result,
};
use datafusion_expr::ColumnarValue;
use datafusion_expr::TypeSignature::*;
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
use std::any::Any;
use std::sync::Arc;

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

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

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

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

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

Ok(Boolean)
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
unimplemented!("starts_with")
}
}

#[cfg(test)]
mod tests {}

0 comments on commit f14bb76

Please sign in to comment.