Skip to content

Commit

Permalink
[MINOR] Add ScalarValue::new_utf8, clean up creation of literals in…
Browse files Browse the repository at this point in the history
… casting tests (#3680)

* Add ScalarValue::new_utf8, clean up construction in tests

* Cleanup

* some more minor cleanup

* Update unwrap_cast
  • Loading branch information
alamb authored Oct 3, 2022
1 parent 1ed7a6f commit 8415368
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 152 deletions.
7 changes: 6 additions & 1 deletion datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum ScalarValue {
Float32(Option<f32>),
/// 64bit float
Float64(Option<f64>),
/// 128bit decimal, using the i128 to represent the decimal
/// 128bit decimal, using the i128 to represent the decimal, precision scale
Decimal128(Option<i128>, u8, u8),
/// signed 8bit int
Int8(Option<i8>),
Expand Down Expand Up @@ -816,6 +816,11 @@ impl ScalarValue {
)))
}

/// Returns a [`ScalarValue::Utf8`] representing `val`
pub fn new_utf8(val: impl Into<String>) -> Self {
ScalarValue::Utf8(Some(val.into()))
}

/// Returns a [`ScalarValue::IntervalYearMonth`] representing
/// `years` years and `months` months
pub fn new_interval_ym(years: i32, months: i32) -> Self {
Expand Down
32 changes: 9 additions & 23 deletions datafusion/optimizer/src/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ mod test {
use datafusion_expr::{
lit,
logical_plan::{EmptyRelation, Projection},
Expr, LogicalPlan, Operator, ReturnTypeFunction, ScalarFunctionImplementation,
ScalarUDF, Signature, Volatility,
Expr, LogicalPlan, ReturnTypeFunction, ScalarFunctionImplementation, ScalarUDF,
Signature, Volatility,
};
use std::sync::Arc;

Expand Down Expand Up @@ -484,11 +484,8 @@ mod test {
let empty = empty();
let return_type: ReturnTypeFunction =
Arc::new(move |_| Ok(Arc::new(DataType::Utf8)));
let fun: ScalarFunctionImplementation = Arc::new(move |_| {
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(
"a".to_string(),
))))
});
let fun: ScalarFunctionImplementation =
Arc::new(move |_| Ok(ColumnarValue::Scalar(ScalarValue::new_utf8("a"))));
let udf = Expr::ScalarUDF {
fun: Arc::new(ScalarUDF::new(
"TestScalarUDF",
Expand Down Expand Up @@ -538,16 +535,8 @@ mod test {
#[test]
fn binary_op_date32_add_interval() -> Result<()> {
//CAST(Utf8("1998-03-18") AS Date32) + IntervalDayTime("386547056640")
let expr = Expr::BinaryExpr {
left: Box::new(Expr::Cast {
expr: Box::new(lit("1998-03-18")),
data_type: DataType::Date32,
}),
op: Operator::Plus,
right: Box::new(Expr::Literal(ScalarValue::IntervalDayTime(Some(
386547056640,
)))),
};
let expr = cast(lit("1998-03-18"), DataType::Date32)
+ lit(ScalarValue::IntervalDayTime(Some(386547056640)));
let empty = Arc::new(LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: Arc::new(DFSchema::empty()),
Expand Down Expand Up @@ -665,7 +654,7 @@ mod test {
fn like_for_type_coercion() -> Result<()> {
// like : utf8 like "abc"
let expr = Box::new(col("a"));
let pattern = Box::new(lit(ScalarValue::Utf8(Some("abc".to_string()))));
let pattern = Box::new(lit(ScalarValue::new_utf8("abc")));
let like_expr = Expr::Like {
negated: false,
expr,
Expand Down Expand Up @@ -703,7 +692,7 @@ mod test {
);

let expr = Box::new(col("a"));
let pattern = Box::new(lit(ScalarValue::Utf8(Some("abc".to_string()))));
let pattern = Box::new(lit(ScalarValue::new_utf8("abc")));
let like_expr = Expr::Like {
negated: false,
expr,
Expand Down Expand Up @@ -792,10 +781,7 @@ mod test {
);
let mut rewriter = TypeCoercionRewriter { schema };
let expr = is_true(lit(12i32).eq(lit(13i64)));
let expected = is_true(
cast(lit(ScalarValue::Int32(Some(12))), DataType::Int64)
.eq(lit(ScalarValue::Int64(Some(13)))),
);
let expected = is_true(cast(lit(12i32), DataType::Int64).eq(lit(13i64)));
let result = expr.rewrite(&mut rewriter)?;
assert_eq!(expected, result);
Ok(())
Expand Down
Loading

0 comments on commit 8415368

Please sign in to comment.