Closed Zhang-X0 closed 1 year ago
create table create_table_select_t1(id int, age int, name char); create table create_table_select_t2(id int, age int, name char); create table create_table_select_t5 as select id, age, id+age as sum from create_table_select_t1; create table create_table_select_t6 as select t1.id, t1.age, t2.name from create_table_select_t1 t1, create_table_select_t2 t2 where t1.id=t2.id; select count(*) from create_table_select_t6; select count(id) from create_table_select_t6; -118 +FAILURE
create table t1(c1 int, c2 int not null); insert into t1 values(1,1),(2,2); create table t2(c1 int, c2 int, c3 int); insert into t2 values(1,1,1),(2,2,2);
-- 未指定、别名field create table t3 select t.c1, t.c2 from t1 t;
-- 未指定、 create table t4 as select c1, c2, c1+1 from t1; create table t4 as select c1, c2, c2+1 as sum from t1;
-- 指定、field create table t5(x1 int, x2 int) as select c1, c2 from t1;
-- 指定、expr create table t6(x1 int, x2 int, x3 int) as select c1, c1+c2, c2+1 from t1; create table t7 as select a1.c1, a1.c2, a2.c3 from t1 a1, t2 a2 where a1.c1=a2.c1;
topic: create_table_select
description:
对其它内容的影响 修改了聚集函数的语法规则,将聚集函数名称的识别从词法阶段移到了语法阶段(将函数名识别成 ID,然后进行字符串比较来确定函数类型)。
语法规则: create table id [attr_def_list] [AS] select_stmt 显式定义了表结构时,注意 select_stmt 输出要与表结构一致。 没有显式定义表结构时,用 select 的输出格式作为表的结构。注意 select_stmt 里使用了类似 t1.c1 的别名时,要把别名里的表名去掉。fieldExpr列可以直接沿用到表中,其他需要额外判断,如计算表达式默认为 NOT NULL, 除非里面有允许为空的列。
实现方式 在语法解析阶段
ParsedSqlNode
里同时存放两种语句的解析结果。Stmt
阶段进行解析,确定表结构。 增加 Create Table 算子,替换掉 Executor。在物理算子下面挂一个 Select 语句的算子。 算子执行时首先创建表,然后逐行插入数据。插入失败时进行 drop_table