diff options
author | jiangxingbo <jiangxb1987@gmail.com> | 2017-01-18 19:13:01 +0800 |
---|---|---|
committer | Wenchen Fan <wenchen@databricks.com> | 2017-01-18 19:13:01 +0800 |
commit | f85f29608de801d7cacc779a77c8edaed8124acf (patch) | |
tree | 6bb10b7ee796baacbd085dbd202d2372ec107bda /sql/hive/src | |
parent | 17ce0b5b3f6a825fc77458bc8608cece1a6019c7 (diff) | |
download | spark-f85f29608de801d7cacc779a77c8edaed8124acf.tar.gz spark-f85f29608de801d7cacc779a77c8edaed8124acf.tar.bz2 spark-f85f29608de801d7cacc779a77c8edaed8124acf.zip |
[SPARK-19024][SQL] Implement new approach to write a permanent view
## What changes were proposed in this pull request?
On CREATE/ALTER a view, it's no longer needed to generate a SQL text string from the LogicalPlan, instead we store the SQL query text、the output column names of the query plan, and current database to CatalogTable. Permanent views created by this approach can be resolved by current view resolution approach.
The main advantage includes:
1. If you update an underlying view, the current view also gets updated;
2. That gives us a change to get ride of SQL generation for operators.
Major changes of this PR:
1. Generate the view-specific properties(e.g. view default database, view query output column names) during permanent view creation and store them as properties in the CatalogTable;
2. Update the commands `CreateViewCommand` and `AlterViewAsCommand`, get rid of SQL generation from them.
## How was this patch tested?
Existing tests.
Author: jiangxingbo <jiangxb1987@gmail.com>
Closes #16613 from jiangxb1987/view-write-path.
Diffstat (limited to 'sql/hive/src')
-rw-r--r-- | sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala | 29 | ||||
-rw-r--r-- | sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLViewSuite.scala | 16 |
2 files changed, 23 insertions, 22 deletions
diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala index 882a184124..edef30823b 100644 --- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala @@ -381,28 +381,30 @@ class HiveDDLSuite spark.range(10).write.saveAsTable(tabName) val viewName = "view1" withView(viewName) { - val catalog = spark.sessionState.catalog + def checkProperties(expected: Map[String, String]): Boolean = { + val properties = spark.sessionState.catalog.getTableMetadata(TableIdentifier(viewName)) + .properties + properties.filterNot { case (key, value) => + Seq("transient_lastDdlTime", CatalogTable.VIEW_DEFAULT_DATABASE).contains(key) || + key.startsWith(CatalogTable.VIEW_QUERY_OUTPUT_PREFIX) + } == expected + } sql(s"CREATE VIEW $viewName AS SELECT * FROM $tabName") - assert(catalog.getTableMetadata(TableIdentifier(viewName)) - .properties.filter(_._1 != "transient_lastDdlTime") == Map()) + checkProperties(Map()) sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'an')") - assert(catalog.getTableMetadata(TableIdentifier(viewName)) - .properties.filter(_._1 != "transient_lastDdlTime") == Map("p" -> "an")) + checkProperties(Map("p" -> "an")) // no exception or message will be issued if we set it again sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'an')") - assert(catalog.getTableMetadata(TableIdentifier(viewName)) - .properties.filter(_._1 != "transient_lastDdlTime") == Map("p" -> "an")) + checkProperties(Map("p" -> "an")) // the value will be updated if we set the same key to a different value sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'b')") - assert(catalog.getTableMetadata(TableIdentifier(viewName)) - .properties.filter(_._1 != "transient_lastDdlTime") == Map("p" -> "b")) + checkProperties(Map("p" -> "b")) sql(s"ALTER VIEW $viewName UNSET TBLPROPERTIES ('p')") - assert(catalog.getTableMetadata(TableIdentifier(viewName)) - .properties.filter(_._1 != "transient_lastDdlTime") == Map()) + checkProperties(Map()) val message = intercept[AnalysisException] { sql(s"ALTER VIEW $viewName UNSET TBLPROPERTIES ('p')") @@ -655,10 +657,7 @@ class HiveDDLSuite Seq( Row("# View Information", "", ""), Row("View Original Text:", "SELECT * FROM tbl", ""), - Row("View Expanded Text:", - "SELECT `gen_attr_0` AS `a` FROM (SELECT `gen_attr_0` FROM " + - "(SELECT `a` AS `gen_attr_0` FROM `default`.`tbl`) AS gen_subquery_0) AS tbl", - "") + Row("View Expanded Text:", "SELECT * FROM tbl", "") ) )) } diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLViewSuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLViewSuite.scala index 9bc078dbb0..2658e2c91f 100644 --- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLViewSuite.scala +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLViewSuite.scala @@ -222,13 +222,15 @@ class SQLViewSuite extends QueryTest with SQLTestUtils with TestHiveSingleton { } test("correctly parse CREATE VIEW statement") { - sql( - """CREATE VIEW IF NOT EXISTS - |default.testView (c1 COMMENT 'blabla', c2 COMMENT 'blabla') - |TBLPROPERTIES ('a' = 'b') - |AS SELECT * FROM jt""".stripMargin) - checkAnswer(sql("SELECT c1, c2 FROM testView ORDER BY c1"), (1 to 9).map(i => Row(i, i))) - sql("DROP VIEW testView") + withView("testView") { + sql( + """CREATE VIEW IF NOT EXISTS + |default.testView (c1 COMMENT 'blabla', c2 COMMENT 'blabla') + |TBLPROPERTIES ('a' = 'b') + |AS SELECT * FROM jt + |""".stripMargin) + checkAnswer(sql("SELECT c1, c2 FROM testView ORDER BY c1"), (1 to 9).map(i => Row(i, i))) + } } test("correctly parse CREATE TEMPORARY VIEW statement") { |