use std::any::Any;
use std::sync::Arc;
use arrow::datatypes::DataType;
use datafusion_common::arrow::datatypes::{Field, FieldRef};
use datafusion_common::types::{NativeType, logical_string};
use datafusion_common::utils::take_function_args;
use datafusion_common::{Result, exec_err, internal_err};
use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext};
use datafusion_expr::{Coercion, Expr, ReturnFieldArgs, TypeSignatureClass, lit};
use datafusion_expr::{
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
};
use datafusion_functions::expr_fn::{decode, encode};
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SparkBase64 {
signature: Signature,
}
impl Default for SparkBase64 {
fn default() -> Self {
Self::new()
}
}
impl SparkBase64 {
pub fn new() -> Self {
Self {
signature: Signature::coercible(
vec![Coercion::new_implicit(
TypeSignatureClass::Binary,
vec![TypeSignatureClass::Native(logical_string())],
NativeType::Binary,
)],
Volatility::Immutable,
),
}
}
}
impl ScalarUDFImpl for SparkBase64 {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"base64"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
internal_err!("return_type should not be called for {}", self.name())
}
fn return_field_from_args(&self, args: ReturnFieldArgs<'_>) -> Result<FieldRef> {
let [bin] = take_function_args(self.name(), args.arg_fields)?;
let return_type = match bin.data_type() {
DataType::LargeBinary => DataType::LargeUtf8,
_ => DataType::Utf8,
};
Ok(Arc::new(Field::new(
self.name(),
return_type,
bin.is_nullable(),
)))
}
fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
exec_err!(
"invoke should not be called on a simplified {} function",
self.name()
)
}
fn simplify(
&self,
args: Vec<Expr>,
_info: &SimplifyContext,
) -> Result<ExprSimplifyResult> {
let [bin] = take_function_args(self.name(), args)?;
Ok(ExprSimplifyResult::Simplified(encode(
bin,
lit("base64pad"),
)))
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SparkUnBase64 {
signature: Signature,
}
impl Default for SparkUnBase64 {
fn default() -> Self {
Self::new()
}
}
impl SparkUnBase64 {
pub fn new() -> Self {
Self {
signature: Signature::coercible(
vec![Coercion::new_implicit(
TypeSignatureClass::Binary,
vec![TypeSignatureClass::Native(logical_string())],
NativeType::Binary,
)],
Volatility::Immutable,
),
}
}
}
impl ScalarUDFImpl for SparkUnBase64 {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"unbase64"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
internal_err!("return_type should not be called for {}", self.name())
}
fn return_field_from_args(&self, args: ReturnFieldArgs<'_>) -> Result<FieldRef> {
let [str] = take_function_args(self.name(), args.arg_fields)?;
let return_type = match str.data_type() {
DataType::LargeBinary => DataType::LargeBinary,
_ => DataType::Binary,
};
Ok(Arc::new(Field::new(
self.name(),
return_type,
str.is_nullable(),
)))
}
fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
exec_err!("{} should have been simplified", self.name())
}
fn simplify(
&self,
args: Vec<Expr>,
_info: &SimplifyContext,
) -> Result<ExprSimplifyResult> {
let [bin] = take_function_args(self.name(), args)?;
Ok(ExprSimplifyResult::Simplified(decode(
bin,
lit("base64pad"),
)))
}
}