Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function cleanups #262

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion core/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod datetime;
mod error;
mod function;
mod io;
Expand Down
14 changes: 8 additions & 6 deletions core/datetime.rs → core/vdbe/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use crate::types::OwnedValue;
use crate::Result;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Timelike, Utc};

pub fn get_date_from_time_value(time_value: &OwnedValue) -> Result<String> {
/// Implementation of the date() SQL function.
pub fn exec_date(time_value: &OwnedValue) -> Result<String> {
let dt = parse_naive_date_time(time_value);
match dt {
Some(dt) => Ok(get_date_from_naive_datetime(dt)),
None => Ok(String::new()),
}
}

pub fn get_time_from_datetime_value(time_value: &OwnedValue) -> Result<String> {
/// Implementation of the time() SQL function.
pub fn exec_time(time_value: &OwnedValue) -> Result<String> {
let dt = parse_naive_date_time(time_value);
match dt {
Some(dt) => Ok(get_time_from_naive_datetime(dt)),
Expand Down Expand Up @@ -363,7 +365,7 @@ mod tests {

for (input, expected) in test_cases {
assert_eq!(
get_date_from_time_value(&input).unwrap(),
exec_date(&input).unwrap(),
expected,
"Failed for input: {:?}",
input
Expand Down Expand Up @@ -402,7 +404,7 @@ mod tests {
];

for case in invalid_cases.iter() {
let result = get_date_from_time_value(case);
let result = exec_date(case);
assert!(
result.is_ok(),
"Error encountered while parsing time value {}: {}",
Expand Down Expand Up @@ -589,7 +591,7 @@ mod tests {

for (input, expected) in test_cases {
assert_eq!(
get_time_from_datetime_value(&input).unwrap(),
exec_time(&input).unwrap(),
expected,
"Failed for input: {:?}",
input
Expand Down Expand Up @@ -628,7 +630,7 @@ mod tests {
];

for case in invalid_cases.iter() {
let result = get_time_from_datetime_value(case);
let result = exec_time(case);
assert!(
result.is_ok(),
"Error encountered while parsing time value {}: {}",
Expand Down
19 changes: 10 additions & 9 deletions core/vdbe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub mod builder;
pub mod explain;
pub mod sorter;

use crate::datetime::{get_date_from_time_value, get_time_from_datetime_value};
mod datetime;

use crate::error::LimboError;
use crate::function::{AggFunc, ScalarFunc};
use crate::pseudo::PseudoCursor;
Expand All @@ -31,6 +32,8 @@ use crate::storage::{btree::BTreeCursor, pager::Pager};
use crate::types::{AggContext, Cursor, CursorResult, OwnedRecord, OwnedValue, Record};
use crate::Result;

use datetime::{exec_date, exec_time};

use regex::Regex;
use std::borrow::BorrowMut;
use std::cell::RefCell;
Expand Down Expand Up @@ -1277,13 +1280,12 @@ impl Program {
}
ScalarFunc::Date => {
if *start_reg == 0 {
let date_str = get_date_from_time_value(&OwnedValue::Text(Rc::new(
"now".to_string(),
)))?;
let date_str =
exec_date(&OwnedValue::Text(Rc::new("now".to_string())))?;
state.registers[*dest] = OwnedValue::Text(Rc::new(date_str));
} else {
let time_value = &state.registers[*start_reg];
let date_str = get_date_from_time_value(time_value);
let date_str = exec_date(time_value);
match date_str {
Ok(date) => {
state.registers[*dest] = OwnedValue::Text(Rc::new(date))
Expand All @@ -1300,13 +1302,12 @@ impl Program {
}
ScalarFunc::Time => {
if *start_reg == 0 {
let time_str = get_time_from_datetime_value(&OwnedValue::Text(
Rc::new("now".to_string()),
))?;
let time_str =
exec_time(&OwnedValue::Text(Rc::new("now".to_string())))?;
state.registers[*dest] = OwnedValue::Text(Rc::new(time_str));
} else {
let datetime_value = &state.registers[*start_reg];
let time_str = get_time_from_datetime_value(datetime_value);
let time_str = exec_time(datetime_value);
match time_str {
Ok(time) => {
state.registers[*dest] = OwnedValue::Text(Rc::new(time))
Expand Down
Loading