From 588ab1af0658ca8d4c3c19c10480e0dfee326733 Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Tue, 23 Jul 2024 23:14:36 +0300 Subject: [PATCH] Run formatting and enforce formatting in CI --- .github/workflows/rust.yml | 6 + core/datetime.rs | 264 ++++++++++++++++++++++++--------- core/function.rs | 10 +- core/lib.rs | 2 +- core/translate/expr.rs | 27 ++-- core/translate/mod.rs | 2 +- core/translate/where_clause.rs | 4 +- core/vdbe/builder.rs | 2 +- core/vdbe/explain.rs | 2 +- core/vdbe/mod.rs | 22 ++- simulator/main.rs | 2 +- 11 files changed, 244 insertions(+), 99 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d49f21d76..fd231cb9c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,6 +10,12 @@ env: CARGO_TERM_COLOR: always jobs: + cargo-fmt-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Check formatting + run: cargo fmt --check build-native: strategy: matrix: diff --git a/core/datetime.rs b/core/datetime.rs index 7e0c6fbdd..786d711c0 100644 --- a/core/datetime.rs +++ b/core/datetime.rs @@ -247,74 +247,198 @@ mod tests { let test_cases = [ // Format 1: YYYY-MM-DD (no timezone applicable) - (OwnedValue::Text(Rc::new("2024-07-21".to_string())), test_date_str), - + ( + OwnedValue::Text(Rc::new("2024-07-21".to_string())), + test_date_str, + ), // Format 2: YYYY-MM-DD HH:MM - (OwnedValue::Text(Rc::new("2024-07-21 22:30".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30+02:00".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30-05:00".to_string())), next_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 01:30+05:00".to_string())), prev_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30Z".to_string())), test_date_str), - + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30+02:00".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30-05:00".to_string())), + next_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 01:30+05:00".to_string())), + prev_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30Z".to_string())), + test_date_str, + ), // Format 3: YYYY-MM-DD HH:MM:SS - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45+02:00".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45-05:00".to_string())), next_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 01:30:45+05:00".to_string())), prev_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45Z".to_string())), test_date_str), - + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45+02:00".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45-05:00".to_string())), + next_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 01:30:45+05:00".to_string())), + prev_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45Z".to_string())), + test_date_str, + ), // Format 4: YYYY-MM-DD HH:MM:SS.SSS - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123+02:00".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123-05:00".to_string())), next_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 01:30:45.123+05:00".to_string())), prev_date_str), - (OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123Z".to_string())), test_date_str), - + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123+02:00".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123-05:00".to_string())), + next_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 01:30:45.123+05:00".to_string())), + prev_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21 22:30:45.123Z".to_string())), + test_date_str, + ), // Format 5: YYYY-MM-DDTHH:MM - (OwnedValue::Text(Rc::new("2024-07-21T22:30".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30+02:00".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30-05:00".to_string())), next_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T01:30+05:00".to_string())), prev_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30Z".to_string())), test_date_str), - + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30+02:00".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30-05:00".to_string())), + next_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T01:30+05:00".to_string())), + prev_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30Z".to_string())), + test_date_str, + ), // Format 6: YYYY-MM-DDTHH:MM:SS - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45+02:00".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45-05:00".to_string())), next_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T01:30:45+05:00".to_string())), prev_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45Z".to_string())), test_date_str), - + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45+02:00".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45-05:00".to_string())), + next_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T01:30:45+05:00".to_string())), + prev_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45Z".to_string())), + test_date_str, + ), // Format 7: YYYY-MM-DDTHH:MM:SS.SSS - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123+02:00".to_string())), test_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123-05:00".to_string())), next_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T01:30:45.123+05:00".to_string())), prev_date_str), - (OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123Z".to_string())), test_date_str), - + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123+02:00".to_string())), + test_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123-05:00".to_string())), + next_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T01:30:45.123+05:00".to_string())), + prev_date_str, + ), + ( + OwnedValue::Text(Rc::new("2024-07-21T22:30:45.123Z".to_string())), + test_date_str, + ), // Format 8: HH:MM (OwnedValue::Text(Rc::new("22:30".to_string())), "2000-01-01"), - (OwnedValue::Text(Rc::new("22:30+02:00".to_string())), "2000-01-01"), - (OwnedValue::Text(Rc::new("22:30-05:00".to_string())), "2000-01-02"), - (OwnedValue::Text(Rc::new("01:30+05:00".to_string())), "1999-12-31"), - (OwnedValue::Text(Rc::new("22:30Z".to_string())), "2000-01-01"), - + ( + OwnedValue::Text(Rc::new("22:30+02:00".to_string())), + "2000-01-01", + ), + ( + OwnedValue::Text(Rc::new("22:30-05:00".to_string())), + "2000-01-02", + ), + ( + OwnedValue::Text(Rc::new("01:30+05:00".to_string())), + "1999-12-31", + ), + ( + OwnedValue::Text(Rc::new("22:30Z".to_string())), + "2000-01-01", + ), // Format 9: HH:MM:SS - (OwnedValue::Text(Rc::new("22:30:45".to_string())), "2000-01-01"), - (OwnedValue::Text(Rc::new("22:30:45+02:00".to_string())), "2000-01-01"), - (OwnedValue::Text(Rc::new("22:30:45-05:00".to_string())), "2000-01-02"), - (OwnedValue::Text(Rc::new("01:30:45+05:00".to_string())), "1999-12-31"), - (OwnedValue::Text(Rc::new("22:30:45Z".to_string())), "2000-01-01"), - + ( + OwnedValue::Text(Rc::new("22:30:45".to_string())), + "2000-01-01", + ), + ( + OwnedValue::Text(Rc::new("22:30:45+02:00".to_string())), + "2000-01-01", + ), + ( + OwnedValue::Text(Rc::new("22:30:45-05:00".to_string())), + "2000-01-02", + ), + ( + OwnedValue::Text(Rc::new("01:30:45+05:00".to_string())), + "1999-12-31", + ), + ( + OwnedValue::Text(Rc::new("22:30:45Z".to_string())), + "2000-01-01", + ), // Format 10: HH:MM:SS.SSS - (OwnedValue::Text(Rc::new("22:30:45.123".to_string())), "2000-01-01"), - (OwnedValue::Text(Rc::new("22:30:45.123+02:00".to_string())), "2000-01-01"), - (OwnedValue::Text(Rc::new("22:30:45.123-05:00".to_string())), "2000-01-02"), - (OwnedValue::Text(Rc::new("01:30:45.123+05:00".to_string())), "1999-12-31"), - (OwnedValue::Text(Rc::new("22:30:45.123Z".to_string())), "2000-01-01"), - + ( + OwnedValue::Text(Rc::new("22:30:45.123".to_string())), + "2000-01-01", + ), + ( + OwnedValue::Text(Rc::new("22:30:45.123+02:00".to_string())), + "2000-01-01", + ), + ( + OwnedValue::Text(Rc::new("22:30:45.123-05:00".to_string())), + "2000-01-02", + ), + ( + OwnedValue::Text(Rc::new("01:30:45.123+05:00".to_string())), + "1999-12-31", + ), + ( + OwnedValue::Text(Rc::new("22:30:45.123Z".to_string())), + "2000-01-01", + ), // Test Format 11: 'now' (OwnedValue::Text(Rc::new("now".to_string())), &now), - // Format 12: DDDDDDDDDD (Julian date as float or integer) (OwnedValue::Float(2460512.5), test_date_str), (OwnedValue::Integer(2460513), test_date_str), @@ -333,13 +457,13 @@ mod tests { #[test] fn test_invalid_get_date_from_time_value() { let invalid_cases = [ - OwnedValue::Text(Rc::new("2024-07-21 25:00".to_string())), // Invalid hour - OwnedValue::Text(Rc::new("2024-07-21 24:00:00".to_string())), // Invalid hour - OwnedValue::Text(Rc::new("2024-07-21 23:60:00".to_string())), // Invalid minute - OwnedValue::Text(Rc::new("2024-07-21 22:58:60".to_string())), // Invalid second - OwnedValue::Text(Rc::new("2024-07-32".to_string())), // Invalid day - OwnedValue::Text(Rc::new("2024-13-01".to_string())), // Invalid month - OwnedValue::Text(Rc::new("invalid_date".to_string())), // Completely invalid string + OwnedValue::Text(Rc::new("2024-07-21 25:00".to_string())), // Invalid hour + OwnedValue::Text(Rc::new("2024-07-21 24:00:00".to_string())), // Invalid hour + OwnedValue::Text(Rc::new("2024-07-21 23:60:00".to_string())), // Invalid minute + OwnedValue::Text(Rc::new("2024-07-21 22:58:60".to_string())), // Invalid second + OwnedValue::Text(Rc::new("2024-07-32".to_string())), // Invalid day + OwnedValue::Text(Rc::new("2024-13-01".to_string())), // Invalid month + OwnedValue::Text(Rc::new("invalid_date".to_string())), // Completely invalid string OwnedValue::Text(Rc::new("".to_string())), // Empty string OwnedValue::Integer(i64::MAX), // Large Julian day OwnedValue::Integer(-1), // Negative Julian day @@ -349,17 +473,15 @@ mod tests { OwnedValue::Float(f64::INFINITY), // Infinity OwnedValue::Null, // Null value OwnedValue::Blob(vec![1, 2, 3].into()), // Blob (unsupported type) - // Invalid timezone tests - OwnedValue::Text(Rc::new("2024-07-21T12:00:00+24:00".to_string())), // Invalid timezone offset (too large) - OwnedValue::Text(Rc::new("2024-07-21T12:00:00-24:00".to_string())), // Invalid timezone offset (too small) - OwnedValue::Text(Rc::new("2024-07-21T12:00:00+00:60".to_string())), // Invalid timezone minutes + OwnedValue::Text(Rc::new("2024-07-21T12:00:00+24:00".to_string())), // Invalid timezone offset (too large) + OwnedValue::Text(Rc::new("2024-07-21T12:00:00-24:00".to_string())), // Invalid timezone offset (too small) + OwnedValue::Text(Rc::new("2024-07-21T12:00:00+00:60".to_string())), // Invalid timezone minutes OwnedValue::Text(Rc::new("2024-07-21T12:00:00+00:00:00".to_string())), // Invalid timezone format (extra seconds) - OwnedValue::Text(Rc::new("2024-07-21T12:00:00+".to_string())), // Incomplete timezone - OwnedValue::Text(Rc::new("2024-07-21T12:00:00+Z".to_string())), // Invalid timezone format + OwnedValue::Text(Rc::new("2024-07-21T12:00:00+".to_string())), // Incomplete timezone + OwnedValue::Text(Rc::new("2024-07-21T12:00:00+Z".to_string())), // Invalid timezone format OwnedValue::Text(Rc::new("2024-07-21T12:00:00+00:00Z".to_string())), // Mixing offset and Z - OwnedValue::Text(Rc::new("2024-07-21T12:00:00UTC".to_string())), // Named timezone (not supported) - + OwnedValue::Text(Rc::new("2024-07-21T12:00:00UTC".to_string())), // Named timezone (not supported) ]; for case in invalid_cases.iter() { diff --git a/core/function.rs b/core/function.rs index 7540bc492..2b8be15c7 100644 --- a/core/function.rs +++ b/core/function.rs @@ -1,4 +1,3 @@ - #[derive(Debug, Clone, PartialEq)] pub enum AggFunc { Avg, @@ -67,14 +66,13 @@ pub enum Func { SingleRow(SingleRowFunc), } -impl Func{ - - pub fn resolve_function(name: &str, arg_count:usize) -> Result{ +impl Func { + pub fn resolve_function(name: &str, arg_count: usize) -> Result { match name { "avg" => Ok(Func::Agg(AggFunc::Avg)), "count" => Ok(Func::Agg(AggFunc::Count)), "group_concat" => Ok(Func::Agg(AggFunc::GroupConcat)), - "max" if arg_count == 0 || arg_count == 1 => Ok(Func::Agg(AggFunc::Max)), + "max" if arg_count == 0 || arg_count == 1 => Ok(Func::Agg(AggFunc::Max)), "max" if arg_count > 1 => Ok(Func::SingleRow(SingleRowFunc::Max)), "min" if arg_count == 0 || arg_count == 1 => Ok(Func::Agg(AggFunc::Min)), "min" if arg_count > 1 => Ok(Func::SingleRow(SingleRowFunc::Min)), @@ -94,4 +92,4 @@ impl Func{ _ => Err(()), } } -} \ No newline at end of file +} diff --git a/core/lib.rs b/core/lib.rs index 315e149fd..56549921f 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -1,5 +1,6 @@ mod btree; mod buffer_pool; +mod datetime; mod function; mod io; mod pager; @@ -11,7 +12,6 @@ mod translate; mod types; mod util; mod vdbe; -mod datetime; #[cfg(not(target_family = "wasm"))] #[global_allocator] diff --git a/core/translate/expr.rs b/core/translate/expr.rs index 18b8b7880..32fc3053e 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -6,7 +6,7 @@ use crate::{ schema::{Schema, Table, Type}, translate::select::{ColumnInfo, Select, SrcTable}, util::normalize_ident, - vdbe::{BranchOffset, Insn, builder::ProgramBuilder}, + vdbe::{builder::ProgramBuilder, BranchOffset, Insn}, }; pub fn build_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result> { @@ -226,7 +226,8 @@ pub fn translate_expr( filter_over: _, } => { let args_count = if let Some(args) = args { args.len() } else { 0 }; - let func_type: Option = Func::resolve_function(normalize_ident(name.0.as_str()).as_str(),args_count).ok(); + let func_type: Option = + Func::resolve_function(normalize_ident(name.0.as_str()).as_str(), args_count).ok(); match func_type { Some(Func::Agg(_)) => { @@ -355,10 +356,16 @@ pub fn translate_expr( anyhow::bail!("Parse error: date function with > 1 arguments. Modifiers are not yet supported."); } else if args.len() == 1 { let arg_reg = program.alloc_register(); - let _ = translate_expr(program, select, &args[0], arg_reg, cursor_hint)?; + let _ = translate_expr( + program, + select, + &args[0], + arg_reg, + cursor_hint, + )?; start_reg = arg_reg; - } - } + } + } program.emit_insn(Insn::Function { start_reg: start_reg, dest: target_register, @@ -605,10 +612,12 @@ pub fn analyze_expr<'a>(expr: &'a Expr, column_info_out: &mut ColumnInfo<'a>) { filter_over: _, } => { let args_count = if let Some(args) = args { args.len() } else { 0 }; - let func_type = match Func::resolve_function(normalize_ident(name.0.as_str()).as_str(),args_count) { - Ok(func) => Some(func), - Err(_) => None, - }; + let func_type = + match Func::resolve_function(normalize_ident(name.0.as_str()).as_str(), args_count) + { + Ok(func) => Some(func), + Err(_) => None, + }; if func_type.is_none() { let args = args.as_ref().unwrap(); if !args.is_empty() { diff --git a/core/translate/mod.rs b/core/translate/mod.rs index 68fa38d04..737573fc6 100644 --- a/core/translate/mod.rs +++ b/core/translate/mod.rs @@ -15,7 +15,7 @@ use crate::translate::where_clause::{ }; use crate::types::{OwnedRecord, OwnedValue}; use crate::util::normalize_ident; -use crate::vdbe::{BranchOffset, Insn, Program, builder::ProgramBuilder}; +use crate::vdbe::{builder::ProgramBuilder, BranchOffset, Insn, Program}; use anyhow::Result; use expr::{build_select, maybe_apply_affinity, translate_expr}; use sqlite3_parser::ast::{self, Literal}; diff --git a/core/translate/where_clause.rs b/core/translate/where_clause.rs index 062d0f124..977624b18 100644 --- a/core/translate/where_clause.rs +++ b/core/translate/where_clause.rs @@ -2,10 +2,10 @@ use anyhow::Result; use sqlite3_parser::ast::{self, JoinOperator}; use crate::{ - translate::expr::{resolve_ident_qualified, resolve_ident_table, translate_expr}, function::SingleRowFunc, + translate::expr::{resolve_ident_qualified, resolve_ident_table, translate_expr}, translate::select::Select, - vdbe::{BranchOffset, Insn, builder::ProgramBuilder}, + vdbe::{builder::ProgramBuilder, BranchOffset, Insn}, }; const HARDCODED_CURSOR_LEFT_TABLE: usize = 0; diff --git a/core/vdbe/builder.rs b/core/vdbe/builder.rs index 9305b65da..b39e3e9da 100644 --- a/core/vdbe/builder.rs +++ b/core/vdbe/builder.rs @@ -292,4 +292,4 @@ impl ProgramBuilder { cursor_ref: self.cursor_ref, } } -} \ No newline at end of file +} diff --git a/core/vdbe/explain.rs b/core/vdbe/explain.rs index c895794b4..8f29c360b 100644 --- a/core/vdbe/explain.rs +++ b/core/vdbe/explain.rs @@ -1,4 +1,4 @@ -use super::{Program, InsnReference, Insn, OwnedValue}; +use super::{Insn, InsnReference, OwnedValue, Program}; use std::rc::Rc; pub fn insn_to_str(program: &Program, addr: InsnReference, insn: &Insn, indent: String) -> String { diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index c103b770b..717048104 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -1101,15 +1101,22 @@ impl Program { } SingleRowFunc::Date => { if *start_reg == 0 { - let date_str = get_date_from_time_value(&OwnedValue::Text(Rc::new("now".to_string())))?; + let date_str = get_date_from_time_value(&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); match date_str { - Ok(date) => state.registers[*dest] = OwnedValue::Text(Rc::new(date)), + Ok(date) => { + state.registers[*dest] = OwnedValue::Text(Rc::new(date)) + } Err(e) => { - anyhow::bail!("Error encountered while parsing time value: {}", e) + anyhow::bail!( + "Error encountered while parsing time value: {}", + e + ) } } } @@ -1141,7 +1148,10 @@ fn trace_insn(program: &Program, addr: InsnReference, insn: &Insn) { if !log::log_enabled!(log::Level::Trace) { return; } - log::trace!("{}", explain::insn_to_str(program, addr, insn, String::new())); + log::trace!( + "{}", + explain::insn_to_str(program, addr, insn, String::new()) + ); } fn print_insn(program: &Program, addr: InsnReference, insn: &Insn, indent: String) { @@ -1285,8 +1295,8 @@ fn exec_if(reg: &OwnedValue, null_reg: &OwnedValue, not: bool) -> bool { #[cfg(test)] mod tests { use super::{ - exec_abs, exec_if, exec_length, exec_like, exec_lower, exec_minmax, exec_random, exec_round, exec_trim, - exec_upper, OwnedValue, + exec_abs, exec_if, exec_length, exec_like, exec_lower, exec_minmax, exec_random, + exec_round, exec_trim, exec_upper, OwnedValue, }; use std::rc::Rc; diff --git a/simulator/main.rs b/simulator/main.rs index 2bb6203d9..6bcfa1f9f 100644 --- a/simulator/main.rs +++ b/simulator/main.rs @@ -42,7 +42,7 @@ fn main() { break; } } - }, + } Err(_) => { continue; }