Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions core/translate/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)
}
}
Expand Down
37 changes: 37 additions & 0 deletions testing/sqltests/tests/scalar-functions-datetime.sqltest
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading