Skip to content

Commit ade72c4

Browse files
adrian-wangmarmbrus
authored andcommitted
[SPARK-4239] [SQL] support view in HiveQl
Currently still not support view like CREATE VIEW view3(valoo) TBLPROPERTIES ("fear" = "factor") AS SELECT upper(value) FROM src WHERE key=86; because the text in metastore for this view is like select \`_c0\` as \`valoo\` from (select upper(\`src\`.\`value\`) from \`default\`.\`src\` where ...) \`view3\` while catalyst cannot resolve \`_c0\` for this query. For view without colname definition in parentheses, it works fine. Author: Daoyuan Wang <daoyuan.wang@intel.com> Closes #3131 from adrian-wang/view and squashes the following commits: 8a56fd6 [Daoyuan Wang] michael's comments e46c056 [Daoyuan Wang] add some golden file 079290a [Daoyuan Wang] remove useless import 88afcad [Daoyuan Wang] support view in HiveQl
1 parent c258db9 commit ade72c4

File tree

42 files changed

+5098
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5098
-17
lines changed

sql/hive/compatibility/src/test/scala/org/apache/spark/sql/hive/execution/HiveCompatibilitySuite.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class HiveCompatibilitySuite extends HiveQueryFileTest with BeforeAndAfter {
103103
"udf5",
104104
"udf_java_method",
105105
"create_merge_compressed",
106+
"create_view_partitioned",
106107
"database_location",
107108
"database_properties",
108109

@@ -969,6 +970,9 @@ class HiveCompatibilitySuite extends HiveQueryFileTest with BeforeAndAfter {
969970
"union_script",
970971
"varchar_2",
971972
"varchar_join1",
972-
"varchar_union1"
973+
"varchar_union1",
974+
"view",
975+
"view_cast",
976+
"view_inputs"
973977
)
974978
}

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveMetastoreCatalog.scala

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ package org.apache.spark.sql.hive
2020
import java.io.IOException
2121
import java.util.{List => JList}
2222

23-
import scala.util.matching.Regex
2423
import scala.util.parsing.combinator.RegexParsers
2524

2625
import org.apache.hadoop.util.ReflectionUtils
27-
import org.apache.hadoop.fs.Path
2826

2927
import org.apache.hadoop.hive.metastore.TableType
3028
import org.apache.hadoop.hive.metastore.api.FieldSchema
3129
import org.apache.hadoop.hive.metastore.api.{Table => TTable, Partition => TPartition}
3230
import org.apache.hadoop.hive.ql.metadata.{Hive, Partition, Table, HiveException}
33-
import org.apache.hadoop.hive.ql.plan.{TableDesc, CreateTableDesc}
31+
import org.apache.hadoop.hive.ql.plan.CreateTableDesc
3432
import org.apache.hadoop.hive.serde.serdeConstants
3533
import org.apache.hadoop.hive.serde2.{Deserializer, SerDeException}
3634
import org.apache.hadoop.hive.serde2.`lazy`.LazySimpleSerDe
@@ -67,20 +65,26 @@ private[hive] class HiveMetastoreCatalog(hive: HiveContext) extends Catalog with
6765
db: Option[String],
6866
tableName: String,
6967
alias: Option[String]): LogicalPlan = synchronized {
70-
val (databaseName, tblName) = processDatabaseAndTableName(
71-
db.getOrElse(hive.sessionState.getCurrentDatabase), tableName)
68+
val (databaseName, tblName) =
69+
processDatabaseAndTableName(db.getOrElse(hive.sessionState.getCurrentDatabase), tableName)
7270
val table = client.getTable(databaseName, tblName)
73-
val partitions: Seq[Partition] =
74-
if (table.isPartitioned) {
75-
HiveShim.getAllPartitionsOf(client, table).toSeq
76-
} else {
77-
Nil
78-
}
71+
if (table.isView) {
72+
// if the unresolved relation is from hive view
73+
// parse the text into logic node.
74+
HiveQl.createPlanForView(table, alias)
75+
} else {
76+
val partitions: Seq[Partition] =
77+
if (table.isPartitioned) {
78+
HiveShim.getAllPartitionsOf(client, table).toSeq
79+
} else {
80+
Nil
81+
}
7982

80-
// Since HiveQL is case insensitive for table names we make them all lowercase.
81-
MetastoreRelation(
82-
databaseName, tblName, alias)(
83-
table.getTTable, partitions.map(part => part.getTPartition))(hive)
83+
// Since HiveQL is case insensitive for table names we make them all lowercase.
84+
MetastoreRelation(
85+
databaseName, tblName, alias)(
86+
table.getTTable, partitions.map(part => part.getTPartition))(hive)
87+
}
8488
}
8589

8690
/**

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.sql.Date
2121
import org.apache.hadoop.hive.conf.HiveConf
2222
import org.apache.hadoop.hive.ql.Context
2323
import org.apache.hadoop.hive.ql.lib.Node
24+
import org.apache.hadoop.hive.ql.metadata.Table
2425
import org.apache.hadoop.hive.ql.parse._
2526
import org.apache.hadoop.hive.ql.plan.PlanUtils
2627

@@ -106,7 +107,6 @@ private[hive] object HiveQl {
106107
"TOK_DROPINDEX",
107108
"TOK_MSCK",
108109

109-
// TODO(marmbrus): Figure out how view are expanded by hive, as we might need to handle this.
110110
"TOK_ALTERVIEW_ADDPARTS",
111111
"TOK_ALTERVIEW_AS",
112112
"TOK_ALTERVIEW_DROPPARTS",
@@ -259,6 +259,14 @@ private[hive] object HiveQl {
259259
}
260260
}
261261

262+
/** Creates LogicalPlan for a given VIEW */
263+
def createPlanForView(view: Table, alias: Option[String]) = alias match {
264+
// because hive use things like `_c0` to build the expanded text
265+
// currently we cannot support view from "create view v1(c1) as ..."
266+
case None => Subquery(view.getTableName, createPlan(view.getViewExpandedText))
267+
case Some(aliasText) => Subquery(aliasText, createPlan(view.getViewExpandedText))
268+
}
269+
262270
def parseDdl(ddl: String): Seq[Attribute] = {
263271
val tree =
264272
try {

sql/hive/src/test/resources/golden/view-0-5528e36b3b0f5b14313898cc45f9c23a

Whitespace-only changes.

sql/hive/src/test/resources/golden/view-1-7650b86c86dd6b1a99c86ddc5a31bd63

Whitespace-only changes.

sql/hive/src/test/resources/golden/view-10-7aae4448a05e8a8a3bace7522e952cd0

Whitespace-only changes.

sql/hive/src/test/resources/golden/view-11-dc95343d3e57846485dd543476391376

Whitespace-only changes.

sql/hive/src/test/resources/golden/view-12-371764e1cae31ea0518c03060528d239

Whitespace-only changes.

sql/hive/src/test/resources/golden/view-13-2abce88008f8a19164758ee821aaa8a6

Whitespace-only changes.

sql/hive/src/test/resources/golden/view-14-deb504f4f70fd7db975950c3c47959ee

Whitespace-only changes.

0 commit comments

Comments
 (0)