From 36aa42f8049001ef7025c6cebdf36d50688fcd00 Mon Sep 17 00:00:00 2001 From: Kould Date: Thu, 7 Nov 2024 02:44:04 +0800 Subject: [PATCH] chore: remove `ValueRef` to reduce Arc usage --- README.md | 4 +- src/binder/expr.rs | 8 +- src/binder/insert.rs | 6 +- src/binder/select.rs | 4 +- src/binder/update.rs | 6 +- src/catalog/column.rs | 4 +- src/db.rs | 28 +- src/execution/ddl/add_column.rs | 3 +- src/execution/dml/analyze.rs | 4 +- src/execution/dml/delete.rs | 4 +- src/execution/dml/insert.rs | 5 +- src/execution/dml/update.rs | 3 +- src/execution/dql/aggregate/avg.rs | 13 +- src/execution/dql/aggregate/count.rs | 19 +- src/execution/dql/aggregate/hash_agg.rs | 34 +- src/execution/dql/aggregate/min_max.rs | 11 +- src/execution/dql/aggregate/mod.rs | 6 +- src/execution/dql/aggregate/simple_agg.rs | 6 +- src/execution/dql/aggregate/sum.rs | 15 +- src/execution/dql/describe.rs | 35 +- src/execution/dql/explain.rs | 5 +- src/execution/dql/join/hash_join.rs | 54 +-- src/execution/dql/join/nested_loop_join.rs | 56 +-- src/execution/dql/mod.rs | 9 +- src/execution/dql/projection.rs | 4 +- src/execution/dql/show_table.rs | 5 +- src/execution/dql/sort.rs | 180 ++------ src/expression/evaluator.rs | 100 +++-- src/expression/mod.rs | 12 +- src/expression/range_detacher.rs | 390 +++++++++--------- src/expression/simplify.rs | 35 +- src/function/numbers.rs | 4 +- src/macros/mod.rs | 4 +- src/optimizer/core/cm_sketch.rs | 3 +- src/optimizer/core/histogram.rs | 253 ++++++------ src/optimizer/core/memo.rs | 4 +- .../rule/normalization/column_pruning.rs | 5 +- .../rule/normalization/combine_operators.rs | 5 +- .../rule/normalization/pushdown_predicates.rs | 3 +- .../rule/normalization/simplification.rs | 34 +- src/planner/operator/values.rs | 4 +- src/serdes/column.rs | 8 +- src/serdes/data_value.rs | 8 +- src/storage/mod.rs | 54 +-- src/storage/rocksdb.rs | 42 +- src/storage/table_codec.rs | 13 +- src/types/evaluator/tuple.rs | 3 +- src/types/index.rs | 6 +- src/types/tuple.rs | 108 +++-- src/types/tuple_builder.rs | 21 +- src/types/value.rs | 33 +- tests/macros-test/src/main.rs | 6 +- 52 files changed, 762 insertions(+), 927 deletions(-) diff --git a/README.md b/README.md index 90989db1..5f7f2219 100755 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ implement_from_tuple!( ``` - User-Defined Function: `features = ["macros"]` ```rust -scala_function!(TestFunction::test(LogicalType::Integer, LogicalType::Integer) -> LogicalType::Integer => |v1: ValueRef, v2: ValueRef| { +scala_function!(TestFunction::test(LogicalType::Integer, LogicalType::Integer) -> LogicalType::Integer => |v1: DataValue, v2: DataValue| { let plus_binary_evaluator = EvaluatorFactory::binary_create(LogicalType::Integer, BinaryOperator::Plus)?; let value = plus_binary_evaluator.binary_eval(&v1, &v2); @@ -130,7 +130,7 @@ let fnck_sql = DataBaseBuilder::path("./data") ``` - User-Defined Table Function: `features = ["macros"]` ```rust -table_function!(MyTableFunction::test_numbers(LogicalType::Integer) -> [c1: LogicalType::Integer, c2: LogicalType::Integer] => (|v1: ValueRef| { +table_function!(MyTableFunction::test_numbers(LogicalType::Integer) -> [c1: LogicalType::Integer, c2: LogicalType::Integer] => (|v1: DataValue| { let num = v1.i32().unwrap(); Ok(Box::new((0..num) diff --git a/src/binder/expr.rs b/src/binder/expr.rs index 590ca25d..869dccd4 100644 --- a/src/binder/expr.rs +++ b/src/binder/expr.rs @@ -48,7 +48,7 @@ impl<'a, T: Transaction> Binder<'a, '_, T> { } Expr::CompoundIdentifier(idents) => self.bind_column_ref_from_identifiers(idents, None), Expr::BinaryOp { left, right, op } => self.bind_binary_op_internal(left, right, op), - Expr::Value(v) => Ok(ScalarExpression::Constant(Arc::new(v.into()))), + Expr::Value(v) => Ok(ScalarExpression::Constant(v.into())), Expr::Function(func) => self.bind_function(func), Expr::Nested(expr) => self.bind_expr(expr), Expr::UnaryOp { expr, op } => self.bind_unary_op_internal(expr, op), @@ -77,7 +77,7 @@ impl<'a, T: Transaction> Binder<'a, '_, T> { } .cast(&logical_type)?; - Ok(ScalarExpression::Constant(Arc::new(value))) + Ok(ScalarExpression::Constant(value)) } Expr::Between { expr, @@ -672,10 +672,10 @@ impl<'a, T: Transaction> Binder<'a, '_, T> { } fn wildcard_expr() -> ScalarExpression { - ScalarExpression::Constant(Arc::new(DataValue::Utf8 { + ScalarExpression::Constant(DataValue::Utf8 { value: Some("*".to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })) + }) } } diff --git a/src/binder/insert.rs b/src/binder/insert.rs index 87e8491f..babe2360 100644 --- a/src/binder/insert.rs +++ b/src/binder/insert.rs @@ -7,7 +7,7 @@ use crate::planner::operator::Operator; use crate::planner::LogicalPlan; use crate::storage::Transaction; use crate::types::tuple::SchemaRef; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use sqlparser::ast::{Expr, Ident, ObjectName}; use std::slice; use std::sync::Arc; @@ -78,7 +78,7 @@ impl Binder<'_, '_, T> { value.check_len(ty)?; if value.logical_type() != *ty { - value = Arc::new(DataValue::clone(&value).cast(ty)?); + value = value.cast(ty)?; } row.push(value); } @@ -108,7 +108,7 @@ impl Binder<'_, '_, T> { pub(crate) fn bind_values( &mut self, - rows: Vec>, + rows: Vec>, schema_ref: SchemaRef, ) -> LogicalPlan { LogicalPlan::new( diff --git a/src/binder/select.rs b/src/binder/select.rs index 851b409f..6ea394be 100644 --- a/src/binder/select.rs +++ b/src/binder/select.rs @@ -697,7 +697,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> { if let Some(expr) = limit_expr { let expr = self.bind_expr(expr)?; match expr { - ScalarExpression::Constant(dv) => match dv.as_ref() { + ScalarExpression::Constant(dv) => match &dv { DataValue::Int32(Some(v)) if *v >= 0 => limit = Some(*v as usize), DataValue::Int64(Some(v)) if *v >= 0 => limit = Some(*v as usize), _ => return Err(DatabaseError::InvalidType), @@ -713,7 +713,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> { if let Some(expr) = offset_expr { let expr = self.bind_expr(&expr.value)?; match expr { - ScalarExpression::Constant(dv) => match dv.as_ref() { + ScalarExpression::Constant(dv) => match &dv { DataValue::Int32(Some(v)) if *v > 0 => offset = Some(*v as usize), DataValue::Int64(Some(v)) if *v > 0 => offset = Some(*v as usize), _ => return Err(DatabaseError::InvalidType), diff --git a/src/binder/update.rs b/src/binder/update.rs index 9ab28994..d75e5f98 100644 --- a/src/binder/update.rs +++ b/src/binder/update.rs @@ -5,7 +5,6 @@ use crate::planner::operator::update::UpdateOperator; use crate::planner::operator::Operator; use crate::planner::LogicalPlan; use crate::storage::Transaction; -use crate::types::value::DataValue; use sqlparser::ast::{Assignment, Expr, TableFactor, TableWithJoins}; use std::slice; use std::sync::Arc; @@ -47,10 +46,11 @@ impl Binder<'_, '_, T> { // Check if the value length is too long value.check_len(ty)?; + let mut value = value.clone(); if value.logical_type() != *ty { - row.push(Arc::new(DataValue::clone(value).cast(ty)?)); + value = value.cast(ty)?; } - row.push(value.clone()); + row.push(value); } ScalarExpression::Empty => { let default_value = column diff --git a/src/catalog/column.rs b/src/catalog/column.rs index 599ced6d..555923ea 100644 --- a/src/catalog/column.rs +++ b/src/catalog/column.rs @@ -2,7 +2,7 @@ use crate::catalog::TableName; use crate::errors::DatabaseError; use crate::expression::ScalarExpression; use crate::types::tuple::EMPTY_TUPLE; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use crate::types::{ColumnId, LogicalType}; use fnck_sql_serde_macros::ReferenceSerialization; use sqlparser::ast::CharLengthUnits; @@ -166,7 +166,7 @@ impl ColumnCatalog { &self.desc.column_datatype } - pub(crate) fn default_value(&self) -> Result, DatabaseError> { + pub(crate) fn default_value(&self) -> Result, DatabaseError> { self.desc .default .as_ref() diff --git a/src/db.rs b/src/db.rs index d9b560bf..394f3bc1 100644 --- a/src/db.rs +++ b/src/db.rs @@ -396,9 +396,7 @@ pub(crate) mod test { tuples, vec![Tuple { id: None, - values: vec![Arc::new(DataValue::Date32(Some( - Local::now().num_days_from_ce() - )))], + values: vec![DataValue::Date32(Some(Local::now().num_days_from_ce()))], }] ); Ok(()) @@ -428,11 +426,11 @@ pub(crate) mod test { vec![ Tuple { id: None, - values: vec![Arc::new(DataValue::Int32(Some(3)))], + values: vec![DataValue::Int32(Some(3))], }, Tuple { id: None, - values: vec![Arc::new(DataValue::Int32(Some(4)))], + values: vec![DataValue::Int32(Some(4))], }, ] ); @@ -463,32 +461,20 @@ pub(crate) mod test { debug_assert_eq!( tuples_1[0].values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))] ); debug_assert_eq!( tuples_1[1].values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(1))] ); debug_assert_eq!( tuples_2[0].values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))] ); debug_assert_eq!( tuples_2[1].values, - vec![ - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(3))) - ] + vec![DataValue::Int32(Some(3)), DataValue::Int32(Some(3))] ); tx_1.commit()?; diff --git a/src/execution/ddl/add_column.rs b/src/execution/ddl/add_column.rs index 00933ad2..beaf1400 100644 --- a/src/execution/ddl/add_column.rs +++ b/src/execution/ddl/add_column.rs @@ -12,7 +12,6 @@ use std::ops::Coroutine; use std::ops::CoroutineState; use std::pin::Pin; use std::slice; -use std::sync::Arc; pub struct AddColumn { op: AddColumnOperator, @@ -61,7 +60,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for AddColumn { } tuple.values.push(value); } else { - tuple.values.push(Arc::new(DataValue::Null)); + tuple.values.push(DataValue::Null); } tuples.push(tuple); } diff --git a/src/execution/dml/analyze.rs b/src/execution/dml/analyze.rs index ab3918af..6e0b8f9b 100644 --- a/src/execution/dml/analyze.rs +++ b/src/execution/dml/analyze.rs @@ -116,11 +116,11 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze { let meta = StatisticsMeta::new(histogram, sketch); throw!(meta.to_file(&temp_path)); - values.push(Arc::new(DataValue::Utf8 { + values.push(DataValue::Utf8 { value: Some(path_str.clone()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })); + }); throw!(transaction.save_table_meta(cache.2, &table_name, path_str, meta)); throw!(fs::rename(&temp_path, &path).map_err(DatabaseError::IO)); diff --git a/src/execution/dml/delete.rs b/src/execution/dml/delete.rs index 6ab02785..ab1702c6 100644 --- a/src/execution/dml/delete.rs +++ b/src/execution/dml/delete.rs @@ -10,7 +10,7 @@ use crate::throw; use crate::types::index::{Index, IndexId, IndexType}; use crate::types::tuple::Tuple; use crate::types::tuple_builder::TupleBuilder; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use std::collections::HashMap; use std::ops::Coroutine; use std::ops::CoroutineState; @@ -106,6 +106,6 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Delete { struct Value { exprs: Vec, - value_rows: Vec>, + value_rows: Vec>, index_ty: IndexType, } diff --git a/src/execution/dml/insert.rs b/src/execution/dml/insert.rs index 1b2e673d..7d74fc82 100644 --- a/src/execution/dml/insert.rs +++ b/src/execution/dml/insert.rs @@ -16,7 +16,6 @@ use std::collections::HashMap; use std::ops::Coroutine; use std::ops::CoroutineState; use std::pin::Pin; -use std::sync::Arc; pub struct Insert { table_name: TableName, @@ -119,7 +118,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Insert { if value.is_none() { value = throw!(col.default_value()); } - value.unwrap_or_else(|| Arc::new(DataValue::none(col.datatype()))) + value.unwrap_or_else(|| DataValue::none(col.datatype())) }; if value.is_null() && !col.nullable() { yield Err(DatabaseError::NotNull); @@ -131,7 +130,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Insert { id: Some(if primary_keys.len() == 1 { tuple_id.pop().unwrap() } else { - Arc::new(DataValue::Tuple(Some(tuple_id))) + DataValue::Tuple(Some(tuple_id)) }), values, }); diff --git a/src/execution/dml/update.rs b/src/execution/dml/update.rs index 3f09fee3..5a14bdcf 100644 --- a/src/execution/dml/update.rs +++ b/src/execution/dml/update.rs @@ -14,7 +14,6 @@ use std::collections::HashMap; use std::ops::Coroutine; use std::ops::CoroutineState; use std::pin::Pin; -use std::sync::Arc; pub struct Update { table_name: TableName, @@ -108,7 +107,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Update { let id = if primary_keys.len() == 1 { primary_keys.pop().unwrap() } else { - Arc::new(DataValue::Tuple(Some(primary_keys))) + DataValue::Tuple(Some(primary_keys)) }; if &id != tuple.id.as_ref().unwrap() { let old_key = tuple.id.replace(id).unwrap(); diff --git a/src/execution/dql/aggregate/avg.rs b/src/execution/dql/aggregate/avg.rs index 7db19d30..404503b7 100644 --- a/src/execution/dql/aggregate/avg.rs +++ b/src/execution/dql/aggregate/avg.rs @@ -3,9 +3,8 @@ use crate::execution::dql::aggregate::sum::SumAccumulator; use crate::execution::dql::aggregate::Accumulator; use crate::expression::BinaryOperator; use crate::types::evaluator::EvaluatorFactory; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use crate::types::LogicalType; -use std::sync::Arc; pub struct AvgAccumulator { inner: SumAccumulator, @@ -22,7 +21,7 @@ impl AvgAccumulator { } impl Accumulator for AvgAccumulator { - fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError> { + fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> { if !value.is_null() { self.inner.update_value(value)?; self.count += 1; @@ -31,12 +30,12 @@ impl Accumulator for AvgAccumulator { Ok(()) } - fn evaluate(&self) -> Result { + fn evaluate(&self) -> Result { let mut value = self.inner.evaluate()?; let value_ty = value.logical_type(); if self.count == 0 { - return Ok(Arc::new(DataValue::init(&value_ty))); + return Ok(DataValue::init(&value_ty)); } let quantity = if value_ty.is_signed_numeric() { DataValue::Int64(Some(self.count as i64)) @@ -46,9 +45,9 @@ impl Accumulator for AvgAccumulator { let quantity_ty = quantity.logical_type(); if value_ty != quantity_ty { - value = Arc::new(DataValue::clone(&value).cast(&quantity_ty)?) + value = DataValue::clone(&value).cast(&quantity_ty)? } let evaluator = EvaluatorFactory::binary_create(quantity_ty, BinaryOperator::Divide)?; - Ok(Arc::new(evaluator.0.binary_eval(&value, &quantity))) + Ok(evaluator.0.binary_eval(&value, &quantity)) } } diff --git a/src/execution/dql/aggregate/count.rs b/src/execution/dql/aggregate/count.rs index 023e02e1..741bad58 100644 --- a/src/execution/dql/aggregate/count.rs +++ b/src/execution/dql/aggregate/count.rs @@ -1,9 +1,8 @@ use crate::errors::DatabaseError; use crate::execution::dql::aggregate::Accumulator; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use ahash::RandomState; use std::collections::HashSet; -use std::sync::Arc; pub struct CountAccumulator { result: i32, @@ -16,7 +15,7 @@ impl CountAccumulator { } impl Accumulator for CountAccumulator { - fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError> { + fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> { if !value.is_null() { self.result += 1; } @@ -24,13 +23,13 @@ impl Accumulator for CountAccumulator { Ok(()) } - fn evaluate(&self) -> Result { - Ok(Arc::new(DataValue::Int32(Some(self.result)))) + fn evaluate(&self) -> Result { + Ok(DataValue::Int32(Some(self.result))) } } pub struct DistinctCountAccumulator { - distinct_values: HashSet, + distinct_values: HashSet, } impl DistinctCountAccumulator { @@ -42,7 +41,7 @@ impl DistinctCountAccumulator { } impl Accumulator for DistinctCountAccumulator { - fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError> { + fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> { if !value.is_null() { self.distinct_values.insert(value.clone()); } @@ -50,9 +49,7 @@ impl Accumulator for DistinctCountAccumulator { Ok(()) } - fn evaluate(&self) -> Result { - Ok(Arc::new(DataValue::Int32(Some( - self.distinct_values.len() as i32 - )))) + fn evaluate(&self) -> Result { + Ok(DataValue::Int32(Some(self.distinct_values.len() as i32))) } } diff --git a/src/execution/dql/aggregate/hash_agg.rs b/src/execution/dql/aggregate/hash_agg.rs index d2252e47..0666e47a 100644 --- a/src/execution/dql/aggregate/hash_agg.rs +++ b/src/execution/dql/aggregate/hash_agg.rs @@ -8,7 +8,7 @@ use crate::planner::LogicalPlan; use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; use crate::throw; use crate::types::tuple::{SchemaRef, Tuple}; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use ahash::HashMap; use itertools::Itertools; use std::ops::{Coroutine, CoroutineState}; @@ -78,7 +78,7 @@ pub(crate) struct HashAggStatus { groupby_exprs: Vec, group_columns: Vec, - group_hash_accs: HashMap, Vec>>, + group_hash_accs: HashMap, Vec>>, } impl HashAggStatus { @@ -109,7 +109,7 @@ impl HashAggStatus { } // 2.1 evaluate agg exprs and collect the result values for later accumulators. - let values: Vec = self + let values: Vec = self .agg_calls .iter() .map(|expr| { @@ -121,7 +121,7 @@ impl HashAggStatus { }) .try_collect()?; - let group_keys: Vec = self + let group_keys: Vec = self .groupby_exprs .iter() .map(|expr| expr.eval(&tuple, &self.schema_ref)) @@ -145,7 +145,7 @@ impl HashAggStatus { .drain() .map(|(group_keys, accs)| { // Tips: Accumulator First - let values: Vec = accs + let values: Vec = accs .iter() .map(|acc| acc.evaluate()) .chain(group_keys.into_iter().map(Ok)) @@ -213,24 +213,24 @@ mod test { operator: Operator::Values(ValuesOperator { rows: vec![ vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(4))), + DataValue::Int32(Some(0)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(4)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(5))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(5)), ], vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), + DataValue::Int32(Some(0)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(3))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(3)), ], ], schema_ref: t1_schema.clone(), diff --git a/src/execution/dql/aggregate/min_max.rs b/src/execution/dql/aggregate/min_max.rs index 72b96e92..bc4200c8 100644 --- a/src/execution/dql/aggregate/min_max.rs +++ b/src/execution/dql/aggregate/min_max.rs @@ -2,12 +2,11 @@ use crate::errors::DatabaseError; use crate::execution::dql::aggregate::Accumulator; use crate::expression::BinaryOperator; use crate::types::evaluator::EvaluatorFactory; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use crate::types::LogicalType; -use std::sync::Arc; pub struct MinMaxAccumulator { - inner: Option, + inner: Option, op: BinaryOperator, ty: LogicalType, } @@ -29,7 +28,7 @@ impl MinMaxAccumulator { } impl Accumulator for MinMaxAccumulator { - fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError> { + fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> { if !value.is_null() { if let Some(inner_value) = &self.inner { let evaluator = EvaluatorFactory::binary_create(value.logical_type(), self.op)?; @@ -49,10 +48,10 @@ impl Accumulator for MinMaxAccumulator { Ok(()) } - fn evaluate(&self) -> Result { + fn evaluate(&self) -> Result { Ok(self .inner .clone() - .unwrap_or_else(|| Arc::new(DataValue::none(&self.ty)))) + .unwrap_or_else(|| DataValue::none(&self.ty))) } } diff --git a/src/execution/dql/aggregate/mod.rs b/src/execution/dql/aggregate/mod.rs index 28d77363..dd1a1617 100644 --- a/src/execution/dql/aggregate/mod.rs +++ b/src/execution/dql/aggregate/mod.rs @@ -12,7 +12,7 @@ use crate::execution::dql::aggregate::min_max::MinMaxAccumulator; use crate::execution::dql::aggregate::sum::{DistinctSumAccumulator, SumAccumulator}; use crate::expression::agg::AggKind; use crate::expression::ScalarExpression; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use itertools::Itertools; /// Tips: Idea for sqlrs @@ -20,10 +20,10 @@ use itertools::Itertools; /// rows and generically accumulates values. pub trait Accumulator: Send + Sync { /// updates the accumulator's state from a vector of arrays. - fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError>; + fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError>; /// returns its value based on its current state. - fn evaluate(&self) -> Result; + fn evaluate(&self) -> Result; } fn create_accumulator(expr: &ScalarExpression) -> Result, DatabaseError> { diff --git a/src/execution/dql/aggregate/simple_agg.rs b/src/execution/dql/aggregate/simple_agg.rs index 368cbc54..7537681e 100644 --- a/src/execution/dql/aggregate/simple_agg.rs +++ b/src/execution/dql/aggregate/simple_agg.rs @@ -6,7 +6,7 @@ use crate::planner::LogicalPlan; use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; use crate::throw; use crate::types::tuple::Tuple; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use itertools::Itertools; use std::ops::Coroutine; use std::ops::CoroutineState; @@ -47,7 +47,7 @@ impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for SimpleAggExecutor { while let CoroutineState::Yielded(tuple) = Pin::new(&mut coroutine).resume(()) { let tuple = throw!(tuple); - let values: Vec = throw!(agg_calls + let values: Vec = throw!(agg_calls .iter() .map(|expr| match expr { ScalarExpression::AggCall { args, .. } => args[0].eval(&tuple, &schema), @@ -59,7 +59,7 @@ impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for SimpleAggExecutor { throw!(acc.update_value(value)); } } - let values: Vec = + let values: Vec = throw!(accs.into_iter().map(|acc| acc.evaluate()).try_collect()); yield Ok(Tuple { id: None, values }); diff --git a/src/execution/dql/aggregate/sum.rs b/src/execution/dql/aggregate/sum.rs index e3c6a304..f258b9da 100644 --- a/src/execution/dql/aggregate/sum.rs +++ b/src/execution/dql/aggregate/sum.rs @@ -2,11 +2,10 @@ use crate::errors::DatabaseError; use crate::execution::dql::aggregate::Accumulator; use crate::expression::BinaryOperator; use crate::types::evaluator::{BinaryEvaluatorBox, EvaluatorFactory}; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use crate::types::LogicalType; use ahash::RandomState; use std::collections::HashSet; -use std::sync::Arc; pub struct SumAccumulator { result: DataValue, @@ -25,7 +24,7 @@ impl SumAccumulator { } impl Accumulator for SumAccumulator { - fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError> { + fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> { if !value.is_null() { if self.result.is_null() { self.result = DataValue::clone(value); @@ -37,13 +36,13 @@ impl Accumulator for SumAccumulator { Ok(()) } - fn evaluate(&self) -> Result { - Ok(Arc::new(self.result.clone())) + fn evaluate(&self) -> Result { + Ok(self.result.clone()) } } pub struct DistinctSumAccumulator { - distinct_values: HashSet, + distinct_values: HashSet, inner: SumAccumulator, } @@ -57,7 +56,7 @@ impl DistinctSumAccumulator { } impl Accumulator for DistinctSumAccumulator { - fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError> { + fn update_value(&mut self, value: &DataValue) -> Result<(), DatabaseError> { if !self.distinct_values.contains(value) { self.distinct_values.insert(value.clone()); self.inner.update_value(value)?; @@ -66,7 +65,7 @@ impl Accumulator for DistinctSumAccumulator { Ok(()) } - fn evaluate(&self) -> Result { + fn evaluate(&self) -> Result { self.inner.evaluate() } } diff --git a/src/execution/dql/describe.rs b/src/execution/dql/describe.rs index a3840733..c4350eaf 100644 --- a/src/execution/dql/describe.rs +++ b/src/execution/dql/describe.rs @@ -5,27 +5,26 @@ use crate::planner::operator::describe::DescribeOperator; use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; use crate::throw; use crate::types::tuple::Tuple; -use crate::types::value::{DataValue, Utf8Type, ValueRef}; +use crate::types::value::{DataValue, Utf8Type}; use lazy_static::lazy_static; use sqlparser::ast::CharLengthUnits; -use std::sync::Arc; lazy_static! { - static ref PRIMARY_KEY_TYPE: ValueRef = Arc::new(DataValue::Utf8 { + 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: ValueRef = Arc::new(DataValue::Utf8 { + }; + 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: ValueRef = Arc::new(DataValue::Utf8 { + }; + static ref EMPTY_KEY_TYPE: DataValue = DataValue::Utf8 { value: Some(String::from("EMPTY")), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters - }); + }; } pub struct Describe { @@ -70,32 +69,32 @@ impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for Describe { .map(|expr| format!("{}", expr)) .unwrap_or_else(|| "null".to_string()); let values = vec![ - Arc::new(DataValue::Utf8 { + DataValue::Utf8 { value: Some(column.name().to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - }), - Arc::new(DataValue::Utf8 { + }, + DataValue::Utf8 { value: Some(datatype.to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - }), - Arc::new(DataValue::Utf8 { + }, + DataValue::Utf8 { value: datatype.raw_len().map(|len| len.to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - }), - Arc::new(DataValue::Utf8 { + }, + DataValue::Utf8 { value: Some(column.nullable().to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - }), + }, key_fn(column), - Arc::new(DataValue::Utf8 { + DataValue::Utf8 { value: Some(default), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - }), + }, ]; yield Ok(Tuple { id: None, values }); } diff --git a/src/execution/dql/explain.rs b/src/execution/dql/explain.rs index 8c5fbdf0..b7772449 100644 --- a/src/execution/dql/explain.rs +++ b/src/execution/dql/explain.rs @@ -4,7 +4,6 @@ use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; use crate::types::tuple::Tuple; use crate::types::value::{DataValue, Utf8Type}; use sqlparser::ast::CharLengthUnits; -use std::sync::Arc; pub struct Explain { plan: LogicalPlan, @@ -25,11 +24,11 @@ impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for Explain { Box::new( #[coroutine] move || { - let values = vec![Arc::new(DataValue::Utf8 { + let values = vec![DataValue::Utf8 { value: Some(self.plan.explain(0)), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })]; + }]; yield Ok(Tuple { id: None, values }); }, diff --git a/src/execution/dql/join/hash_join.rs b/src/execution/dql/join/hash_join.rs index 3602f479..9d812808 100644 --- a/src/execution/dql/join/hash_join.rs +++ b/src/execution/dql/join/hash_join.rs @@ -8,7 +8,7 @@ use crate::planner::LogicalPlan; use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; use crate::throw; use crate::types::tuple::{Schema, SchemaRef, Tuple}; -use crate::types::value::{DataValue, ValueRef, NULL_VALUE}; +use crate::types::value::{DataValue, NULL_VALUE}; use crate::utils::bit_vector::BitVector; use ahash::HashMap; use itertools::Itertools; @@ -109,7 +109,7 @@ impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for HashJoin { pub(crate) struct HashJoinStatus { ty: JoinType, filter: Option, - build_map: HashMap, (Vec, bool, bool)>, + build_map: HashMap, (Vec, bool, bool)>, full_schema_ref: SchemaRef, left_schema_len: usize, @@ -289,7 +289,7 @@ impl HashJoinStatus { left_schema_len: usize, ) -> Result, DatabaseError> { if let (Some(expr), false) = (filter, matches!(join_ty, JoinType::Full | JoinType::Cross)) { - match expr.eval(&tuple, schema)?.as_ref() { + match &expr.eval(&tuple, schema)? { DataValue::Boolean(Some(false) | None) => { let full_schema_len = schema.len(); @@ -341,7 +341,7 @@ impl HashJoinStatus { } fn right_null_tuple<'a>( - build_map: &'a mut HashMap, (Vec, bool, bool)>, + build_map: &'a mut HashMap, (Vec, bool, bool)>, schema: &'a Schema, ) -> Executor<'a> { Box::new( @@ -363,7 +363,7 @@ impl HashJoinStatus { } fn one_side_tuple<'a>( - build_map: &'a mut HashMap, (Vec, bool, bool)>, + build_map: &'a mut HashMap, (Vec, bool, bool)>, schema: &'a Schema, filter: &'a Option, join_ty: &'a JoinType, @@ -407,7 +407,7 @@ impl HashJoinStatus { on_keys: &[ScalarExpression], tuple: &Tuple, schema: &[ColumnRef], - ) -> Result, DatabaseError> { + ) -> Result, DatabaseError> { let mut values = Vec::with_capacity(on_keys.len()); for expr in on_keys { @@ -466,19 +466,19 @@ mod test { operator: Operator::Values(ValuesOperator { rows: vec![ vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(4))), + DataValue::Int32(Some(0)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(4)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(5))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(5)), ], vec![ - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(5))), - Arc::new(DataValue::Int32(Some(7))), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(5)), + DataValue::Int32(Some(7)), ], ], schema_ref: Arc::new(t1_columns), @@ -492,24 +492,24 @@ mod test { operator: Operator::Values(ValuesOperator { rows: vec![ vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(4))), + DataValue::Int32(Some(0)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(4)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(5))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(5)), ], vec![ - Arc::new(DataValue::Int32(Some(4))), - Arc::new(DataValue::Int32(Some(6))), - Arc::new(DataValue::Int32(Some(8))), + DataValue::Int32(Some(4)), + DataValue::Int32(Some(6)), + DataValue::Int32(Some(8)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), ], ], schema_ref: Arc::new(t2_columns), diff --git a/src/execution/dql/join/nested_loop_join.rs b/src/execution/dql/join/nested_loop_join.rs index 118b9def..12e58411 100644 --- a/src/execution/dql/join/nested_loop_join.rs +++ b/src/execution/dql/join/nested_loop_join.rs @@ -178,7 +178,7 @@ impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for NestedLoopJoin { (Some(filter), true) => { let new_tuple = Self::merge_tuple(&left_tuple, &right_tuple, &ty); let value = throw!(filter.eval(&new_tuple, &output_schema_ref)); - match value.as_ref() { + match &value { DataValue::Boolean(Some(true)) => { let tuple = match ty { JoinType::LeftAnti => None, @@ -441,24 +441,24 @@ mod test { operator: Operator::Values(ValuesOperator { rows: vec![ vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(4))), + DataValue::Int32(Some(0)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(4)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(5))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(5)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(5))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(5)), ], vec![ - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(5))), - Arc::new(DataValue::Int32(Some(7))), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(5)), + DataValue::Int32(Some(7)), ], ], schema_ref: Arc::new(t1_columns), @@ -472,24 +472,24 @@ mod test { operator: Operator::Values(ValuesOperator { rows: vec![ vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(4))), + DataValue::Int32(Some(0)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(4)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(5))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(5)), ], vec![ - Arc::new(DataValue::Int32(Some(4))), - Arc::new(DataValue::Int32(Some(6))), - Arc::new(DataValue::Int32(Some(8))), + DataValue::Int32(Some(4)), + DataValue::Int32(Some(6)), + DataValue::Int32(Some(8)), ], vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), ], ], schema_ref: Arc::new(t2_columns), @@ -514,7 +514,7 @@ mod test { (on_keys, values_t1, values_t2, filter) } - fn valid_result(expected: &mut HashSet>>, actual: &[Tuple]) { + fn valid_result(expected: &mut HashSet>, actual: &[Tuple]) { debug_assert_eq!(actual.len(), expected.len()); for tuple in actual { @@ -522,8 +522,8 @@ mod test { .values .iter() .map(|v| { - if matches!(v.as_ref(), DataValue::Null) { - Arc::new(DataValue::Int32(None)) + if matches!(v, DataValue::Null) { + DataValue::Int32(None) } else { v.clone() } diff --git a/src/execution/dql/mod.rs b/src/execution/dql/mod.rs index 9e40e6bc..54daf4ec 100644 --- a/src/execution/dql/mod.rs +++ b/src/execution/dql/mod.rs @@ -16,13 +16,10 @@ pub(crate) mod values; #[cfg(test)] pub(crate) mod test { - use crate::types::value::{DataValue, ValueRef}; + use crate::types::value::DataValue; use itertools::Itertools; - use std::sync::Arc; - pub(crate) fn build_integers(ints: Vec>) -> Vec { - ints.into_iter() - .map(|i| Arc::new(DataValue::Int32(i))) - .collect_vec() + pub(crate) fn build_integers(ints: Vec>) -> Vec { + ints.into_iter().map(|i| DataValue::Int32(i)).collect_vec() } } diff --git a/src/execution/dql/projection.rs b/src/execution/dql/projection.rs index 7ec0e056..aa840062 100644 --- a/src/execution/dql/projection.rs +++ b/src/execution/dql/projection.rs @@ -7,7 +7,7 @@ use crate::planner::LogicalPlan; use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; use crate::throw; use crate::types::tuple::Tuple; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use std::ops::Coroutine; use std::ops::CoroutineState; use std::pin::Pin; @@ -52,7 +52,7 @@ impl Projection { tuple: &Tuple, exprs: &[ScalarExpression], schmea: &[ColumnRef], - ) -> Result, DatabaseError> { + ) -> Result, DatabaseError> { let mut values = Vec::with_capacity(exprs.len()); for expr in exprs.iter() { diff --git a/src/execution/dql/show_table.rs b/src/execution/dql/show_table.rs index 9b773c7f..37e20d5e 100644 --- a/src/execution/dql/show_table.rs +++ b/src/execution/dql/show_table.rs @@ -5,7 +5,6 @@ use crate::throw; use crate::types::tuple::Tuple; use crate::types::value::{DataValue, Utf8Type}; use sqlparser::ast::CharLengthUnits; -use std::sync::Arc; pub struct ShowTables; @@ -21,11 +20,11 @@ impl<'a, T: Transaction + 'a> ReadExecutor<'a, T> for ShowTables { let metas = throw!(transaction.table_metas()); for TableMeta { table_name } in metas { - let values = vec![Arc::new(DataValue::Utf8 { + let values = vec![DataValue::Utf8 { value: Some(table_name.to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })]; + }]; yield Ok(Tuple { id: None, values }); } diff --git a/src/execution/dql/sort.rs b/src/execution/dql/sort.rs index fdd1212b..d93e5eec 100644 --- a/src/execution/dql/sort.rs +++ b/src/execution/dql/sort.rs @@ -314,89 +314,89 @@ mod test { 0_usize, Tuple { id: None, - values: vec![Arc::new(DataValue::Int32(None))], + values: vec![DataValue::Int32(None)], }, )), Some(( 1_usize, Tuple { id: None, - values: vec![Arc::new(DataValue::Int32(Some(0)))], + values: vec![DataValue::Int32(Some(0))], }, )), Some(( 2_usize, Tuple { id: None, - values: vec![Arc::new(DataValue::Int32(Some(1)))], + values: vec![DataValue::Int32(Some(1))], }, )), ]); let fn_asc_and_nulls_last_eq = |mut iter: Box>| { if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(0)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(0))]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(1)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(1))]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(None))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(None)]) } else { unreachable!() } }; let fn_desc_and_nulls_last_eq = |mut iter: Box>| { if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(1)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(1))]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(0)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(0))]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(None))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(None)]) } else { unreachable!() } }; let fn_asc_and_nulls_first_eq = |mut iter: Box>| { if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(None))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(None)]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(0)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(0))]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(1)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(1))]) } else { unreachable!() } }; let fn_desc_and_nulls_first_eq = |mut iter: Box>| { if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(None))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(None)]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(1)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(1))]) } else { unreachable!() } if let Some(tuple) = iter.next() { - debug_assert_eq!(tuple.values, vec![Arc::new(DataValue::Int32(Some(0)))]) + debug_assert_eq!(tuple.values, vec![DataValue::Int32(Some(0))]) } else { unreachable!() } @@ -489,60 +489,42 @@ mod test { 0_usize, Tuple { id: None, - values: vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(None)), - ], + values: vec![DataValue::Int32(None), DataValue::Int32(None)], }, )), Some(( 1_usize, Tuple { id: None, - values: vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(None)), - ], + values: vec![DataValue::Int32(Some(0)), DataValue::Int32(None)], }, )), Some(( 2_usize, Tuple { id: None, - values: vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)), - ], + values: vec![DataValue::Int32(Some(1)), DataValue::Int32(None)], }, )), Some(( 3_usize, Tuple { id: None, - values: vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(0))), - ], + values: vec![DataValue::Int32(None), DataValue::Int32(Some(0))], }, )), Some(( 4_usize, Tuple { id: None, - values: vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(0))), - ], + values: vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))], }, )), Some(( 5_usize, Tuple { id: None, - values: vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(0))), - ], + values: vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(0))], }, )), ]); @@ -551,10 +533,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(None), DataValue::Int32(None)] ) } else { unreachable!() @@ -562,10 +541,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(None), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -573,10 +549,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(None)] ) } else { unreachable!() @@ -584,10 +557,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -595,10 +565,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(None)] ) } else { unreachable!() @@ -606,10 +573,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -620,10 +584,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(None)] ) } else { unreachable!() @@ -631,10 +592,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -642,10 +600,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(None)] ) } else { unreachable!() @@ -653,10 +608,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -664,10 +616,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(None), DataValue::Int32(None)] ) } else { unreachable!() @@ -675,10 +624,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(None), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -689,10 +635,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(None), DataValue::Int32(None)] ) } else { unreachable!() @@ -700,10 +643,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(None), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -711,10 +651,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(None)] ) } else { unreachable!() @@ -722,10 +659,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -733,10 +667,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(None)] ) } else { unreachable!() @@ -744,10 +675,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -758,10 +686,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(None)] ) } else { unreachable!() @@ -769,10 +694,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -780,10 +702,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(None)] ) } else { unreachable!() @@ -791,10 +710,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(Some(0)), DataValue::Int32(Some(0))] ) } else { unreachable!() @@ -802,10 +718,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(None)) - ] + vec![DataValue::Int32(None), DataValue::Int32(None)] ) } else { unreachable!() @@ -813,10 +726,7 @@ mod test { if let Some(tuple) = iter.next() { debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(0))) - ] + vec![DataValue::Int32(None), DataValue::Int32(Some(0))] ) } else { unreachable!() diff --git a/src/expression/evaluator.rs b/src/expression/evaluator.rs index dff88a90..0e08eadc 100644 --- a/src/expression/evaluator.rs +++ b/src/expression/evaluator.rs @@ -4,7 +4,7 @@ use crate::expression::function::scala::ScalarFunction; use crate::expression::{AliasType, BinaryOperator, ScalarExpression}; use crate::types::evaluator::EvaluatorFactory; use crate::types::tuple::Tuple; -use crate::types::value::{DataValue, Utf8Type, ValueRef}; +use crate::types::value::{DataValue, Utf8Type}; use crate::types::LogicalType; use itertools::Itertools; use lazy_static::lazy_static; @@ -12,34 +12,34 @@ use regex::Regex; use sqlparser::ast::{CharLengthUnits, TrimWhereField}; use std::cmp; use std::cmp::Ordering; -use std::sync::Arc; lazy_static! { - static ref NULL_VALUE: ValueRef = Arc::new(DataValue::Null); + static ref NULL_VALUE: DataValue = DataValue::Null; } macro_rules! eval_to_num { ($num_expr:expr, $tuple:expr, $schema:expr) => { - if let Some(num_i32) = DataValue::clone($num_expr.eval($tuple, $schema)?.as_ref()) + if let Some(num_i32) = $num_expr + .eval($tuple, $schema)? .cast(&LogicalType::Integer)? .i32() { num_i32 } else { - return Ok(Arc::new(DataValue::Utf8 { + return Ok(DataValue::Utf8 { value: None, ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })); + }); } }; } impl ScalarExpression { - pub fn eval(&self, tuple: &Tuple, schema: &[ColumnRef]) -> Result { - let check_cast = |value: ValueRef, return_type: &LogicalType| { + pub fn eval(&self, tuple: &Tuple, schema: &[ColumnRef]) -> Result { + let check_cast = |value: DataValue, return_type: &LogicalType| { if value.logical_type() != *return_type { - return Ok(Arc::new(DataValue::clone(&value).cast(return_type)?)); + return DataValue::clone(&value).cast(return_type); } Ok(value) }; @@ -77,7 +77,7 @@ impl ScalarExpression { ScalarExpression::TypeCast { expr, ty, .. } => { let value = expr.eval(tuple, schema)?; - Ok(Arc::new(DataValue::clone(&value).cast(ty)?)) + Ok(DataValue::clone(&value).cast(ty)?) } ScalarExpression::Binary { left_expr, @@ -88,20 +88,18 @@ impl ScalarExpression { let left = left_expr.eval(tuple, schema)?; let right = right_expr.eval(tuple, schema)?; - Ok(Arc::new( - evaluator - .as_ref() - .ok_or(DatabaseError::EvaluatorNotFound)? - .0 - .binary_eval(&left, &right), - )) + Ok(evaluator + .as_ref() + .ok_or(DatabaseError::EvaluatorNotFound)? + .0 + .binary_eval(&left, &right)) } ScalarExpression::IsNull { expr, negated } => { let mut is_null = expr.eval(tuple, schema)?.is_null(); if *negated { is_null = !is_null; } - Ok(Arc::new(DataValue::Boolean(Some(is_null)))) + Ok(DataValue::Boolean(Some(is_null))) } ScalarExpression::In { expr, @@ -110,14 +108,14 @@ impl ScalarExpression { } => { let value = expr.eval(tuple, schema)?; if value.is_null() { - return Ok(Arc::new(DataValue::Boolean(None))); + return Ok(DataValue::Boolean(None)); } let mut is_in = false; for arg in args { let arg_value = arg.eval(tuple, schema)?; if arg_value.is_null() { - return Ok(Arc::new(DataValue::Boolean(None))); + return Ok(DataValue::Boolean(None)); } if arg_value == value { is_in = true; @@ -127,20 +125,18 @@ impl ScalarExpression { if *negated { is_in = !is_in; } - Ok(Arc::new(DataValue::Boolean(Some(is_in)))) + Ok(DataValue::Boolean(Some(is_in))) } ScalarExpression::Unary { expr, evaluator, .. } => { let value = expr.eval(tuple, schema)?; - Ok(Arc::new( - evaluator - .as_ref() - .ok_or(DatabaseError::EvaluatorNotFound)? - .0 - .unary_eval(&value), - )) + Ok(evaluator + .as_ref() + .ok_or(DatabaseError::EvaluatorNotFound)? + .0 + .unary_eval(&value)) } ScalarExpression::AggCall { .. } => { unreachable!("must use `NormalizationRuleImpl::ExpressionRemapper`") @@ -160,20 +156,21 @@ impl ScalarExpression { value.partial_cmp(&right).map(Ordering::is_le), ) { (Some(true), Some(true)) => true, - (None, _) | (_, None) => return Ok(Arc::new(DataValue::Boolean(None))), + (None, _) | (_, None) => return Ok(DataValue::Boolean(None)), _ => false, }; if *negated { is_between = !is_between; } - Ok(Arc::new(DataValue::Boolean(Some(is_between)))) + Ok(DataValue::Boolean(Some(is_between))) } ScalarExpression::SubString { expr, for_expr, from_expr, } => { - if let Some(mut string) = DataValue::clone(expr.eval(tuple, schema)?.as_ref()) + if let Some(mut string) = expr + .eval(tuple, schema)? .cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))? .utf8() { @@ -185,11 +182,11 @@ impl ScalarExpression { from += len_i + 1; } if from > len_i { - return Ok(Arc::new(DataValue::Utf8 { + return Ok(DataValue::Utf8 { value: None, ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })); + }); } string = string.split_off(from as usize); } @@ -199,31 +196,32 @@ impl ScalarExpression { let _ = string.split_off(for_i); } - Ok(Arc::new(DataValue::Utf8 { + Ok(DataValue::Utf8 { value: Some(string), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })) + }) } else { - Ok(Arc::new(DataValue::Utf8 { + Ok(DataValue::Utf8 { value: None, ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })) + }) } } ScalarExpression::Position { expr, in_expr } => { let unpack = |expr: &ScalarExpression| -> Result { - Ok(DataValue::clone(expr.eval(tuple, schema)?.as_ref()) + Ok(expr + .eval(tuple, schema)? .cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))? .utf8() .unwrap_or("".to_owned())) }; let pattern = unpack(expr)?; let str = unpack(in_expr)?; - Ok(Arc::new(DataValue::Int32(Some( + Ok(DataValue::Int32(Some( str.find(&pattern).map(|pos| pos as i32 + 1).unwrap_or(0), - )))) + ))) } ScalarExpression::Trim { expr, @@ -231,13 +229,15 @@ impl ScalarExpression { trim_where, } => { let mut value = None; - if let Some(string) = DataValue::clone(expr.eval(tuple, schema)?.as_ref()) + if let Some(string) = expr + .eval(tuple, schema)? .cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))? .utf8() { let mut trim_what = String::from(" "); if let Some(trim_what_expr) = trim_what_expr { - trim_what = DataValue::clone(trim_what_expr.eval(tuple, schema)?.as_ref()) + trim_what = trim_what_expr + .eval(tuple, schema)? .cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))? .utf8() .unwrap_or_default(); @@ -261,11 +261,11 @@ impl ScalarExpression { value = Some(string_trimmed) } - Ok(Arc::new(DataValue::Utf8 { + Ok(DataValue::Utf8 { value, ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })) + }) } ScalarExpression::Reference { pos, .. } => Ok(tuple .values @@ -278,13 +278,11 @@ impl ScalarExpression { for expr in exprs { values.push(expr.eval(tuple, schema)?); } - Ok(Arc::new(DataValue::Tuple( - (!values.is_empty()).then_some(values), - ))) + Ok(DataValue::Tuple((!values.is_empty()).then_some(values))) + } + ScalarExpression::ScalaFunction(ScalarFunction { inner, args, .. }) => { + inner.eval(args, tuple, schema)?.cast(inner.return_type()) } - ScalarExpression::ScalaFunction(ScalarFunction { inner, args, .. }) => Ok(Arc::new( - inner.eval(args, tuple, schema)?.cast(inner.return_type())?, - )), ScalarExpression::Empty => unreachable!(), ScalarExpression::If { condition, @@ -355,7 +353,7 @@ impl ScalarExpression { EvaluatorFactory::binary_create(ty.clone(), BinaryOperator::Eq)?; if when_value.logical_type() != ty { - when_value = Arc::new(DataValue::clone(&when_value).cast(&ty)?); + when_value = when_value.cast(&ty)?; } evaluator .0 diff --git a/src/expression/mod.rs b/src/expression/mod.rs index 07fcbf4c..ef8d4ffc 100644 --- a/src/expression/mod.rs +++ b/src/expression/mod.rs @@ -4,7 +4,7 @@ use crate::errors::DatabaseError; use crate::expression::function::scala::ScalarFunction; use crate::expression::function::table::TableFunction; use crate::types::evaluator::{BinaryEvaluatorBox, EvaluatorFactory, UnaryEvaluatorBox}; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use crate::types::LogicalType; use fnck_sql_serde_macros::ReferenceSerialization; use itertools::Itertools; @@ -34,7 +34,7 @@ pub enum AliasType { /// b -> ScalarExpression::ColumnRef() #[derive(Debug, PartialEq, Eq, Clone, Hash, ReferenceSerialization)] pub enum ScalarExpression { - Constant(ValueRef), + Constant(DataValue), ColumnRef(ColumnRef), Alias { expr: Box, @@ -1360,23 +1360,23 @@ mod test { fn_assert( &mut cursor, - ScalarExpression::Constant(Arc::new(DataValue::Int32(None))), + ScalarExpression::Constant(DataValue::Int32(None)), Some((&transaction, &table_cache)), &mut reference_tables, )?; fn_assert( &mut cursor, - ScalarExpression::Constant(Arc::new(DataValue::Int32(Some(42)))), + ScalarExpression::Constant(DataValue::Int32(Some(42))), Some((&transaction, &table_cache)), &mut reference_tables, )?; fn_assert( &mut cursor, - ScalarExpression::Constant(Arc::new(DataValue::Utf8 { + ScalarExpression::Constant(DataValue::Utf8 { value: Some("hello".to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })), + }), Some((&transaction, &table_cache)), &mut reference_tables, )?; diff --git a/src/expression/range_detacher.rs b/src/expression/range_detacher.rs index c0b9590d..ab45dee4 100644 --- a/src/expression/range_detacher.rs +++ b/src/expression/range_detacher.rs @@ -1,13 +1,12 @@ use crate::catalog::ColumnRef; use crate::expression::{BinaryOperator, ScalarExpression}; -use crate::types::value::{DataValue, ValueRef, NULL_VALUE}; +use crate::types::value::{DataValue, NULL_VALUE}; use crate::types::ColumnId; use fnck_sql_serde_macros::ReferenceSerialization; use itertools::Itertools; use std::cmp::Ordering; use std::collections::Bound; use std::fmt::Formatter; -use std::sync::Arc; use std::{fmt, mem}; /// Used to represent binary relationships between fields and constants @@ -16,10 +15,10 @@ use std::{fmt, mem}; #[derive(Debug, PartialEq, Eq, Clone, Hash, ReferenceSerialization)] pub enum Range { Scope { - min: Bound, - max: Bound, + min: Bound, + max: Bound, }, - Eq(ValueRef), + Eq(DataValue), Dummy, SortedRanges(Vec), } @@ -59,12 +58,12 @@ impl TreeNode { } } -fn build_tree(ranges: &[Range], current_level: usize) -> Option> { +fn build_tree(ranges: &[Range], current_level: usize) -> Option> { fn build_subtree<'a>( ranges: &'a [Range], range: &'a Range, current_level: usize, - ) -> Option> { + ) -> Option> { let value = match range { Range::Eq(value) => value, _ => return None, @@ -108,18 +107,20 @@ impl Range { pub(crate) fn combining_eqs(&self, eqs: &[Range]) -> Option { #[allow(clippy::map_clone)] - fn merge_value(tuple: &[&ValueRef], value: ValueRef) -> ValueRef { + fn merge_value(tuple: &[&DataValue], value: DataValue) -> DataValue { let mut merge_tuple = Vec::with_capacity(tuple.len() + 1); - merge_tuple.extend(tuple.iter().map(|v| Arc::clone(v))); + for value in tuple { + merge_tuple.push((*value).clone()); + } merge_tuple.push(value); - Arc::new(DataValue::Tuple(Some(merge_tuple))) + DataValue::Tuple(Some(merge_tuple)) } - fn _to_tuple_range(tuple: &[&ValueRef], range: Range) -> Range { + fn _to_tuple_range(tuple: &[&DataValue], range: Range) -> Range { fn merge_value_on_bound( - tuple: &[&ValueRef], - bound: Bound, - ) -> Bound { + tuple: &[&DataValue], + bound: Bound, + ) -> Bound { match bound { Bound::Included(v) => Bound::Included(merge_value(tuple, v)), Bound::Excluded(v) => Bound::Excluded(merge_value(tuple, v)), @@ -277,10 +278,10 @@ impl<'a> RangeDetacher<'a> { fn merge_binary(op: BinaryOperator, left_binary: Range, right_binary: Range) -> Option { fn process_exclude_bound_with_eq( - bound: Bound, - eq: &ValueRef, + bound: Bound, + eq: &DataValue, op: BinaryOperator, - ) -> Bound { + ) -> Bound { match bound { Bound::Excluded(bound_val) => { if &bound_val == eq && op == BinaryOperator::Or { @@ -322,7 +323,7 @@ impl<'a> RangeDetacher<'a> { // e.g. c1 > 1 ? c1 = 1 (Range::Scope { min, max }, Range::Eq(eq)) | (Range::Eq(eq), Range::Scope { min, max }) => { - let unpack_bound = |bound_eq: Bound| match bound_eq { + let unpack_bound = |bound_eq: Bound| match bound_eq { Bound::Included(val) | Bound::Excluded(val) => val, _ => unreachable!(), }; @@ -575,10 +576,10 @@ impl<'a> RangeDetacher<'a> { } fn or_scope_merge( - left_min: Bound, - left_max: Bound, - right_min: Bound, - right_max: Bound, + left_min: Bound, + left_max: Bound, + right_min: Bound, + right_max: Bound, ) -> Range { if matches!( Self::bound_compared(&left_max, &right_min, false), @@ -633,10 +634,10 @@ impl<'a> RangeDetacher<'a> { } fn and_scope_merge( - left_min: Bound, - left_max: Bound, - right_min: Bound, - right_max: Bound, + left_min: Bound, + left_max: Bound, + right_min: Bound, + right_max: Bound, ) -> Range { let min = if let Some(true) = Self::bound_compared(&left_min, &right_min, true).map(Ordering::is_gt) @@ -674,8 +675,8 @@ impl<'a> RangeDetacher<'a> { } fn bound_compared( - left_bound: &Bound, - right_bound: &Bound, + left_bound: &Bound, + right_bound: &Bound, is_min: bool, ) -> Option { fn is_min_then_reverse(is_min: bool, order: Ordering) -> Ordering { @@ -704,7 +705,7 @@ impl<'a> RangeDetacher<'a> { &mut self, mut op: BinaryOperator, col: ColumnRef, - val: ValueRef, + val: DataValue, is_flip: bool, ) -> Option { if !Self::_is_belong(self.table_name, &col) || col.id() != Some(*self.column_id) { @@ -796,7 +797,6 @@ mod test { use crate::storage::rocksdb::RocksTransaction; use crate::types::value::DataValue; use std::ops::Bound; - use std::sync::Arc; fn plan_filter(plan: LogicalPlan) -> Result, DatabaseError> { let best_plan = HepOptimizer::new(plan.clone()) @@ -823,7 +823,7 @@ mod test { .detach(&op.predicate) .unwrap(); println!("c1 = 1 => {}", range); - debug_assert_eq!(range, Range::Eq(Arc::new(DataValue::Int32(Some(1))))) + debug_assert_eq!(range, Range::Eq(DataValue::Int32(Some(1)))) } { let plan = table_state.plan("select * from t1 where c1 != 1")?; @@ -843,7 +843,7 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), max: Bound::Unbounded, } ) @@ -858,7 +858,7 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Included(DataValue::Int32(Some(1))), max: Bound::Unbounded, } ) @@ -874,7 +874,7 @@ mod test { range, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), + max: Bound::Excluded(DataValue::Int32(Some(1))), } ) } @@ -889,7 +889,7 @@ mod test { range, Range::Scope { min: Bound::Unbounded, - max: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + max: Bound::Included(DataValue::Int32(Some(1))), } ) } @@ -903,8 +903,8 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Included(DataValue::Int32(Some(0))), + max: Bound::Excluded(DataValue::Int32(Some(1))), } ) } @@ -943,8 +943,8 @@ mod test { debug_assert_eq!( range, Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(0)))), - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), + Range::Eq(DataValue::Int32(Some(0))), + Range::Eq(DataValue::Int32(Some(1))), ]) ) } @@ -955,7 +955,7 @@ mod test { .detach(&op.predicate) .unwrap(); println!("c1 = 1 and c1 = 1 => c1: {}", range); - debug_assert_eq!(range, Range::Eq(Arc::new(DataValue::Int32(Some(1))))) + debug_assert_eq!(range, Range::Eq(DataValue::Int32(Some(1)))) } { let plan = table_state.plan("select * from t1 where c1 = 1 or c1 = 1")?; @@ -964,7 +964,7 @@ mod test { .detach(&op.predicate) .unwrap(); println!("c1 = 1 or c1 = 1 => c1: {}", range); - debug_assert_eq!(range, Range::Eq(Arc::new(DataValue::Int32(Some(1))))) + debug_assert_eq!(range, Range::Eq(DataValue::Int32(Some(1)))) } { @@ -983,7 +983,7 @@ mod test { .detach(&op.predicate) .unwrap(); println!("c1 >= 1 and c1 = 1 => c1: {}", range); - debug_assert_eq!(range, Range::Eq(Arc::new(DataValue::Int32(Some(1))))) + debug_assert_eq!(range, Range::Eq(DataValue::Int32(Some(1)))) } { let plan = table_state.plan("select * from t1 where c1 > 1 or c1 = 1")?; @@ -995,7 +995,7 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Included(DataValue::Int32(Some(1))), max: Bound::Unbounded, } ) @@ -1010,7 +1010,7 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Included(DataValue::Int32(Some(1))), max: Bound::Unbounded, } ) @@ -1030,8 +1030,8 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(3)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), + max: Bound::Excluded(DataValue::Int32(Some(3))), } ) } @@ -1049,8 +1049,8 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(4)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), + max: Bound::Excluded(DataValue::Int32(Some(4))), } ) } @@ -1098,10 +1098,10 @@ mod test { debug_assert_eq!( range, Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(0)))), + Range::Eq(DataValue::Int32(Some(0))), Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(3)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), + max: Bound::Excluded(DataValue::Int32(Some(3))), } ]) ) @@ -1121,8 +1121,8 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(4)))), + min: Bound::Included(DataValue::Int32(Some(0))), + max: Bound::Excluded(DataValue::Int32(Some(4))), } ) } @@ -1146,8 +1146,8 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Included(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Included(DataValue::Int32(Some(0))), + max: Bound::Included(DataValue::Int32(Some(2))), } ) } @@ -1163,12 +1163,12 @@ mod test { range, Range::SortedRanges(vec![ Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), + max: Bound::Excluded(DataValue::Int32(Some(2))), }, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(5)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(6)))), + min: Bound::Excluded(DataValue::Int32(Some(5))), + max: Bound::Excluded(DataValue::Int32(Some(6))), }, ]) ) @@ -1184,12 +1184,12 @@ mod test { range, Range::SortedRanges(vec![ Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(3)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), + max: Bound::Excluded(DataValue::Int32(Some(3))), }, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(4)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(7)))), + min: Bound::Excluded(DataValue::Int32(Some(4))), + max: Bound::Excluded(DataValue::Int32(Some(7))), }, ]) ) @@ -1244,8 +1244,8 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(5)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(12)))), + min: Bound::Included(DataValue::Int32(Some(5))), + max: Bound::Excluded(DataValue::Int32(Some(12))), } ) } @@ -1267,10 +1267,10 @@ mod test { Range::SortedRanges(vec![ Range::Scope { min: Bound::Unbounded, - max: Bound::Included(Arc::new(DataValue::Int32(Some(-4)))), + max: Bound::Included(DataValue::Int32(Some(-4))), }, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(0)))), + min: Bound::Included(DataValue::Int32(Some(0))), max: Bound::Unbounded, } ]) @@ -1292,7 +1292,7 @@ mod test { .detach(&op.predicate) .unwrap(); println!("c1 = null => c1: {}", range); - debug_assert_eq!(range, Range::Eq(Arc::new(DataValue::Int32(None)))) + debug_assert_eq!(range, Range::Eq(DataValue::Int32(None))) } { let plan = table_state.plan("select * from t1 where c1 = null or c1 = 1")?; @@ -1304,8 +1304,8 @@ mod test { debug_assert_eq!( range, Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(None))), - Range::Eq(Arc::new(DataValue::Int32(Some(1)))) + Range::Eq(DataValue::Int32(None)), + Range::Eq(DataValue::Int32(Some(1))) ]) ) } @@ -1320,7 +1320,7 @@ mod test { range, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(5)))), + max: Bound::Excluded(DataValue::Int32(Some(5))), } ) } @@ -1335,10 +1335,10 @@ mod test { debug_assert_eq!( range, Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(None))), + Range::Eq(DataValue::Int32(None)), Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(5)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), + max: Bound::Excluded(DataValue::Int32(Some(5))), }, ]) ) @@ -1350,7 +1350,7 @@ mod test { .detach(&op.predicate) .unwrap(); println!("c1 = null and c1 < 5 => c1: {}", range); - debug_assert_eq!(range, Range::Eq(Arc::new(DataValue::Int32(None)))) + debug_assert_eq!(range, Range::Eq(DataValue::Int32(None))) } { let plan = @@ -1407,7 +1407,7 @@ mod test { range, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(5)))), + max: Bound::Excluded(DataValue::Int32(Some(5))), } ) } @@ -1422,8 +1422,8 @@ mod test { debug_assert_eq!( range, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(5)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), + max: Bound::Excluded(DataValue::Int32(Some(5))), } ) } @@ -1437,14 +1437,14 @@ mod test { debug_assert_eq!( range, Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(None))), + Range::Eq(DataValue::Int32(None)), Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(3)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), + max: Bound::Excluded(DataValue::Int32(Some(3))), }, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(4)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(7)))), + min: Bound::Excluded(DataValue::Int32(Some(4))), + max: Bound::Excluded(DataValue::Int32(Some(7))), } ]) ) @@ -1459,14 +1459,14 @@ mod test { debug_assert_eq!( range, Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(None))), + Range::Eq(DataValue::Int32(None)), Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(3)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), + max: Bound::Excluded(DataValue::Int32(Some(3))), }, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(4)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(7)))), + min: Bound::Excluded(DataValue::Int32(Some(4))), + max: Bound::Excluded(DataValue::Int32(Some(7))), } ]) ) @@ -1482,12 +1482,12 @@ mod test { range, Range::SortedRanges(vec![ Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), + max: Bound::Excluded(DataValue::Int32(Some(2))), }, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(5)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(6)))), + min: Bound::Excluded(DataValue::Int32(Some(5))), + max: Bound::Excluded(DataValue::Int32(Some(6))), } ]) ) @@ -1503,12 +1503,12 @@ mod test { range, Range::SortedRanges(vec![ Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), + max: Bound::Excluded(DataValue::Int32(Some(2))), }, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(5)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(6)))), + min: Bound::Excluded(DataValue::Int32(Some(5))), + max: Bound::Excluded(DataValue::Int32(Some(6))), } ]) ) @@ -1520,20 +1520,20 @@ mod test { #[test] fn test_to_tuple_range_some() { let eqs_ranges = vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), + Range::Eq(DataValue::Int32(Some(1))), Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(None))), - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), - Range::Eq(Arc::new(DataValue::Int32(Some(2)))), + Range::Eq(DataValue::Int32(None)), + Range::Eq(DataValue::Int32(Some(1))), + Range::Eq(DataValue::Int32(Some(2))), ]), Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), - Range::Eq(Arc::new(DataValue::Int32(Some(2)))), + Range::Eq(DataValue::Int32(Some(1))), + Range::Eq(DataValue::Int32(Some(2))), ]), ]; let range = Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Included(DataValue::Int32(Some(1))), max: Bound::Unbounded, } .combining_eqs(&eqs_ranges); @@ -1541,19 +1541,19 @@ mod test { debug_assert_eq!( range, Some(Range::Scope { - min: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - ])))), + min: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(None), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + ]))), max: Bound::Unbounded, }) ); let range = Range::Scope { min: Bound::Unbounded, - max: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + max: Bound::Included(DataValue::Int32(Some(1))), } .combining_eqs(&eqs_ranges); @@ -1561,18 +1561,18 @@ mod test { range, Some(Range::Scope { min: Bound::Unbounded, - max: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(1))), - ])))), + max: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(1)), + ]))), }) ); let range = Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Included(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Included(DataValue::Int32(Some(1))), + max: Bound::Included(DataValue::Int32(Some(2))), } .combining_eqs(&eqs_ranges); @@ -1580,88 +1580,88 @@ mod test { range, Some(Range::SortedRanges(vec![ Range::Scope { - min: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - ])))), - max: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - ])))), + min: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(None), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + ]))), + max: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(None), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + ]))), }, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(1))), - ])))), - max: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(2))), - ])))), + min: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(None), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(1)), + ]))), + max: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(None), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(2)), + ]))), }, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - ])))), - max: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - ])))), + min: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + ]))), + max: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + ]))), }, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(1))), - ])))), - max: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(2))), - ])))), + min: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(1)), + ]))), + max: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(2)), + ]))), }, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))), - ])))), - max: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - ])))), + min: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(1)), + ]))), + max: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + ]))), }, Range::Scope { - min: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(1))), - ])))), - max: Bound::Included(Arc::new(DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(2))), - ])))), + min: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(1)), + ]))), + max: Bound::Included(DataValue::Tuple(Some(vec![ + DataValue::Int32(Some(1)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(2)), + ]))), }, ])) ) @@ -1670,9 +1670,9 @@ mod test { #[test] fn test_to_tuple_range_none() { let eqs_ranges_1 = vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), + Range::Eq(DataValue::Int32(Some(1))), Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), + Range::Eq(DataValue::Int32(Some(1))), Range::Scope { min: Bound::Unbounded, max: Bound::Unbounded, @@ -1680,7 +1680,7 @@ mod test { ]), ]; let eqs_ranges_2 = vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), + Range::Eq(DataValue::Int32(Some(1))), Range::Scope { min: Bound::Unbounded, max: Bound::Unbounded, @@ -1688,12 +1688,12 @@ mod test { ]; let range_1 = Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Included(DataValue::Int32(Some(1))), max: Bound::Unbounded, } .combining_eqs(&eqs_ranges_1); let range_2 = Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Included(DataValue::Int32(Some(1))), max: Bound::Unbounded, } .combining_eqs(&eqs_ranges_2); diff --git a/src/expression/simplify.rs b/src/expression/simplify.rs index 50c5228b..5b1ac50e 100644 --- a/src/expression/simplify.rs +++ b/src/expression/simplify.rs @@ -4,10 +4,9 @@ use crate::expression::function::scala::ScalarFunction; use crate::expression::function::table::TableFunction; use crate::expression::{BinaryOperator, ScalarExpression, UnaryOperator}; use crate::types::evaluator::EvaluatorFactory; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use crate::types::{ColumnId, LogicalType}; use std::mem; -use std::sync::Arc; #[derive(Debug)] enum Replace { @@ -150,17 +149,17 @@ impl ScalarExpression { } } - pub(crate) fn unpack_val(&self) -> Option { + pub(crate) fn unpack_val(&self) -> Option { match self { ScalarExpression::Constant(val) => Some(val.clone()), ScalarExpression::Alias { expr, .. } => expr.unpack_val(), - ScalarExpression::TypeCast { expr, ty, .. } => expr - .unpack_val() - .and_then(|val| DataValue::clone(&val).cast(ty).ok().map(Arc::new)), + ScalarExpression::TypeCast { expr, ty, .. } => { + expr.unpack_val().and_then(|val| val.cast(ty).ok()) + } ScalarExpression::IsNull { expr, .. } => { let is_null = expr.unpack_val().map(|val| val.is_null()); - Some(Arc::new(DataValue::Boolean(is_null))) + Some(DataValue::Boolean(is_null)) } ScalarExpression::Unary { expr, @@ -178,7 +177,7 @@ impl ScalarExpression { .0 .unary_eval(&value) }; - Some(Arc::new(unary_value)) + Some(unary_value) } ScalarExpression::Binary { left_expr, @@ -191,10 +190,10 @@ impl ScalarExpression { let mut left = left_expr.unpack_val()?; let mut right = right_expr.unpack_val()?; if &left.logical_type() != ty { - left = Arc::new(DataValue::clone(&left).cast(ty).ok()?); + left = left.cast(ty).ok()?; } if &right.logical_type() != ty { - right = Arc::new(DataValue::clone(&right).cast(ty).ok()?); + right = right.cast(ty).ok()?; } let binary_value = if let Some(evaluator) = evaluator { evaluator.0.binary_eval(&left, &right) @@ -204,7 +203,7 @@ impl ScalarExpression { .0 .binary_eval(&left, &right) }; - Some(Arc::new(binary_value)) + Some(binary_value) } _ => None, } @@ -255,7 +254,7 @@ impl ScalarExpression { .0 .unary_eval(unary_val) }; - let _ = mem::replace(self, ScalarExpression::Constant(Arc::new(value))); + let _ = mem::replace(self, ScalarExpression::Constant(value)); } } ScalarExpression::Binary { @@ -279,13 +278,13 @@ impl ScalarExpression { let evaluator = EvaluatorFactory::binary_create(ty.clone(), *op)?; if left_val.logical_type() != ty { - *left_val = Arc::new(DataValue::clone(left_val).cast(&ty)?); + *left_val = left_val.clone().cast(&ty)?; } if right_val.logical_type() != ty { - *right_val = Arc::new(DataValue::clone(right_val).cast(&ty)?); + *right_val = right_val.clone().cast(&ty)?; } let value = evaluator.0.binary_eval(left_val, right_val); - let _ = mem::replace(self, ScalarExpression::Constant(Arc::new(value))); + let _ = mem::replace(self, ScalarExpression::Constant(value)); } } ScalarExpression::Alias { expr, .. } => expr.constant_calculation()?, @@ -475,9 +474,7 @@ impl ScalarExpression { if let Some(val) = expr.unpack_val() { let _ = mem::replace( self, - ScalarExpression::Constant(Arc::new(DataValue::Boolean(Some( - val.is_null(), - )))), + ScalarExpression::Constant(DataValue::Boolean(Some(val.is_null()))), ); } } @@ -496,7 +493,7 @@ impl ScalarExpression { .0 .unary_eval(&value) }; - let new_expr = ScalarExpression::Constant(Arc::new(value)); + let new_expr = ScalarExpression::Constant(value); let _ = mem::replace(self, new_expr); } else { replaces.push(Replace::Unary(ReplaceUnary { diff --git a/src/function/numbers.rs b/src/function/numbers.rs index 24ef5237..8d475a64 100644 --- a/src/function/numbers.rs +++ b/src/function/numbers.rs @@ -62,14 +62,14 @@ impl TableFunctionImpl for Numbers { let mut value = args[0].eval(&tuple, &[])?; if value.logical_type() != LogicalType::Integer { - value = Arc::new(DataValue::clone(&value).cast(&LogicalType::Integer)?); + value = value.cast(&LogicalType::Integer)?; } let num = value.i32().ok_or(DatabaseError::NotNull)?; Ok(Box::new((0..num).map(|i| { Ok(Tuple { id: None, - values: vec![Arc::new(DataValue::Int32(Some(i)))], + values: vec![DataValue::Int32(Some(i))], }) })) as Box>>) diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 39e1ff64..c973bc72 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -56,7 +56,7 @@ macro_rules! implement_from_tuple { /// # Examples /// /// ``` -/// scala_function!(MyFunction::sum(LogicalType::Integer, LogicalType::Integer) -> LogicalType::Integer => |v1: ValueRef, v2: ValueRef| { +/// scala_function!(MyFunction::sum(LogicalType::Integer, LogicalType::Integer) -> LogicalType::Integer => |v1: DataValue, v2: DataValue| { /// DataValue::binary_op(&v1, &v2, &BinaryOperator::Plus) /// }); /// @@ -127,7 +127,7 @@ macro_rules! scala_function { /// # Examples /// /// ``` -/// table_function!(MyTableFunction::test_numbers(LogicalType::Integer) -> [c1: LogicalType::Integer, c2: LogicalType::Integer] => (|v1: ValueRef| { +/// table_function!(MyTableFunction::test_numbers(LogicalType::Integer) -> [c1: LogicalType::Integer, c2: LogicalType::Integer] => (|v1: DataValue| { /// let num = v1.i32().unwrap(); /// /// Ok(Box::new((0..num) diff --git a/src/optimizer/core/cm_sketch.rs b/src/optimizer/core/cm_sketch.rs index b5b3516b..32e96052 100644 --- a/src/optimizer/core/cm_sketch.rs +++ b/src/optimizer/core/cm_sketch.rs @@ -206,7 +206,6 @@ mod tests { use crate::optimizer::core::cm_sketch::CountMinSketch; use crate::types::value::DataValue; use std::collections::Bound; - use std::sync::Arc; #[test] fn test_increment() { @@ -236,7 +235,7 @@ mod tests { } debug_assert_eq!( cms.collect_count(&vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(300)))), + Range::Eq(DataValue::Int32(Some(300))), Range::Scope { min: Bound::Unbounded, max: Bound::Unbounded, diff --git a/src/optimizer/core/histogram.rs b/src/optimizer/core/histogram.rs index 480fe4f4..f2ca702b 100644 --- a/src/optimizer/core/histogram.rs +++ b/src/optimizer/core/histogram.rs @@ -5,19 +5,18 @@ use crate::expression::BinaryOperator; use crate::optimizer::core::cm_sketch::CountMinSketch; use crate::types::evaluator::EvaluatorFactory; use crate::types::index::{IndexId, IndexMeta}; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use crate::types::LogicalType; use fnck_sql_serde_macros::ReferenceSerialization; use ordered_float::OrderedFloat; use std::collections::Bound; -use std::sync::Arc; use std::{cmp, mem}; pub struct HistogramBuilder { index_id: IndexId, null_count: usize, - values: NullableVec<(usize, ValueRef)>, + values: NullableVec<(usize, DataValue)>, sort_keys: Vec<(usize, Vec)>, value_index: usize, @@ -41,8 +40,8 @@ pub struct Histogram { #[derive(Debug, Clone, PartialEq, ReferenceSerialization)] struct Bucket { - lower: ValueRef, - upper: ValueRef, + lower: DataValue, + upper: DataValue, count: u64, // repeat: u64, } @@ -58,7 +57,7 @@ impl HistogramBuilder { }) } - pub fn append(&mut self, value: &ValueRef) -> Result<(), DatabaseError> { + pub fn append(&mut self, value: &DataValue) -> Result<(), DatabaseError> { if value.is_null() { self.null_count += 1; } else { @@ -114,11 +113,11 @@ impl HistogramBuilder { } let mut corr_xy_sum = 0.0; let mut number_of_distinct_value = 0; - let mut last_value: Option = None; + let mut last_value: Option = None; for (i, index) in sorted_indices.into_iter().enumerate() { let (ordinal, value) = values.take(index); - sketch.increment(value.as_ref()); + sketch.increment(&value); if let None | Some(true) = last_value.as_ref().map(|last_value| last_value != &value) { last_value = Some(value.clone()); @@ -163,11 +162,11 @@ impl HistogramBuilder { } fn is_under( - value: &ValueRef, - target: &Bound, + value: &DataValue, + target: &Bound, is_min: bool, ) -> Result { - let _is_under = |value: &ValueRef, target: &ValueRef, is_min: bool| { + let _is_under = |value: &DataValue, target: &DataValue, is_min: bool| { let evaluator = EvaluatorFactory::binary_create( value.logical_type(), if is_min { @@ -188,11 +187,11 @@ fn is_under( } fn is_above( - value: &ValueRef, - target: &Bound, + value: &DataValue, + target: &Bound, is_min: bool, ) -> Result { - let _is_above = |value: &ValueRef, target: &ValueRef, is_min: bool| { + let _is_above = |value: &DataValue, target: &DataValue, is_min: bool| { let evaluator = EvaluatorFactory::binary_create( value.logical_type(), if is_min { @@ -352,7 +351,7 @@ impl Histogram { let bucket = &self.buckets[*bucket_i]; let mut temp_count = 0; - let is_eq = |value: &ValueRef, target: &Bound| match target { + let is_eq = |value: &DataValue, target: &Bound| match target { Bound::Included(target) => target.eq(value), _ => false, }; @@ -447,7 +446,7 @@ impl Histogram { impl Bucket { fn empty() -> Self { - let empty_value = Arc::new(DataValue::Null); + let empty_value = DataValue::Null; Bucket { lower: empty_value.clone(), @@ -484,26 +483,26 @@ mod tests { fn test_sort_tuples_on_histogram() -> Result<(), DatabaseError> { let mut builder = HistogramBuilder::new(&index_meta(), Some(15))?; - builder.append(&Arc::new(DataValue::Int32(Some(0))))?; - builder.append(&Arc::new(DataValue::Int32(Some(1))))?; - builder.append(&Arc::new(DataValue::Int32(Some(2))))?; - builder.append(&Arc::new(DataValue::Int32(Some(3))))?; - builder.append(&Arc::new(DataValue::Int32(Some(4))))?; + builder.append(&DataValue::Int32(Some(0)))?; + builder.append(&DataValue::Int32(Some(1)))?; + builder.append(&DataValue::Int32(Some(2)))?; + builder.append(&DataValue::Int32(Some(3)))?; + builder.append(&DataValue::Int32(Some(4)))?; - builder.append(&Arc::new(DataValue::Int32(Some(5))))?; - builder.append(&Arc::new(DataValue::Int32(Some(6))))?; - builder.append(&Arc::new(DataValue::Int32(Some(7))))?; - builder.append(&Arc::new(DataValue::Int32(Some(8))))?; - builder.append(&Arc::new(DataValue::Int32(Some(9))))?; + builder.append(&DataValue::Int32(Some(5)))?; + builder.append(&DataValue::Int32(Some(6)))?; + builder.append(&DataValue::Int32(Some(7)))?; + builder.append(&DataValue::Int32(Some(8)))?; + builder.append(&DataValue::Int32(Some(9)))?; - builder.append(&Arc::new(DataValue::Int32(Some(10))))?; - builder.append(&Arc::new(DataValue::Int32(Some(11))))?; - builder.append(&Arc::new(DataValue::Int32(Some(12))))?; - builder.append(&Arc::new(DataValue::Int32(Some(13))))?; - builder.append(&Arc::new(DataValue::Int32(Some(14))))?; + builder.append(&DataValue::Int32(Some(10)))?; + builder.append(&DataValue::Int32(Some(11)))?; + builder.append(&DataValue::Int32(Some(12)))?; + builder.append(&DataValue::Int32(Some(13)))?; + builder.append(&DataValue::Int32(Some(14)))?; - builder.append(&Arc::new(DataValue::Null))?; - builder.append(&Arc::new(DataValue::Int32(None)))?; + builder.append(&DataValue::Null)?; + builder.append(&DataValue::Int32(None))?; // debug_assert!(matches!(builder.build(10), Err(DataBaseError::TooManyBuckets))); @@ -516,28 +515,28 @@ mod tests { histogram.buckets, vec![ Bucket { - lower: Arc::new(DataValue::Int32(Some(0))), - upper: Arc::new(DataValue::Int32(Some(2))), + lower: DataValue::Int32(Some(0)), + upper: DataValue::Int32(Some(2)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(3))), - upper: Arc::new(DataValue::Int32(Some(5))), + lower: DataValue::Int32(Some(3)), + upper: DataValue::Int32(Some(5)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(6))), - upper: Arc::new(DataValue::Int32(Some(8))), + lower: DataValue::Int32(Some(6)), + upper: DataValue::Int32(Some(8)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(9))), - upper: Arc::new(DataValue::Int32(Some(11))), + lower: DataValue::Int32(Some(9)), + upper: DataValue::Int32(Some(11)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(12))), - upper: Arc::new(DataValue::Int32(Some(14))), + lower: DataValue::Int32(Some(12)), + upper: DataValue::Int32(Some(14)), count: 3, }, ] @@ -550,26 +549,26 @@ mod tests { fn test_rev_sort_tuples_on_histogram() -> Result<(), DatabaseError> { let mut builder = HistogramBuilder::new(&index_meta(), Some(15))?; - builder.append(&Arc::new(DataValue::Int32(Some(14))))?; - builder.append(&Arc::new(DataValue::Int32(Some(13))))?; - builder.append(&Arc::new(DataValue::Int32(Some(12))))?; - builder.append(&Arc::new(DataValue::Int32(Some(11))))?; - builder.append(&Arc::new(DataValue::Int32(Some(10))))?; + builder.append(&DataValue::Int32(Some(14)))?; + builder.append(&DataValue::Int32(Some(13)))?; + builder.append(&DataValue::Int32(Some(12)))?; + builder.append(&DataValue::Int32(Some(11)))?; + builder.append(&DataValue::Int32(Some(10)))?; - builder.append(&Arc::new(DataValue::Int32(Some(9))))?; - builder.append(&Arc::new(DataValue::Int32(Some(8))))?; - builder.append(&Arc::new(DataValue::Int32(Some(7))))?; - builder.append(&Arc::new(DataValue::Int32(Some(6))))?; - builder.append(&Arc::new(DataValue::Int32(Some(5))))?; + builder.append(&DataValue::Int32(Some(9)))?; + builder.append(&DataValue::Int32(Some(8)))?; + builder.append(&DataValue::Int32(Some(7)))?; + builder.append(&DataValue::Int32(Some(6)))?; + builder.append(&DataValue::Int32(Some(5)))?; - builder.append(&Arc::new(DataValue::Int32(Some(4))))?; - builder.append(&Arc::new(DataValue::Int32(Some(3))))?; - builder.append(&Arc::new(DataValue::Int32(Some(2))))?; - builder.append(&Arc::new(DataValue::Int32(Some(1))))?; - builder.append(&Arc::new(DataValue::Int32(Some(0))))?; + builder.append(&DataValue::Int32(Some(4)))?; + builder.append(&DataValue::Int32(Some(3)))?; + builder.append(&DataValue::Int32(Some(2)))?; + builder.append(&DataValue::Int32(Some(1)))?; + builder.append(&DataValue::Int32(Some(0)))?; - builder.append(&Arc::new(DataValue::Null))?; - builder.append(&Arc::new(DataValue::Int32(None)))?; + builder.append(&DataValue::Null)?; + builder.append(&DataValue::Int32(None))?; let (histogram, _) = builder.build(5)?; @@ -580,28 +579,28 @@ mod tests { histogram.buckets, vec![ Bucket { - lower: Arc::new(DataValue::Int32(Some(0))), - upper: Arc::new(DataValue::Int32(Some(2))), + lower: DataValue::Int32(Some(0)), + upper: DataValue::Int32(Some(2)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(3))), - upper: Arc::new(DataValue::Int32(Some(5))), + lower: DataValue::Int32(Some(3)), + upper: DataValue::Int32(Some(5)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(6))), - upper: Arc::new(DataValue::Int32(Some(8))), + lower: DataValue::Int32(Some(6)), + upper: DataValue::Int32(Some(8)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(9))), - upper: Arc::new(DataValue::Int32(Some(11))), + lower: DataValue::Int32(Some(9)), + upper: DataValue::Int32(Some(11)), count: 3, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(12))), - upper: Arc::new(DataValue::Int32(Some(14))), + lower: DataValue::Int32(Some(12)), + upper: DataValue::Int32(Some(14)), count: 3, }, ] @@ -614,26 +613,26 @@ mod tests { fn test_non_average_on_histogram() -> Result<(), DatabaseError> { let mut builder = HistogramBuilder::new(&index_meta(), Some(15))?; - builder.append(&Arc::new(DataValue::Int32(Some(14))))?; - builder.append(&Arc::new(DataValue::Int32(Some(13))))?; - builder.append(&Arc::new(DataValue::Int32(Some(12))))?; - builder.append(&Arc::new(DataValue::Int32(Some(11))))?; - builder.append(&Arc::new(DataValue::Int32(Some(10))))?; + builder.append(&DataValue::Int32(Some(14)))?; + builder.append(&DataValue::Int32(Some(13)))?; + builder.append(&DataValue::Int32(Some(12)))?; + builder.append(&DataValue::Int32(Some(11)))?; + builder.append(&DataValue::Int32(Some(10)))?; - builder.append(&Arc::new(DataValue::Int32(Some(4))))?; - builder.append(&Arc::new(DataValue::Int32(Some(3))))?; - builder.append(&Arc::new(DataValue::Int32(Some(2))))?; - builder.append(&Arc::new(DataValue::Int32(Some(1))))?; - builder.append(&Arc::new(DataValue::Int32(Some(0))))?; + builder.append(&DataValue::Int32(Some(4)))?; + builder.append(&DataValue::Int32(Some(3)))?; + builder.append(&DataValue::Int32(Some(2)))?; + builder.append(&DataValue::Int32(Some(1)))?; + builder.append(&DataValue::Int32(Some(0)))?; - builder.append(&Arc::new(DataValue::Int32(Some(9))))?; - builder.append(&Arc::new(DataValue::Int32(Some(8))))?; - builder.append(&Arc::new(DataValue::Int32(Some(7))))?; - builder.append(&Arc::new(DataValue::Int32(Some(6))))?; - builder.append(&Arc::new(DataValue::Int32(Some(5))))?; + builder.append(&DataValue::Int32(Some(9)))?; + builder.append(&DataValue::Int32(Some(8)))?; + builder.append(&DataValue::Int32(Some(7)))?; + builder.append(&DataValue::Int32(Some(6)))?; + builder.append(&DataValue::Int32(Some(5)))?; - builder.append(&Arc::new(DataValue::Null))?; - builder.append(&Arc::new(DataValue::Int32(None)))?; + builder.append(&DataValue::Null)?; + builder.append(&DataValue::Int32(None))?; let (histogram, _) = builder.build(4)?; @@ -644,23 +643,23 @@ mod tests { histogram.buckets, vec![ Bucket { - lower: Arc::new(DataValue::Int32(Some(0))), - upper: Arc::new(DataValue::Int32(Some(3))), + lower: DataValue::Int32(Some(0)), + upper: DataValue::Int32(Some(3)), count: 4, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(4))), - upper: Arc::new(DataValue::Int32(Some(7))), + lower: DataValue::Int32(Some(4)), + upper: DataValue::Int32(Some(7)), count: 4, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(8))), - upper: Arc::new(DataValue::Int32(Some(11))), + lower: DataValue::Int32(Some(8)), + upper: DataValue::Int32(Some(11)), count: 4, }, Bucket { - lower: Arc::new(DataValue::Int32(Some(12))), - upper: Arc::new(DataValue::Int32(Some(14))), + lower: DataValue::Int32(Some(12)), + upper: DataValue::Int32(Some(14)), count: 3, }, ] @@ -673,35 +672,35 @@ mod tests { fn test_collect_count() -> Result<(), DatabaseError> { let mut builder = HistogramBuilder::new(&index_meta(), Some(15))?; - builder.append(&Arc::new(DataValue::Int32(Some(14))))?; - builder.append(&Arc::new(DataValue::Int32(Some(13))))?; - builder.append(&Arc::new(DataValue::Int32(Some(12))))?; - builder.append(&Arc::new(DataValue::Int32(Some(11))))?; - builder.append(&Arc::new(DataValue::Int32(Some(10))))?; + builder.append(&DataValue::Int32(Some(14)))?; + builder.append(&DataValue::Int32(Some(13)))?; + builder.append(&DataValue::Int32(Some(12)))?; + builder.append(&DataValue::Int32(Some(11)))?; + builder.append(&DataValue::Int32(Some(10)))?; - builder.append(&Arc::new(DataValue::Int32(Some(4))))?; - builder.append(&Arc::new(DataValue::Int32(Some(3))))?; - builder.append(&Arc::new(DataValue::Int32(Some(2))))?; - builder.append(&Arc::new(DataValue::Int32(Some(1))))?; - builder.append(&Arc::new(DataValue::Int32(Some(0))))?; + builder.append(&DataValue::Int32(Some(4)))?; + builder.append(&DataValue::Int32(Some(3)))?; + builder.append(&DataValue::Int32(Some(2)))?; + builder.append(&DataValue::Int32(Some(1)))?; + builder.append(&DataValue::Int32(Some(0)))?; - builder.append(&Arc::new(DataValue::Int32(Some(9))))?; - builder.append(&Arc::new(DataValue::Int32(Some(8))))?; - builder.append(&Arc::new(DataValue::Int32(Some(7))))?; - builder.append(&Arc::new(DataValue::Int32(Some(6))))?; - builder.append(&Arc::new(DataValue::Int32(Some(5))))?; + builder.append(&DataValue::Int32(Some(9)))?; + builder.append(&DataValue::Int32(Some(8)))?; + builder.append(&DataValue::Int32(Some(7)))?; + builder.append(&DataValue::Int32(Some(6)))?; + builder.append(&DataValue::Int32(Some(5)))?; - builder.append(&Arc::new(DataValue::Null))?; - builder.append(&Arc::new(DataValue::Int32(None)))?; + builder.append(&DataValue::Null)?; + builder.append(&DataValue::Int32(None))?; let (histogram, sketch) = builder.build(4)?; let count_1 = histogram.collect_count( &vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(2)))), + Range::Eq(DataValue::Int32(Some(2))), Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(4)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(12)))), + min: Bound::Included(DataValue::Int32(Some(4))), + max: Bound::Excluded(DataValue::Int32(Some(12))), }, ], &sketch, @@ -711,7 +710,7 @@ mod tests { let count_2 = histogram.collect_count( &vec![Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(4)))), + min: Bound::Included(DataValue::Int32(Some(4))), max: Bound::Unbounded, }], &sketch, @@ -721,7 +720,7 @@ mod tests { let count_3 = histogram.collect_count( &vec![Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(7)))), + min: Bound::Excluded(DataValue::Int32(Some(7))), max: Bound::Unbounded, }], &sketch, @@ -732,7 +731,7 @@ mod tests { let count_4 = histogram.collect_count( &vec![Range::Scope { min: Bound::Unbounded, - max: Bound::Included(Arc::new(DataValue::Int32(Some(11)))), + max: Bound::Included(DataValue::Int32(Some(11))), }], &sketch, )?; @@ -742,7 +741,7 @@ mod tests { let count_5 = histogram.collect_count( &vec![Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(8)))), + max: Bound::Excluded(DataValue::Int32(Some(8))), }], &sketch, )?; @@ -751,7 +750,7 @@ mod tests { let count_6 = histogram.collect_count( &vec![Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Included(DataValue::Int32(Some(2))), max: Bound::Unbounded, }], &sketch, @@ -761,7 +760,7 @@ mod tests { let count_7 = histogram.collect_count( &vec![Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), max: Bound::Unbounded, }], &sketch, @@ -772,7 +771,7 @@ mod tests { let count_8 = histogram.collect_count( &vec![Range::Scope { min: Bound::Unbounded, - max: Bound::Included(Arc::new(DataValue::Int32(Some(12)))), + max: Bound::Included(DataValue::Int32(Some(12))), }], &sketch, )?; @@ -782,7 +781,7 @@ mod tests { let count_9 = histogram.collect_count( &vec![Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(13)))), + max: Bound::Excluded(DataValue::Int32(Some(13))), }], &sketch, )?; @@ -791,8 +790,8 @@ mod tests { let count_10 = histogram.collect_count( &vec![Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(3)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), + max: Bound::Excluded(DataValue::Int32(Some(3))), }], &sketch, )?; @@ -801,8 +800,8 @@ mod tests { let count_11 = histogram.collect_count( &vec![Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(1)))), - max: Bound::Included(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Included(DataValue::Int32(Some(1))), + max: Bound::Included(DataValue::Int32(Some(2))), }], &sketch, )?; diff --git a/src/optimizer/core/memo.rs b/src/optimizer/core/memo.rs index 6b7bebb0..c1986ee1 100644 --- a/src/optimizer/core/memo.rs +++ b/src/optimizer/core/memo.rs @@ -190,9 +190,9 @@ mod tests { ty: IndexType::PrimaryKey, }), range: Some(Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(2)))), + Range::Eq(DataValue::Int32(Some(2))), Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(40)))), + min: Bound::Excluded(DataValue::Int32(Some(40))), max: Bound::Unbounded, } ])), diff --git a/src/optimizer/rule/normalization/column_pruning.rs b/src/optimizer/rule/normalization/column_pruning.rs index 9af5a47d..03f2cd59 100644 --- a/src/optimizer/rule/normalization/column_pruning.rs +++ b/src/optimizer/rule/normalization/column_pruning.rs @@ -12,7 +12,6 @@ use itertools::Itertools; use lazy_static::lazy_static; use sqlparser::ast::CharLengthUnits; use std::collections::HashSet; -use std::sync::Arc; lazy_static! { static ref COLUMN_PRUNING_RULE: Pattern = { @@ -62,11 +61,11 @@ impl ColumnPruning { Self::clear_exprs(&column_references, &mut op.agg_calls); if op.agg_calls.is_empty() && op.groupby_exprs.is_empty() { - let value = Arc::new(DataValue::Utf8 { + let value = DataValue::Utf8 { value: Some("*".to_string()), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - }); + }; // only single COUNT(*) is not depend on any column // removed all expressions from the aggregate: push a COUNT(*) op.agg_calls.push(ScalarExpression::AggCall { diff --git a/src/optimizer/rule/normalization/combine_operators.rs b/src/optimizer/rule/normalization/combine_operators.rs index 8665b24c..6be43ae9 100644 --- a/src/optimizer/rule/normalization/combine_operators.rs +++ b/src/optimizer/rule/normalization/combine_operators.rs @@ -157,7 +157,6 @@ mod tests { use crate::storage::rocksdb::RocksTransaction; use crate::types::value::DataValue; use crate::types::LogicalType; - use std::sync::Arc; #[test] fn test_collapse_project() -> Result<(), DatabaseError> { @@ -213,8 +212,8 @@ mod tests { if let Operator::Filter(op) = &mut new_filter_op { op.predicate = ScalarExpression::Binary { op: BinaryOperator::Eq, - left_expr: Box::new(Constant(Arc::new(DataValue::Int8(Some(1))))), - right_expr: Box::new(Constant(Arc::new(DataValue::Int8(Some(1))))), + left_expr: Box::new(Constant(DataValue::Int8(Some(1)))), + right_expr: Box::new(Constant(DataValue::Int8(Some(1)))), evaluator: None, ty: LogicalType::Boolean, } diff --git a/src/optimizer/rule/normalization/pushdown_predicates.rs b/src/optimizer/rule/normalization/pushdown_predicates.rs index d4694ab3..146ea53d 100644 --- a/src/optimizer/rule/normalization/pushdown_predicates.rs +++ b/src/optimizer/rule/normalization/pushdown_predicates.rs @@ -277,7 +277,6 @@ mod tests { use crate::types::value::DataValue; use crate::types::LogicalType; use std::collections::Bound; - use std::sync::Arc; #[test] fn test_push_predicate_into_scan() -> Result<(), DatabaseError> { @@ -300,7 +299,7 @@ mod tests { if let Operator::TableScan(op) = &best_plan.childrens[0].childrens[0].operator { let mock_range = Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), max: Bound::Unbounded, }; diff --git a/src/optimizer/rule/normalization/simplification.rs b/src/optimizer/rule/normalization/simplification.rs index d48f6b5a..3202260c 100644 --- a/src/optimizer/rule/normalization/simplification.rs +++ b/src/optimizer/rule/normalization/simplification.rs @@ -145,7 +145,7 @@ mod test { ) .find_best::(None)?; if let Operator::Project(project_op) = best_plan.clone().operator { - let constant_expr = ScalarExpression::Constant(Arc::new(DataValue::Int32(Some(3)))); + let constant_expr = ScalarExpression::Constant(DataValue::Int32(Some(3))); if let ScalarExpression::Binary { right_expr, .. } = &project_op.exprs[0] { debug_assert_eq!(right_expr.as_ref(), &constant_expr); } else { @@ -163,7 +163,7 @@ mod test { range, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(-2)))), + max: Bound::Excluded(DataValue::Int32(Some(-2))), } ); } else { @@ -289,8 +289,8 @@ mod test { left_expr: Box::new(ScalarExpression::ColumnRef(ColumnRef::from( c1_col ))), - right_expr: Box::new(ScalarExpression::Constant(Arc::new( - DataValue::Int32(Some(1)) + right_expr: Box::new(ScalarExpression::Constant(DataValue::Int32( + Some(1) ))), evaluator: None, ty: LogicalType::Integer, @@ -356,20 +356,20 @@ mod test { range_1_c1, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(-2)))) + max: Bound::Excluded(DataValue::Int32(Some(-2))) } ); debug_assert_eq!( range_1_c2, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Excluded(DataValue::Int32(Some(2))), max: Bound::Unbounded } ); debug_assert_eq!( range_2_c1, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(2)))), + min: Bound::Excluded(DataValue::Int32(Some(2))), max: Bound::Unbounded } ); @@ -377,27 +377,27 @@ mod test { range_2_c2, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(-2)))) + max: Bound::Excluded(DataValue::Int32(Some(-2))) } ); debug_assert_eq!( range_3_c1, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(-1)))) + max: Bound::Excluded(DataValue::Int32(Some(-1))) } ); debug_assert_eq!( range_3_c2, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), max: Bound::Unbounded } ); debug_assert_eq!( range_4_c1, Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), max: Bound::Unbounded } ); @@ -405,7 +405,7 @@ mod test { range_4_c2, Range::Scope { min: Bound::Unbounded, - max: Bound::Excluded(Arc::new(DataValue::Int32(Some(-1)))) + max: Bound::Excluded(DataValue::Int32(Some(-1))) } ); @@ -434,7 +434,7 @@ mod test { debug_assert_eq!( plan_filter(&plan_1, table_state.column_id_by_name("c1"))?, Some(Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(1)))), + min: Bound::Excluded(DataValue::Int32(Some(1))), max: Bound::Unbounded, }) ); @@ -449,7 +449,7 @@ mod test { debug_assert_eq!( plan_filter(&plan_1, table_state.column_id_by_name("c1"))?, - Some(Range::Eq(Arc::new(DataValue::Null))) + Some(Range::Eq(DataValue::Null)) ); Ok(()) @@ -476,9 +476,9 @@ mod test { debug_assert_eq!( plan_filter(&plan_1, table_state.column_id_by_name("c1"))?, Some(Range::SortedRanges(vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(1)))), - Range::Eq(Arc::new(DataValue::Int32(Some(2)))), - Range::Eq(Arc::new(DataValue::Int32(Some(3)))), + Range::Eq(DataValue::Int32(Some(1))), + Range::Eq(DataValue::Int32(Some(2))), + Range::Eq(DataValue::Int32(Some(3))), ])) ); diff --git a/src/planner/operator/values.rs b/src/planner/operator/values.rs index 712e1a5c..ddac52ad 100644 --- a/src/planner/operator/values.rs +++ b/src/planner/operator/values.rs @@ -1,5 +1,5 @@ use crate::types::tuple::SchemaRef; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use fnck_sql_serde_macros::ReferenceSerialization; use itertools::Itertools; use std::fmt; @@ -7,7 +7,7 @@ use std::fmt::Formatter; #[derive(Debug, PartialEq, Eq, Clone, Hash, ReferenceSerialization)] pub struct ValuesOperator { - pub rows: Vec>, + pub rows: Vec>, pub schema_ref: SchemaRef, } diff --git a/src/serdes/column.rs b/src/serdes/column.rs index 14f15e0d..45a8446b 100644 --- a/src/serdes/column.rs +++ b/src/serdes/column.rs @@ -227,9 +227,7 @@ pub(crate) mod test { LogicalType::Integer, false, false, - Some(ScalarExpression::Constant(Arc::new(DataValue::UInt64( - Some(42), - )))), + Some(ScalarExpression::Constant(DataValue::UInt64(Some(42)))), )?, false, ))); @@ -317,9 +315,7 @@ pub(crate) mod test { LogicalType::Integer, false, false, - Some(ScalarExpression::Constant(Arc::new(DataValue::UInt64( - Some(42), - )))), + Some(ScalarExpression::Constant(DataValue::UInt64(Some(42)))), )?; desc.encode(&mut cursor, false, &mut reference_tables)?; cursor.seek(SeekFrom::Start(0))?; diff --git a/src/serdes/data_value.rs b/src/serdes/data_value.rs index 45733dca..872446de 100644 --- a/src/serdes/data_value.rs +++ b/src/serdes/data_value.rs @@ -4,7 +4,6 @@ use crate::storage::{TableCache, Transaction}; use crate::types::value::DataValue; use crate::types::LogicalType; use std::io::{Read, Write}; -use std::sync::Arc; impl DataValue { // FIXME: redundant code @@ -64,7 +63,7 @@ impl DataValue { let mut vec = Vec::with_capacity(len); for ty in types.iter() { - vec.push(Arc::new(Self::inner_decode(reader, ty)?)); + vec.push(Self::inner_decode(reader, ty)?); } Some(vec) } @@ -120,7 +119,6 @@ pub(crate) mod test { use crate::types::value::{DataValue, Utf8Type}; use sqlparser::ast::CharLengthUnits; use std::io::{Cursor, Seek, SeekFrom}; - use std::sync::Arc; #[test] fn test_serialization() -> Result<(), DatabaseError> { @@ -138,8 +136,8 @@ pub(crate) mod test { }; let source_4 = DataValue::Tuple(None); let source_5 = DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int32(None)), - Arc::new(DataValue::Int32(Some(42))), + DataValue::Int32(None), + DataValue::Int32(Some(42)), ])); let mut reference_tables = ReferenceTables::new(); diff --git a/src/storage/mod.rs b/src/storage/mod.rs index e73c8967..71de5b0c 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -10,7 +10,7 @@ use crate::serdes::ReferenceTables; use crate::storage::table_codec::TableCodec; use crate::types::index::{Index, IndexId, IndexMetaRef, IndexType}; use crate::types::tuple::{Tuple, TupleId}; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use crate::types::{ColumnId, LogicalType}; use crate::utils::lru::SharedLruCache; use bytes::Bytes; @@ -585,14 +585,14 @@ trait IndexImpl { fn eq_to_res<'a>( &self, - value: &ValueRef, + value: &DataValue, params: &IndexImplParams<'a, T>, ) -> Result, DatabaseError>; fn bound_key( &self, params: &IndexImplParams, - value: &ValueRef, + value: &DataValue, is_upper: bool, ) -> Result, DatabaseError>; } @@ -666,7 +666,7 @@ impl IndexImpl for IndexImplEnum { fn eq_to_res<'a>( &self, - value: &ValueRef, + value: &DataValue, params: &IndexImplParams<'a, T>, ) -> Result, DatabaseError> { match self { @@ -680,7 +680,7 @@ impl IndexImpl for IndexImplEnum { fn bound_key( &self, params: &IndexImplParams, - value: &ValueRef, + value: &DataValue, is_upper: bool, ) -> Result, DatabaseError> { match self { @@ -708,7 +708,7 @@ impl IndexImpl for PrimaryKeyIndexImpl { fn eq_to_res<'a>( &self, - value: &ValueRef, + value: &DataValue, params: &IndexImplParams<'a, T>, ) -> Result, DatabaseError> { let bytes = params @@ -729,7 +729,7 @@ impl IndexImpl for PrimaryKeyIndexImpl { fn bound_key( &self, params: &IndexImplParams, - val: &ValueRef, + val: &DataValue, _: bool, ) -> Result, DatabaseError> { TableCodec::encode_tuple_key(params.table_name, val) @@ -757,7 +757,7 @@ impl IndexImpl for UniqueIndexImpl { fn eq_to_res<'a>( &self, - value: &ValueRef, + value: &DataValue, params: &IndexImplParams<'a, T>, ) -> Result, DatabaseError> { let bytes = params @@ -776,7 +776,7 @@ impl IndexImpl for UniqueIndexImpl { fn bound_key( &self, params: &IndexImplParams, - value: &ValueRef, + value: &DataValue, _: bool, ) -> Result, DatabaseError> { let index = Index::new( @@ -800,7 +800,7 @@ impl IndexImpl for NormalIndexImpl { fn eq_to_res<'a>( &self, - value: &ValueRef, + value: &DataValue, params: &IndexImplParams<'a, T>, ) -> Result, DatabaseError> { let min = self.bound_key(params, value, false)?; @@ -816,7 +816,7 @@ impl IndexImpl for NormalIndexImpl { fn bound_key( &self, params: &IndexImplParams, - value: &ValueRef, + value: &DataValue, is_upper: bool, ) -> Result, DatabaseError> { let index = Index::new( @@ -840,7 +840,7 @@ impl IndexImpl for CompositeIndexImpl { fn eq_to_res<'a>( &self, - value: &ValueRef, + value: &DataValue, params: &IndexImplParams<'a, T>, ) -> Result, DatabaseError> { let min = self.bound_key(params, value, false)?; @@ -856,10 +856,10 @@ impl IndexImpl for CompositeIndexImpl { fn bound_key( &self, params: &IndexImplParams, - value: &ValueRef, + value: &DataValue, is_upper: bool, ) -> Result, DatabaseError> { - let values = if let DataValue::Tuple(Some(values)) = value.as_ref() { + let values = if let DataValue::Tuple(Some(values)) = &value { values.as_slice() } else { slice::from_ref(value) @@ -974,7 +974,7 @@ impl Iter for IndexIter<'_, T> { let table_name = self.params.table_name; let index_meta = &self.params.index_meta; let bound_encode = - |bound: Bound, is_upper: bool| -> Result<_, DatabaseError> { + |bound: Bound, is_upper: bool| -> Result<_, DatabaseError> { match bound { Bound::Included(val) => Ok(Bound::Included(self.inner.bound_key( &self.params, @@ -1092,27 +1092,27 @@ mod test { fn build_tuples() -> Vec { vec![ Tuple { - id: Some(Arc::new(DataValue::Int32(Some(0)))), + id: Some(DataValue::Int32(Some(0))), values: vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Boolean(Some(true))), - Arc::new(DataValue::Int32(Some(0))), + DataValue::Int32(Some(0)), + DataValue::Boolean(Some(true)), + DataValue::Int32(Some(0)), ], }, Tuple { - id: Some(Arc::new(DataValue::Int32(Some(1)))), + id: Some(DataValue::Int32(Some(1))), values: vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Boolean(Some(true))), - Arc::new(DataValue::Int32(Some(1))), + DataValue::Int32(Some(1)), + DataValue::Boolean(Some(true)), + DataValue::Int32(Some(1)), ], }, Tuple { - id: Some(Arc::new(DataValue::Int32(Some(2)))), + id: Some(DataValue::Int32(Some(2))), values: vec![ - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Boolean(Some(false))), - Arc::new(DataValue::Int32(Some(0))), + DataValue::Int32(Some(2)), + DataValue::Boolean(Some(false)), + DataValue::Int32(Some(0)), ], }, ] diff --git a/src/storage/rocksdb.rs b/src/storage/rocksdb.rs index be4f4020..6bf48cbb 100644 --- a/src/storage/rocksdb.rs +++ b/src/storage/rocksdb.rs @@ -190,11 +190,8 @@ mod test { transaction.append_tuple( &"test".to_string(), Tuple { - id: Some(Arc::new(DataValue::Int32(Some(1)))), - values: vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Boolean(Some(true))), - ], + id: Some(DataValue::Int32(Some(1))), + values: vec![DataValue::Int32(Some(1)), DataValue::Boolean(Some(true))], }, &[LogicalType::Integer, LogicalType::Boolean], false, @@ -202,11 +199,8 @@ mod test { transaction.append_tuple( &"test".to_string(), Tuple { - id: Some(Arc::new(DataValue::Int32(Some(2)))), - values: vec![ - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Boolean(Some(false))), - ], + id: Some(DataValue::Int32(Some(2))), + values: vec![DataValue::Int32(Some(2)), DataValue::Boolean(Some(false))], }, &[LogicalType::Integer, LogicalType::Boolean], false, @@ -220,10 +214,7 @@ mod test { )?; let option_1 = iter.next_tuple()?; - debug_assert_eq!( - option_1.unwrap().id, - Some(Arc::new(DataValue::Int32(Some(2)))) - ); + debug_assert_eq!(option_1.unwrap().id, Some(DataValue::Int32(Some(2)))); let option_2 = iter.next_tuple()?; debug_assert_eq!(option_2, None); @@ -247,10 +238,10 @@ mod test { .clone(); let a_column_id = table.get_column_id_by_name("a").unwrap(); let tuple_ids = vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Int32(Some(2))), - Arc::new(DataValue::Int32(Some(3))), - Arc::new(DataValue::Int32(Some(4))), + DataValue::Int32(Some(0)), + DataValue::Int32(Some(2)), + DataValue::Int32(Some(3)), + DataValue::Int32(Some(4)), ]; let mut iter = IndexIter { offset: 0, @@ -271,10 +262,10 @@ mod test { tx: &transaction, }, ranges: VecDeque::from(vec![ - Range::Eq(Arc::new(DataValue::Int32(Some(0)))), + Range::Eq(DataValue::Int32(Some(0))), Range::Scope { - min: Bound::Included(Arc::new(DataValue::Int32(Some(2)))), - max: Bound::Included(Arc::new(DataValue::Int32(Some(4)))), + min: Bound::Included(DataValue::Int32(Some(2))), + max: Bound::Included(DataValue::Int32(Some(4))), }, ]), scope_iter: None, @@ -312,20 +303,17 @@ mod test { columns, table.indexes[0].clone(), vec![Range::Scope { - min: Bound::Excluded(Arc::new(DataValue::Int32(Some(0)))), + min: Bound::Excluded(DataValue::Int32(Some(0))), max: Bound::Unbounded, }], ) .unwrap(); while let Some(tuple) = iter.next_tuple()? { - debug_assert_eq!(tuple.id, Some(Arc::new(DataValue::Int32(Some(1))))); + debug_assert_eq!(tuple.id, Some(DataValue::Int32(Some(1)))); debug_assert_eq!( tuple.values, - vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::Int32(Some(1))) - ] + vec![DataValue::Int32(Some(1)), DataValue::Int32(Some(1))] ) } diff --git a/src/storage/table_codec.rs b/src/storage/table_codec.rs index 25189376..2fe3eb63 100644 --- a/src/storage/table_codec.rs +++ b/src/storage/table_codec.rs @@ -11,7 +11,6 @@ use bytes::Bytes; use integer_encoding::FixedInt; use lazy_static::lazy_static; use std::io::{Cursor, Read, Seek, SeekFrom, Write}; -use std::sync::Arc; const BOUND_MIN_TAG: u8 = 0; const BOUND_MAX_TAG: u8 = 1; @@ -353,10 +352,10 @@ impl TableCodec { bytes: &[u8], primary_key_ty: &LogicalType, ) -> Result { - Ok(Arc::new(DataValue::inner_decode( + DataValue::inner_decode( &mut Cursor::new(bytes), primary_key_ty, - )?)) + ) } /// Key: {TableName}{COLUMN_TAG}{BOUND_MIN_TAG}{ColumnId} @@ -534,10 +533,10 @@ mod tests { let table_catalog = build_table_codec(); let tuple = Tuple { - id: Some(Arc::new(DataValue::Int32(Some(0)))), + id: Some(DataValue::Int32(Some(0))), values: vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::Decimal(Some(Decimal::new(1, 0)))), + DataValue::Int32(Some(0)), + DataValue::Decimal(Some(Decimal::new(1, 0))), ], }; let (_, bytes) = TableCodec::encode_tuple( @@ -602,7 +601,7 @@ mod tests { let table_catalog = build_table_codec(); let value = Arc::new(DataValue::Int32(Some(0))); let index = Index::new(0, slice::from_ref(&value), IndexType::PrimaryKey); - let tuple_id = Arc::new(DataValue::Int32(Some(0))); + let tuple_id = DataValue::Int32(Some(0)); let (_, bytes) = TableCodec::encode_index(&table_catalog.name, &index, &tuple_id)?; debug_assert_eq!( diff --git a/src/types/evaluator/tuple.rs b/src/types/evaluator/tuple.rs index 2fa1943d..9421420d 100644 --- a/src/types/evaluator/tuple.rs +++ b/src/types/evaluator/tuple.rs @@ -1,6 +1,5 @@ use crate::types::evaluator::BinaryEvaluator; use crate::types::evaluator::DataValue; -use crate::types::value::ValueRef; use serde::{Deserialize, Serialize}; use std::cmp::Ordering; use std::hint; @@ -18,7 +17,7 @@ pub struct TupleLtBinaryEvaluator; #[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)] pub struct TupleLtEqBinaryEvaluator; -fn tuple_cmp(v1: &[ValueRef], v2: &[ValueRef]) -> Option { +fn tuple_cmp(v1: &[DataValue], v2: &[DataValue]) -> Option { let mut order = Ordering::Equal; let mut v1_iter = v1.iter(); let mut v2_iter = v2.iter(); diff --git a/src/types/index.rs b/src/types/index.rs index ee153ec5..f9e9b83a 100644 --- a/src/types/index.rs +++ b/src/types/index.rs @@ -2,7 +2,7 @@ use crate::catalog::{TableCatalog, TableName}; use crate::errors::DatabaseError; use crate::expression::range_detacher::Range; use crate::expression::ScalarExpression; -use crate::types::value::ValueRef; +use crate::types::value::DataValue; use crate::types::{ColumnId, LogicalType}; use fnck_sql_serde_macros::ReferenceSerialization; use std::fmt; @@ -60,12 +60,12 @@ impl IndexMeta { #[derive(Debug, Clone)] pub struct Index<'a> { pub id: IndexId, - pub column_values: &'a [ValueRef], + pub column_values: &'a [DataValue], pub ty: IndexType, } impl<'a> Index<'a> { - pub fn new(id: IndexId, column_values: &'a [ValueRef], ty: IndexType) -> Self { + pub fn new(id: IndexId, column_values: &'a [DataValue], ty: IndexType) -> Self { Index { id, column_values, diff --git a/src/types/tuple.rs b/src/types/tuple.rs index eba5eff2..c2bd58f8 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -1,6 +1,6 @@ use crate::catalog::ColumnRef; use crate::errors::DatabaseError; -use crate::types::value::{DataValue, ValueRef}; +use crate::types::value::DataValue; use crate::types::LogicalType; use comfy_table::{Cell, Table}; use integer_encoding::FixedInt; @@ -19,7 +19,7 @@ lazy_static! { const BITS_MAX_INDEX: usize = 8; -pub type TupleId = ValueRef; +pub type TupleId = DataValue; pub type Schema = Vec; pub type SchemaRef = Arc; @@ -33,7 +33,7 @@ pub fn types(schema: &Schema) -> Vec { #[derive(Clone, Debug, PartialEq)] pub struct Tuple { pub id: Option, - pub values: Vec, + pub values: Vec, } impl Tuple { @@ -64,16 +64,13 @@ impl Tuple { } if is_none(bytes[i / BITS_MAX_INDEX], i % BITS_MAX_INDEX) { if projections[projection_i] == i { - tuple_values.push(Arc::new(DataValue::none(logic_type))); + tuple_values.push(DataValue::none(logic_type)); Self::values_push(schema, &tuple_values, &mut primary_keys, &mut projection_i); } } else if let Some(len) = logic_type.raw_len() { /// fixed length (e.g.: int) if projections[projection_i] == i { - tuple_values.push(Arc::new(DataValue::from_raw( - &bytes[pos..pos + len], - logic_type, - ))); + tuple_values.push(DataValue::from_raw(&bytes[pos..pos + len], logic_type)); Self::values_push(schema, &tuple_values, &mut primary_keys, &mut projection_i); } pos += len; @@ -82,10 +79,7 @@ impl Tuple { let len = u32::decode_fixed(&bytes[pos..pos + 4]) as usize; pos += 4; if projections[projection_i] == i { - tuple_values.push(Arc::new(DataValue::from_raw( - &bytes[pos..pos + len], - logic_type, - ))); + tuple_values.push(DataValue::from_raw(&bytes[pos..pos + len], logic_type)); Self::values_push(schema, &tuple_values, &mut primary_keys, &mut projection_i); } pos += len; @@ -96,7 +90,7 @@ impl Tuple { if primary_keys.len() == 1 { primary_keys.pop().unwrap() } else { - Arc::new(DataValue::Tuple(Some(primary_keys))) + DataValue::Tuple(Some(primary_keys)) } }); Tuple { @@ -107,8 +101,8 @@ impl Tuple { fn values_push( tuple_columns: &Schema, - tuple_values: &[ValueRef], - primary_keys: &mut Vec, + tuple_values: &[DataValue], + primary_keys: &mut Vec, projection_i: &mut usize, ) { if tuple_columns[*projection_i].desc().is_primary() { @@ -299,77 +293,77 @@ mod tests { let tuples = vec![ Tuple { - id: Some(Arc::new(DataValue::Int32(Some(0)))), + id: Some(DataValue::Int32(Some(0))), values: vec![ - Arc::new(DataValue::Int32(Some(0))), - Arc::new(DataValue::UInt32(Some(1))), - Arc::new(DataValue::Utf8 { + DataValue::Int32(Some(0)), + DataValue::UInt32(Some(1)), + DataValue::Utf8 { value: Some("LOL".to_string()), ty: Utf8Type::Variable(Some(2)), unit: CharLengthUnits::Characters, - }), - Arc::new(DataValue::Int16(Some(1))), - Arc::new(DataValue::UInt16(Some(1))), - Arc::new(DataValue::Float32(Some(0.1))), - Arc::new(DataValue::Float64(Some(0.1))), - Arc::new(DataValue::Int8(Some(1))), - Arc::new(DataValue::UInt8(Some(1))), - Arc::new(DataValue::Boolean(Some(true))), - Arc::new(DataValue::Date64(Some(0))), - Arc::new(DataValue::Date32(Some(0))), - Arc::new(DataValue::Decimal(Some(Decimal::new(0, 3)))), - Arc::new(DataValue::Utf8 { + }, + DataValue::Int16(Some(1)), + DataValue::UInt16(Some(1)), + DataValue::Float32(Some(0.1)), + DataValue::Float64(Some(0.1)), + DataValue::Int8(Some(1)), + DataValue::UInt8(Some(1)), + DataValue::Boolean(Some(true)), + DataValue::Date64(Some(0)), + DataValue::Date32(Some(0)), + DataValue::Decimal(Some(Decimal::new(0, 3))), + DataValue::Utf8 { value: Some("K".to_string()), ty: Utf8Type::Fixed(1), unit: CharLengthUnits::Characters, - }), - Arc::new(DataValue::Utf8 { + }, + DataValue::Utf8 { value: Some("LOL".to_string()), ty: Utf8Type::Variable(Some(2)), unit: CharLengthUnits::Octets, - }), - Arc::new(DataValue::Utf8 { + }, + DataValue::Utf8 { value: Some("K".to_string()), ty: Utf8Type::Fixed(1), unit: CharLengthUnits::Octets, - }), + }, ], }, Tuple { - id: Some(Arc::new(DataValue::Int32(Some(1)))), + id: Some(DataValue::Int32(Some(1))), values: vec![ - Arc::new(DataValue::Int32(Some(1))), - Arc::new(DataValue::UInt32(None)), - Arc::new(DataValue::Utf8 { + DataValue::Int32(Some(1)), + DataValue::UInt32(None), + DataValue::Utf8 { value: None, ty: Utf8Type::Variable(Some(2)), unit: CharLengthUnits::Characters, - }), - Arc::new(DataValue::Int16(None)), - Arc::new(DataValue::UInt16(None)), - Arc::new(DataValue::Float32(None)), - Arc::new(DataValue::Float64(None)), - Arc::new(DataValue::Int8(None)), - Arc::new(DataValue::UInt8(None)), - Arc::new(DataValue::Boolean(None)), - Arc::new(DataValue::Date64(None)), - Arc::new(DataValue::Date32(None)), - Arc::new(DataValue::Decimal(None)), - Arc::new(DataValue::Utf8 { + }, + DataValue::Int16(None), + DataValue::UInt16(None), + DataValue::Float32(None), + DataValue::Float64(None), + DataValue::Int8(None), + DataValue::UInt8(None), + DataValue::Boolean(None), + DataValue::Date64(None), + DataValue::Date32(None), + DataValue::Decimal(None), + DataValue::Utf8 { value: None, ty: Utf8Type::Fixed(1), unit: CharLengthUnits::Characters, - }), - Arc::new(DataValue::Utf8 { + }, + DataValue::Utf8 { value: None, ty: Utf8Type::Variable(Some(2)), unit: CharLengthUnits::Octets, - }), - Arc::new(DataValue::Utf8 { + }, + DataValue::Utf8 { value: None, ty: Utf8Type::Fixed(1), unit: CharLengthUnits::Octets, - }), + }, ], }, ]; diff --git a/src/types/tuple_builder.rs b/src/types/tuple_builder.rs index 97f05165..443b1986 100644 --- a/src/types/tuple_builder.rs +++ b/src/types/tuple_builder.rs @@ -2,7 +2,6 @@ use crate::errors::DatabaseError; use crate::types::tuple::{Schema, Tuple}; use crate::types::value::{DataValue, Utf8Type}; use sqlparser::ast::CharLengthUnits; -use std::sync::Arc; pub struct TupleBuilder<'a> { schema: &'a Schema, @@ -14,11 +13,11 @@ impl<'a> TupleBuilder<'a> { } pub fn build_result(message: String) -> Tuple { - let values = vec![Arc::new(DataValue::Utf8 { + let values = vec![DataValue::Utf8 { value: Some(message), ty: Utf8Type::Variable(None), unit: CharLengthUnits::Characters, - })]; + }]; Tuple { id: None, values } } @@ -31,14 +30,12 @@ impl<'a> TupleBuilder<'a> { let mut primary_keys = Vec::new(); for (i, value) in row.into_iter().enumerate() { - let data_value = Arc::new( - DataValue::Utf8 { - value: Some(value.to_string()), - ty: Utf8Type::Variable(None), - unit: CharLengthUnits::Characters, - } - .cast(self.schema[i].datatype())?, - ); + let data_value = DataValue::Utf8 { + value: Some(value.to_string()), + ty: Utf8Type::Variable(None), + unit: CharLengthUnits::Characters, + } + .cast(self.schema[i].datatype())?; if self.schema[i].desc().is_primary() { primary_keys.push(data_value.clone()); @@ -52,7 +49,7 @@ impl<'a> TupleBuilder<'a> { if primary_keys.len() == 1 { primary_keys.pop().unwrap() } else { - Arc::new(DataValue::Tuple(Some(primary_keys))) + DataValue::Tuple(Some(primary_keys)) } }); diff --git a/src/types/value.rs b/src/types/value.rs index b9ea9c21..337b0128 100644 --- a/src/types/value.rs +++ b/src/types/value.rs @@ -14,11 +14,10 @@ use std::fmt::Formatter; use std::hash::Hash; use std::io::Write; use std::str::FromStr; -use std::sync::Arc; use std::{cmp, fmt, mem}; lazy_static! { - pub static ref NULL_VALUE: ValueRef = Arc::new(DataValue::Null); + 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(); } @@ -30,8 +29,6 @@ pub const TIME_FMT: &str = "%H:%M:%S"; const ENCODE_GROUP_SIZE: usize = 8; const ENCODE_MARKER: u8 = 0xFF; -pub type ValueRef = Arc; - #[derive(Clone)] pub enum Utf8Type { Variable(Option), @@ -63,7 +60,7 @@ pub enum DataValue { Date64(Option), Time(Option), Decimal(Option), - Tuple(Option>), + Tuple(Option>), } macro_rules! generate_get_option { @@ -477,10 +474,7 @@ impl DataValue { LogicalType::Time => DataValue::Time(Some(UNIX_TIME.num_seconds_from_midnight())), LogicalType::Decimal(_, _) => DataValue::Decimal(Some(Decimal::new(0, 0))), LogicalType::Tuple(types) => { - let values = types - .iter() - .map(|ty| Arc::new(DataValue::init(ty))) - .collect_vec(); + let values = types.iter().map(DataValue::init).collect_vec(); DataValue::Tuple(Some(values)) } @@ -1381,7 +1375,7 @@ impl DataValue { LogicalType::Tuple(types) => Ok(if let Some(mut values) = values { for (i, value) in values.iter_mut().enumerate() { if types[i] != value.logical_type() { - *value = Arc::new(DataValue::clone(value).cast(&types[i])?); + *value = DataValue::clone(value).cast(&types[i])?; } } DataValue::Tuple(Some(values)) @@ -1631,7 +1625,6 @@ impl fmt::Debug for DataValue { mod test { use crate::errors::DatabaseError; use crate::types::value::DataValue; - use std::sync::Arc; #[test] fn test_mem_comparable_int() -> Result<(), DatabaseError> { @@ -1728,21 +1721,21 @@ mod test { let mut key_tuple_3 = Vec::new(); DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int8(None)), - Arc::new(DataValue::Int8(Some(0))), - Arc::new(DataValue::Int8(Some(1))), + DataValue::Int8(None), + DataValue::Int8(Some(0)), + DataValue::Int8(Some(1)), ])) .memcomparable_encode(&mut key_tuple_1)?; DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int8(Some(0))), - Arc::new(DataValue::Int8(Some(0))), - Arc::new(DataValue::Int8(Some(1))), + DataValue::Int8(Some(0)), + DataValue::Int8(Some(0)), + DataValue::Int8(Some(1)), ])) .memcomparable_encode(&mut key_tuple_2)?; DataValue::Tuple(Some(vec![ - Arc::new(DataValue::Int8(Some(0))), - Arc::new(DataValue::Int8(Some(0))), - Arc::new(DataValue::Int8(Some(2))), + DataValue::Int8(Some(0)), + DataValue::Int8(Some(0)), + DataValue::Int8(Some(2)), ])) .memcomparable_encode(&mut key_tuple_3)?; diff --git a/tests/macros-test/src/main.rs b/tests/macros-test/src/main.rs index 05743dd2..f428d232 100644 --- a/tests/macros-test/src/main.rs +++ b/tests/macros-test/src/main.rs @@ -11,7 +11,7 @@ mod test { use fnck_sql::expression::ScalarExpression; use fnck_sql::types::evaluator::EvaluatorFactory; use fnck_sql::types::tuple::{SchemaRef, Tuple}; - use fnck_sql::types::value::ValueRef; + use fnck_sql::types::value::DataValue; use fnck_sql::types::value::{DataValue, Utf8Type}; use fnck_sql::types::LogicalType; use fnck_sql::{implement_from_tuple, scala_function, table_function}; @@ -81,13 +81,13 @@ mod test { assert_eq!(my_struct.c2, "LOL"); } - scala_function!(MyScalaFunction::sum(LogicalType::Integer, LogicalType::Integer) -> LogicalType::Integer => (|v1: ValueRef, v2: ValueRef| { + scala_function!(MyScalaFunction::sum(LogicalType::Integer, LogicalType::Integer) -> LogicalType::Integer => (|v1: DataValue, v2: DataValue| { let plus_evaluator = EvaluatorFactory::binary_create(LogicalType::Integer, BinaryOperator::Plus)?; Ok(plus_evaluator.0.binary_eval(&v1, &v2)) })); - table_function!(MyTableFunction::test_numbers(LogicalType::Integer) -> [c1: LogicalType::Integer, c2: LogicalType::Integer] => (|v1: ValueRef| { + table_function!(MyTableFunction::test_numbers(LogicalType::Integer) -> [c1: LogicalType::Integer, c2: LogicalType::Integer] => (|v1: DataValue| { let num = v1.i32().unwrap(); Ok(Box::new((0..num)