From b2f47746a94cdd8b9de1e855e1d19dc1f77a1853 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 25 Jul 2024 17:27:45 +0300 Subject: [PATCH 1/6] core: Move Select struct at the top of select.rs It's the main data structure of the file so let's make it the first one. --- core/translate/select.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/core/translate/select.rs b/core/translate/select.rs index 54570c74d..301b4beef 100644 --- a/core/translate/select.rs +++ b/core/translate/select.rs @@ -19,6 +19,26 @@ use crate::{function::Func, schema::Table, vdbe::BranchOffset}; use super::SortInfo; +pub struct Select<'a> { + pub columns: &'a Vec, + pub column_info: Vec>, + pub src_tables: Vec>, // Tables we use to get data from. This includes "from" and "joins" + pub limit: &'a Option, + pub order_by: &'a Option>, + pub exist_aggregation: bool, + pub where_clause: &'a Option, + /// Ordered list of opened read table loops + /// Used for generating a loop that looks like this: + /// cursor 0 = open table 0 + /// for each row in cursor 0 + /// cursor 1 = open table 1 + /// for each row in cursor 1 + /// ... + /// end cursor 1 + /// end cursor 0 + pub loops: Vec, +} + #[derive(Debug)] pub struct SrcTable<'a> { pub table: Table, @@ -91,26 +111,6 @@ pub struct LoopInfo { pub open_cursor: usize, } -pub struct Select<'a> { - pub columns: &'a Vec, - pub column_info: Vec>, - pub src_tables: Vec>, // Tables we use to get data from. This includes "from" and "joins" - pub limit: &'a Option, - pub order_by: &'a Option>, - pub exist_aggregation: bool, - pub where_clause: &'a Option, - /// Ordered list of opened read table loops - /// Used for generating a loop that looks like this: - /// cursor 0 = open table 0 - /// for each row in cursor 0 - /// cursor 1 = open table 1 - /// for each row in cursor 1 - /// ... - /// end cursor 1 - /// end cursor 0 - pub loops: Vec, -} - pub fn build_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result> { match &select.body.select { ast::OneSelect::Select { From a4afadfd2ea6d8ba8fc4e5c6b0fcaa8a0adbc5e3 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 25 Jul 2024 17:32:50 +0300 Subject: [PATCH 2/6] core: Select struct documentation --- core/translate/select.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/core/translate/select.rs b/core/translate/select.rs index 301b4beef..a40813fdc 100644 --- a/core/translate/select.rs +++ b/core/translate/select.rs @@ -19,23 +19,35 @@ use crate::{function::Func, schema::Table, vdbe::BranchOffset}; use super::SortInfo; +/// A representation of a `SELECT` statement that has all the information +/// needed for code generation. pub struct Select<'a> { + /// The columns that are being selected. pub columns: &'a Vec, + /// Information about each column. pub column_info: Vec>, - pub src_tables: Vec>, // Tables we use to get data from. This includes "from" and "joins" + /// The tables we are retrieving data from, including tables mentioned + /// in `FROM` and `JOIN` clauses. + pub src_tables: Vec>, + /// The `LIMIT` clause. pub limit: &'a Option, + /// The `ORDER BY` clause. pub order_by: &'a Option>, + /// Whether the query contains an aggregation function. pub exist_aggregation: bool, + /// The `WHERE` clause. pub where_clause: &'a Option, - /// Ordered list of opened read table loops - /// Used for generating a loop that looks like this: - /// cursor 0 = open table 0 - /// for each row in cursor 0 - /// cursor 1 = open table 1 - /// for each row in cursor 1 - /// ... - /// end cursor 1 - /// end cursor 0 + /// Ordered list of opened read table loops. + /// + /// The list is used to generate inner loops like this: + /// + /// cursor 0 = open table 0 + /// for each row in cursor 0 + /// cursor 1 = open table 1 + /// for each row in cursor 1 + /// ... + /// end cursor 1 + /// end cursor 0 pub loops: Vec, } From 97afd768659d902c1e54c374ab5539305e75844f Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 25 Jul 2024 17:39:05 +0300 Subject: [PATCH 3/6] core: Clean up imports in select.rs --- core/translate/select.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/core/translate/select.rs b/core/translate/select.rs index a40813fdc..42ad53a1d 100644 --- a/core/translate/select.rs +++ b/core/translate/select.rs @@ -1,23 +1,17 @@ -use std::rc::Rc; - -use crate::Result; -use sqlite3_parser::ast::{self, JoinOperator, JoinType}; - -use crate::function::AggFunc; -use crate::schema::{Column, PseudoTable, Schema}; -use crate::translate::expr::analyze_columns; -use crate::translate::expr::maybe_apply_affinity; -use crate::translate::expr::translate_expr; -use crate::translate::normalize_ident; +use crate::function::{AggFunc, Func}; +use crate::schema::{Column, PseudoTable, Schema, Table}; +use crate::translate::expr::{analyze_columns, maybe_apply_affinity, translate_expr}; use crate::translate::where_clause::{ process_where, translate_processed_where, translate_where, ProcessedWhereClause, }; -use crate::translate::{Insn, LimitInfo}; +use crate::translate::{normalize_ident, Insn, LimitInfo, SortInfo}; use crate::types::{OwnedRecord, OwnedValue}; -use crate::vdbe::{builder::ProgramBuilder, Program}; -use crate::{function::Func, schema::Table, vdbe::BranchOffset}; +use crate::vdbe::{builder::ProgramBuilder, BranchOffset, Program}; +use crate::Result; -use super::SortInfo; +use sqlite3_parser::ast::{self, JoinOperator, JoinType}; + +use std::rc::Rc; /// A representation of a `SELECT` statement that has all the information /// needed for code generation. From 16e0e740dff6d3be6c3f411e4407978a4fdbd9ef Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 25 Jul 2024 17:40:46 +0300 Subject: [PATCH 4/6] core: Rename build_select() to prepare_select() Let's follow SQLite's naming here. --- core/translate/mod.rs | 4 ++-- core/translate/select.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/translate/mod.rs b/core/translate/mod.rs index f0abe0b62..8def3d613 100644 --- a/core/translate/mod.rs +++ b/core/translate/mod.rs @@ -11,7 +11,7 @@ use crate::sqlite3_ondisk::{DatabaseHeader, MIN_PAGE_CACHE_SIZE}; use crate::util::normalize_ident; use crate::vdbe::{builder::ProgramBuilder, BranchOffset, Insn, Program}; use crate::Result; -use select::{build_select, translate_select}; +use select::{prepare_select, translate_select}; use sqlite3_parser::ast; struct LimitInfo { @@ -36,7 +36,7 @@ pub fn translate( ) -> Result { match stmt { ast::Stmt::Select(select) => { - let select = build_select(schema, &select)?; + let select = prepare_select(schema, &select)?; translate_select(select) } ast::Stmt::Pragma(name, body) => translate_pragma(&name, body, database_header, pager), diff --git a/core/translate/select.rs b/core/translate/select.rs index 42ad53a1d..bc7243bb1 100644 --- a/core/translate/select.rs +++ b/core/translate/select.rs @@ -117,7 +117,7 @@ pub struct LoopInfo { pub open_cursor: usize, } -pub fn build_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result> { +pub fn prepare_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result> { match &select.body.select { ast::OneSelect::Select { columns, From b25f63f360819164aaaf62cc7bc14ac960b2c813 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 25 Jul 2024 17:42:08 +0300 Subject: [PATCH 5/6] core: Move LimitInfo to select.rs That's the only place where it's used. --- core/translate/mod.rs | 6 ------ core/translate/select.rs | 8 +++++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/translate/mod.rs b/core/translate/mod.rs index 8def3d613..f7ae16f32 100644 --- a/core/translate/mod.rs +++ b/core/translate/mod.rs @@ -14,12 +14,6 @@ use crate::Result; use select::{prepare_select, translate_select}; use sqlite3_parser::ast; -struct LimitInfo { - limit_reg: usize, - num: i64, - goto_label: BranchOffset, -} - #[derive(Debug)] struct SortInfo { sorter_cursor: usize, diff --git a/core/translate/select.rs b/core/translate/select.rs index bc7243bb1..40db56e20 100644 --- a/core/translate/select.rs +++ b/core/translate/select.rs @@ -4,7 +4,7 @@ use crate::translate::expr::{analyze_columns, maybe_apply_affinity, translate_ex use crate::translate::where_clause::{ process_where, translate_processed_where, translate_where, ProcessedWhereClause, }; -use crate::translate::{normalize_ident, Insn, LimitInfo, SortInfo}; +use crate::translate::{normalize_ident, Insn, SortInfo}; use crate::types::{OwnedRecord, OwnedValue}; use crate::vdbe::{builder::ProgramBuilder, BranchOffset, Program}; use crate::Result; @@ -117,6 +117,12 @@ pub struct LoopInfo { pub open_cursor: usize, } +struct LimitInfo { + limit_reg: usize, + num: i64, + goto_label: BranchOffset, +} + pub fn prepare_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result> { match &select.body.select { ast::OneSelect::Select { From 9dd505803f16f1759ead3664e4ad16ca2ab8f512 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 25 Jul 2024 17:43:03 +0300 Subject: [PATCH 6/6] core: Move SortInfo to select.rs It's the only place where it's used. --- core/translate/mod.rs | 9 +-------- core/translate/select.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/translate/mod.rs b/core/translate/mod.rs index f7ae16f32..9eb2859ae 100644 --- a/core/translate/mod.rs +++ b/core/translate/mod.rs @@ -9,18 +9,11 @@ use crate::pager::Pager; use crate::schema::Schema; use crate::sqlite3_ondisk::{DatabaseHeader, MIN_PAGE_CACHE_SIZE}; use crate::util::normalize_ident; -use crate::vdbe::{builder::ProgramBuilder, BranchOffset, Insn, Program}; +use crate::vdbe::{builder::ProgramBuilder, Insn, Program}; use crate::Result; use select::{prepare_select, translate_select}; use sqlite3_parser::ast; -#[derive(Debug)] -struct SortInfo { - sorter_cursor: usize, - sorter_reg: usize, - count: usize, -} - /// Translate SQL statement into bytecode program. pub fn translate( schema: &Schema, diff --git a/core/translate/select.rs b/core/translate/select.rs index 40db56e20..3cd9ebb2d 100644 --- a/core/translate/select.rs +++ b/core/translate/select.rs @@ -4,7 +4,7 @@ use crate::translate::expr::{analyze_columns, maybe_apply_affinity, translate_ex use crate::translate::where_clause::{ process_where, translate_processed_where, translate_where, ProcessedWhereClause, }; -use crate::translate::{normalize_ident, Insn, SortInfo}; +use crate::translate::{normalize_ident, Insn}; use crate::types::{OwnedRecord, OwnedValue}; use crate::vdbe::{builder::ProgramBuilder, BranchOffset, Program}; use crate::Result; @@ -123,6 +123,13 @@ struct LimitInfo { goto_label: BranchOffset, } +#[derive(Debug)] +struct SortInfo { + sorter_cursor: usize, + sorter_reg: usize, + count: usize, +} + pub fn prepare_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result> { match &select.body.select { ast::OneSelect::Select {