Skip to content

Commit e2db2b4

Browse files
authored
chore(query): add some function alias (#17920)
1 parent 978f295 commit e2db2b4

File tree

13 files changed

+705
-35
lines changed

13 files changed

+705
-35
lines changed

src/meta/app/src/schema/create_option.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ impl CreateOption {
5454
matches!(self, CreateOption::CreateOrReplace)
5555
}
5656

57+
pub fn if_not_exist(&self) -> bool {
58+
matches!(self, CreateOption::CreateIfNotExists)
59+
}
60+
5761
pub fn if_return_error(&self) -> bool {
5862
matches!(self, CreateOption::Create)
5963
}

src/query/ast/src/ast/statements/task.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::fmt::Formatter;
1919
use derive_visitor::Drive;
2020
use derive_visitor::DriveMut;
2121

22+
use super::CreateOption;
2223
use crate::ast::quote::QuotedString;
2324
use crate::ast::write_comma_separated_string_list;
2425
use crate::ast::write_comma_separated_string_map;
@@ -61,7 +62,7 @@ pub enum CreateTaskOption {
6162

6263
#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
6364
pub struct CreateTaskStmt {
64-
pub if_not_exists: bool,
65+
pub create_option: CreateOption,
6566
pub name: String,
6667
pub warehouse: Option<String>,
6768
pub schedule_opts: Option<ScheduleOptions>,
@@ -105,11 +106,14 @@ impl CreateTaskStmt {
105106

106107
impl Display for CreateTaskStmt {
107108
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
108-
write!(f, "CREATE TASK")?;
109-
if self.if_not_exists {
109+
write!(f, "CREATE")?;
110+
if let CreateOption::CreateOrReplace = self.create_option {
111+
write!(f, " OR REPLACE")?;
112+
}
113+
write!(f, " TASK")?;
114+
if let CreateOption::CreateIfNotExists = self.create_option {
110115
write!(f, " IF NOT EXISTS")?;
111116
}
112-
113117
write!(f, " {}", self.name)?;
114118

115119
if let Some(warehouse) = self.warehouse.as_ref() {

src/query/ast/src/parser/statement.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,31 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
122122
},
123123
);
124124

125-
let create_task = map(
125+
let create_task = map_res(
126126
rule! {
127-
CREATE ~ TASK ~ ( IF ~ ^NOT ~ ^EXISTS )?
127+
CREATE ~ ( OR ~ ^REPLACE )? ~ TASK ~ ( IF ~ ^NOT ~ ^EXISTS )?
128128
~ #ident
129129
~ #create_task_option*
130130
~ #set_table_option?
131131
~ AS ~ #task_sql_block
132132
},
133-
|(_, _, opt_if_not_exists, task, create_task_opts, session_opts, _, sql)| {
133+
|(
134+
_,
135+
opt_or_replace,
136+
_,
137+
opt_if_not_exists,
138+
task,
139+
create_task_opts,
140+
session_opts,
141+
_,
142+
sql,
143+
)| {
134144
let session_opts = session_opts.unwrap_or_default();
145+
let create_option =
146+
parse_create_option(opt_or_replace.is_some(), opt_if_not_exists.is_some())?;
147+
135148
let mut stmt = CreateTaskStmt {
136-
if_not_exists: opt_if_not_exists.is_some(),
149+
create_option,
137150
name: task.to_string(),
138151
warehouse: None,
139152
schedule_opts: None,
@@ -148,7 +161,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
148161
for opt in create_task_opts {
149162
stmt.apply_opt(opt);
150163
}
151-
Statement::CreateTask(stmt)
164+
Ok(Statement::CreateTask(stmt))
152165
},
153166
);
154167

@@ -2771,9 +2784,9 @@ pub fn insert_stmt(allow_raw: bool) -> impl FnMut(Input) -> IResult<Statement> {
27712784
} else {
27722785
insert_source
27732786
};
2774-
map(
2787+
map_res(
27752788
rule! {
2776-
#with? ~ INSERT ~ #hint? ~ ( INTO | OVERWRITE ) ~ TABLE?
2789+
#with? ~ INSERT ~ #hint? ~ OVERWRITE? ~ INTO? ~ TABLE?
27772790
~ #dot_separated_idents_1_to_3
27782791
~ ( "(" ~ #comma_separated_list1(ident) ~ ")" )?
27792792
~ #insert_source_parser
@@ -2783,12 +2796,18 @@ pub fn insert_stmt(allow_raw: bool) -> impl FnMut(Input) -> IResult<Statement> {
27832796
_,
27842797
opt_hints,
27852798
overwrite,
2799+
into,
27862800
_,
27872801
(catalog, database, table),
27882802
opt_columns,
27892803
source,
27902804
)| {
2791-
Statement::Insert(InsertStmt {
2805+
if overwrite.is_none() && into.is_none() {
2806+
return Err(nom::Err::Failure(ErrorKind::Other(
2807+
"INSERT statement must be followed by 'overwrite' or 'into'",
2808+
)));
2809+
}
2810+
Ok(Statement::Insert(InsertStmt {
27922811
hints: opt_hints,
27932812
with,
27942813
catalog,
@@ -2798,8 +2817,8 @@ pub fn insert_stmt(allow_raw: bool) -> impl FnMut(Input) -> IResult<Statement> {
27982817
.map(|(_, columns, _)| columns)
27992818
.unwrap_or_default(),
28002819
source,
2801-
overwrite: overwrite.kind == OVERWRITE,
2802-
})
2820+
overwrite: overwrite.is_some(),
2821+
}))
28032822
},
28042823
)(i)
28052824
}

src/query/ast/tests/it/parser.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ fn test_statement() {
248248
r#"insert into t (c1, c2) values (1, 2), (3, 4);"#,
249249
r#"insert into t (c1, c2) values (1, 2);"#,
250250
r#"insert into table t select * from t2;"#,
251+
r#"insert overwrite into table t select * from t2;"#,
252+
r#"insert overwrite table t select * from t2;"#,
253+
r#"INSERT ALL
254+
WHEN c3 = 1 THEN
255+
INTO t1
256+
WHEN c3 = 3 THEN
257+
INTO t2
258+
SELECT * from s;"#,
259+
r#"INSERT overwrite ALL
260+
WHEN c3 = 1 THEN
261+
INTO t1
262+
WHEN c3 = 3 THEN
263+
INTO t2
264+
SELECT * from s;"#,
251265
r#"select parse_json('{"k1": [0, 1, 2]}').k1[0];"#,
252266
r#"SELECT avg((number > 314)::UInt32);"#,
253267
r#"SELECT 1 - (2 + 3);"#,
@@ -941,6 +955,7 @@ fn test_statement_error() {
941955
r#"CREATE TABLE t(c1 int, c2 int) partition by (c1, c2) PROPERTIES ("read.split.target-size"='134217728', "read.split.metadata-target-size"=33554432);"#,
942956
r#"drop table if a.b"#,
943957
r#"truncate table a.b.c.d"#,
958+
r#"insert table t select * from t2;"#,
944959
r#"truncate a"#,
945960
r#"drop a"#,
946961
r#"insert into t format"#,

src/query/ast/tests/it/testdata/stmt-error.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ error:
137137
| ^ unexpected `.`, expecting `FORMAT` or `;`
138138

139139

140+
---------- Input ----------
141+
insert table t select * from t2;
142+
---------- Output ---------
143+
error:
144+
--> SQL:1:1
145+
|
146+
1 | insert table t select * from t2;
147+
| ^^^^^^
148+
| |
149+
| INSERT statement must be followed by 'overwrite' or 'into'
150+
| while parsing `INSERT INTO [TABLE] <table> [(<column>, ...)] (FORMAT <format> | VALUES <values> | <query>)`
151+
152+
140153
---------- Input ----------
141154
truncate a
142155
---------- Output ---------

0 commit comments

Comments
 (0)