Skip to content

Commit

Permalink
update uuid function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiayu Liu committed Oct 31, 2022
1 parent a505622 commit 32bb065
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion datafusion/expr/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub fn return_type(
utf8_to_int_type(&input_expr_types[0], "octet_length")
}
BuiltinScalarFunction::Random => Ok(DataType::Float64),
BuiltinScalarFunction::Uuid => Ok(DataType::Utf8),
BuiltinScalarFunction::RegexpReplace => {
utf8_to_str_type(&input_expr_types[0], "regex_replace")
}
Expand Down Expand Up @@ -191,7 +192,6 @@ pub fn return_type(
BuiltinScalarFunction::StartsWith => Ok(DataType::Boolean),
BuiltinScalarFunction::Strpos => utf8_to_int_type(&input_expr_types[0], "strpos"),
BuiltinScalarFunction::Substr => utf8_to_str_type(&input_expr_types[0], "substr"),
BuiltinScalarFunction::Uuid => Ok(DataType::Utf8),
BuiltinScalarFunction::ToHex => Ok(match input_expr_types[0] {
DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => {
DataType::Utf8
Expand Down
14 changes: 13 additions & 1 deletion datafusion/physical-expr/src/string_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use datafusion_common::ScalarValue;
use datafusion_common::{DataFusionError, Result};
use datafusion_expr::ColumnarValue;
use std::any::type_name;
use std::iter;
use std::sync::Arc;
use uuid::Uuid;

Expand Down Expand Up @@ -591,5 +592,16 @@ pub fn upper(args: &[ColumnarValue]) -> Result<ColumnarValue> {
/// Prints random (v4) uuid values per row
/// uuid() = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
pub fn uuid(args: &[ColumnarValue]) -> Result<ColumnarValue> {
handle(args, |_| Uuid::new_v4().to_string(), "uuid")
let len: usize = match &args[0] {
ColumnarValue::Array(array) => array.len(),
_ => {
return Err(DataFusionError::Internal(
"Expect uuid function to take no param".to_string(),
))
}
};

let values = iter::repeat_with(|| Uuid::new_v4().to_string()).take(len);
let array = GenericStringArray::<i32>::from_iter_values(values);
Ok(ColumnarValue::Array(Arc::new(array)))
}
1 change: 1 addition & 0 deletions datafusion/proto/src/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ pub fn parse_expr(
parse_expr(&args[1], registry)?,
)),
ScalarFunction::Random => Ok(random()),
ScalarFunction::Uuid => Ok(uuid()),
ScalarFunction::Repeat => Ok(repeat(
parse_expr(&args[0], registry)?,
parse_expr(&args[1], registry)?,
Expand Down

0 comments on commit 32bb065

Please sign in to comment.