-
Notifications
You must be signed in to change notification settings - Fork 2
support hive view #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package org.apache.spark.sql.hive.execution | ||
|
|
||
| import org.apache.hadoop.hive.ql.metadata.HiveUtils | ||
| import org.apache.spark.sql.hive.HiveContext | ||
| import org.apache.spark.sql.{AnalysisException, Row, SQLContext} | ||
| import org.apache.spark.sql.execution.RunnableCommand | ||
| import org.apache.spark.sql.hive.client.HiveTable | ||
|
|
||
| private[hive] | ||
| case class CreateViewAsSelect( | ||
| tableDesc: HiveTable, | ||
| newNames: Seq[String], | ||
| allowExisting: Boolean) extends RunnableCommand { | ||
|
|
||
| override def run(sqlContext: SQLContext): Seq[Row] = { | ||
| val hiveContext = sqlContext.asInstanceOf[HiveContext] | ||
| val database = tableDesc.database | ||
| val viewName = tableDesc.name | ||
|
|
||
| if (hiveContext.catalog.tableExists(Seq(database, viewName))) { | ||
| if (allowExisting) { | ||
| // view already exists, will do nothing, to keep consistent with Hive | ||
| } else { | ||
| throw new AnalysisException(s"$database.$viewName already exists.") | ||
| } | ||
| } else { | ||
| val tbl = if (newNames.nonEmpty) { | ||
| val sb = new StringBuilder | ||
| sb.append("SELECT ") | ||
| for (i <- 0 until newNames.length) { | ||
| if (i > 0) { | ||
| sb.append(", ") | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'm not sure perf matters here enough to not just use |
||
| sb.append(HiveUtils.unparseIdentifier(tableDesc.schema(i).name)) | ||
| sb.append(" AS ") | ||
| sb.append(HiveUtils.unparseIdentifier(newNames(i))) | ||
| } | ||
| sb.append(" FROM (") | ||
| sb.append(tableDesc.viewText.get) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use a similar trick and grab all the names from the analyzed plan |
||
| sb.append(") ") | ||
| sb.append(HiveUtils.unparseIdentifier(tableDesc.name)) | ||
| tableDesc.copy(viewText = Some(sb.toString)) | ||
| } else tableDesc | ||
|
|
||
| hiveContext.catalog.client.createView(tbl) | ||
| } | ||
|
|
||
| Seq.empty[Row] | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you can use
NativePlaceholder.