KurtYoung commented on a change in pull request #11727:
URL: https://github.com/apache/flink/pull/11727#discussion_r412038186
##########
File path:
flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/api/TableEnvironmentTest.scala
##########
@@ -271,6 +271,378 @@ class TableEnvironmentTest {
tableEnv.executeSql("select * from MyTable")
}
+ @Test
+ def testExecuteSqlWithCreateDropView(): Unit = {
+ val createTableStmt =
+ """
+ |CREATE TABLE tbl1 (
+ | a bigint,
+ | b int,
+ | c varchar
+ |) with (
+ | 'connector' = 'COLLECTION',
+ | 'is-bounded' = 'false'
+ |)
+ """.stripMargin
+ tableEnv.executeSql(createTableStmt)
+
+ val viewResult1 = tableEnv.executeSql("CREATE VIEW IF NOT EXISTS v1 AS
SELECT * FROM tbl1")
+ assertEquals(ResultKind.SUCCESS, viewResult1.getResultKind)
+ assertTrue(tableEnv.getCatalog(tableEnv.getCurrentCatalog).get()
+
.tableExists(ObjectPath.fromString(s"${tableEnv.getCurrentDatabase}.v1")))
+
+ val viewResult2 = tableEnv.executeSql("DROP VIEW IF EXISTS v1")
+ assertEquals(ResultKind.SUCCESS, viewResult2.getResultKind)
+ assertFalse(tableEnv.getCatalog(tableEnv.getCurrentCatalog).get()
+
.tableExists(ObjectPath.fromString(s"${tableEnv.getCurrentDatabase}.v1")))
+ }
+
+ @Test
+ def testExecuteSqlWithCreateDropTemporaryView(): Unit = {
+ val createTableStmt =
+ """
+ |CREATE TABLE tbl1 (
+ | a bigint,
+ | b int,
+ | c varchar
+ |) with (
+ | 'connector' = 'COLLECTION',
+ | 'is-bounded' = 'false'
+ |)
+ """.stripMargin
+ tableEnv.executeSql(createTableStmt)
+
+ val viewResult1 = tableEnv.executeSql(
+ "CREATE TEMPORARY VIEW IF NOT EXISTS v1 AS SELECT * FROM tbl1")
+ assertEquals(ResultKind.SUCCESS, viewResult1.getResultKind)
+ assert(tableEnv.listTables().sameElements(Array[String]("tbl1", "v1")))
+
+ val viewResult2 = tableEnv.executeSql("DROP TEMPORARY VIEW IF EXISTS v1")
+ assertEquals(ResultKind.SUCCESS, viewResult2.getResultKind)
+ assert(tableEnv.listTables().sameElements(Array[String]("tbl1")))
+ }
+
+ @Test
+ def testCreateViewWithWrongFieldList(): Unit = {
+ thrown.expect(classOf[SqlConversionException])
+ thrown.expectMessage("VIEW definition and input fields not match:\n" +
+ "\tDef fields: [d].\n" +
+ "\tInput fields: [a, b, c].")
+ val sourceDDL =
+ """
+ |CREATE TABLE T1(
+ | a int,
+ | b varchar,
+ | c int
+ |) with (
+ | 'connector' = 'COLLECTION'
+ |)
+ """.stripMargin
+
+ val sinkDDL =
+ """
+ |CREATE TABLE T2(
+ | a int,
+ | b varchar,
+ | c int
+ |) with (
+ | 'connector' = 'COLLECTION'
+ |)
+ """.stripMargin
+
+ val viewDDL =
+ """
+ |CREATE VIEW IF NOT EXISTS T3(d) AS SELECT * FROM T1
+ """.stripMargin
+
+ tableEnv.executeSql(sourceDDL)
+ tableEnv.executeSql(sinkDDL)
+ tableEnv.executeSql(viewDDL)
+ }
+
+ @Test
+ def testCreateViewTwice(): Unit = {
+ thrown.expect(classOf[ValidationException])
+ thrown.expectMessage(
+ "Could not execute CreateTable in path
`default_catalog`.`default_database`.`T3`")
+ val sourceDDL =
+ """
+ |CREATE TABLE T1(
+ | a int,
+ | b varchar,
+ | c int
+ |) with (
+ | 'connector' = 'COLLECTION'
+ |)
+ """.stripMargin
+
+ val sinkDDL =
+ """
+ |CREATE TABLE T2(
+ | a int,
+ | b varchar,
+ | c int
+ |) with (
+ | 'connector' = 'COLLECTION'
+ |)
+ """.stripMargin
+
+ val viewWith3ColumnDDL =
+ """
+ |CREATE VIEW T3(d, e, f) AS SELECT a, b, c FROM T1
+ """.stripMargin
+
+ val viewWith2ColumnDDL =
+ """
+ |CREATE VIEW T3(d, e) AS SELECT a, b FROM T1
+ """.stripMargin
+
+ tableEnv.executeSql(sourceDDL)
+ tableEnv.executeSql(sinkDDL)
+ tableEnv.executeSql(viewWith3ColumnDDL)
+ tableEnv.executeSql(viewWith2ColumnDDL) // fail the case
+ }
+
+ @Test
+ def testDropViewWithFullPath(): Unit = {
+ val sourceDDL =
+ """
+ |CREATE TABLE T1(
+ | a int,
+ | b varchar,
+ | c int
+ |) with (
+ | 'connector' = 'COLLECTION'
+ |)
+ """.stripMargin
+
+ val view1DDL =
+ """
+ |CREATE VIEW T2(d, e, f) AS SELECT a, b, c FROM T1
+ """.stripMargin
+
+ val view2DDL =
+ """
+ |CREATE VIEW T3(x, y, z) AS SELECT a, b, c FROM T1
+ """.stripMargin
+
+ tableEnv.executeSql(sourceDDL)
+ tableEnv.executeSql(view1DDL)
+ tableEnv.executeSql(view2DDL)
+
+ assert(tableEnv.listTables().sameElements(Array[String]("T1", "T2", "T3")))
+
+ tableEnv.sqlUpdate("DROP VIEW default_catalog.default_database.T2")
Review comment:
use executeSql instead
##########
File path:
flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/catalog/CatalogViewITCase.scala
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.table.planner.catalog
+
+import java.util
+
+import org.apache.flink.table.api.config.{ExecutionConfigOptions,
TableConfigOptions}
+import org.apache.flink.table.api.{EnvironmentSettings, TableEnvironment}
+import org.apache.flink.table.api.internal.TableEnvironmentImpl
+import
org.apache.flink.table.planner.factories.utils.TestCollectionTableFactory
+import org.apache.flink.test.util.AbstractTestBase
+import org.apache.flink.types.Row
+import org.junit.Assert.assertEquals
+import org.junit.{Before, Rule, Test}
+import org.junit.rules.ExpectedException
+import org.junit.runner.RunWith
+import org.junit.runners.Parameterized
+
+import scala.collection.JavaConversions._
+
+/** Test cases for view related DDLs. */
+@RunWith(classOf[Parameterized])
+class CatalogViewITCase(isStreamingMode: Boolean) extends AbstractTestBase {
Review comment:
could you also add a case for mixing temporary views and normal views.
For example, if there is a temporary view and normal view with the same name,
the temporary one will be chosen. And also if we are dropping the temporary
one, the normal one can still be used.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]