diff options
author | gatorsmile <gatorsmile@gmail.com> | 2016-04-14 08:08:09 -0700 |
---|---|---|
committer | Yin Huai <yhuai@databricks.com> | 2016-04-14 08:08:09 -0700 |
commit | 3cf3db17b35c98a408014e1810cb797d8415ffd3 (patch) | |
tree | 31e7f7110dedeea227a5582a5917ee5dbdffad60 /sql | |
parent | 6fc3dc8839eaed673c64ec87af6dfe24f8cebe0c (diff) | |
download | spark-3cf3db17b35c98a408014e1810cb797d8415ffd3.tar.gz spark-3cf3db17b35c98a408014e1810cb797d8415ffd3.tar.bz2 spark-3cf3db17b35c98a408014e1810cb797d8415ffd3.zip |
[SPARK-14518][SQL] Support Comment in CREATE VIEW
#### What changes were proposed in this pull request?
**HQL Syntax**: [Create View](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Create/Drop/AlterView
)
```SQL
CREATE VIEW [IF NOT EXISTS] [db_name.]view_name [(column_name [COMMENT column_comment], ...) ]
[COMMENT view_comment]
[TBLPROPERTIES (property_name = property_value, ...)]
AS SELECT ...;
```
Add a support for the `[COMMENT view_comment]` clause
#### How was this patch tested?
Modified the existing test cases to verify the correctness.
Author: gatorsmile <gatorsmile@gmail.com>
Author: xiaoli <lixiao1983@gmail.com>
Author: Xiao Li <xiaoli@Xiaos-MacBook-Pro.local>
Closes #12288 from gatorsmile/addCommentInCreateView.
Diffstat (limited to 'sql')
3 files changed, 24 insertions, 19 deletions
diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/HiveSqlParser.scala b/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/HiveSqlParser.scala index b14db7fe71..8c707079a1 100644 --- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/HiveSqlParser.scala +++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/HiveSqlParser.scala @@ -26,18 +26,13 @@ import org.apache.hadoop.hive.ql.session.SessionState import org.apache.hadoop.hive.serde.serdeConstants import org.apache.hadoop.hive.serde2.`lazy`.LazySimpleSerDe -import org.apache.spark.sql.AnalysisException -import org.apache.spark.sql.catalyst.analysis.UnresolvedGenerator import org.apache.spark.sql.catalyst.catalog._ -import org.apache.spark.sql.catalyst.expressions._ import org.apache.spark.sql.catalyst.parser._ import org.apache.spark.sql.catalyst.parser.SqlBaseParser._ import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan import org.apache.spark.sql.execution.SparkSqlAstBuilder import org.apache.spark.sql.execution.command.CreateTable -import org.apache.spark.sql.hive.{CreateTableAsSelect => CTAS, CreateViewAsSelect => CreateView} -import org.apache.spark.sql.hive.{HiveGenericUDTF, HiveMetastoreTypes, HiveSerDe} -import org.apache.spark.sql.hive.HiveShim.HiveFunctionWrapper +import org.apache.spark.sql.hive.{CreateTableAsSelect => CTAS, CreateViewAsSelect => CreateView, HiveSerDe} /** * Concrete parser for HiveQl statements. @@ -252,9 +247,6 @@ class HiveSqlAstBuilder extends SparkSqlAstBuilder { if (ctx.identifierList != null) { throw new ParseException(s"Operation not allowed: partitioned views", ctx) } else { - if (ctx.STRING != null) { - throw new ParseException("Unsupported operation: COMMENT clause", ctx) - } val identifiers = Option(ctx.identifierCommentList).toSeq.flatMap(_.identifierComment.asScala) val schema = identifiers.map { ic => CatalogColumn(ic.identifier.getText, null, nullable = true, Option(ic.STRING).map(string)) @@ -262,6 +254,7 @@ class HiveSqlAstBuilder extends SparkSqlAstBuilder { createView( ctx, ctx.tableIdentifier, + comment = Option(ctx.STRING).map(string), schema, ctx.query, Option(ctx.tablePropertyList).map(visitTablePropertyList).getOrElse(Map.empty), @@ -278,6 +271,7 @@ class HiveSqlAstBuilder extends SparkSqlAstBuilder { createView( ctx, ctx.tableIdentifier, + comment = None, Seq.empty, ctx.query, Map.empty, @@ -291,6 +285,7 @@ class HiveSqlAstBuilder extends SparkSqlAstBuilder { private def createView( ctx: ParserRuleContext, name: TableIdentifierContext, + comment: Option[String], schema: Seq[CatalogColumn], query: QueryContext, properties: Map[String, String], @@ -304,7 +299,8 @@ class HiveSqlAstBuilder extends SparkSqlAstBuilder { storage = EmptyStorageFormat, properties = properties, viewOriginalText = sql, - viewText = sql) + viewText = sql, + comment = comment) CreateView(tableDesc, plan(query), allowExist, replace, command(ctx)) } diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveDDLCommandSuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveDDLCommandSuite.scala index 68d3ea6ed9..b87f035cd1 100644 --- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveDDLCommandSuite.scala +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveDDLCommandSuite.scala @@ -236,15 +236,6 @@ class HiveDDLCommandSuite extends PlanTest { |FROM testData """.stripMargin) } - intercept[ParseException] { - parser.parsePlan( - """ - |CREATE OR REPLACE VIEW IF NOT EXISTS view1 (col1, col3) - |COMMENT 'blabla' - |TBLPROPERTIES('prop1Key'="prop1Val") - |AS SELECT * FROM tab1 - """.stripMargin) - } } test("Invalid interval term should throw AnalysisException") { @@ -532,6 +523,7 @@ class HiveDDLCommandSuite extends PlanTest { """ |CREATE OR REPLACE VIEW IF NOT EXISTS view1 |(col1, col3) + |COMMENT 'BLABLA' |TBLPROPERTIES('prop1Key'="prop1Val") |AS SELECT * FROM tab1 """.stripMargin @@ -551,6 +543,7 @@ class HiveDDLCommandSuite extends PlanTest { assert(desc.storage.outputFormat.isEmpty) assert(desc.storage.serde.isEmpty) assert(desc.properties == Map("prop1Key" -> "prop1Val")) + assert(desc.comment == Option("BLABLA")) } test("create view -- partitioned view") { 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 78ccdc7adb..c82c7f6ca6 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 @@ -110,6 +110,22 @@ class HiveDDLSuite extends QueryTest with SQLTestUtils with TestHiveSingleton { } } + test("create table and view with comment") { + val catalog = hiveContext.sessionState.catalog + val tabName = "tab1" + withTable(tabName) { + sql(s"CREATE TABLE $tabName(c1 int) COMMENT 'BLABLA'") + val viewName = "view1" + withView(viewName) { + sql(s"CREATE VIEW $viewName COMMENT 'no comment' AS SELECT * FROM $tabName") + val tableMetadata = catalog.getTableMetadata(TableIdentifier(tabName, Some("default"))) + val viewMetadata = catalog.getTableMetadata(TableIdentifier(viewName, Some("default"))) + assert(tableMetadata.properties.get("comment") == Option("BLABLA")) + assert(viewMetadata.properties.get("comment") == Option("no comment")) + } + } + } + test("drop views") { withTable("tab1") { val tabName = "tab1" |