Skip to content

Commit

Permalink
Support subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 5, 2024
1 parent ef15241 commit 3be9b63
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bustubx/src/expression/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::catalog::Schema;
use crate::catalog::{Column, DataType};
use crate::common::ScalarValue;
use crate::common::TableReference;
use crate::error::{BustubxResult};
use crate::error::BustubxResult;
use crate::expression::ExprTrait;
use crate::storage::Tuple;

Expand Down
9 changes: 5 additions & 4 deletions bustubx/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use crate::error::BustubxResult;
use sqlparser::{ast::Statement, dialect::PostgreSqlDialect, parser::Parser};
use tracing::span;

pub fn parse_sql(sql: &str) -> BustubxResult<Vec<Statement>> {
let stmts = Parser::parse_sql(&PostgreSqlDialect {}, sql)?;
Ok(stmts)
}

#[cfg(test)]
mod tests {

#[test]
pub fn test_sql() {
let sql = "select * from t1, t2, t3 inner join t4 on t3.id = t4.id";
pub fn test_parser() {
let sql = "select * from (select * from t1)";
let stmts = super::parse_sql(sql).unwrap();
println!("{:?}", stmts);
println!("{:#?}", stmts[0]);
}
}
3 changes: 3 additions & 0 deletions bustubx/src/planner/logical_planner/plan_set_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ impl LogicalPlanner<'_> {
// TODO handle alias
self.plan_table_with_joins(table_with_joins)
}
sqlparser::ast::TableFactor::Derived {
subquery, alias, ..
} => self.plan_query(subquery),
_ => Err(BustubxError::Plan(format!(
"sqlparser relation {} not supported",
relation
Expand Down
17 changes: 17 additions & 0 deletions tests/sqllogictest/slt/subquery.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
statement ok
create table t1 (a int, b int)

statement ok
insert into t1 values (2, 3), (5, 4)

query II rowsort
select * from (select * from t1)
----
2 3
5 4

query II rowsort
select * from (select b from t1)
----
3
4

0 comments on commit 3be9b63

Please sign in to comment.