Skip to content

Commit

Permalink
fix bug with to_timestamp and InitCap logical serialization, add …
Browse files Browse the repository at this point in the history
…roundtrip test between expression and proto, (#8868)

* add roundtrip test between expression and proto
---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
  • Loading branch information
Weijun-H and alamb authored Jan 17, 2024
1 parent ffaa679 commit 31094b0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions datafusion/proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ serde_json = { workspace = true, optional = true }

[dev-dependencies]
doc-comment = { workspace = true }
strum = { version = "0.25.0", features = ["derive"] }
tokio = "1.18"
17 changes: 13 additions & 4 deletions datafusion/proto/src/logical_plan/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ use datafusion_expr::{
current_date, current_time, date_bin, date_part, date_trunc, decode, degrees, digest,
encode, exp,
expr::{self, InList, Sort, WindowFunction},
factorial, find_in_set, flatten, floor, from_unixtime, gcd, gen_range, isnan, iszero,
lcm, left, levenshtein, ln, log, log10, log2,
factorial, find_in_set, flatten, floor, from_unixtime, gcd, gen_range, initcap,
isnan, iszero, lcm, left, levenshtein, ln, log, log10, log2,
logical_plan::{PlanType, StringifiedPlan},
lower, lpad, ltrim, md5, nanvl, now, nullif, octet_length, overlay, pi, power,
radians, random, regexp_match, regexp_replace, repeat, replace, reverse, right,
Expand Down Expand Up @@ -1585,7 +1585,7 @@ pub fn parse_expr(
Ok(character_length(parse_expr(&args[0], registry)?))
}
ScalarFunction::Chr => Ok(chr(parse_expr(&args[0], registry)?)),
ScalarFunction::InitCap => Ok(ascii(parse_expr(&args[0], registry)?)),
ScalarFunction::InitCap => Ok(initcap(parse_expr(&args[0], registry)?)),
ScalarFunction::Gcd => Ok(gcd(
parse_expr(&args[0], registry)?,
parse_expr(&args[1], registry)?,
Expand Down Expand Up @@ -1742,7 +1742,16 @@ pub fn parse_expr(
Ok(arrow_typeof(parse_expr(&args[0], registry)?))
}
ScalarFunction::ToTimestamp => {
Ok(to_timestamp_seconds(parse_expr(&args[0], registry)?))
let args: Vec<_> = args
.iter()
.map(|expr| parse_expr(expr, registry))
.collect::<Result<_, _>>()?;
Ok(Expr::ScalarFunction(
datafusion_expr::expr::ScalarFunction::new(
BuiltinScalarFunction::ToTimestamp,
args,
),
))
}
ScalarFunction::Flatten => Ok(flatten(parse_expr(&args[0], registry)?)),
ScalarFunction::StringToArray => Ok(string_to_array(
Expand Down
37 changes: 37 additions & 0 deletions datafusion/proto/tests/cases/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,40 @@ fn context_with_udf() -> SessionContext {

ctx
}

#[test]
fn test_expression_serialization_roundtrip() {
use datafusion_common::ScalarValue;
use datafusion_expr::expr::ScalarFunction;
use datafusion_expr::BuiltinScalarFunction;
use datafusion_proto::logical_plan::from_proto::parse_expr;
use datafusion_proto::protobuf::LogicalExprNode;
use strum::IntoEnumIterator;

let ctx = SessionContext::new();
let lit = Expr::Literal(ScalarValue::Utf8(None));
for builtin_fun in BuiltinScalarFunction::iter() {
// default to 4 args (though some exprs like substr have error checking)
let num_args = match builtin_fun {
BuiltinScalarFunction::Substr => 3,
_ => 4,
};
let args: Vec<_> = std::iter::repeat(&lit).take(num_args).cloned().collect();
let expr = Expr::ScalarFunction(ScalarFunction::new(builtin_fun, args));

let proto = LogicalExprNode::try_from(&expr).unwrap();
let deserialize = parse_expr(&proto, &ctx).unwrap();

let serialize_name = extract_function_name(&expr);
let deserialize_name = extract_function_name(&deserialize);

assert_eq!(serialize_name, deserialize_name);
}

/// Extracts the first part of a function name
/// 'foo(bar)' -> 'foo'
fn extract_function_name(expr: &Expr) -> String {
let name = expr.display_name().unwrap();
name.split('(').next().unwrap().to_string()
}
}

0 comments on commit 31094b0

Please sign in to comment.