From 851aea212d1f5120c8f56fc9d3c731e1799ddffe Mon Sep 17 00:00:00 2001 From: jussisaurio Date: Sat, 13 Jul 2024 23:03:25 +0300 Subject: [PATCH] add coalesce(), refactor/rename add_label() --- Cargo.lock | 48 +++++ core/function.rs | 30 +++ core/translate.rs | 477 +++++++++++++++++++++++++++------------------- core/vdbe.rs | 81 ++++++-- testing/all.test | 28 +++ 5 files changed, 451 insertions(+), 213 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cba209390..079e5b2f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,6 +342,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "const-random" version = "0.1.18" @@ -777,6 +786,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "home" version = "0.5.9" @@ -958,9 +973,11 @@ dependencies = [ "log", "mimalloc", "ordered-multimap", + "polling", "pprof", "rstest", "rusqlite", + "rustix", "sieve-cache", "sqlite3-parser", "thiserror", @@ -1229,6 +1246,21 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "polling" +version = "3.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ed00ed3fbf728b5816498ecd316d1716eecaced9c0c8d2c5a6740ca214985b" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi 0.4.0", + "pin-project-lite", + "rustix", + "tracing", + "windows-sys 0.52.0", +] + [[package]] name = "pprof" version = "0.12.1" @@ -1756,6 +1788,22 @@ dependencies = [ "serde", ] +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" + [[package]] name = "uncased" version = "0.9.10" diff --git a/core/function.rs b/core/function.rs index d8664c701..144c83b9a 100644 --- a/core/function.rs +++ b/core/function.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + #[derive(Debug, Clone, PartialEq)] pub enum AggFunc { Avg, @@ -24,3 +26,31 @@ impl AggFunc { } } } + +pub enum SingleRowFunc { + Coalesce, +} + +pub enum Func { + Agg(AggFunc), + SingleRow(SingleRowFunc), +} + +impl FromStr for Func { + type Err = (); + + fn from_str(s: &str) -> Result { + match s { + "avg" => Ok(Func::Agg(AggFunc::Avg)), + "count" => Ok(Func::Agg(AggFunc::Count)), + "group_concat" => Ok(Func::Agg(AggFunc::GroupConcat)), + "max" => Ok(Func::Agg(AggFunc::Max)), + "min" => Ok(Func::Agg(AggFunc::Min)), + "string_agg" => Ok(Func::Agg(AggFunc::StringAgg)), + "sum" => Ok(Func::Agg(AggFunc::Sum)), + "total" => Ok(Func::Agg(AggFunc::Total)), + "coalesce" => Ok(Func::SingleRow(SingleRowFunc::Coalesce)), + _ => Err(()), + } + } +} diff --git a/core/translate.rs b/core/translate.rs index e38ab4f01..f4c69efb9 100644 --- a/core/translate.rs +++ b/core/translate.rs @@ -1,7 +1,7 @@ use std::cell::RefCell; use std::rc::Rc; -use crate::function::AggFunc; +use crate::function::{AggFunc, Func, SingleRowFunc}; use crate::pager::Pager; use crate::schema::{Column, Schema, Table}; use crate::sqlite3_ondisk::{DatabaseHeader, MIN_PAGE_CACHE_SIZE}; @@ -42,7 +42,7 @@ struct SrcTable { } struct ColumnInfo { - func: Option, + func: Option, args: Option>, columns_to_allocate: usize, /* number of result columns this col will result on */ } @@ -57,7 +57,10 @@ impl ColumnInfo { } pub fn is_aggregation_function(&self) -> bool { - self.func.is_some() + match self.func { + Some(Func::Agg(_)) => true, + _ => false, + } } } @@ -132,7 +135,9 @@ fn build_select(schema: &Schema, select: ast::Select) -> Result { .. } => { let column_info = analyze_columns(&columns, &Vec::new()); - let exist_aggregation = column_info.iter().any(|info| info.func.is_some()); + let exist_aggregation = column_info + .iter() + .any(|info| info.is_aggregation_function()); Ok(Select { columns, column_info, @@ -169,10 +176,12 @@ fn build_select(schema: &Schema, select: ast::Select) -> Result