Skip to content

Commit

Permalink
Merge pull request #5256 from Veeupup/select_with_from
Browse files Browse the repository at this point in the history
feat(planner): select without from
  • Loading branch information
mergify[bot] authored May 9, 2022
2 parents d6253aa + 4503184 commit 19a3f7c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
30 changes: 29 additions & 1 deletion query/src/sql/planner/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use std::sync::Arc;

use async_recursion::async_recursion;
use common_ast::ast::Expr;
use common_ast::ast::Indirection;
use common_ast::ast::Query;
use common_ast::ast::SelectStmt;
use common_ast::ast::SelectTarget;
use common_ast::ast::SetExpr;
use common_ast::ast::TableReference;
use common_datavalues::DataTypeImpl;
Expand Down Expand Up @@ -73,7 +75,7 @@ impl Binder {
let mut input_context = if let Some(from) = &stmt.from {
self.bind_table_reference(from, bind_context).await?
} else {
BindContext::new()
self.bind_one_table(stmt).await?
};

if let Some(expr) = &stmt.selection {
Expand Down Expand Up @@ -101,6 +103,32 @@ impl Binder {
Ok(output_context)
}

pub(super) async fn bind_one_table(&mut self, stmt: &SelectStmt) -> Result<BindContext> {
for select_target in &stmt.select_list {
if let SelectTarget::QualifiedName(names) = select_target {
for indirect in names {
if indirect == &Indirection::Star {
return Err(ErrorCode::SemanticError(
"SELECT * with no tables specified is not valid",
));
}
}
}
}
let database = "system";
let tenant = self.ctx.get_tenant();
let table_meta: Arc<dyn Table> = self
.resolve_data_source(tenant.as_str(), database, "one")
.await?;
let source = table_meta.read_plan(self.ctx.clone(), None).await?;
let table_index = self
.metadata
.add_table(database.to_string(), table_meta, source);

let result = self.bind_base_table(table_index).await?;
Ok(result)
}

pub(super) async fn bind_table_reference(
&mut self,
stmt: &TableReference,
Expand Down
12 changes: 12 additions & 0 deletions tests/suites/0_stateless/20+_others/20_0001_planner_v2.result
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
====SELECT_FROM_NUMBERS====
0
1
2
Expand All @@ -8,12 +9,18 @@
7
8
9
====COMPARSION====
5
====CAST====
5
====BINARY_OPERATOR====
-0.75
====FUNCTIONS====
0.8414709848078965
====IN_LIST====
1
3
====AGGREGATER====
2
4
3
Expand Down Expand Up @@ -45,6 +52,7 @@
0
0
9
====INNER_JOIN====
1 1
2 2
3 3
Expand All @@ -64,3 +72,7 @@
1 2
2 3
1000
====SELECT_WITHOUT_FROM====
2
8
new_planner
15 changes: 15 additions & 0 deletions tests/suites/0_stateless/20+_others/20_0001_planner_v2.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
set enable_planner_v2 = 1;

select '====SELECT_FROM_NUMBERS====';
select * from numbers(10);

-- Comparison expressions
select '====COMPARSION====';
select * from numbers(10) where number between 1 and 9 and number > 2 and number < 8 and number is not null and number = 5 and number >= 5 and number <= 5;

-- Cast expression
select '====CAST====';
select * from numbers(10) where cast(number as string) = '5';

-- Binary operator
select '====BINARY_OPERATOR====';
select (number + 1 - 2) * 3 / 4 from numbers(1);

-- Functions
select '====FUNCTIONS====';
select sin(cos(number)) from numbers(1);

-- In list
select '====IN_LIST====';
select * from numbers(5) where number in (1, 3);

-- Aggregator operator
select '====AGGREGATER====';
create table t(a int, b int);
insert into t values(1, 2), (2, 3), (3, 4);
select sum(a) + 1 from t group by a;
Expand Down Expand Up @@ -45,6 +52,7 @@ SELECT max(number) FROM numbers_mt (10) where number > 99999999998;
SELECT max(number) FROM numbers_mt (10) where number > 2;

-- Inner join
select '====INNER_JOIN====';
create table t(a int);
insert into t values(1),(2),(3);
create table t1(b float);
Expand All @@ -67,4 +75,11 @@ drop table t2;

select count(*) from numbers(1000) as t inner join numbers(1000) as t1 on t.number = t1.number;

-- Select without from
select '====SELECT_WITHOUT_FROM====';
select 1 + 1;
select to_int(8);
select "new_planner";
select *; -- {ErrorCode 1065}

set enable_planner_v2 = 0;

0 comments on commit 19a3f7c

Please sign in to comment.