-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
192 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// 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. | ||
|
||
//! Metadata information for "encode" and "decode" functions | ||
use datafusion_common::arrow::datatypes::DataType; | ||
use datafusion_common::{plan_err, DataFusionError, Result}; | ||
use datafusion_expr::TypeSignature::*; | ||
use datafusion_expr::{ColumnarValue, FunctionImplementation, Signature, Volatility}; | ||
use std::sync::OnceLock; | ||
use DataType::*; | ||
|
||
pub(super) struct EncodeFunc {} | ||
|
||
static ENCODE_SIGNATURE: OnceLock<Signature> = OnceLock::new(); | ||
|
||
impl FunctionImplementation for EncodeFunc { | ||
fn name(&self) -> &str { | ||
"encode" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
ENCODE_SIGNATURE.get_or_init(|| { | ||
Signature::one_of( | ||
vec![ | ||
Exact(vec![Utf8, Utf8]), | ||
Exact(vec![LargeUtf8, Utf8]), | ||
Exact(vec![Binary, Utf8]), | ||
Exact(vec![LargeBinary, Utf8]), | ||
], | ||
Volatility::Immutable, | ||
) | ||
}) | ||
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(match arg_types[0] { | ||
Utf8 => Utf8, | ||
LargeUtf8 => LargeUtf8, | ||
Binary => Utf8, | ||
LargeBinary => LargeUtf8, | ||
Null => Null, | ||
_ => { | ||
return plan_err!("The encode function can only accept utf8 or binary."); | ||
} | ||
}) | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
// Put a feature flag here to make sure this is only compiled when the feature is activated | ||
super::inner::encode(args) | ||
} | ||
} | ||
|
||
pub(super) struct DecodeFunc {} | ||
|
||
static DECODE_SIGNATURE: OnceLock<Signature> = OnceLock::new(); | ||
|
||
impl FunctionImplementation for DecodeFunc { | ||
fn name(&self) -> &str { | ||
"decode" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
DECODE_SIGNATURE.get_or_init(|| { | ||
Signature::one_of( | ||
vec![ | ||
Exact(vec![Utf8, Utf8]), | ||
Exact(vec![LargeUtf8, Utf8]), | ||
Exact(vec![Binary, Utf8]), | ||
Exact(vec![LargeBinary, Utf8]), | ||
], | ||
Volatility::Immutable, | ||
) | ||
}) | ||
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(match arg_types[0] { | ||
Utf8 => Binary, | ||
LargeUtf8 => LargeBinary, | ||
Binary => Binary, | ||
LargeBinary => LargeBinary, | ||
Null => Null, | ||
_ => { | ||
return plan_err!("The decode function can only accept utf8 or binary."); | ||
} | ||
}) | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
// Put a feature flag here to make sure this is only compiled when the feature is activated | ||
super::inner::decode(args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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::datatypes::DataType; | ||
use datafusion_common::{exec_err, plan_err, DataFusionError, Result}; | ||
use datafusion_expr::{ | ||
ColumnarValue, FunctionImplementation, ScalarUDF, Signature, Volatility, | ||
}; | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, OnceLock}; | ||
|
||
/// Insert a function into the registry | ||
pub(crate) fn insert( | ||
registry: &mut HashMap<String, Arc<ScalarUDF>>, | ||
udf: impl FunctionImplementation + Send + Sync + 'static, | ||
) { | ||
let udf = ScalarUDF::new_from_impl(Arc::new(udf)); | ||
registry.insert(udf.name().to_string(), Arc::new(udf)); | ||
} | ||
|
||
/// A scalar function that always errors with a hint. This is used to stub out | ||
/// functions that are not enabled with the current set of crate features. | ||
pub struct StubFunc { | ||
name: &'static str, | ||
hint: &'static str, | ||
} | ||
|
||
impl StubFunc { | ||
/// Create a new stub function | ||
pub fn new(name: &'static str, hint: &'static str) -> Self { | ||
Self { name, hint } | ||
} | ||
} | ||
|
||
static STUB_SIGNATURE: OnceLock<Signature> = OnceLock::new(); | ||
|
||
impl FunctionImplementation for StubFunc { | ||
fn name(&self) -> &str { | ||
self.name | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
STUB_SIGNATURE.get_or_init(|| Signature::variadic_any(Volatility::Volatile)) | ||
} | ||
|
||
fn return_type(&self, _args: &[DataType]) -> Result<DataType> { | ||
plan_err!("function {} not available. {}", self.name, self.hint) | ||
} | ||
fn invoke(&self, _args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
exec_err!("function {} not available. {}", self.name, self.hint) | ||
} | ||
} |