Skip to content

Commit

Permalink
[FLINK-36993][table] Support parsing ALTER MATERIALIZED TABLE AS stat…
Browse files Browse the repository at this point in the history
…ement
  • Loading branch information
hackergin committed Jan 6, 2025
1 parent 3084561 commit e0cfa9b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"org.apache.flink.sql.parser.ddl.SqlAlterDatabase"
"org.apache.flink.sql.parser.ddl.SqlAlterFunction"
"org.apache.flink.sql.parser.ddl.SqlAlterMaterializedTable"
"org.apache.flink.sql.parser.ddl.SqlAlterMaterializedTableAsQuery"
"org.apache.flink.sql.parser.ddl.SqlAlterMaterializedTableFreshness"
"org.apache.flink.sql.parser.ddl.SqlAlterMaterializedTableOptions"
"org.apache.flink.sql.parser.ddl.SqlAlterMaterializedTableRefreshMode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,7 @@ SqlAlterMaterializedTable SqlAlterMaterializedTable() :
SqlNodeList propertyKeyList = SqlNodeList.EMPTY;
SqlNodeList partSpec = SqlNodeList.EMPTY;
SqlNode freshness = null;
SqlNode asQuery = null;
}
{
<ALTER> <MATERIALIZED> <TABLE> { startPos = getPos();}
Expand Down Expand Up @@ -2089,6 +2090,15 @@ SqlAlterMaterializedTable SqlAlterMaterializedTable() :
tableIdentifier,
propertyKeyList);
}
|
<AS>
asQuery = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY)
{
return new SqlAlterMaterializedTableAsQuery(
startPos.plus(getPos()),
tableIdentifier,
asQuery);
}
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.sql.parser.ddl;

import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableNullableList;

import java.util.List;

/**
* SqlNode to describe the ALTER TABLE [catalogName.] [dataBasesName.]tableName AS &lt;query&gt;
* statement.
*/
public class SqlAlterMaterializedTableAsQuery extends SqlAlterMaterializedTable {

private final SqlNode asQuery;

public SqlAlterMaterializedTableAsQuery(
SqlParserPos pos, SqlIdentifier tableName, SqlNode asQuery) {
super(pos, tableName);
this.asQuery = asQuery;
}

public SqlNode getAsQuery() {
return asQuery;
}

@Override
public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(getTableName(), asQuery);
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
super.unparse(writer, leftPrec, rightPrec);
writer.keyword("AS");
asQuery.unparse(writer, leftPrec, rightPrec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ void testAlterMaterializedTableSuspend() {
.fails(
"Encountered \"<EOF>\" at line 1, column 28.\n"
+ "Was expecting one of:\n"
+ " \"AS\" ...\n"
+ " \"RESET\" ...\n"
+ " \"SET\" ...\n"
+ " \"SUSPEND\" ...\n"
Expand Down Expand Up @@ -384,6 +385,27 @@ void testAlterMaterializedTableReset() {
+ " ");
}

@Test
void testAlterMaterializedTableAsQuery() {
final String sql = "ALTER MATERIALIZED TABLE tbl1 AS SELECT * FROM t";
final String expected = "ALTER MATERIALIZED TABLE `TBL1` AS SELECT *\nFROM `T`";
sql(sql).ok(expected);

final String sql2 = "ALTER MATERIALIZED TABLE tbl1 AS SELECT * FROM t A^S^";
sql(sql2)
.fails(
"Encountered \"<EOF>\" at line 1, column 51.\n"
+ "Was expecting one of:\n"
+ " <BRACKET_QUOTED_IDENTIFIER> ...\n"
+ " <QUOTED_IDENTIFIER> ...\n"
+ " <BACK_QUOTED_IDENTIFIER> ...\n"
+ " <BIG_QUERY_BACK_QUOTED_IDENTIFIER> ...\n"
+ " <HYPHENATED_IDENTIFIER> ...\n"
+ " <IDENTIFIER> ...\n"
+ " <UNICODE_QUOTED_IDENTIFIER> ...\n"
+ " ");
}

@Test
void testDropMaterializedTable() {
final String sql = "DROP MATERIALIZED TABLE tbl1";
Expand Down

0 comments on commit e0cfa9b

Please sign in to comment.