godfreyhe commented on a change in pull request #11727: [FLINK-17106][table] 
Support create and drop view in Flink SQL
URL: https://github.com/apache/flink/pull/11727#discussion_r410069654
 
 

 ##########
 File path: 
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/catalog/CatalogTableITCase.scala
 ##########
 @@ -670,6 +670,409 @@ class CatalogTableITCase(isStreaming: Boolean) extends 
AbstractTestBase {
     expectedProperty.put("k2", "b")
     assertEquals(expectedProperty, database.getProperties)
   }
+
+  @Test(expected = classOf[ValidationException])
+  def testCreateViewTwice(): Unit = {
+    val sourceData = List(
+      toRow(1, 1000),
+      toRow(2, 2000),
+      toRow(3, 3000)
+    )
+
+    TestCollectionTableFactory.initData(sourceData)
+
+    val sourceDDL =
+      """
+        |CREATE TABLE T1(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val sinkDDL =
+      """
+        |CREATE TABLE T2(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val viewDDL =
+      """
+        |CREATE VIEW T3(c, d) AS SELECT a, b FROM T1
+      """.stripMargin
+
+    tableEnv.sqlUpdate(sourceDDL)
+    tableEnv.sqlUpdate(sinkDDL)
+    tableEnv.sqlUpdate(viewDDL)
+    tableEnv.sqlUpdate(viewDDL)
+  }
+
+  @Test(expected = classOf[ValidationException])
+  def testCreateTemporaryViewTwice(): Unit = {
+    val sourceData = List(
+      toRow(1, 1000),
+      toRow(2, 2000),
+      toRow(3, 3000)
+    )
+
+    TestCollectionTableFactory.initData(sourceData)
+
+    val sourceDDL =
+      """
+        |CREATE TABLE T1(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val sinkDDL =
+      """
+        |CREATE TABLE T2(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val viewDDL =
+      """
+        |CREATE TEMPORARY VIEW T3(c, d) AS SELECT a, b FROM T1
+      """.stripMargin
+
+    tableEnv.sqlUpdate(sourceDDL)
+    tableEnv.sqlUpdate(sinkDDL)
+    tableEnv.sqlUpdate(viewDDL)
+    tableEnv.sqlUpdate(viewDDL)
+  }
+
+  @Test
+  def testCreateViewIfNotExistsTwice(): Unit = {
+    val sourceData = List(
+      toRow(1, 1000),
+      toRow(2, 2000),
+      toRow(3, 3000)
+    )
+
+    TestCollectionTableFactory.initData(sourceData)
+
+    val sourceDDL =
+      """
+        |CREATE TABLE T1(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val sinkDDL =
+      """
+        |CREATE TABLE T2(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val viewDDL =
+      """
+        |CREATE VIEW IF NOT EXISTS T3(c, d) AS SELECT a, b FROM T1
+      """.stripMargin
+
+    val query = "SELECT c, d FROM T3"
+
+    tableEnv.sqlUpdate(sourceDDL)
+    tableEnv.sqlUpdate(sinkDDL)
+    tableEnv.sqlUpdate(viewDDL)
+    tableEnv.sqlUpdate(viewDDL)
+
+    val result = tableEnv.sqlQuery(query)
+    result.insertInto("T2")
+    execJob("testJob")
+    assertEquals(sourceData.sorted, TestCollectionTableFactory.RESULT.sorted)
+  }
+
+  @Test
+  def testCreateTemporaryViewIfNotExistsTwice(): Unit = {
+    val sourceData = List(
+      toRow(1, 1000),
+      toRow(2, 2000),
+      toRow(3, 3000)
+    )
+
+    TestCollectionTableFactory.initData(sourceData)
+
+    val sourceDDL =
+      """
+        |CREATE TABLE T1(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val sinkDDL =
+      """
+        |CREATE TABLE T2(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val viewDDL =
+      """
+        |CREATE TEMPORARY VIEW IF NOT EXISTS T3(c, d) AS SELECT a, b FROM T1
+      """.stripMargin
+
+    val query = "SELECT c, d FROM T3"
+
+    tableEnv.sqlUpdate(sourceDDL)
+    tableEnv.sqlUpdate(sinkDDL)
+    tableEnv.sqlUpdate(viewDDL)
+    tableEnv.sqlUpdate(viewDDL)
+
+    val result = tableEnv.sqlQuery(query)
+    result.insertInto("T2")
+    execJob("testJob")
+    assertEquals(sourceData.sorted, TestCollectionTableFactory.RESULT.sorted)
+  }
+
+  @Test
+  def testDropViewWithFullPath(): Unit = {
+    val sourceDDL =
+      """
+        |CREATE TABLE T1(
+        |  a int,
+        |  b int
+        |) with (
+        |  'connector' = 'COLLECTION'
+        |)
+      """.stripMargin
+
+    val view1DDL =
+      """
+        |CREATE VIEW T2(c, d) AS SELECT a, b FROM T1
+      """.stripMargin
+
+    val view2DDL =
+      """
+        |CREATE VIEW T3(x, y) AS SELECT a, b FROM T1
+      """.stripMargin
+
+    tableEnv.sqlUpdate(sourceDDL)
+    tableEnv.sqlUpdate(view1DDL)
+    tableEnv.sqlUpdate(view2DDL)
+
+    assert(tableEnv.listTables().sameElements(Array[String]("T1", "T2", "T3")))
+
+    tableEnv.sqlUpdate("DROP VIEW default_catalog.default_database.T2")
+    assert(tableEnv.listTables().sameElements(Array[String]("T1", "T3")))
+
+    tableEnv.sqlUpdate("DROP VIEW default_catalog.default_database.T3")
+    assert(tableEnv.listTables().sameElements(Array[String]("T1")))
+  }
+
+  @Test
+  def testDropViewWithPartialPath(): Unit = {
 
 Review comment:
   ditto

----------------------------------------------------------------
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]


With regards,
Apache Git Services

Reply via email to