From e988ca0129ddfb3ec2fcce6f5bc0c5c929f7b1ca Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 4 Jul 2024 12:19:17 +0300 Subject: [PATCH] Consolidate AggregateFunction and AggFunc enums --- core/function.rs | 25 +++++++++++++++++ core/lib.rs | 1 + core/translate.rs | 68 ++++++++++++++++++++--------------------------- core/vdbe.rs | 24 +++++++---------- 4 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 core/function.rs diff --git a/core/function.rs b/core/function.rs new file mode 100644 index 000000000..f7d1f52ad --- /dev/null +++ b/core/function.rs @@ -0,0 +1,25 @@ +pub enum AggFunc { + Avg, + Count, + GroupConcat, + Max, + Min, + StringAgg, + Sum, + Total, +} + +impl AggFunc { + pub fn to_string(&self) -> &str { + match self { + AggFunc::Avg => "avg", + AggFunc::Count => "count", + AggFunc::GroupConcat => "group_concat", + AggFunc::Max => "max", + AggFunc::Min => "min", + AggFunc::StringAgg => "string_agg", + AggFunc::Sum => "sum", + AggFunc::Total => "total", + } + } +} diff --git a/core/lib.rs b/core/lib.rs index 087220a81..3263cfe42 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -1,5 +1,6 @@ mod btree; mod buffer_pool; +mod function; mod io; mod pager; mod schema; diff --git a/core/translate.rs b/core/translate.rs index 493e1ad3d..9f7ea0de9 100644 --- a/core/translate.rs +++ b/core/translate.rs @@ -1,11 +1,12 @@ use std::cell::RefCell; use std::rc::Rc; +use crate::function::AggFunc; use crate::pager::Pager; use crate::schema::{Schema, Table}; use crate::sqlite3_ondisk::{DatabaseHeader, MIN_PAGE_CACHE_SIZE}; use crate::util::normalize_ident; -use crate::vdbe::{AggFunc, Insn, Program, ProgramBuilder}; +use crate::vdbe::{Insn, Program, ProgramBuilder}; use anyhow::Result; use sqlite3_parser::ast; @@ -17,19 +18,8 @@ struct Select<'a> { exist_aggregation: bool, } -enum AggregationFunc { - Avg, - Count, - GroupConcat, - Max, - Min, - StringAgg, - Sum, - Total, -} - struct ColumnInfo { - func: Option, + func: Option, args: Option>, columns_to_allocate: usize, /* number of result columns this col will result on */ } @@ -154,14 +144,14 @@ fn translate_select(select: Select) -> Result { for info in &select.column_info { if info.is_aggregation_function() { let func = match info.func.as_ref().unwrap() { - AggregationFunc::Avg => AggFunc::Avg, - AggregationFunc::Count => todo!(), - AggregationFunc::GroupConcat => todo!(), - AggregationFunc::Max => todo!(), - AggregationFunc::Min => todo!(), - AggregationFunc::StringAgg => todo!(), - AggregationFunc::Sum => AggFunc::Sum, - AggregationFunc::Total => todo!(), + AggFunc::Avg => AggFunc::Avg, + AggFunc::Count => todo!(), + AggFunc::GroupConcat => todo!(), + AggFunc::Max => todo!(), + AggFunc::Min => todo!(), + AggFunc::StringAgg => todo!(), + AggFunc::Sum => AggFunc::Sum, + AggFunc::Total => todo!(), }; program.emit_insn(Insn::AggFinal { register: target, @@ -338,14 +328,14 @@ fn analyze_column(column: &sqlite3_parser::ast::ResultColumn, column_info_out: & filter_over: _, } => { let func_type = match normalize_ident(name.0.as_str()).as_str() { - "avg" => Some(AggregationFunc::Avg), - "count" => Some(AggregationFunc::Count), - "group_concat" => Some(AggregationFunc::GroupConcat), - "max" => Some(AggregationFunc::Max), - "min" => Some(AggregationFunc::Min), - "string_agg" => Some(AggregationFunc::StringAgg), - "sum" => Some(AggregationFunc::Sum), - "total" => Some(AggregationFunc::Total), + "avg" => Some(AggFunc::Avg), + "count" => Some(AggFunc::Count), + "group_concat" => Some(AggFunc::GroupConcat), + "max" => Some(AggFunc::Max), + "min" => Some(AggFunc::Min), + "string_agg" => Some(AggFunc::StringAgg), + "sum" => Some(AggFunc::Sum), + "total" => Some(AggFunc::Total), _ => None, }; if func_type.is_none() { @@ -448,7 +438,7 @@ fn translate_aggregation( let func = info.func.as_ref().unwrap(); let args = info.args.as_ref().unwrap(); let dest = match func { - AggregationFunc::Avg => { + AggFunc::Avg => { if args.len() != 1 { anyhow::bail!("Parse error: avg bad number of arguments"); } @@ -458,16 +448,16 @@ fn translate_aggregation( program.emit_insn(Insn::AggStep { acc_reg: target_register, col: expr_reg, - func: crate::vdbe::AggFunc::Avg, + func: AggFunc::Avg, }); target_register } - AggregationFunc::Count => todo!(), - AggregationFunc::GroupConcat => todo!(), - AggregationFunc::Max => todo!(), - AggregationFunc::Min => todo!(), - AggregationFunc::StringAgg => todo!(), - AggregationFunc::Sum => { + AggFunc::Count => todo!(), + AggFunc::GroupConcat => todo!(), + AggFunc::Max => todo!(), + AggFunc::Min => todo!(), + AggFunc::StringAgg => todo!(), + AggFunc::Sum => { if args.len() != 1 { anyhow::bail!("Parse error: sum bad number of arguments"); } @@ -477,11 +467,11 @@ fn translate_aggregation( program.emit_insn(Insn::AggStep { acc_reg: target_register, col: expr_reg, - func: crate::vdbe::AggFunc::Sum, + func: AggFunc::Sum, }); target_register } - AggregationFunc::Total => todo!(), + AggFunc::Total => todo!(), }; Ok(dest) } diff --git a/core/vdbe.rs b/core/vdbe.rs index 88d0738fb..bee20d31e 100644 --- a/core/vdbe.rs +++ b/core/vdbe.rs @@ -1,4 +1,5 @@ use crate::btree::BTreeCursor; +use crate::function::AggFunc; use crate::pager::Pager; use crate::types::{AggContext, Cursor, CursorResult, OwnedValue, Record}; @@ -112,20 +113,6 @@ pub enum Insn { }, } -pub enum AggFunc { - Avg, - Sum, -} - -impl AggFunc { - fn to_string(&self) -> &str { - match self { - AggFunc::Avg => "avg", - AggFunc::Sum => "sum", - } - } -} - pub struct ProgramBuilder { next_free_register: usize, next_free_cursor_id: usize, @@ -376,6 +363,9 @@ impl Program { AggFunc::Sum => { OwnedValue::Agg(Box::new(AggContext::Sum(OwnedValue::Float(0.0)))) } + _ => { + todo!(); + } }; } match func { @@ -402,6 +392,9 @@ impl Program { }; *acc += col; } + _ => { + todo!(); + } }; state.pc += 1; } @@ -418,6 +411,9 @@ impl Program { *acc /= count.clone(); } AggFunc::Sum => {} + _ => { + todo!(); + } }; state.pc += 1; }