From 907b9522e42524ca3ba72286b84f786cfefee89e Mon Sep 17 00:00:00 2001 From: Kould <2435992353@qq.com> Date: Sun, 10 Nov 2024 19:09:41 +0800 Subject: [PATCH] Chore/remove useless dependencies (#240) * chore: remove useless dependencies `lazy_static` & `integer-encoding` * chore: remove useless dependencies `rand` & `strum_macros` & `tracing` * chore: update `describe.slt` * ci: cache rust dependencies * ci: remove cache --- Cargo.toml | 24 ++-- src/execution/dql/describe.rs | 36 +++--- src/expression/evaluator.rs | 6 +- src/function/numbers.rs | 26 ++-- src/lib.rs | 1 + src/macros/mod.rs | 18 ++- src/optimizer/core/cm_sketch.rs | 5 +- .../rule/implementation/ddl/add_column.rs | 14 +-- .../rule/implementation/ddl/create_table.rs | 14 +-- .../rule/implementation/ddl/drop_column.rs | 14 +-- .../rule/implementation/ddl/drop_table.rs | 14 +-- .../rule/implementation/ddl/truncate.rs | 14 +-- .../rule/implementation/dml/analyze.rs | 14 +-- .../rule/implementation/dml/copy_from_file.rs | 14 +-- .../rule/implementation/dml/copy_to_file.rs | 14 +-- .../rule/implementation/dml/delete.rs | 14 +-- .../rule/implementation/dml/insert.rs | 14 +-- .../rule/implementation/dml/update.rs | 14 +-- .../rule/implementation/dql/aggregate.rs | 41 +++---- .../rule/implementation/dql/dummy.rs | 14 +-- .../rule/implementation/dql/filter.rs | 14 +-- .../rule/implementation/dql/function_scan.rs | 14 +-- src/optimizer/rule/implementation/dql/join.rs | 14 +-- .../rule/implementation/dql/limit.rs | 14 +-- .../rule/implementation/dql/projection.rs | 14 +-- src/optimizer/rule/implementation/dql/sort.rs | 14 +-- .../rule/implementation/dql/table_scan.rs | 14 +-- .../rule/implementation/dql/values.rs | 14 +-- .../rule/normalization/column_pruning.rs | 14 +-- .../rule/normalization/combine_operators.rs | 68 +++++----- .../normalization/compilation_in_advance.rs | 26 ++-- .../rule/normalization/pushdown_limit.rs | 65 ++++------ .../rule/normalization/pushdown_predicates.rs | 61 ++++----- .../rule/normalization/simplification.rs | 32 +++-- src/planner/operator/join.rs | 21 +++- src/storage/table_codec.rs | 13 +- src/types/mod.rs | 40 +++++- src/types/tuple.rs | 17 +-- src/types/value.rs | 116 ++++++++++-------- src/utils/bit_vector.rs | 10 +- tests/slt/describe.slt | 12 +- 41 files changed, 422 insertions(+), 510 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 71c644bc..0bbdaaf7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,41 +34,39 @@ harness = false [dependencies] ahash = { version = "0.8" } -async-trait = { version = "0.1", optional = true } bincode = { version = "1" } bytes = { version = "1" } chrono = { version = "0.4" } -clap = { version = "4.5", features = ["derive"], optional = true } comfy-table = { version = "7" } csv = { version = "1" } encode_unicode = { version = "1" } dirs = { version = "5" } -env_logger = { version = "0.11", optional = true } -futures = { version = "0.3", optional = true } -integer-encoding = { version = "3" } itertools = { version = "0.12" } -lazy_static = { version = "1" } -log = { version = "0.4", optional = true } ordered-float = { version = "4" } paste = { version = "1" } parking_lot = { version = "0.12", features = ["arc_lock"] } petgraph = { version = "0.6" } -pgwire = { version = "0.19", optional = true } -rand = { version = "0.9.0-alpha" } regex = { version = "1" } -rocksdb = { version = "0.22.0" } +rocksdb = { version = "0.22" } rust_decimal = { version = "1" } serde = { version = "1", features = ["derive", "rc"] } fnck_sql_serde_macros = { version = "0.1.0", path = "fnck_sql_serde_macros" } siphasher = { version = "1", features = ["serde"] } sqlparser = { version = "0.34", features = ["serde"] } -strum_macros = { version = "0.26.2" } thiserror = { version = "1" } -tokio = { version = "1.36", features = ["full"], optional = true } -tracing = { version = "0.1" } typetag = { version = "0.2" } ulid = { version = "1", features = ["serde"] } +# Feature: net +async-trait = { version = "0.1", optional = true } +clap = { version = "4.5", features = ["derive"], optional = true } +env_logger = { version = "0.11", optional = true } +futures = { version = "0.3", optional = true } +log = { version = "0.4", optional = true } +pgwire = { version = "0.19", optional = true } +tokio = { version = "1.36", features = ["full"], optional = true } + + [dev-dependencies] cargo-tarpaulin = { version = "0.27" } criterion = { version = "0.5", features = ["html_reports"] } diff --git a/src/execution/dql/describe.rs b/src/execution/dql/describe.rs index c4350eaf..c939a55c 100644 --- a/src/execution/dql/describe.rs +++ b/src/execution/dql/describe.rs @@ -6,26 +6,26 @@ use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; use crate::throw; use crate::types::tuple::Tuple; use crate::types::value::{DataValue, Utf8Type}; -use lazy_static::lazy_static; use sqlparser::ast::CharLengthUnits; +use std::sync::LazyLock; -lazy_static! { - static ref PRIMARY_KEY_TYPE: DataValue = DataValue::Utf8 { - value: Some(String::from("PRIMARY")), - ty: Utf8Type::Variable(None), - unit: CharLengthUnits::Characters - }; - static ref UNIQUE_KEY_TYPE: DataValue = DataValue::Utf8 { - value: Some(String::from("UNIQUE")), - ty: Utf8Type::Variable(None), - unit: CharLengthUnits::Characters - }; - static ref EMPTY_KEY_TYPE: DataValue = DataValue::Utf8 { - value: Some(String::from("EMPTY")), - ty: Utf8Type::Variable(None), - unit: CharLengthUnits::Characters - }; -} +static PRIMARY_KEY_TYPE: LazyLock = LazyLock::new(|| DataValue::Utf8 { + value: Some(String::from("PRIMARY")), + ty: Utf8Type::Variable(None), + unit: CharLengthUnits::Characters, +}); + +static UNIQUE_KEY_TYPE: LazyLock = LazyLock::new(|| DataValue::Utf8 { + value: Some(String::from("UNIQUE")), + ty: Utf8Type::Variable(None), + unit: CharLengthUnits::Characters, +}); + +static EMPTY_KEY_TYPE: LazyLock = LazyLock::new(|| DataValue::Utf8 { + value: Some(String::from("EMPTY")), + ty: Utf8Type::Variable(None), + unit: CharLengthUnits::Characters, +}); pub struct Describe { table_name: TableName, diff --git a/src/expression/evaluator.rs b/src/expression/evaluator.rs index 0e08eadc..053cd462 100644 --- a/src/expression/evaluator.rs +++ b/src/expression/evaluator.rs @@ -7,15 +7,13 @@ use crate::types::tuple::Tuple; use crate::types::value::{DataValue, Utf8Type}; use crate::types::LogicalType; use itertools::Itertools; -use lazy_static::lazy_static; use regex::Regex; use sqlparser::ast::{CharLengthUnits, TrimWhereField}; use std::cmp; use std::cmp::Ordering; +use std::sync::LazyLock; -lazy_static! { - static ref NULL_VALUE: DataValue = DataValue::Null; -} +static NULL_VALUE: LazyLock = LazyLock::new(|| DataValue::Null); macro_rules! eval_to_num { ($num_expr:expr, $tuple:expr, $schema:expr) => { diff --git a/src/function/numbers.rs b/src/function/numbers.rs index c6b942fa..ebccdf2b 100644 --- a/src/function/numbers.rs +++ b/src/function/numbers.rs @@ -9,24 +9,22 @@ use crate::types::tuple::SchemaRef; use crate::types::tuple::Tuple; use crate::types::value::DataValue; use crate::types::LogicalType; -use lazy_static::lazy_static; use serde::Deserialize; use serde::Serialize; use std::sync::Arc; +use std::sync::LazyLock; -lazy_static! { - static ref NUMBERS: TableCatalog = { - TableCatalog::new( - Arc::new("numbers".to_lowercase()), - vec![ColumnCatalog::new( - "number".to_lowercase(), - true, - ColumnDesc::new(LogicalType::Integer, None, false, None).unwrap(), - )], - ) - .unwrap() - }; -} +static NUMBERS: LazyLock = LazyLock::new(|| { + TableCatalog::new( + Arc::new("numbers".to_lowercase()), + vec![ColumnCatalog::new( + "number".to_lowercase(), + true, + ColumnDesc::new(LogicalType::Integer, None, false, None).unwrap(), + )], + ) + .unwrap() +}); #[derive(Debug, Serialize, Deserialize)] pub(crate) struct Numbers { diff --git a/src/lib.rs b/src/lib.rs index dc316679..513358a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,6 +93,7 @@ #![feature(iterator_try_collect)] #![feature(slice_pattern)] #![feature(stmt_expr_attributes)] +#![feature(random)] extern crate core; pub mod binder; diff --git a/src/macros/mod.rs b/src/macros/mod.rs index bb283799..76545524 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -149,16 +149,14 @@ macro_rules! scala_function { #[macro_export] macro_rules! table_function { ($struct_name:ident::$function_name:ident($($arg_ty:expr),*) -> [$($output_name:ident: $output_ty:expr),*] => $closure:expr) => { - ::lazy_static::lazy_static! { - static ref $function_name: ::fnck_sql::catalog::table::TableCatalog = { - let mut columns = Vec::new(); - - $({ - columns.push(::fnck_sql::catalog::column::ColumnCatalog::new(stringify!($output_name).to_lowercase(), true, ::fnck_sql::catalog::column::ColumnDesc::new($output_ty, None, false, None).unwrap())); - })* - ::fnck_sql::catalog::table::TableCatalog::new(Arc::new(stringify!($function_name).to_lowercase()), columns).unwrap() - }; - } + static $function_name: ::std::sync::LazyLock<::fnck_sql::catalog::table::TableCatalog> = ::std::sync::LazyLock::new(|| { + let mut columns = Vec::new(); + + $({ + columns.push(::fnck_sql::catalog::column::ColumnCatalog::new(stringify!($output_name).to_lowercase(), true, ::fnck_sql::catalog::column::ColumnDesc::new($output_ty, None, false, None).unwrap())); + })* + ::fnck_sql::catalog::table::TableCatalog::new(Arc::new(stringify!($function_name).to_lowercase()), columns).unwrap() + }); #[derive(Debug, ::serde::Serialize, ::serde::Deserialize)] pub(crate) struct $struct_name { diff --git a/src/optimizer/core/cm_sketch.rs b/src/optimizer/core/cm_sketch.rs index 32e96052..054bcfa1 100644 --- a/src/optimizer/core/cm_sketch.rs +++ b/src/optimizer/core/cm_sketch.rs @@ -3,12 +3,12 @@ use crate::expression::range_detacher::Range; use crate::serdes::{ReferenceSerialization, ReferenceTables}; use crate::storage::{TableCache, Transaction}; use crate::types::value::DataValue; -use rand::RngCore; use siphasher::sip::SipHasher13; use std::borrow::Borrow; use std::hash::{Hash, Hasher}; use std::io::{Read, Write}; use std::marker::PhantomData; +use std::random::random; use std::{cmp, mem}; pub(crate) type FastHasher = SipHasher13; @@ -138,8 +138,7 @@ impl CountMinSketch { } fn sip_new() -> FastHasher { - let mut rng = rand::thread_rng(); - FastHasher::new_with_keys(rng.next_u64(), rng.next_u64()) + FastHasher::new_with_keys(random(), random()) } fn offset(&self, hashes: &mut [u64; 2], key: &Q, k_i: usize) -> usize diff --git a/src/optimizer/rule/implementation/ddl/add_column.rs b/src/optimizer/rule/implementation/ddl/add_column.rs index 120df156..5234f093 100644 --- a/src/optimizer/rule/implementation/ddl/add_column.rs +++ b/src/optimizer/rule/implementation/ddl/add_column.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref ADD_COLUMN_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::AddColumn(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static ADD_COLUMN_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::AddColumn(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct AddColumnImplementation; diff --git a/src/optimizer/rule/implementation/ddl/create_table.rs b/src/optimizer/rule/implementation/ddl/create_table.rs index 78d52255..32c52e0f 100644 --- a/src/optimizer/rule/implementation/ddl/create_table.rs +++ b/src/optimizer/rule/implementation/ddl/create_table.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref CREATE_TABLE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::CreateTable(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static CREATE_TABLE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::CreateTable(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct CreateTableImplementation; diff --git a/src/optimizer/rule/implementation/ddl/drop_column.rs b/src/optimizer/rule/implementation/ddl/drop_column.rs index 15ec7398..be1ab5c3 100644 --- a/src/optimizer/rule/implementation/ddl/drop_column.rs +++ b/src/optimizer/rule/implementation/ddl/drop_column.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref DROP_COLUMN_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::DropColumn(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static DROP_COLUMN_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::DropColumn(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct DropColumnImplementation; diff --git a/src/optimizer/rule/implementation/ddl/drop_table.rs b/src/optimizer/rule/implementation/ddl/drop_table.rs index 1e388293..bddbee02 100644 --- a/src/optimizer/rule/implementation/ddl/drop_table.rs +++ b/src/optimizer/rule/implementation/ddl/drop_table.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref DROP_TABLE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::DropTable(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static DROP_TABLE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::DropTable(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct DropTableImplementation; diff --git a/src/optimizer/rule/implementation/ddl/truncate.rs b/src/optimizer/rule/implementation/ddl/truncate.rs index 017b708a..f96ed4d4 100644 --- a/src/optimizer/rule/implementation/ddl/truncate.rs +++ b/src/optimizer/rule/implementation/ddl/truncate.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref TRUNCATE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::DropTable(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static TRUNCATE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::DropTable(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct TruncateImplementation; diff --git a/src/optimizer/rule/implementation/dml/analyze.rs b/src/optimizer/rule/implementation/dml/analyze.rs index 4eb03bb0..5d1700fe 100644 --- a/src/optimizer/rule/implementation/dml/analyze.rs +++ b/src/optimizer/rule/implementation/dml/analyze.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref ANALYZE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Analyze(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static ANALYZE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Analyze(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct AnalyzeImplementation; diff --git a/src/optimizer/rule/implementation/dml/copy_from_file.rs b/src/optimizer/rule/implementation/dml/copy_from_file.rs index f56c983b..4d026ed3 100644 --- a/src/optimizer/rule/implementation/dml/copy_from_file.rs +++ b/src/optimizer/rule/implementation/dml/copy_from_file.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref COPY_FROM_FILE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::CopyFromFile(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static COPY_FROM_FILE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::CopyFromFile(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct CopyFromFileImplementation; diff --git a/src/optimizer/rule/implementation/dml/copy_to_file.rs b/src/optimizer/rule/implementation/dml/copy_to_file.rs index ef979de7..178cdb4a 100644 --- a/src/optimizer/rule/implementation/dml/copy_to_file.rs +++ b/src/optimizer/rule/implementation/dml/copy_to_file.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref COPY_TO_FILE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::CopyToFile(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static COPY_TO_FILE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::CopyToFile(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct CopyToFileImplementation; diff --git a/src/optimizer/rule/implementation/dml/delete.rs b/src/optimizer/rule/implementation/dml/delete.rs index a917ccb1..13b706d7 100644 --- a/src/optimizer/rule/implementation/dml/delete.rs +++ b/src/optimizer/rule/implementation/dml/delete.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref DELETE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Delete(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static DELETE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Delete(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct DeleteImplementation; diff --git a/src/optimizer/rule/implementation/dml/insert.rs b/src/optimizer/rule/implementation/dml/insert.rs index 62aa4811..fc34f3c9 100644 --- a/src/optimizer/rule/implementation/dml/insert.rs +++ b/src/optimizer/rule/implementation/dml/insert.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref INSERT_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Insert(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static INSERT_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Insert(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct InsertImplementation; diff --git a/src/optimizer/rule/implementation/dml/update.rs b/src/optimizer/rule/implementation/dml/update.rs index 83efc09e..81d610c5 100644 --- a/src/optimizer/rule/implementation/dml/update.rs +++ b/src/optimizer/rule/implementation/dml/update.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref UPDATE_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Update(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static UPDATE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Update(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct UpdateImplementation; diff --git a/src/optimizer/rule/implementation/dql/aggregate.rs b/src/optimizer/rule/implementation/dql/aggregate.rs index 036f9b34..25e79d80 100644 --- a/src/optimizer/rule/implementation/dql/aggregate.rs +++ b/src/optimizer/rule/implementation/dql/aggregate.rs @@ -6,32 +6,27 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref GROUP_BY_AGGREGATE_PATTERN: Pattern = { - Pattern { - predicate: |op| { - if let Operator::Aggregate(op) = op { - return !op.groupby_exprs.is_empty(); - } - false - }, - children: PatternChildrenPredicate::None, +static GROUP_BY_AGGREGATE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| { + if let Operator::Aggregate(op) = op { + return !op.groupby_exprs.is_empty(); } - }; - static ref SIMPLE_AGGREGATE_PATTERN: Pattern = { - Pattern { - predicate: |op| { - if let Operator::Aggregate(op) = op { - return op.groupby_exprs.is_empty(); - } - false - }, - children: PatternChildrenPredicate::None, + false + }, + children: PatternChildrenPredicate::None, +}); + +static SIMPLE_AGGREGATE_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| { + if let Operator::Aggregate(op) = op { + return op.groupby_exprs.is_empty(); } - }; -} + false + }, + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct GroupByAggregateImplementation; diff --git a/src/optimizer/rule/implementation/dql/dummy.rs b/src/optimizer/rule/implementation/dql/dummy.rs index 49e0e8d3..8369ca08 100644 --- a/src/optimizer/rule/implementation/dql/dummy.rs +++ b/src/optimizer/rule/implementation/dql/dummy.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref DUMMY_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Dummy), - children: PatternChildrenPredicate::None, - } - }; -} +static DUMMY_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Dummy), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct DummyImplementation; diff --git a/src/optimizer/rule/implementation/dql/filter.rs b/src/optimizer/rule/implementation/dql/filter.rs index cba4d391..a2931d84 100644 --- a/src/optimizer/rule/implementation/dql/filter.rs +++ b/src/optimizer/rule/implementation/dql/filter.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref FILTER_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Filter(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static FILTER_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Filter(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct FilterImplementation; diff --git a/src/optimizer/rule/implementation/dql/function_scan.rs b/src/optimizer/rule/implementation/dql/function_scan.rs index b676124d..229110d2 100644 --- a/src/optimizer/rule/implementation/dql/function_scan.rs +++ b/src/optimizer/rule/implementation/dql/function_scan.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref FUNCTION_SCAN_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::FunctionScan(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static FUNCTION_SCAN_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::FunctionScan(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct FunctionScanImplementation; diff --git a/src/optimizer/rule/implementation/dql/join.rs b/src/optimizer/rule/implementation/dql/join.rs index caf2edf4..f1c62154 100644 --- a/src/optimizer/rule/implementation/dql/join.rs +++ b/src/optimizer/rule/implementation/dql/join.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::join::{JoinCondition, JoinOperator}; use crate::planner::operator::{Operator, PhysicalOption}; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref JOIN_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Join(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static JOIN_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Join(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct JoinImplementation; diff --git a/src/optimizer/rule/implementation/dql/limit.rs b/src/optimizer/rule/implementation/dql/limit.rs index 586b1bb0..410990ad 100644 --- a/src/optimizer/rule/implementation/dql/limit.rs +++ b/src/optimizer/rule/implementation/dql/limit.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref LIMIT_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Limit(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static LIMIT_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Limit(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct LimitImplementation; diff --git a/src/optimizer/rule/implementation/dql/projection.rs b/src/optimizer/rule/implementation/dql/projection.rs index 5c2639fe..856af173 100644 --- a/src/optimizer/rule/implementation/dql/projection.rs +++ b/src/optimizer/rule/implementation/dql/projection.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref PROJECTION_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Project(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static PROJECTION_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Project(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct ProjectionImplementation; diff --git a/src/optimizer/rule/implementation/dql/sort.rs b/src/optimizer/rule/implementation/dql/sort.rs index 771bbea1..ed489c49 100644 --- a/src/optimizer/rule/implementation/dql/sort.rs +++ b/src/optimizer/rule/implementation/dql/sort.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref SORT_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Sort(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static SORT_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Sort(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct SortImplementation; diff --git a/src/optimizer/rule/implementation/dql/table_scan.rs b/src/optimizer/rule/implementation/dql/table_scan.rs index c6d7435a..edb1d28e 100644 --- a/src/optimizer/rule/implementation/dql/table_scan.rs +++ b/src/optimizer/rule/implementation/dql/table_scan.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::storage::Transaction; use crate::types::index::IndexType; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref TABLE_SCAN_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::TableScan(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static TABLE_SCAN_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::TableScan(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct SeqScanImplementation; diff --git a/src/optimizer/rule/implementation/dql/values.rs b/src/optimizer/rule/implementation/dql/values.rs index 770eccff..bd10902b 100644 --- a/src/optimizer/rule/implementation/dql/values.rs +++ b/src/optimizer/rule/implementation/dql/values.rs @@ -6,16 +6,12 @@ use crate::optimizer::core::statistics_meta::StatisticMetaLoader; use crate::planner::operator::{Operator, PhysicalOption}; use crate::single_mapping; use crate::storage::Transaction; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref VALUES_PATTERN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Values(_)), - children: PatternChildrenPredicate::None, - } - }; -} +static VALUES_PATTERN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Values(_)), + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct ValuesImplementation; diff --git a/src/optimizer/rule/normalization/column_pruning.rs b/src/optimizer/rule/normalization/column_pruning.rs index 03f2cd59..4ee89992 100644 --- a/src/optimizer/rule/normalization/column_pruning.rs +++ b/src/optimizer/rule/normalization/column_pruning.rs @@ -9,18 +9,14 @@ use crate::planner::operator::Operator; use crate::types::value::{DataValue, Utf8Type}; use crate::types::LogicalType; use itertools::Itertools; -use lazy_static::lazy_static; use sqlparser::ast::CharLengthUnits; use std::collections::HashSet; +use std::sync::LazyLock; -lazy_static! { - static ref COLUMN_PRUNING_RULE: Pattern = { - Pattern { - predicate: |_| true, - children: PatternChildrenPredicate::None, - } - }; -} +static COLUMN_PRUNING_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |_| true, + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct ColumnPruning; diff --git a/src/optimizer/rule/normalization/combine_operators.rs b/src/optimizer/rule/normalization/combine_operators.rs index 6be43ae9..d8eb775f 100644 --- a/src/optimizer/rule/normalization/combine_operators.rs +++ b/src/optimizer/rule/normalization/combine_operators.rs @@ -6,44 +6,38 @@ use crate::optimizer::heuristic::graph::{HepGraph, HepNodeId}; use crate::optimizer::rule::normalization::is_subset_exprs; use crate::planner::operator::Operator; use crate::types::LogicalType; -use lazy_static::lazy_static; use std::collections::HashSet; - -lazy_static! { - static ref COLLAPSE_PROJECT_RULE: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Project(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::Project(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; - static ref COMBINE_FILTERS_RULE: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Filter(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::Filter(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; - static ref COLLAPSE_GROUP_BY_AGG: Pattern = { - Pattern { - predicate: |op| match op { - Operator::Aggregate(agg_op) => !agg_op.groupby_exprs.is_empty(), - _ => false, - }, - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| match op { - Operator::Aggregate(agg_op) => !agg_op.groupby_exprs.is_empty(), - _ => false, - }, - children: PatternChildrenPredicate::None, - }]), - } - }; -} +use std::sync::LazyLock; + +static COLLAPSE_PROJECT_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Project(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::Project(_)), + children: PatternChildrenPredicate::None, + }]), +}); + +static COMBINE_FILTERS_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Filter(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::Filter(_)), + children: PatternChildrenPredicate::None, + }]), +}); + +static COLLAPSE_GROUP_BY_AGG: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| match op { + Operator::Aggregate(agg_op) => !agg_op.groupby_exprs.is_empty(), + _ => false, + }, + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| match op { + Operator::Aggregate(agg_op) => !agg_op.groupby_exprs.is_empty(), + _ => false, + }, + children: PatternChildrenPredicate::None, + }]), +}); /// Combine two adjacent project operators into one. pub struct CollapseProject; diff --git a/src/optimizer/rule/normalization/compilation_in_advance.rs b/src/optimizer/rule/normalization/compilation_in_advance.rs index 19ab9e98..8d5faddb 100644 --- a/src/optimizer/rule/normalization/compilation_in_advance.rs +++ b/src/optimizer/rule/normalization/compilation_in_advance.rs @@ -5,25 +5,17 @@ use crate::optimizer::core::rule::{MatchPattern, NormalizationRule}; use crate::optimizer::heuristic::graph::{HepGraph, HepNodeId}; use crate::planner::operator::join::JoinCondition; use crate::planner::operator::Operator; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref EXPRESSION_REMAPPER_RULE: Pattern = { - Pattern { - predicate: |_| true, - children: PatternChildrenPredicate::None, - } - }; -} +static EXPRESSION_REMAPPER_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |_| true, + children: PatternChildrenPredicate::None, +}); -lazy_static! { - static ref EVALUATOR_BIND_RULE: Pattern = { - Pattern { - predicate: |_| true, - children: PatternChildrenPredicate::None, - } - }; -} +static EVALUATOR_BIND_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |_| true, + children: PatternChildrenPredicate::None, +}); #[derive(Clone)] pub struct ExpressionRemapper; diff --git a/src/optimizer/rule/normalization/pushdown_limit.rs b/src/optimizer/rule/normalization/pushdown_limit.rs index 9a7b3e6e..9f6ef1e9 100644 --- a/src/optimizer/rule/normalization/pushdown_limit.rs +++ b/src/optimizer/rule/normalization/pushdown_limit.rs @@ -6,46 +6,31 @@ use crate::optimizer::heuristic::graph::{HepGraph, HepNodeId}; use crate::planner::operator::join::JoinType; use crate::planner::operator::Operator; use itertools::Itertools; -use lazy_static::lazy_static; - -lazy_static! { - static ref LIMIT_PROJECT_TRANSPOSE_RULE: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Limit(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::Project(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; - static ref ELIMINATE_LIMITS_RULE: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Limit(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::Limit(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; - static ref PUSH_LIMIT_THROUGH_JOIN_RULE: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Limit(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::Join(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; - static ref PUSH_LIMIT_INTO_TABLE_SCAN_RULE: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Limit(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::TableScan(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; -} +use std::sync::LazyLock; + +static LIMIT_PROJECT_TRANSPOSE_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Limit(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::Project(_)), + children: PatternChildrenPredicate::None, + }]), +}); + +static PUSH_LIMIT_THROUGH_JOIN_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Limit(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::Join(_)), + children: PatternChildrenPredicate::None, + }]), +}); + +static PUSH_LIMIT_INTO_TABLE_SCAN_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Limit(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::TableScan(_)), + children: PatternChildrenPredicate::None, + }]), +}); pub struct LimitProjectTranspose; diff --git a/src/optimizer/rule/normalization/pushdown_predicates.rs b/src/optimizer/rule/normalization/pushdown_predicates.rs index 4dce53ec..2fab7acb 100644 --- a/src/optimizer/rule/normalization/pushdown_predicates.rs +++ b/src/optimizer/rule/normalization/pushdown_predicates.rs @@ -12,40 +12,33 @@ use crate::planner::operator::Operator; use crate::types::index::{IndexInfo, IndexMetaRef, IndexType}; use crate::types::LogicalType; use itertools::Itertools; -use lazy_static::lazy_static; - -lazy_static! { - static ref PUSH_PREDICATE_THROUGH_JOIN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Filter(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::Join(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; - - static ref PUSH_PREDICATE_INTO_SCAN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Filter(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::TableScan(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; - - // TODO: 感觉是只是处理projection中的alias反向替换为filter中表达式 - static ref PUSH_PREDICATE_THROUGH_NON_JOIN: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Filter(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| matches!(op, Operator::Project(_)), - children: PatternChildrenPredicate::None, - }]), - } - }; -} +use std::sync::LazyLock; + +static PUSH_PREDICATE_THROUGH_JOIN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Filter(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::Join(_)), + children: PatternChildrenPredicate::None, + }]), +}); + +static PUSH_PREDICATE_INTO_SCAN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Filter(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::TableScan(_)), + children: PatternChildrenPredicate::None, + }]), +}); + +// TODO: 感觉是只是处理projection中的alias反向替换为filter中表达式 +#[allow(dead_code)] +static PUSH_PREDICATE_THROUGH_NON_JOIN: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Filter(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| matches!(op, Operator::Project(_)), + children: PatternChildrenPredicate::None, + }]), +}); fn split_conjunctive_predicates(expr: &ScalarExpression) -> Vec { match expr { diff --git a/src/optimizer/rule/normalization/simplification.rs b/src/optimizer/rule/normalization/simplification.rs index 4bfc74b0..ed9470bb 100644 --- a/src/optimizer/rule/normalization/simplification.rs +++ b/src/optimizer/rule/normalization/simplification.rs @@ -5,24 +5,20 @@ use crate::optimizer::heuristic::graph::{HepGraph, HepNodeId}; use crate::planner::operator::join::JoinCondition; use crate::planner::operator::Operator; use itertools::Itertools; -use lazy_static::lazy_static; -lazy_static! { - static ref CONSTANT_CALCULATION_RULE: Pattern = { - Pattern { - predicate: |_| true, - children: PatternChildrenPredicate::None, - } - }; - static ref SIMPLIFY_FILTER_RULE: Pattern = { - Pattern { - predicate: |op| matches!(op, Operator::Filter(_)), - children: PatternChildrenPredicate::Predicate(vec![Pattern { - predicate: |op| !matches!(op, Operator::Aggregate(_)), - children: PatternChildrenPredicate::Recursive, - }]), - } - }; -} +use std::sync::LazyLock; + +static CONSTANT_CALCULATION_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |_| true, + children: PatternChildrenPredicate::None, +}); + +static SIMPLIFY_FILTER_RULE: LazyLock = LazyLock::new(|| Pattern { + predicate: |op| matches!(op, Operator::Filter(_)), + children: PatternChildrenPredicate::Predicate(vec![Pattern { + predicate: |op| !matches!(op, Operator::Aggregate(_)), + children: PatternChildrenPredicate::Recursive, + }]), +}); #[derive(Copy, Clone)] pub struct ConstantCalculation; diff --git a/src/planner/operator/join.rs b/src/planner/operator/join.rs index 8cce29d8..73859843 100644 --- a/src/planner/operator/join.rs +++ b/src/planner/operator/join.rs @@ -5,11 +5,8 @@ use fnck_sql_serde_macros::ReferenceSerialization; use itertools::Itertools; use std::fmt; use std::fmt::Formatter; -use strum_macros::Display; -#[derive( - Debug, Display, PartialEq, Eq, Clone, Copy, Hash, Ord, PartialOrd, ReferenceSerialization, -)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Ord, PartialOrd, ReferenceSerialization)] pub enum JoinType { Inner, LeftOuter, @@ -50,6 +47,22 @@ impl JoinOperator { } } +impl fmt::Display for JoinType { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + JoinType::Inner => write!(f, "Inner")?, + JoinType::LeftOuter => write!(f, "LeftOuter")?, + JoinType::LeftSemi => write!(f, "LeftSemi")?, + JoinType::LeftAnti => write!(f, "LeftAnti")?, + JoinType::RightOuter => write!(f, "RightOuter")?, + JoinType::Full => write!(f, "Full")?, + JoinType::Cross => write!(f, "Cross")?, + } + + Ok(()) + } +} + impl fmt::Display for JoinOperator { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{} Join{}", self.join_type, self.on)?; diff --git a/src/storage/table_codec.rs b/src/storage/table_codec.rs index cbcb8199..83b7ff13 100644 --- a/src/storage/table_codec.rs +++ b/src/storage/table_codec.rs @@ -9,18 +9,15 @@ use crate::types::tuple_builder::TupleIdBuilder; use crate::types::value::DataValue; use crate::types::LogicalType; use bytes::Bytes; -use integer_encoding::FixedInt; -use lazy_static::lazy_static; use std::io::{Cursor, Read, Seek, SeekFrom, Write}; +use std::sync::LazyLock; const BOUND_MIN_TAG: u8 = 0; const BOUND_MAX_TAG: u8 = 1; -lazy_static! { - static ref ROOT_BYTES: Vec = b"Root".to_vec(); - static ref VIEW_BYTES: Vec = b"View".to_vec(); - static ref EMPTY_REFERENCE_TABLES: ReferenceTables = ReferenceTables::new(); -} +static ROOT_BYTES: LazyLock> = LazyLock::new(|| b"Root".to_vec()); +static VIEW_BYTES: LazyLock> = LazyLock::new(|| b"View".to_vec()); +static EMPTY_REFERENCE_TABLES: LazyLock = LazyLock::new(ReferenceTables::new); #[derive(Clone)] pub struct TableCodec {} @@ -415,7 +412,7 @@ impl TableCodec { let mut key_prefix = Self::key_prefix(CodecType::Statistics, table_name); key_prefix.push(BOUND_MIN_TAG); - key_prefix.extend_from_slice(index_id.encode_fixed_light()); + key_prefix.extend(index_id.to_le_bytes()); key_prefix } diff --git a/src/types/mod.rs b/src/types/mod.rs index 5715aaa9..d4f0b492 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -13,7 +13,6 @@ use std::cmp; use crate::errors::DatabaseError; use fnck_sql_serde_macros::ReferenceSerialization; use sqlparser::ast::{CharLengthUnits, ExactNumberInfo, TimezoneInfo}; -use strum_macros::AsRefStr; use ulid::Ulid; pub type ColumnId = Ulid; @@ -28,7 +27,6 @@ pub type ColumnId = Ulid; Hash, PartialOrd, Ord, - AsRefStr, Serialize, Deserialize, ReferenceSerialization, @@ -449,7 +447,43 @@ impl TryFrom for LogicalType { impl std::fmt::Display for LogicalType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.as_ref().to_uppercase()) + match self { + LogicalType::Invalid => write!(f, "Invalid")?, + LogicalType::SqlNull => write!(f, "SqlNull")?, + LogicalType::Boolean => write!(f, "Boolean")?, + LogicalType::Tinyint => write!(f, "Tinyint")?, + LogicalType::UTinyint => write!(f, "UTinyint")?, + LogicalType::Smallint => write!(f, "Smallint")?, + LogicalType::USmallint => write!(f, "USmallint")?, + LogicalType::Integer => write!(f, "Integer")?, + LogicalType::UInteger => write!(f, "UInteger")?, + LogicalType::Bigint => write!(f, "Bigint")?, + LogicalType::UBigint => write!(f, "UBigint")?, + LogicalType::Float => write!(f, "Float")?, + LogicalType::Double => write!(f, "Double")?, + LogicalType::Char(len, units) => write!(f, "Char({}, {})", len, units)?, + LogicalType::Varchar(len, units) => write!(f, "Varchar({:?}, {})", len, units)?, + LogicalType::Date => write!(f, "Date")?, + LogicalType::DateTime => write!(f, "DateTime")?, + LogicalType::Time => write!(f, "Time")?, + LogicalType::Decimal(precision, scale) => { + write!(f, "Decimal({:?}, {:?})", precision, scale)? + } + LogicalType::Tuple(types) => { + write!(f, "(")?; + let mut first = true; + for ty in types { + if !first { + write!(f, ", ")?; + } + first = false; + write!(f, "{}", ty)?; + } + write!(f, ")")? + } + } + + Ok(()) } } diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 18da935c..39a0a6c3 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -5,17 +5,13 @@ use crate::types::value::DataValue; use crate::types::LogicalType; use comfy_table::{Cell, Table}; use itertools::Itertools; -use lazy_static::lazy_static; use std::sync::Arc; +use std::sync::LazyLock; -lazy_static! { - pub static ref EMPTY_TUPLE: Tuple = { - Tuple { - id: None, - values: vec![], - } - }; -} +pub static EMPTY_TUPLE: LazyLock = LazyLock::new(|| Tuple { + id: None, + values: vec![], +}); const BITS_MAX_INDEX: usize = 8; @@ -76,8 +72,7 @@ impl Tuple { pos += len; } else { /// variable length (e.g.: varchar) - let le_bytes: [u8; 4] = bytes[pos..pos + 4].try_into().unwrap(); - let len = u32::from_le_bytes(le_bytes) as usize; + let len = u32::from_le_bytes(bytes[pos..pos + 4].try_into().unwrap()) as usize; pos += 4; if projections[projection_i] == i { tuple_values.push(DataValue::from_raw(&bytes[pos..pos + len], logic_type)); diff --git a/src/types/value.rs b/src/types/value.rs index 039a6e47..04f0ff99 100644 --- a/src/types/value.rs +++ b/src/types/value.rs @@ -2,9 +2,7 @@ use super::LogicalType; use crate::errors::DatabaseError; use chrono::format::{DelayedFormat, StrftimeItems}; use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike}; -use integer_encoding::{FixedInt, FixedIntWriter}; use itertools::Itertools; -use lazy_static::lazy_static; use ordered_float::OrderedFloat; use rust_decimal::prelude::{FromPrimitive, ToPrimitive}; use rust_decimal::Decimal; @@ -14,13 +12,15 @@ use std::fmt::Formatter; use std::hash::Hash; use std::io::Write; use std::str::FromStr; +use std::sync::LazyLock; use std::{cmp, fmt, mem}; -lazy_static! { - pub static ref NULL_VALUE: DataValue = DataValue::Null; - static ref UNIX_DATETIME: NaiveDateTime = DateTime::from_timestamp(0, 0).unwrap().naive_utc(); - static ref UNIX_TIME: NaiveTime = NaiveTime::from_hms_opt(0, 0, 0).unwrap(); -} +pub static NULL_VALUE: LazyLock = LazyLock::new(|| DataValue::Null); + +static UNIX_DATETIME: LazyLock = + LazyLock::new(|| DateTime::from_timestamp(0, 0).unwrap().naive_utc()); + +static UNIX_TIME: LazyLock = LazyLock::new(|| NaiveTime::from_hms_opt(0, 0, 0).unwrap()); pub const DATE_FMT: &str = "%Y-%m-%d"; pub const DATE_TIME_FMT: &str = "%Y-%m-%d %H:%M:%S"; @@ -487,7 +487,8 @@ impl DataValue { DataValue::Null => (), DataValue::Boolean(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v as u8)?); + writer.write_all(&[*v as u8])?; + return Ok(1); } } DataValue::Float32(v) => { @@ -504,42 +505,50 @@ impl DataValue { } DataValue::Int8(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(1); } } DataValue::Int16(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(2); } } DataValue::Int32(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(4); } } DataValue::Int64(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(8); } } DataValue::UInt8(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(1); } } DataValue::UInt16(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(2); } } DataValue::UInt32(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(4); } } DataValue::UInt64(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(8); } } DataValue::Utf8 { value: v, ty, unit } => { @@ -577,17 +586,20 @@ impl DataValue { } DataValue::Date32(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(4); } } DataValue::Date64(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(8); } } DataValue::Time(v) => { if let Some(v) = v { - return Ok(writer.write_fixedint(*v)?); + writer.write_all(&v.to_le_bytes())?; + return Ok(4); } } DataValue::Decimal(v) => { @@ -606,30 +618,30 @@ impl DataValue { LogicalType::Invalid => panic!("invalid logical type"), LogicalType::SqlNull => DataValue::Null, LogicalType::Boolean => DataValue::Boolean(bytes.first().map(|v| *v != 0)), - LogicalType::Tinyint => { - DataValue::Int8((!bytes.is_empty()).then(|| i8::decode_fixed(bytes))) - } - LogicalType::UTinyint => { - DataValue::UInt8((!bytes.is_empty()).then(|| u8::decode_fixed(bytes))) - } - LogicalType::Smallint => { - DataValue::Int16((!bytes.is_empty()).then(|| i16::decode_fixed(bytes))) - } - LogicalType::USmallint => { - DataValue::UInt16((!bytes.is_empty()).then(|| u16::decode_fixed(bytes))) - } - LogicalType::Integer => { - DataValue::Int32((!bytes.is_empty()).then(|| i32::decode_fixed(bytes))) - } - LogicalType::UInteger => { - DataValue::UInt32((!bytes.is_empty()).then(|| u32::decode_fixed(bytes))) - } - LogicalType::Bigint => { - DataValue::Int64((!bytes.is_empty()).then(|| i64::decode_fixed(bytes))) - } - LogicalType::UBigint => { - DataValue::UInt64((!bytes.is_empty()).then(|| u64::decode_fixed(bytes))) - } + LogicalType::Tinyint => DataValue::Int8( + (!bytes.is_empty()).then(|| i8::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::UTinyint => DataValue::UInt8( + (!bytes.is_empty()).then(|| u8::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::Smallint => DataValue::Int16( + (!bytes.is_empty()).then(|| i16::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::USmallint => DataValue::UInt16( + (!bytes.is_empty()).then(|| u16::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::Integer => DataValue::Int32( + (!bytes.is_empty()).then(|| i32::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::UInteger => DataValue::UInt32( + (!bytes.is_empty()).then(|| u32::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::Bigint => DataValue::Int64( + (!bytes.is_empty()).then(|| i64::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::UBigint => DataValue::UInt64( + (!bytes.is_empty()).then(|| u64::from_le_bytes(bytes.try_into().unwrap())), + ), LogicalType::Float => DataValue::Float32((!bytes.is_empty()).then(|| { let mut buf = [0; 4]; buf.copy_from_slice(bytes); @@ -664,15 +676,15 @@ impl DataValue { unit: *unit, } } - LogicalType::Date => { - DataValue::Date32((!bytes.is_empty()).then(|| i32::decode_fixed(bytes))) - } - LogicalType::DateTime => { - DataValue::Date64((!bytes.is_empty()).then(|| i64::decode_fixed(bytes))) - } - LogicalType::Time => { - DataValue::Time((!bytes.is_empty()).then(|| u32::decode_fixed(bytes))) - } + LogicalType::Date => DataValue::Date32( + (!bytes.is_empty()).then(|| i32::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::DateTime => DataValue::Date64( + (!bytes.is_empty()).then(|| i64::from_le_bytes(bytes.try_into().unwrap())), + ), + LogicalType::Time => DataValue::Time( + (!bytes.is_empty()).then(|| u32::from_le_bytes(bytes.try_into().unwrap())), + ), LogicalType::Decimal(_, _) => DataValue::Decimal( (!bytes.is_empty()) .then(|| Decimal::deserialize(<[u8; 16]>::try_from(bytes).unwrap())), diff --git a/src/utils/bit_vector.rs b/src/utils/bit_vector.rs index 887e23a1..3e11b777 100644 --- a/src/utils/bit_vector.rs +++ b/src/utils/bit_vector.rs @@ -1,6 +1,4 @@ -use integer_encoding::FixedInt; use itertools::Itertools; -use std::slice; #[derive(Debug, Default)] pub struct BitVector { @@ -44,19 +42,19 @@ impl BitVector { #[allow(dead_code)] pub fn to_raw(&self, bytes: &mut Vec) { - bytes.append(&mut u64::encode_fixed_vec(self.len)); + bytes.extend(self.len.to_le_bytes()); for bits in &self.bit_groups { - bytes.append(&mut bits.encode_fixed_vec()); + bytes.extend(bits.to_le_bytes()); } } #[allow(dead_code)] pub fn from_raw(bytes: &[u8]) -> Self { - let len = u64::decode_fixed(&bytes[0..8]); + let len = u64::from_le_bytes(bytes[0..8].try_into().unwrap()); let bit_groups = bytes[8..] .iter() - .map(|bit| i8::decode_fixed(slice::from_ref(bit))) + .map(|bit| i8::from_le_bytes([*bit])) .collect_vec(); BitVector { len, bit_groups } diff --git a/tests/slt/describe.slt b/tests/slt/describe.slt index 18bd3071..5bcc9403 100644 --- a/tests/slt/describe.slt +++ b/tests/slt/describe.slt @@ -4,9 +4,9 @@ create table t9 (c1 int primary key, c2 int default 0, c3 varchar unique); query TTTTI describe t9; ---- -c1 INTEGER 4 false PRIMARY null -c2 INTEGER 4 true EMPTY 0 -c3 VARCHAR null true UNIQUE null +c1 Integer 4 false PRIMARY null +c2 Integer 4 true EMPTY 0 +c3 Varchar(None, CHARACTERS) null true UNIQUE null statement ok drop table t9; @@ -17,9 +17,9 @@ create table t9_m (c1 int primary key, c2 int primary key, c3 varchar unique); query TTTTI describe t9_m; ---- -c1 INTEGER 4 false PRIMARY null -c2 INTEGER 4 false PRIMARY null -c3 VARCHAR null true UNIQUE null +c1 Integer 4 false PRIMARY null +c2 Integer 4 false PRIMARY null +c3 Varchar(None, CHARACTERS) null true UNIQUE null statement ok drop table t9_m;