From a51957487a56b2efefaadfd302d6dc548bae3937 Mon Sep 17 00:00:00 2001 From: 0xhsn Date: Sat, 16 May 2026 00:30:39 +0200 Subject: [PATCH] fix: evaluate CURRENT_TIMESTAMP/CURRENT_DATE/CURRENT_TIME at runtime, not compile time --- core/translate/expr.rs | 42 +++++++++++-------- .../tests/scalar-functions-datetime.sqltest | 37 ++++++++++++++++ 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/core/translate/expr.rs b/core/translate/expr.rs index aa446b75f..30e5c4ea6 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -13,7 +13,6 @@ use crate::function::FtsFunc; #[cfg(feature = "json")] use crate::function::JsonFunc; use crate::function::{AggFunc, Func, FuncCtx, MathFuncArity, ScalarFunc, VectorFunc}; -use crate::functions::datetime; use crate::schema::{ BTreeTable, ColDef, Column, ColumnLayout, GeneratedType, Table, Type, TypeDef, }; @@ -7315,33 +7314,42 @@ pub fn emit_literal( Ok(target_register) } ast::Literal::CurrentDate => { - program.emit_insn(Insn::String8 { - value: datetime::exec_date::<&[_; 0], std::slice::Iter<'_, Value>, &Value>(&[]) - .to_string(), + let start_reg = program.alloc_register(); + program.emit_insn(Insn::Function { + constant_mask: 0, + start_reg, dest: target_register, + func: FuncCtx { + func: Func::Scalar(ScalarFunc::Date), + arg_count: 0, + }, }); Ok(target_register) } ast::Literal::CurrentTime => { - program.emit_insn(Insn::String8 { - value: datetime::exec_time::<&[_; 0], std::slice::Iter<'_, Value>, &Value>(&[]) - .to_string(), + let start_reg = program.alloc_register(); + program.emit_insn(Insn::Function { + constant_mask: 0, + start_reg, dest: target_register, + func: FuncCtx { + func: Func::Scalar(ScalarFunc::Time), + arg_count: 0, + }, }); Ok(target_register) } ast::Literal::CurrentTimestamp => { - program.emit_insn( - Insn::String8 { - value: datetime::exec_datetime_full::< - &[_; 0], - std::slice::Iter<'_, Value>, - &Value, - >(&[]) - .to_string(), - dest: target_register, + let start_reg = program.alloc_register(); + program.emit_insn(Insn::Function { + constant_mask: 0, + start_reg, + dest: target_register, + func: FuncCtx { + func: Func::Scalar(ScalarFunc::DateTime), + arg_count: 0, }, - ); + }); Ok(target_register) } } diff --git a/testing/sqltests/tests/scalar-functions-datetime.sqltest b/testing/sqltests/tests/scalar-functions-datetime.sqltest index 6a33dd231..bb928e8cb 100644 --- a/testing/sqltests/tests/scalar-functions-datetime.sqltest +++ b/testing/sqltests/tests/scalar-functions-datetime.sqltest @@ -2454,3 +2454,40 @@ test strftime-format-bool-false { expect { 0 } + +test current-timestamp-returns-datetime-format { + SELECT length(CURRENT_TIMESTAMP) = 19; +} +expect { + 1 +} + +test current-date-returns-date-format { + SELECT length(CURRENT_DATE) = 10; +} +expect { + 1 +} + +test current-time-returns-time-format { + SELECT length(CURRENT_TIME) = 8; +} +expect { + 1 +} + +test current-timestamp-matches-datetime { + SELECT CURRENT_TIMESTAMP = datetime('now'); +} +expect { + 1 +} + +test current-timestamp-in-insert { + CREATE TABLE ts_test (id INTEGER PRIMARY KEY, created_at TEXT DEFAULT CURRENT_TIMESTAMP); + INSERT INTO ts_test (id) VALUES (1); + SELECT length(created_at) = 19 FROM ts_test WHERE id = 1; +} +expect { + 1 +}