Skip to content

Commit

Permalink
feat: substitute __NOW__ to current timestamp in nanoseconds (#220)
Browse files Browse the repository at this point in the history
Co-authored-by: xxchan <xxchan22f@gmail.com>
  • Loading branch information
BugenZhao and xxchan authored Jun 28, 2024
1 parent 2441dff commit 2a451cc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions sqllogictest/src/substitution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/substitution/basic.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/substitution/substitution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl sqllogictest::DB for FakeDB {
x.to_string()
}
}
Some(("time", x)) => {
let _ = x.parse::<u128>().map_err(|_| FakeDBError)?;
x.to_string()
}
_ => return Err(FakeDBError),
};

Expand Down

0 comments on commit 2a451cc

Please sign in to comment.