Skip to content

Commit

Permalink
sql: use Rows in Engine trait
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 17, 2024
1 parent 1c7aeac commit d7912fc
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 18 deletions.
14 changes: 6 additions & 8 deletions src/sql/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::Session;
use crate::errinput;
use crate::error::Result;
use crate::sql::types::schema::Table;
use crate::sql::types::{Expression, Row, Value};
use crate::sql::types::{Expression, Row, Rows, Value};

use std::collections::HashSet;

Expand Down Expand Up @@ -53,18 +53,16 @@ pub trait Transaction: Catalog {
/// Reads an index entry, if it exists
fn read_index(&self, table: &str, column: &str, value: &Value) -> Result<HashSet<Value>>;
/// Scans a table's rows
fn scan(&self, table: &str, filter: Option<Expression>) -> Result<Scan>;
/// Scans a column's index entries
fn scan(&self, table: &str, filter: Option<Expression>) -> Result<Rows>;
/// Scans a column's index entries.
/// TODO: this is only used for tests. Remove it?
fn scan_index(&self, table: &str, column: &str) -> Result<IndexScan>;
/// Updates a table row
fn update(&mut self, table: &str, id: &Value, row: Row) -> Result<()>;
}

/// A row scan iterator
pub type Scan = Box<dyn DoubleEndedIterator<Item = Result<Row>> + Send>;

/// An index scan iterator
pub type IndexScan = Box<dyn DoubleEndedIterator<Item = Result<(Value, HashSet<Value>)>> + Send>;
/// An index scan iterator.
pub type IndexScan = Box<dyn Iterator<Item = Result<(Value, HashSet<Value>)>>>;

/// The catalog stores schema information
pub trait Catalog {
Expand Down
4 changes: 2 additions & 2 deletions src/sql/engine/local.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::super::types::schema::Table;
use super::super::types::{Expression, Row, Value};
use super::super::types::{Expression, Row, Rows, Value};
use super::{Catalog, Transaction as _};
use crate::encoding::{self, Key as _, Value as _};
use crate::error::Result;
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<E: storage::Engine> super::Transaction for Transaction<E> {
self.index_load(table, column, value)
}

fn scan(&self, table: &str, filter: Option<Expression>) -> Result<super::Scan> {
fn scan(&self, table: &str, filter: Option<Expression>) -> Result<Rows> {
let table = self.must_read_table(table)?;
Ok(Box::new(
self.txn
Expand Down
2 changes: 1 addition & 1 deletion src/sql/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod local;
mod raft;
mod session;

pub use engine::{Catalog, Engine, IndexScan, Scan, Transaction};
pub use engine::{Catalog, Engine, IndexScan, Transaction};
pub use local::Local;
pub use raft::{Raft, Status};
pub use session::{Session, StatementResult};
6 changes: 3 additions & 3 deletions src/sql/engine/raft.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::types::schema::Table;
use super::super::types::{Expression, Row, Value};
use super::{Catalog, Engine as _, IndexScan, Scan, Transaction as _};
use super::super::types::{Expression, Row, Rows, Value};
use super::{Catalog, Engine as _, IndexScan, Transaction as _};
use crate::encoding::{self, bincode, Value as _};
use crate::errdata;
use crate::error::Result;
Expand Down Expand Up @@ -232,7 +232,7 @@ impl super::Transaction for Transaction {
})
}

fn scan(&self, table: &str, filter: Option<Expression>) -> Result<Scan> {
fn scan(&self, table: &str, filter: Option<Expression>) -> Result<Rows> {
Ok(Box::new(
self.client
.query::<Vec<_>>(Query::Scan {
Expand Down
5 changes: 2 additions & 3 deletions src/sql/execution/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::source::{IndexLookup, KeyLookup, Nothing, Scan};
use crate::error::Result;
use crate::sql::engine::Transaction;
use crate::sql::plan::{Node, Plan};
use crate::sql::types::{Columns, Row};
use crate::sql::types::{Columns, Row, Rows};

/// A plan execution result.
pub enum ExecutionResult {
Expand All @@ -23,8 +23,7 @@ pub enum ExecutionResult {
pub struct QueryIterator {
// TODO: use a different type here.
pub columns: Columns,
// TODO: remove Send, it's only needed for the ResultSet conversion.
pub rows: Box<dyn Iterator<Item = Result<Row>> + Send>,
pub rows: Rows,
}

impl Iterator for QueryIterator {
Expand Down
2 changes: 1 addition & 1 deletion src/sql/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub type Row = Vec<Value>;
/// A row iterator.
///
/// TODO: try to avoid boxing here.
pub type Rows = Box<dyn Iterator<Item = Result<Row>> + Send>;
pub type Rows = Box<dyn Iterator<Item = Result<Row>>>;

/// A column (in a result set, see schema::Column for table columns).
///
Expand Down

0 comments on commit d7912fc

Please sign in to comment.