Skip to content

Commit

Permalink
feat: support Select Into
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Feb 17, 2024
1 parent 07c40ea commit 8ad71df
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/binder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<'a, T: Transaction> Binder<'a, T> {
let mut rows = Vec::with_capacity(expr_rows.len());
for expr_row in expr_rows {
if expr_row.len() != values_len {
return Err(DatabaseError::ValuesLenNotSame());
return Err(DatabaseError::ValuesLenMismatch(expr_row.len(), values_len));
}
let mut row = Vec::with_capacity(expr_row.len());

Expand Down
23 changes: 22 additions & 1 deletion src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::catalog::{ColumnCatalog, ColumnSummary, TableName};
use crate::errors::DatabaseError;
use crate::execution::volcano::dql::join::joins_nullable;
use crate::expression::{AliasType, BinaryOperator};
use crate::planner::operator::insert::InsertOperator;
use crate::planner::operator::join::JoinCondition;
use crate::planner::operator::sort::{SortField, SortOperator};
use crate::planner::operator::union::UnionOperator;
Expand All @@ -30,7 +31,8 @@ use crate::types::LogicalType;
use itertools::Itertools;
use sqlparser::ast::{
Distinct, Expr, Ident, Join, JoinConstraint, JoinOperator, Offset, OrderByExpr, Query, Select,
SelectItem, SetExpr, SetOperator, SetQuantifier, TableAlias, TableFactor, TableWithJoins,
SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, TableAlias, TableFactor,
TableWithJoins,
};

impl<'a, T: Transaction> Binder<'a, T> {
Expand Down Expand Up @@ -111,6 +113,25 @@ impl<'a, T: Transaction> Binder<'a, T> {

plan = self.bind_project(plan, select_list)?;

if let Some(SelectInto {
name,
unlogged,
temporary,
..
}) = &select.into
{
if *unlogged || *temporary {
todo!()
}
plan = LogicalPlan::new(
Operator::Insert(InsertOperator {
table_name: Arc::new(lower_case_name(name)?),
is_overwrite: false,
}),
vec![plan],
)
}

Ok(plan)
}

Expand Down
2 changes: 0 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ pub enum DatabaseError {
AmbiguousColumn(String),
#[error("values length not match, expect {0}, got {1}")]
ValuesLenMismatch(usize, usize),
#[error("values list must all be the same length")]
ValuesLenNotSame(),
#[error("binary operator types mismatch: {0} != {1}")]
BinaryOpTypeMismatch(String, String),
#[error("subquery error: {0}")]
Expand Down
18 changes: 9 additions & 9 deletions tests/slt/basic_test.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ select 1
----
1

# query R
# select 10000.00::FLOAT + 234.567::FLOAT
# ----
# 10234.567

# query R
# select 100.0::DOUBLE/8.0::DOUBLE
# ----
# 12.5
query R
select 10000.00::FLOAT + 234.567::FLOAT
----
10234.567

query R
select 100.0::DOUBLE/8.0::DOUBLE
----
12.5

query B
select 2>1
Expand Down
1 change: 0 additions & 1 deletion tests/slt/group_by.slt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ create table t (id int primary key, v1 int, v2 int)
statement ok
insert into t values (0,1,1), (1,2,1), (2,3,2), (3,4,2), (4,5,3)

# TODO: check on binder
statement error
select v2 + 1, v1 from t group by v2 + 1

Expand Down
9 changes: 4 additions & 5 deletions tests/slt/having.slt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ select count(x) as a, y + 1 as b from test group by b having b + 1 = 24;
----
1 23

# TODO: Filter pushed down to Agg
# query II
# select x from test group by x having max(y) = 22
# ----
# 11
query II
select x from test group by x having max(y) = 22
----
11

# query II
# select y + 1 as i from test group by y + 1 having count(x) > 1 and y + 1 = 3 or y + 1 = 23 order by i;
Expand Down
44 changes: 44 additions & 0 deletions tests/slt/select_into.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
statement ok
create table t1 (v1 int not null primary key, v2 int not null);

statement ok
create table t2 (v1 int not null primary key, v2 int not null);

statement ok
create table t3 (v1 int not null primary key, v2 int not null);

statement ok
insert into t1 values (1, 1), (4, 6), (3, 2), (2, 1);

query II rowsort
select * from t1;
----
1 1
2 1
3 2
4 6

statement ok
select * into t2 from t1;

statement ok
select v2, v1 into t3 from t2 where v2 != 6;

query II rowsort
select * from t2;
----
1 1
2 1
3 2
4 6

query II rowsort
select * from t3;
----
1 1
2 1
3 2

statement ok
drop table t1;

11 changes: 5 additions & 6 deletions tests/slt/where.slt
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ create table t(id int primary key, v1 int null, v2 int);
statement ok
insert into t values (0, 1, 1), (1, null, 2), (2, null, 3), (3, 4, 4);

# TODO: Support `IsNull`
# query I
# select v2 from t where v1 is null;
# ----
# 2
# 3
query I
select v2 from t where v1 is null;
----
2
3

# query I
# select v2 from t where v1 is not null;
Expand Down

0 comments on commit 8ad71df

Please sign in to comment.