From 2a451cc44290d00162d0fd18f5b19befe906188d Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Fri, 28 Jun 2024 10:34:01 +0800 Subject: [PATCH] feat: substitute `__NOW__` to current timestamp in nanoseconds (#220) Co-authored-by: xxchan --- CHANGELOG.md | 2 ++ sqllogictest/src/substitution.rs | 7 +++++++ tests/substitution/basic.slt | 4 ++++ tests/substitution/substitution.rs | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d0243d..189f6e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +**Breaking changes**: * runner: `RecordOutput` is now returned by `Runner::run` (or `Runner::run_async`). This allows users to access the output of each record, or check whether the record is skipped. +* substitution: add a special variable `__NOW__` which will be replaced with the current Unix timestamp in nanoseconds. ## [0.20.6] - 2024-06-21 diff --git a/sqllogictest/src/substitution.rs b/sqllogictest/src/substitution.rs index f350163..d66041a 100644 --- a/sqllogictest/src/substitution.rs +++ b/sqllogictest/src/substitution.rs @@ -23,6 +23,13 @@ impl<'a> subst::VariableMap<'a> for Substitution { test_dir.path().to_string_lossy().into_owned().into() } + "__NOW__" => std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("failed to get current time") + .as_nanos() + .to_string() + .into(), + key => Env.get(key), } } diff --git a/tests/substitution/basic.slt b/tests/substitution/basic.slt index bc7d130..7ff4378 100644 --- a/tests/substitution/basic.slt +++ b/tests/substitution/basic.slt @@ -25,6 +25,10 @@ echo "$MY_USERNAME" statement ok path $__TEST_DIR__ +# a special variable `__NOW__` to get the current timestamp in nanoseconds +statement ok +time $__NOW__ + # non existent variables without default values are errors statement error No such variable check $NONEXISTENT_VARIABLE diff --git a/tests/substitution/substitution.rs b/tests/substitution/substitution.rs index 8b62d81..a7e5607 100644 --- a/tests/substitution/substitution.rs +++ b/tests/substitution/substitution.rs @@ -37,6 +37,10 @@ impl sqllogictest::DB for FakeDB { x.to_string() } } + Some(("time", x)) => { + let _ = x.parse::().map_err(|_| FakeDBError)?; + x.to_string() + } _ => return Err(FakeDBError), };