From 80c048528c9428f00834aa6b482668d55de8aa4d Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Fri, 7 Jan 2022 12:23:14 -0500 Subject: [PATCH] refactor: Clean up code by using `lit` and `lit_timestamp_nano` rather than `Expr::Literal(ScalarValue(Some(...)))` (#3427) --- query/src/frontend/influxrpc.rs | 4 ++-- query/src/frontend/reorg.rs | 8 ++------ read_buffer/src/row_group.rs | 29 +++++++++++------------------ 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/query/src/frontend/influxrpc.rs b/query/src/frontend/influxrpc.rs index 7624e8eaee..c94ef4e805 100644 --- a/query/src/frontend/influxrpc.rs +++ b/query/src/frontend/influxrpc.rs @@ -1910,7 +1910,7 @@ impl<'a> ExprRewriter for FieldValueRewriter<'a> { op, right: right.as_expr(), }); - return Ok(Expr::Literal(ScalarValue::Boolean(Some(true)))); + return Ok(lit(true)); } } expr @@ -1954,7 +1954,7 @@ impl<'a> ExprRewriter for FieldColumnRewriter<'a> { if let Expr::Literal(ScalarValue::Utf8(Some(name))) = &**right { self.field_projections.insert(name.to_owned()); - return Ok(Expr::Literal(ScalarValue::Boolean(Some(true)))); + return Ok(lit(true)); } } expr diff --git a/query/src/frontend/reorg.rs b/query/src/frontend/reorg.rs index 52c5deca1e..de41187851 100644 --- a/query/src/frontend/reorg.rs +++ b/query/src/frontend/reorg.rs @@ -2,10 +2,7 @@ use std::sync::Arc; -use datafusion::{ - logical_plan::{col, Expr, LogicalPlan, LogicalPlanBuilder}, - scalar::ScalarValue, -}; +use datafusion::logical_plan::{col, lit_timestamp_nano, LogicalPlan, LogicalPlanBuilder}; use observability_deps::tracing::{debug, trace}; use schema::{sort::SortKey, Schema, TIME_COLUMN_NAME}; @@ -204,8 +201,7 @@ impl ReorgPlanner { trace!(output_schema=?schema, "Setting sort key on schema for split plan"); // time <= split_time - let ts_literal = Expr::Literal(ScalarValue::TimestampNanosecond(Some(split_time), None)); - let split_expr = col(TIME_COLUMN_NAME).lt_eq(ts_literal); + let split_expr = col(TIME_COLUMN_NAME).lt_eq(lit_timestamp_nano(split_time)); let plan = plan_builder.build().context(BuildingPlan)?; diff --git a/read_buffer/src/row_group.rs b/read_buffer/src/row_group.rs index df8ad9f576..54b9f0ce3d 100644 --- a/read_buffer/src/row_group.rs +++ b/read_buffer/src/row_group.rs @@ -4036,10 +4036,7 @@ west,host-c,pro,10,6 assert_eq!(result, to_map(vec![])); } - use datafusion::{ - logical_plan::{col, Expr}, - scalar::ScalarValue, - }; + use datafusion::logical_plan::{col, lit, lit_timestamp_nano}; use std::convert::TryFrom; #[test] @@ -4047,50 +4044,47 @@ west,host-c,pro,10,6 let cases = vec![ ( // a = 22 - col("a").eq(Expr::Literal(ScalarValue::Int64(Some(22)))), + col("a").eq(lit(22_i64)), BinaryExpr::from(("a", "=", 22_i64)), ), ( // a > 10 - col("a").gt(Expr::Literal(ScalarValue::Int64(Some(10)))), + col("a").gt(lit(10_i64)), BinaryExpr::from(("a", ">", 10_i64)), ), ( // 100 = c - Expr::Literal(ScalarValue::Int64(Some(100))).eq(col("c")), + lit(100_i64).eq(col("c")), BinaryExpr::from(("c", "=", 100_i64)), ), ( // 100 != c - Expr::Literal(ScalarValue::Int64(Some(100))).not_eq(col("c")), + lit(100_i64).not_eq(col("c")), BinaryExpr::from(("c", "!=", 100_i64)), ), ( // 100 < c - Expr::Literal(ScalarValue::Int64(Some(100))).lt(col("c")), + lit(100_i64).lt(col("c")), BinaryExpr::from(("c", ">", 100_i64)), ), ( // 100 <= c - Expr::Literal(ScalarValue::Int64(Some(100))).lt_eq(col("c")), + lit(100_i64).lt_eq(col("c")), BinaryExpr::from(("c", ">=", 100_i64)), ), ( // 100 >= c - Expr::Literal(ScalarValue::Int64(Some(100))).gt_eq(col("c")), + lit(100_i64).gt_eq(col("c")), BinaryExpr::from(("c", "<=", 100_i64)), ), ( // 100 > c - Expr::Literal(ScalarValue::Int64(Some(100))).gt(col("c")), + lit(100_i64).gt(col("c")), BinaryExpr::from(("c", "<", 100_i64)), ), ( // a = timestamp(100000) - col("a").eq(Expr::Literal(ScalarValue::TimestampNanosecond( - Some(1000000), - None, - ))), + col("a").eq(lit_timestamp_nano(1000000)), BinaryExpr::from(("a", "=", 1000000_i64)), ), ]; @@ -4102,8 +4096,7 @@ west,host-c,pro,10,6 // Error cases let cases = vec![ // 33 = 33 - Expr::Literal(ScalarValue::Int64(Some(33))) - .eq(Expr::Literal(ScalarValue::Int64(Some(33)))), + lit(33i64).eq(lit(33i64)), // a > b col("a").gt(col("b")), ];