Skip to content

feat(parser): support emit_mode #7783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/frontend/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ pub async fn handle(

with_options: _, // It is put in OptimizerContext
or_replace, // not supported
emit_mode,
} => {
if or_replace {
return Err(ErrorCode::NotImplemented(
Expand All @@ -324,6 +325,13 @@ pub async fn handle(
)
.into());
}
if emit_mode == Some(EmitMode::OnWindowClose) {
return Err(ErrorCode::NotImplemented(
"CREATE MATERIALIZED VIEW EMIT ON WINDOW CLOSE".to_string(),
None.into(),
)
.into());
}
if materialized {
create_mv::handle_create_mv(handler_args, name, *query, columns).await
} else {
Expand Down
21 changes: 21 additions & 0 deletions src/sqlparser/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ pub enum Statement {
name: ObjectName,
columns: Vec<Ident>,
query: Box<Query>,
emit_mode: Option<EmitMode>,
with_options: Vec<SqlOption>,
},
/// CREATE TABLE
Expand Down Expand Up @@ -1235,6 +1236,7 @@ impl fmt::Display for Statement {
query,
materialized,
with_options,
emit_mode,
} => {
write!(
f,
Expand All @@ -1243,6 +1245,9 @@ impl fmt::Display for Statement {
materialized = if *materialized { "MATERIALIZED " } else { "" },
name = name
)?;
if let Some(emit_mode) = emit_mode {
write!(f, " EMIT {}", emit_mode)?;
}
if !with_options.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with_options))?;
}
Expand Down Expand Up @@ -1907,6 +1912,22 @@ impl fmt::Display for SqlOption {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum EmitMode {
Immediately,
OnWindowClose,
}

impl fmt::Display for EmitMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
EmitMode::Immediately => "IMMEDIATELY",
EmitMode::OnWindowClose => "ON WINDOW CLOSE",
})
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TransactionMode {
Expand Down
2 changes: 2 additions & 0 deletions src/sqlparser/src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ define_keywords!(
EACH,
ELEMENT,
ELSE,
EMIT,
ENCRYPTED,
END,
END_EXEC = "END-EXEC",
Expand Down Expand Up @@ -250,6 +251,7 @@ define_keywords!(
IF,
IGNORE,
ILIKE,
IMMEDIATELY,
IMMUTABLE,
IN,
INCLUDE,
Expand Down
25 changes: 25 additions & 0 deletions src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,11 @@ impl Parser {
// ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
let name = self.parse_object_name()?;
let columns = self.parse_parenthesized_column_list(Optional)?;
let emit_mode = if materialized {
self.parse_emit_mode()?
} else {
None
};
let with_options = self.parse_options(Keyword::WITH)?;
self.expect_keyword(Keyword::AS)?;
let query = Box::new(self.parse_query()?);
Expand All @@ -1665,6 +1670,7 @@ impl Parser {
materialized,
or_replace,
with_options,
emit_mode,
})
}

Expand Down Expand Up @@ -2168,6 +2174,25 @@ impl Parser {
Ok(SqlOption { name, value })
}

pub fn parse_emit_mode(&mut self) -> Result<Option<EmitMode>, ParserError> {
if self.parse_keyword(Keyword::EMIT) {
match self.parse_one_of_keywords(&[Keyword::IMMEDIATELY, Keyword::ON]) {
Some(Keyword::IMMEDIATELY) => Ok(Some(EmitMode::Immediately)),
Some(Keyword::ON) => {
self.expect_keywords(&[Keyword::WINDOW, Keyword::CLOSE])?;
Ok(Some(EmitMode::OnWindowClose))
}
Some(_) => unreachable!(),
None => self.expected(
"IMMEDIATELY or ON WINDOW CLOSE after EMIT",
self.peek_token(),
),
}
} else {
Ok(None)
}
}

pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
if self.parse_keyword(Keyword::TABLE) {
self.parse_alter_table()
Expand Down
67 changes: 64 additions & 3 deletions src/sqlparser/tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2920,13 +2920,15 @@ fn parse_create_view() {
or_replace,
materialized,
with_options,
emit_mode,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(!materialized);
assert!(!or_replace);
assert_eq!(with_options, vec![]);
assert_eq!(emit_mode, None);
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -2966,13 +2968,15 @@ fn parse_create_view_with_columns() {
with_options,
query,
materialized,
emit_mode,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![Ident::new("has"), Ident::new("cols")]);
assert_eq!(with_options, vec![]);
assert_eq!("SELECT 1, 2", query.to_string());
assert!(!materialized);
assert!(!or_replace)
assert!(!or_replace);
assert_eq!(emit_mode, None);
}
_ => unreachable!(),
}
Expand All @@ -2988,13 +2992,15 @@ fn parse_create_or_replace_view() {
with_options,
query,
materialized,
emit_mode,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
assert_eq!(with_options, vec![]);
assert_eq!("SELECT 1", query.to_string());
assert!(!materialized);
assert!(or_replace)
assert!(or_replace);
assert_eq!(emit_mode, None);
}
_ => unreachable!(),
}
Expand All @@ -3015,13 +3021,15 @@ fn parse_create_or_replace_materialized_view() {
with_options,
query,
materialized,
emit_mode,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![]);
assert_eq!(with_options, vec![]);
assert_eq!("SELECT 1", query.to_string());
assert!(materialized);
assert!(or_replace)
assert!(or_replace);
assert_eq!(emit_mode, None);
}
_ => unreachable!(),
}
Expand All @@ -3038,13 +3046,66 @@ fn parse_create_materialized_view() {
query,
materialized,
with_options,
emit_mode,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(materialized);
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(emit_mode, None);
}
_ => unreachable!(),
}
}

#[test]
fn parse_create_materialized_view_emit_immediately() {
let sql = "CREATE MATERIALIZED VIEW myschema.myview EMIT IMMEDIATELY AS SELECT foo FROM bar";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it should be part of the query i.e. inside the SELECT ... clause. Did we change our mind?

Copy link
Contributor Author

@TennyZhuang TennyZhuang Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we defined them on SELECT, what's the semantic on subquery or CTE?

Currently, our conclusion is that we should ensure a mview or a sink can only have one emit option, the only way is defined on view.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

risingwavelabs/rfcs#30 defines the semantics in terms of relational algebra (i.e. given input set, what should be the output set), so the semantics is still well-defined for subqueries or CTEs.

match verified_stmt(sql) {
Statement::CreateView {
name,
or_replace,
columns,
query,
materialized,
with_options,
emit_mode,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(materialized);
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(emit_mode, Some(EmitMode::Immediately));
}
_ => unreachable!(),
}
}

#[test]
fn parse_create_materialized_view_emit_on_window_close() {
let sql =
"CREATE MATERIALIZED VIEW myschema.myview EMIT ON WINDOW CLOSE AS SELECT foo FROM bar";
match verified_stmt(sql) {
Statement::CreateView {
name,
or_replace,
columns,
query,
materialized,
with_options,
emit_mode,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert!(materialized);
assert_eq!(with_options, vec![]);
assert!(!or_replace);
assert_eq!(emit_mode, Some(EmitMode::OnWindowClose));
}
_ => unreachable!(),
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/sqlsmith/src/sql_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl<'a, R: Rng> SqlGenerator<'a, R> {
columns: vec![],
query,
with_options: vec![],
emit_mode: None,
};
(mview, table)
}
Expand Down