Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Jan 2, 2025
1 parent 7e023e8 commit 8683df7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
6 changes: 3 additions & 3 deletions core/translate/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,11 +1150,11 @@ fn open_loop(
fn try_index_cover(
program: &mut ProgramBuilder,
related_columns: &BTreeSet<ColumnBinding>,
table_reference: &BTreeTableReference,
table_reference: &TableReference,
index_cursor_id: CursorID,
table_cursor_id: CursorID,
) -> bool {
let BTreeTableReference {
let TableReference {
table, table_index, ..
} = table_reference;

Expand All @@ -1167,7 +1167,7 @@ fn try_index_cover(
index_column_map.insert(pos, i);
}
if let Some(pos) = table
.columns
.columns()
.iter()
.position(|column| column.is_rowid_alias)
{
Expand Down
60 changes: 40 additions & 20 deletions core/translate/optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::collections::BTreeSet;
use std::rc::Rc;

use super::plan::{
get_table_ref_bitmask_for_ast_expr, get_table_ref_bitmask_for_operator, Aggregate,
ColumnBinding, DeletePlan, Direction, GroupBy, IterationDirection, Plan, ResultSetColumn,
Search, SelectPlan, SourceOperator, TableReference, TableReferenceType,
};
use crate::translate::optimizer;
use crate::{schema::Index, Result};
use sqlite3_parser::ast;
use sqlite3_parser::ast::{
FrameBound, FromClause, JoinConstraint, OneSelect, Over, ResultColumn, Select, SelectBody,
SelectTable, SortedColumn, Window,
};

use super::plan::{
get_table_ref_bitmask_for_ast_expr, get_table_ref_bitmask_for_operator, Aggregate,
TableReference, TableReferenceType, ColumnBinding, DeletePlan, Direction, GroupBy, IterationDirection, Plan,
ResultSetColumn, Search, SelectPlan, SourceOperator,
};

pub fn optimize_plan(plan: &mut Plan) -> Result<()> {
match plan {
Plan::Select(plan) => optimize_select_plan(plan),
Expand Down Expand Up @@ -666,7 +666,7 @@ fn related_columns(
order_by: &Option<Vec<(ast::Expr, Direction)>>,
aggregates: &[Aggregate],
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
source_related_columns(operator, related_columns, referenced_tables)?;
for column in result_columns.iter() {
Expand Down Expand Up @@ -706,7 +706,7 @@ fn related_columns(
fn source_related_columns(
source_operator: &SourceOperator,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
match source_operator {
SourceOperator::Join {
Expand Down Expand Up @@ -747,7 +747,27 @@ fn source_related_columns(
}
}
}
SourceOperator::Nothing => (),
SourceOperator::Nothing { .. } => (),
SourceOperator::Subquery {
plan, predicates, ..
} => {
optimizer::related_columns(
&plan.source,
&plan.result_columns,
&plan.where_clause,
&plan.group_by,
&plan.order_by,
&plan.aggregates,
related_columns,
&plan.referenced_tables,
)?;

if let Some(predicates) = predicates {
for expr in predicates {
expr_related_columns(expr, related_columns, referenced_tables)?;
}
}
}
}

Ok(())
Expand All @@ -756,7 +776,7 @@ fn source_related_columns(
fn from_clause_related_columns(
from: &FromClause,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
if let Some(select_table) = &from.select {
select_table_related_columns(select_table, related_columns, referenced_tables)?;
Expand All @@ -782,7 +802,7 @@ fn from_clause_related_columns(
fn select_table_related_columns(
select_table: &SelectTable,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
match select_table {
SelectTable::TableCall(_, exprs, _) => {
Expand All @@ -807,7 +827,7 @@ fn select_table_related_columns(
fn one_select_related_columns(
one_select: &OneSelect,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
match one_select {
OneSelect::Select {
Expand All @@ -829,14 +849,14 @@ fn one_select_related_columns(
ResultColumn::TableStar(table_name) => {
let Some(table_pos) = referenced_tables
.iter()
.position(|table| table.table.name == table_name.0)
.position(|table| table.table.get_name() == table_name.0)
else {
crate::bail_corrupt_error!(
"Optimize error: no such table: {}",
table_name
)
};
for i in 0..referenced_tables[table_pos].table.columns.len() {
for i in 0..referenced_tables[table_pos].table.columns().len() {
related_columns.insert(ColumnBinding {
table: table_pos,
column: i,
Expand Down Expand Up @@ -880,7 +900,7 @@ fn one_select_related_columns(
fn select_related_columns(
select: &Select,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
let Select {
body: SelectBody { select, compounds },
Expand Down Expand Up @@ -910,7 +930,7 @@ fn select_related_columns(
fn expr_related_columns(
expr: &ast::Expr,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
match expr {
ast::Expr::Between {
Expand Down Expand Up @@ -1041,7 +1061,7 @@ fn expr_related_columns(
fn window_related_columns(
window: &Window,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
if let Some(partition_by) = &window.partition_by {
for expr in partition_by {
Expand Down Expand Up @@ -1070,10 +1090,10 @@ fn window_related_columns(
#[inline]
fn full_related_columns(
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) {
for (table_pos, reference_table) in referenced_tables.iter().enumerate() {
for i in 0..reference_table.table.columns.len() {
for i in 0..reference_table.table.columns().len() {
related_columns.insert(ColumnBinding {
table: table_pos,
column: i,
Expand All @@ -1086,7 +1106,7 @@ fn full_related_columns(
fn order_by_related_columns(
order_by: &Option<Vec<SortedColumn>>,
related_columns: &mut BTreeSet<ColumnBinding>,
referenced_tables: &[BTreeTableReference],
referenced_tables: &[TableReference],
) -> Result<()> {
if let Some(sort_columns) = order_by {
for column in sort_columns {
Expand Down
12 changes: 3 additions & 9 deletions core/translate/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ use crate::{
vdbe::BranchOffset,
Result,
};
use core::fmt;
use sqlite3_parser::ast;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::{
fmt::{Display, Formatter},
rc::Rc,
};
use crate::{
schema::{PseudoTable, Type},
translate::plan::Plan::{Delete, Select},
};
use std::cmp::Ordering;
use std::collections::BTreeSet;

#[derive(Debug, Clone)]
pub struct ResultSetColumn {
Expand All @@ -45,7 +39,7 @@ pub enum Plan {
Delete(DeletePlan),
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ColumnBinding {
pub table: usize,
pub column: usize,
Expand Down

0 comments on commit 8683df7

Please sign in to comment.