Skip to content

Commit 9a8a482

Browse files
committed
refactor: remove ScalarExpression::Position
1 parent 7ba54ec commit 9a8a482

34 files changed

+751
-338
lines changed

src/binder/aggregate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A>
155155
}
156156
}
157157
ScalarExpression::Constant(_) | ScalarExpression::ColumnRef { .. } => (),
158-
ScalarExpression::Reference { .. } | ScalarExpression::Empty => unreachable!(),
158+
ScalarExpression::Empty => unreachable!(),
159159
ScalarExpression::Tuple(args)
160160
| ScalarExpression::ScalaFunction(ScalarFunction { args, .. })
161161
| ScalarExpression::Coalesce { exprs: args, .. } => {
@@ -390,7 +390,7 @@ impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A>
390390
Ok(())
391391
}
392392
ScalarExpression::Constant(_) => Ok(()),
393-
ScalarExpression::Reference { .. } | ScalarExpression::Empty => unreachable!(),
393+
ScalarExpression::Empty => unreachable!(),
394394
ScalarExpression::Tuple(args)
395395
| ScalarExpression::ScalaFunction(ScalarFunction { args, .. })
396396
| ScalarExpression::Coalesce { exprs: args, .. } => {

src/binder/create_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A>
4343
for expr in exprs {
4444
// TODO: Expression Index
4545
match self.bind_expr(&expr.expr)? {
46-
ScalarExpression::ColumnRef(column) => columns.push(column),
46+
ScalarExpression::ColumnRef { column, .. } => columns.push(column),
4747
expr => {
4848
return Err(DatabaseError::UnsupportedStmt(format!(
4949
"'CREATE INDEX' by {}",

src/binder/create_view.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A>
4646
column.set_ref_table(view_name.clone(), Ulid::new(), true);
4747

4848
ScalarExpression::Alias {
49-
expr: Box::new(ScalarExpression::ColumnRef(mapping_column.clone())),
50-
alias: AliasType::Expr(Box::new(ScalarExpression::ColumnRef(ColumnRef::from(
49+
expr: Box::new(ScalarExpression::column_expr(mapping_column.clone())),
50+
alias: AliasType::Expr(Box::new(ScalarExpression::column_expr(ColumnRef::from(
5151
column,
5252
)))),
5353
}

src/binder/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
259259

260260
let alias_expr = ScalarExpression::Alias {
261261
expr: Box::new(expr),
262-
alias: AliasType::Expr(Box::new(ScalarExpression::ColumnRef(ColumnRef::from(
262+
alias: AliasType::Expr(Box::new(ScalarExpression::column_expr(ColumnRef::from(
263263
alias_column,
264264
)))),
265265
};
@@ -311,13 +311,13 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
311311

312312
let columns = sub_query_schema
313313
.iter()
314-
.map(|column| ScalarExpression::ColumnRef(column.clone()))
314+
.map(|column| ScalarExpression::column_expr(column.clone()))
315315
.collect::<Vec<_>>();
316316
ScalarExpression::Tuple(columns)
317317
} else {
318318
fn_check(1)?;
319319

320-
ScalarExpression::ColumnRef(sub_query_schema[0].clone())
320+
ScalarExpression::column_expr(sub_query_schema[0].clone())
321321
};
322322
Ok((sub_query, expr))
323323
}
@@ -371,7 +371,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
371371
let source = self.context.bind_source(&table)?;
372372
let schema_buf = self.table_schema_buf.entry(Arc::new(table)).or_default();
373373

374-
Ok(ScalarExpression::ColumnRef(
374+
Ok(ScalarExpression::column_expr(
375375
source
376376
.column(&full_name.1, schema_buf)
377377
.ok_or_else(|| DatabaseError::ColumnNotFound(full_name.1.to_string()))?,
@@ -403,7 +403,7 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
403403
table_schema_buf.entry(table_name.clone()).or_default();
404404
source.column(&full_name.1, schema_buf)
405405
} {
406-
*got_column = Some(ScalarExpression::ColumnRef(column));
406+
*got_column = Some(ScalarExpression::column_expr(column));
407407
}
408408
}
409409
};

src/binder/insert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A>
5151
slice::from_ref(ident),
5252
Some(table_name.to_string()),
5353
)? {
54-
ScalarExpression::ColumnRef(catalog) => columns.push(catalog),
54+
ScalarExpression::ColumnRef { column, .. } => columns.push(column),
5555
_ => return Err(DatabaseError::UnsupportedStmt(ident.to_string())),
5656
}
5757
}

src/binder/select.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use crate::execution::dql::join::joins_nullable;
2323
use crate::expression::agg::AggKind;
2424
use crate::expression::simplify::ConstantCalculator;
2525
use crate::expression::visitor_mut::VisitorMut;
26-
use crate::expression::ScalarExpression::Constant;
2726
use crate::expression::{AliasType, BinaryOperator};
2827
use crate::planner::operator::aggregate::AggregateOperator;
2928
use crate::planner::operator::except::ExceptOperator;
@@ -103,7 +102,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
103102
}
104103
plan
105104
};
106-
let mut select_list = self.normalize_select_item(&select.projection, &plan)?;
105+
let mut select_list = self.normalize_select_item(&select.projection, &mut plan)?;
107106

108107
if let Some(predicate) = &select.selection {
109108
plan = self.bind_where(plan, predicate)?;
@@ -159,6 +158,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
159158
Ok(plan)
160159
}
161160

161+
/// FIXME: temp values need to register BindContext.bind_table
162162
fn bind_temp_values(&mut self, expr_rows: &[Vec<Expr>]) -> Result<LogicalPlan, DatabaseError> {
163163
let values_len = expr_rows[0].len();
164164

@@ -176,7 +176,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
176176
let mut expression = self.bind_expr(expr)?;
177177
ConstantCalculator.visit(&mut expression)?;
178178

179-
if let Constant(value) = expression {
179+
if let ScalarExpression::Constant(value) = expression {
180180
let value_type = value.logical_type();
181181

182182
inferred_types[col_index] = match &inferred_types[col_index] {
@@ -230,19 +230,19 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
230230
LogicalType::max_logical_type(left_schema.datatype(), right_schema.datatype())?;
231231
if &cast_type != left_schema.datatype() {
232232
left_cast.push(ScalarExpression::TypeCast {
233-
expr: Box::new(ScalarExpression::ColumnRef(left_schema.clone())),
233+
expr: Box::new(ScalarExpression::column_expr(left_schema.clone())),
234234
ty: cast_type.clone(),
235235
});
236236
} else {
237-
left_cast.push(ScalarExpression::ColumnRef(left_schema.clone()));
237+
left_cast.push(ScalarExpression::column_expr(left_schema.clone()));
238238
}
239239
if &cast_type != right_schema.datatype() {
240240
right_cast.push(ScalarExpression::TypeCast {
241-
expr: Box::new(ScalarExpression::ColumnRef(right_schema.clone())),
241+
expr: Box::new(ScalarExpression::column_expr(right_schema.clone())),
242242
ty: cast_type.clone(),
243243
});
244244
} else {
245-
right_cast.push(ScalarExpression::ColumnRef(right_schema.clone()));
245+
right_cast.push(ScalarExpression::column_expr(right_schema.clone()));
246246
}
247247
}
248248

@@ -312,7 +312,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
312312
let distinct_exprs = left_schema
313313
.iter()
314314
.cloned()
315-
.map(ScalarExpression::ColumnRef)
315+
.map(ScalarExpression::column_expr)
316316
.collect_vec();
317317

318318
let union_op = Operator::Union(UnionOperator {
@@ -344,7 +344,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
344344
let distinct_exprs = left_schema
345345
.iter()
346346
.cloned()
347-
.map(ScalarExpression::ColumnRef)
347+
.map(ScalarExpression::column_expr)
348348
.collect_vec();
349349

350350
let except_op = Operator::Except(ExceptOperator {
@@ -492,8 +492,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
492492
);
493493

494494
let alias_column_expr = ScalarExpression::Alias {
495-
expr: Box::new(ScalarExpression::ColumnRef(column)),
496-
alias: AliasType::Expr(Box::new(ScalarExpression::ColumnRef(ColumnRef::from(
495+
expr: Box::new(ScalarExpression::column_expr(column)),
496+
alias: AliasType::Expr(Box::new(ScalarExpression::column_expr(ColumnRef::from(
497497
alias_column,
498498
)))),
499499
};
@@ -548,7 +548,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
548548
fn normalize_select_item(
549549
&mut self,
550550
items: &[SelectItem],
551-
plan: &LogicalPlan,
551+
plan: &mut LogicalPlan,
552552
) -> Result<Vec<ScalarExpression>, DatabaseError> {
553553
let mut select_items = vec![];
554554

@@ -624,8 +624,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
624624
.expr_aliases
625625
.iter()
626626
.filter(|(_, expr)| {
627-
if let ScalarExpression::ColumnRef(col) = expr.unpack_alias_ref() {
628-
if fn_not_on_using(col) {
627+
if let ScalarExpression::ColumnRef { column, .. } = expr.unpack_alias_ref() {
628+
if fn_not_on_using(column) {
629629
exprs.push(ScalarExpression::clone(expr));
630630
return true;
631631
}
@@ -651,7 +651,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
651651
if !fn_not_on_using(column) {
652652
continue;
653653
}
654-
exprs.push(ScalarExpression::ColumnRef(column.clone()));
654+
exprs.push(ScalarExpression::column_expr(column.clone()));
655655
}
656656
Ok(())
657657
}
@@ -751,7 +751,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
751751
} else {
752752
BinaryOperator::Eq
753753
},
754-
left_expr: Box::new(ScalarExpression::ColumnRef(
754+
left_expr: Box::new(ScalarExpression::column_expr(
755755
agg.output_schema()[0].clone(),
756756
)),
757757
right_expr: Box::new(ScalarExpression::Constant(DataValue::Int32(
@@ -928,7 +928,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
928928
}
929929

930930
for column in select_items {
931-
if let ScalarExpression::ColumnRef(col) = column {
931+
if let ScalarExpression::ColumnRef { column, .. } = column {
932932
let _ = table_force_nullable
933933
.iter()
934934
.find(|(table_name, source, _)| {
@@ -937,11 +937,11 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
937937
.entry((*table_name).clone())
938938
.or_default();
939939

940-
source.column(col.name(), schema_buf).is_some()
940+
source.column(column.name(), schema_buf).is_some()
941941
})
942942
.map(|(_, _, nullable)| {
943-
if let Some(new_column) = col.nullable_for_join(*nullable) {
944-
*col = new_column;
943+
if let Some(new_column) = column.nullable_for_join(*nullable) {
944+
*column = new_column;
945945
}
946946
});
947947
}
@@ -1003,8 +1003,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
10031003
};
10041004
self.context.add_using(join_type, left_column, right_column);
10051005
on_keys.push((
1006-
ScalarExpression::ColumnRef(left_column.clone()),
1007-
ScalarExpression::ColumnRef(right_column.clone()),
1006+
ScalarExpression::column_expr(left_column.clone()),
1007+
ScalarExpression::column_expr(right_column.clone()),
10081008
));
10091009
}
10101010
Ok(JoinCondition::On {
@@ -1024,8 +1024,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
10241024
left_schema.iter().find(|column| column.name() == *name),
10251025
right_schema.iter().find(|column| column.name() == *name),
10261026
) {
1027-
let left_expr = ScalarExpression::ColumnRef(left_column.clone());
1028-
let right_expr = ScalarExpression::ColumnRef(right_column.clone());
1027+
let left_expr = ScalarExpression::column_expr(left_column.clone());
1028+
let right_expr = ScalarExpression::column_expr(right_column.clone());
10291029

10301030
self.context.add_using(join_type, left_column, right_column);
10311031
on_keys.push((left_expr, right_expr));
@@ -1077,7 +1077,10 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
10771077
BinaryOperator::Eq => {
10781078
match (left_expr.unpack_alias_ref(), right_expr.unpack_alias_ref()) {
10791079
// example: foo = bar
1080-
(ScalarExpression::ColumnRef(l), ScalarExpression::ColumnRef(r)) => {
1080+
(
1081+
ScalarExpression::ColumnRef { column: l, .. },
1082+
ScalarExpression::ColumnRef { column: r, .. },
1083+
) => {
10811084
// reorder left and right joins keys to pattern: (left, right)
10821085
if fn_contains(left_schema, l.summary())
10831086
&& fn_contains(right_schema, r.summary())
@@ -1099,8 +1102,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
10991102
});
11001103
}
11011104
}
1102-
(ScalarExpression::ColumnRef(column), _)
1103-
| (_, ScalarExpression::ColumnRef(column)) => {
1105+
(ScalarExpression::ColumnRef { column, .. }, _)
1106+
| (_, ScalarExpression::ColumnRef { column, .. }) => {
11041107
if fn_or_contains(left_schema, right_schema, column.summary()) {
11051108
accum_filter.push(ScalarExpression::Binary {
11061109
left_expr,

src/binder/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A>
4141
slice::from_ref(ident),
4242
Some(table_name.to_string()),
4343
)? {
44-
ScalarExpression::ColumnRef(column) => {
44+
ScalarExpression::ColumnRef { column, .. } => {
4545
let mut expr = if matches!(expression, ScalarExpression::Empty) {
4646
let default_value = column
4747
.default_value()?

src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<S: Storage> State<S> {
236236
"Expression Remapper".to_string(),
237237
HepBatchStrategy::once_topdown(),
238238
vec![
239-
NormalizationRuleImpl::ExpressionRemapper,
239+
NormalizationRuleImpl::BindExpressionPosition,
240240
// TIPS: This rule is necessary
241241
NormalizationRuleImpl::EvaluatorBind,
242242
],

src/errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::expression::{BinaryOperator, UnaryOperator};
1+
use crate::expression::{BinaryOperator, ScalarExpression, UnaryOperator};
22
use crate::types::tuple::TupleId;
33
use crate::types::LogicalType;
44
use chrono::ParseError;
@@ -161,6 +161,8 @@ pub enum DatabaseError {
161161
TupleIdNotFound(TupleId),
162162
#[error("there are more buckets: {0} than elements: {1}")]
163163
TooManyBuckets(usize, usize),
164+
#[error("this scalar expression: '{0}' unbind position")]
165+
UnbindExpressionPosition(ScalarExpression),
164166
#[error("unsupported unary operator: {0} cannot support {1} for calculations")]
165167
UnsupportedUnaryOperator(LogicalType, UnaryOperator),
166168
#[error("unsupported binary operator: {0} cannot support {1} for calculations")]

src/execution/ddl/create_index.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::execution::dql::projection::Projection;
22
use crate::execution::DatabaseError;
33
use crate::execution::{build_read, Executor, WriteExecutor};
4-
use crate::expression::ScalarExpression;
4+
use crate::expression::{BindPosition, ScalarExpression};
55
use crate::planner::operator::create_index::CreateIndexOperator;
66
use crate::planner::LogicalPlan;
77
use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache};
@@ -11,6 +11,7 @@ use crate::types::tuple::Tuple;
1111
use crate::types::tuple_builder::TupleBuilder;
1212
use crate::types::value::DataValue;
1313
use crate::types::ColumnId;
14+
use std::borrow::Cow;
1415
use std::ops::Coroutine;
1516
use std::ops::CoroutineState;
1617
use std::pin::Pin;
@@ -43,15 +44,21 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for CreateIndex {
4344
ty,
4445
} = self.op;
4546

46-
let (column_ids, column_exprs): (Vec<ColumnId>, Vec<ScalarExpression>) = columns
47-
.into_iter()
48-
.filter_map(|column| {
49-
column
50-
.id()
51-
.map(|id| (id, ScalarExpression::ColumnRef(column)))
52-
})
53-
.unzip();
47+
let (column_ids, mut column_exprs): (Vec<ColumnId>, Vec<ScalarExpression>) =
48+
columns
49+
.into_iter()
50+
.filter_map(|column| {
51+
column
52+
.id()
53+
.map(|id| (id, ScalarExpression::column_expr(column)))
54+
})
55+
.unzip();
5456
let schema = self.input.output_schema().clone();
57+
throw!(BindPosition::bind_exprs(
58+
column_exprs.iter_mut(),
59+
|| schema.iter().map(Cow::Borrowed),
60+
|a, b| a == b
61+
));
5562
let index_id = match unsafe { &mut (*transaction) }.add_index_meta(
5663
cache.0,
5764
&table_name,

0 commit comments

Comments
 (0)