Skip to content

Commit

Permalink
draft implement
Browse files Browse the repository at this point in the history
  • Loading branch information
goldmedal committed Jul 20, 2024
1 parent 827d0e3 commit 063b92e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
18 changes: 18 additions & 0 deletions datafusion/core/tests/dataframe/dataframe_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use datafusion_common::{DFSchema, ScalarValue};
use datafusion_expr::expr::Alias;
use datafusion_expr::ExprSchemable;
use datafusion_functions_aggregate::expr_fn::{approx_median, approx_percentile_cont};
use datafusion_functions_array::map::map;

fn test_schema() -> SchemaRef {
Arc::new(Schema::new(vec![
Expand Down Expand Up @@ -1087,3 +1088,20 @@ async fn test_fn_array_to_string() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn test_fn_map() -> Result<()> {
let expr = map(vec![lit("a"), lit("b"), lit("c")], vec![lit(1), lit(2), lit(3)]);
let expected = [
"+---------------------------------------------------------------------------------------+",
"| map(make_array(Utf8(\"a\"),Utf8(\"b\"),Utf8(\"c\")),make_array(Int32(1),Int32(2),Int32(3))) |",
"+---------------------------------------------------------------------------------------+",
"| {a: 1, b: 2, c: 3} |",
"| {a: 1, b: 2, c: 3} |",
"| {a: 1, b: 2, c: 3} |",
"| {a: 1, b: 2, c: 3} |",
"+---------------------------------------------------------------------------------------+",
];
assert_fn_batches!(expr, expected);
Ok(())
}
2 changes: 2 additions & 0 deletions datafusion/functions-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub mod set_ops;
pub mod sort;
pub mod string;
pub mod utils;
pub mod map;

use datafusion_common::Result;
use datafusion_execution::FunctionRegistry;
use datafusion_expr::ScalarUDF;
Expand Down
3 changes: 2 additions & 1 deletion datafusion/functions-array/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use datafusion_expr::{
planner::{ExprPlanner, PlannerResult, RawBinaryExpr, RawFieldAccessExpr},
sqlparser, Expr, ExprSchemable, GetFieldAccess,
};
use datafusion_functions::core::map_udf;
use datafusion_functions::expr_fn::get_field;
use datafusion_functions_aggregate::nth_value::nth_value_udaf;

Expand Down Expand Up @@ -112,7 +113,7 @@ impl ExprPlanner for ArrayFunctionPlanner {

Ok(PlannerResult::Planned(Expr::ScalarFunction(
ScalarFunction::new_udf(
datafusion_functions::core::map(),
map_udf(),
vec![keys, values],
),
)))
Expand Down
4 changes: 2 additions & 2 deletions datafusion/functions/benches/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use arrow_buffer::{OffsetBuffer, ScalarBuffer};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use datafusion_common::ScalarValue;
use datafusion_expr::ColumnarValue;
use datafusion_functions::core::map;
use datafusion_functions::core::map_udf;
use rand::prelude::ThreadRng;
use rand::Rng;
use std::sync::Arc;
Expand Down Expand Up @@ -68,7 +68,7 @@ fn criterion_benchmark(c: &mut Criterion) {

b.iter(|| {
black_box(
map()
map_udf()
.invoke(&[keys.clone(), values.clone()])
.expect("map should work on valid values"),
);
Expand Down
4 changes: 3 additions & 1 deletion datafusion/functions/src/core/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ use arrow::array::{Array, ArrayData, ArrayRef, MapArray, StructArray};
use arrow::datatypes::{DataType, Field, SchemaBuilder};
use arrow_buffer::{Buffer, ToByteSlice};

use datafusion_common::Result;
use datafusion_common::{exec_err, ScalarValue};
use datafusion_common::Result;
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};

make_udf_function!(MapFunc, MAP, map_udf);

/// Check if we can evaluate the expr to constant directly.
///
/// # Example
Expand Down
6 changes: 3 additions & 3 deletions datafusion/functions/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use datafusion_expr::ScalarUDF;
use std::sync::Arc;
pub use crate::core::map::map_udf;

pub mod arrow_cast;
pub mod arrowtypeof;
Expand All @@ -43,7 +44,6 @@ make_udf_function!(r#struct::StructFunc, STRUCT, r#struct);
make_udf_function!(named_struct::NamedStructFunc, NAMED_STRUCT, named_struct);
make_udf_function!(getfield::GetFieldFunc, GET_FIELD, get_field);
make_udf_function!(coalesce::CoalesceFunc, COALESCE, coalesce);
make_udf_function!(map::MapFunc, MAP, map);

pub mod expr_fn {
use datafusion_expr::{Expr, Literal};
Expand Down Expand Up @@ -81,7 +81,7 @@ pub mod expr_fn {
"Returns `coalesce(args...)`, which evaluates to the value of the first expr which is not NULL",
args,
),(
map,
map_udf,
"Returns a map created from a key list and a value list",
args,
));
Expand All @@ -102,6 +102,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
named_struct(),
get_field(),
coalesce(),
map(),
map_udf(),
]
}

0 comments on commit 063b92e

Please sign in to comment.