Skip to content

Commit 1c858ce

Browse files
author
Nga Tran
authored
feat: add lit_timestamp_nanosecond (apache#1030)
* feat: add lit_timestamp_nanosecond * refactor: make a traie for timestamp literal * refactor: cleanup * refactor: let nonly support appropriate numbers for now
1 parent 2392788 commit 1c858ce

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

datafusion/src/logical_plan/expr.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,11 @@ pub trait Literal {
13621362
fn lit(&self) -> Expr;
13631363
}
13641364

1365+
/// Trait for converting a type to a literal timestamp
1366+
pub trait TimestampLiteral {
1367+
fn lit_timestamp_nano(&self) -> Expr;
1368+
}
1369+
13651370
impl Literal for &str {
13661371
fn lit(&self) -> Expr {
13671372
Expr::Literal(ScalarValue::Utf8(Some((*self).to_owned())))
@@ -1391,6 +1396,19 @@ macro_rules! make_literal {
13911396
};
13921397
}
13931398

1399+
macro_rules! make_timestamp_literal {
1400+
($TYPE:ty, $SCALAR:ident, $DOC: expr) => {
1401+
#[doc = $DOC]
1402+
impl TimestampLiteral for $TYPE {
1403+
fn lit_timestamp_nano(&self) -> Expr {
1404+
Expr::Literal(ScalarValue::TimestampNanosecond(Some(
1405+
(self.clone()).into(),
1406+
)))
1407+
}
1408+
}
1409+
};
1410+
}
1411+
13941412
make_literal!(bool, Boolean, "literal expression containing a bool");
13951413
make_literal!(f32, Float32, "literal expression containing an f32");
13961414
make_literal!(f64, Float64, "literal expression containing an f64");
@@ -1403,11 +1421,24 @@ make_literal!(u16, UInt16, "literal expression containing a u16");
14031421
make_literal!(u32, UInt32, "literal expression containing a u32");
14041422
make_literal!(u64, UInt64, "literal expression containing a u64");
14051423

1424+
make_timestamp_literal!(i8, Int8, "literal expression containing an i8");
1425+
make_timestamp_literal!(i16, Int16, "literal expression containing an i16");
1426+
make_timestamp_literal!(i32, Int32, "literal expression containing an i32");
1427+
make_timestamp_literal!(i64, Int64, "literal expression containing an i64");
1428+
make_timestamp_literal!(u8, UInt8, "literal expression containing a u8");
1429+
make_timestamp_literal!(u16, UInt16, "literal expression containing a u16");
1430+
make_timestamp_literal!(u32, UInt32, "literal expression containing a u32");
1431+
14061432
/// Create a literal expression
14071433
pub fn lit<T: Literal>(n: T) -> Expr {
14081434
n.lit()
14091435
}
14101436

1437+
/// Create a literal timestamp expression
1438+
pub fn lit_timestamp_nano<T: TimestampLiteral>(n: T) -> Expr {
1439+
n.lit_timestamp_nano()
1440+
}
1441+
14111442
/// Concatenates the text representations of all the arguments. NULL arguments are ignored.
14121443
pub fn concat(args: &[Expr]) -> Expr {
14131444
Expr::ScalarFunction {
@@ -1871,6 +1902,21 @@ mod tests {
18711902
assert!(maybe_expr.is_err());
18721903
}
18731904

1905+
#[test]
1906+
fn test_lit_timestamp_nano() {
1907+
let expr = col("time").eq(lit_timestamp_nano(10)); // 10 is an implicit i32
1908+
let expected = col("time").eq(lit(ScalarValue::TimestampNanosecond(Some(10))));
1909+
assert_eq!(expr, expected);
1910+
1911+
let i: i64 = 10;
1912+
let expr = col("time").eq(lit_timestamp_nano(i));
1913+
assert_eq!(expr, expected);
1914+
1915+
let i: u32 = 10;
1916+
let expr = col("time").eq(lit_timestamp_nano(i));
1917+
assert_eq!(expr, expected);
1918+
}
1919+
18741920
#[test]
18751921
fn rewriter_visit() {
18761922
let mut rewriter = RecordingRewriter::default();

datafusion/src/logical_plan/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ pub use expr::{
3939
abs, acos, and, array, ascii, asin, atan, avg, binary_expr, bit_length, btrim, case,
4040
ceil, character_length, chr, col, columnize_expr, combine_filters, concat, concat_ws,
4141
cos, count, count_distinct, create_udaf, create_udf, date_part, date_trunc, exp,
42-
exprlist_to_fields, floor, in_list, initcap, left, length, lit, ln, log10, log2,
43-
lower, lpad, ltrim, max, md5, min, normalize_col, normalize_cols, now, octet_length,
44-
or, random, regexp_match, regexp_replace, repeat, replace, replace_col, reverse,
45-
right, round, rpad, rtrim, sha224, sha256, sha384, sha512, signum, sin, split_part,
46-
sqrt, starts_with, strpos, substr, sum, tan, to_hex, translate, trim, trunc,
47-
unnormalize_col, unnormalize_cols, upper, when, Column, Expr, ExprRewriter,
48-
ExpressionVisitor, Literal, Recursion, RewriteRecursion,
42+
exprlist_to_fields, floor, in_list, initcap, left, length, lit, lit_timestamp_nano,
43+
ln, log10, log2, lower, lpad, ltrim, max, md5, min, normalize_col, normalize_cols,
44+
now, octet_length, or, random, regexp_match, regexp_replace, repeat, replace,
45+
replace_col, reverse, right, round, rpad, rtrim, sha224, sha256, sha384, sha512,
46+
signum, sin, split_part, sqrt, starts_with, strpos, substr, sum, tan, to_hex,
47+
translate, trim, trunc, unnormalize_col, unnormalize_cols, upper, when, Column, Expr,
48+
ExprRewriter, ExpressionVisitor, Literal, Recursion, RewriteRecursion,
4949
};
5050
pub use extension::UserDefinedLogicalNode;
5151
pub use operators::Operator;

0 commit comments

Comments
 (0)