diff --git a/bustubx/src/execution/physical_plan/empty.rs b/bustubx/src/execution/physical_plan/empty.rs index 4675761..0788222 100644 --- a/bustubx/src/execution/physical_plan/empty.rs +++ b/bustubx/src/execution/physical_plan/empty.rs @@ -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> { - Ok(None) + fn init(&self, _context: &mut ExecutionContext) -> BustubxResult<()> { + self.outputted_count.store(0, Ordering::SeqCst); + Ok(()) + } + fn next(&self, _context: &mut ExecutionContext) -> BustubxResult> { + 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 { diff --git a/bustubx/src/expression/literal.rs b/bustubx/src/expression/literal.rs index ea08431..7bcf557 100644 --- a/bustubx/src/expression/literal.rs +++ b/bustubx/src/expression/literal.rs @@ -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 { @@ -24,10 +23,10 @@ impl ExprTrait for Literal { Ok(self.value.clone()) } - fn to_column(&self, _input_schema: &Schema) -> BustubxResult { - Err(BustubxError::Plan(format!( - "expr {:?} as column not supported", - self - ))) + fn to_column(&self, input_schema: &Schema) -> BustubxResult { + Ok(Column::new( + format!("{}", self.value), + self.data_type(input_schema)?, + )) } } diff --git a/bustubx/src/planner/physical_planner/physical_planner.rs b/bustubx/src/planner/physical_planner/physical_planner.rs index 8f0d74d..69df16a 100644 --- a/bustubx/src/planner/physical_planner/physical_planner.rs +++ b/bustubx/src/planner/physical_planner/physical_planner.rs @@ -135,9 +135,10 @@ pub fn build_plan(logical_plan: Arc) -> 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 } diff --git a/tests/sqllogictest/slt/select.slt b/tests/sqllogictest/slt/select.slt index 970b124..e245198 100644 --- a/tests/sqllogictest/slt/select.slt +++ b/tests/sqllogictest/slt/select.slt @@ -9,4 +9,9 @@ select a, b from t1 ---- 1 1 2 3 -5 4 \ No newline at end of file +5 4 + +query I +select 1 +---- +1 \ No newline at end of file