Skip to content
Snippets Groups Projects
Commit 3cf3db17 authored by gatorsmile's avatar gatorsmile Committed by Yin Huai
Browse files

[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.
parent 6fc3dc88
No related branches found
No related tags found
No related merge requests found
......@@ -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))
}
......
......@@ -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") {
......
......@@ -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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment