-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-14038][SQL] enable native view by default #11860
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,7 @@ | |
|
||
package org.apache.spark.sql.hive.execution | ||
|
||
import scala.util.control.NonFatal | ||
|
||
import org.apache.spark.sql.{AnalysisException, Row, SQLContext} | ||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.catalyst.catalog.{CatalogColumn, CatalogTable} | ||
import org.apache.spark.sql.catalyst.expressions.Alias | ||
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Project} | ||
|
@@ -31,8 +28,6 @@ import org.apache.spark.sql.hive.{ HiveContext, HiveMetastoreTypes, SQLBuilder} | |
* Create Hive view on non-hive-compatible tables by specifying schema ourselves instead of | ||
* depending on Hive meta-store. | ||
*/ | ||
// TODO: Note that this class can NOT canonicalize the view SQL string entirely, which is different | ||
// from Hive and may not work for some cases like create view on self join. | ||
private[hive] case class CreateViewAsSelect( | ||
tableDesc: CatalogTable, | ||
child: LogicalPlan, | ||
|
@@ -73,15 +68,18 @@ private[hive] case class CreateViewAsSelect( | |
} | ||
|
||
private def prepareTable(sqlContext: SQLContext): CatalogTable = { | ||
val expandedText = if (sqlContext.conf.canonicalView) { | ||
try rebuildViewQueryString(sqlContext) catch { | ||
case NonFatal(e) => wrapViewTextWithSelect | ||
} | ||
val logicalPlan = if (tableDesc.schema.isEmpty) { | ||
child | ||
} else { | ||
wrapViewTextWithSelect | ||
val projectList = childSchema.zip(tableDesc.schema).map { | ||
case (attr, col) => Alias(attr, col.name)() | ||
} | ||
sqlContext.executePlan(Project(projectList, child)).analyzed | ||
} | ||
|
||
val viewSchema = { | ||
val viewText = new SQLBuilder(logicalPlan, sqlContext).toSQL | ||
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. How about catching exceptions thrown from |
||
|
||
val viewSchema: Seq[CatalogColumn] = { | ||
if (tableDesc.schema.isEmpty) { | ||
childSchema.map { a => | ||
CatalogColumn(a.name, HiveMetastoreTypes.toMetastoreType(a.dataType)) | ||
|
@@ -97,41 +95,6 @@ private[hive] case class CreateViewAsSelect( | |
} | ||
} | ||
|
||
tableDesc.copy(schema = viewSchema, viewText = Some(expandedText)) | ||
tableDesc.copy(schema = viewSchema, viewText = Some(viewText)) | ||
} | ||
|
||
private def wrapViewTextWithSelect: String = { | ||
// When user specified column names for view, we should create a project to do the renaming. | ||
// When no column name specified, we still need to create a project to declare the columns | ||
// we need, to make us more robust to top level `*`s. | ||
val viewOutput = { | ||
val columnNames = childSchema.map(f => quote(f.name)) | ||
if (tableDesc.schema.isEmpty) { | ||
columnNames.mkString(", ") | ||
} else { | ||
columnNames.zip(tableDesc.schema.map(f => quote(f.name))).map { | ||
case (name, alias) => s"$name AS $alias" | ||
}.mkString(", ") | ||
} | ||
} | ||
|
||
val viewText = tableDesc.viewText.get | ||
val viewName = quote(tableDesc.name.table) | ||
s"SELECT $viewOutput FROM ($viewText) $viewName" | ||
} | ||
|
||
private def rebuildViewQueryString(sqlContext: SQLContext): String = { | ||
val logicalPlan = if (tableDesc.schema.isEmpty) { | ||
child | ||
} else { | ||
val projectList = childSchema.zip(tableDesc.schema).map { | ||
case (attr, col) => Alias(attr, col.name)() | ||
} | ||
sqlContext.executePlan(Project(projectList, child)).analyzed | ||
} | ||
new SQLBuilder(logicalPlan, sqlContext).toSQL | ||
} | ||
|
||
// escape backtick with double-backtick in column name and wrap it with backtick. | ||
private def quote(name: String) = s"`${name.replaceAll("`", "``")}`" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We can remove the last sentence and instead suggest the user to turn it off when
toSQL
fails.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.
Is there any known issue? Disabling it does not really work for data source tables. So, for this kind of cases, setting
spark.sql.nativeView
to true andspark.sql.nativeView.canonical
to false is the only workaround, right?