Skip to content

Commit

Permalink
Support select 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 2, 2024
1 parent 054ab50 commit ee315cb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
28 changes: 24 additions & 4 deletions bustubx/src/execution/physical_plan/empty.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
use crate::catalog::{Schema, SchemaRef};
use crate::catalog::SchemaRef;
use crate::execution::{ExecutionContext, VolcanoExecutor};
use crate::{BustubxResult, Tuple};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

#[derive(Debug)]
pub struct PhysicalEmpty {
pub produce_row_count: usize,
pub schema: SchemaRef,
outputted_count: AtomicUsize,
}

impl PhysicalEmpty {
pub fn new(produce_row_count: usize, schema: SchemaRef) -> Self {
Self {
produce_row_count,
schema,
outputted_count: AtomicUsize::new(0),
}
}
}

impl VolcanoExecutor for PhysicalEmpty {
fn next(&self, context: &mut ExecutionContext) -> BustubxResult<Option<Tuple>> {
Ok(None)
fn init(&self, _context: &mut ExecutionContext) -> BustubxResult<()> {
self.outputted_count.store(0, Ordering::SeqCst);
Ok(())
}
fn next(&self, _context: &mut ExecutionContext) -> BustubxResult<Option<Tuple>> {
if self.outputted_count.fetch_add(1, Ordering::SeqCst) < self.produce_row_count {
Ok(Some(Tuple::new(self.schema.clone(), vec![])))
} else {
Ok(None)
}
}

fn output_schema(&self) -> SchemaRef {
Expand Down
11 changes: 5 additions & 6 deletions bustubx/src/expression/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::common::ScalarValue;
use crate::error::BustubxResult;
use crate::expression::ExprTrait;
use crate::storage::Tuple;
use crate::BustubxError;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Literal {
Expand All @@ -24,10 +23,10 @@ impl ExprTrait for Literal {
Ok(self.value.clone())
}

fn to_column(&self, _input_schema: &Schema) -> BustubxResult<Column> {
Err(BustubxError::Plan(format!(
"expr {:?} as column not supported",
self
)))
fn to_column(&self, input_schema: &Schema) -> BustubxResult<Column> {
Ok(Column::new(
format!("{}", self.value),
self.data_type(input_schema)?,
))
}
}
7 changes: 4 additions & 3 deletions bustubx/src/planner/physical_planner/physical_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ pub fn build_plan(logical_plan: Arc<LogicalPlan>) -> PhysicalPlan {
LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row,
schema,
}) => PhysicalPlan::Empty(PhysicalEmpty {
schema: schema.clone(),
}),
}) => PhysicalPlan::Empty(PhysicalEmpty::new(
if *produce_one_row { 1 } else { 0 },
schema.clone(),
)),
};
plan
}
7 changes: 6 additions & 1 deletion tests/sqllogictest/slt/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ select a, b from t1
----
1 1
2 3
5 4
5 4

query I
select 1
----
1

0 comments on commit ee315cb

Please sign in to comment.