-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-12719][SQL] SQL generation support for Generate #11696
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,6 +125,9 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi | |
| case w: Window => | ||
| windowToSQL(w) | ||
|
|
||
| case g: Generate => | ||
| generateToSQL(g) | ||
|
|
||
| case Limit(limitExpr, child) => | ||
| s"${toSQL(child)} LIMIT ${limitExpr.sql}" | ||
|
|
||
|
|
@@ -221,6 +224,42 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi | |
| ) | ||
| } | ||
|
|
||
| private def generateToSQL(g: Generate): String = { | ||
| val columnAliases = g.generatorOutput.map(_.sql).mkString(",") | ||
|
|
||
| val childSQL = if (g.child == OneRowRelation) { | ||
| // This only happens when we put UDTF in project list and there is no FROM clause. Because we | ||
| // always generate LATERAL VIEW for `Generate`, here we use a trick to put a dummy sub-query | ||
| // after FROM clause, so that we can generate a valid LATERAL VIEW SQL string. | ||
| // For example, if the original SQL is: "SELECT EXPLODE(ARRAY(1, 2))", we will convert in to | ||
| // LATERAL VIEW format, and generate: | ||
| // SELECT col FROM (SELECT 1) sub-q0 LATERAL VIEW EXPLODE(ARRAY(1, 2)) sub_q1 AS col | ||
|
Contributor
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: |
||
| s"(SELECT 1) ${SQLBuilder.newSubqueryName}" | ||
| } else { | ||
| toSQL(g.child) | ||
| } | ||
|
|
||
| // The final SQL string for Generate contains 7 parts: | ||
| // 1. the SQL of child, can be a table or sub-query | ||
| // 2. the LATERAL VIEW keyword | ||
| // 3. an optional OUTER keyword | ||
| // 4. the SQL of generator, e.g. EXPLODE(array_col) | ||
| // 5. the table alias for output columns of generator. | ||
| // 6. the AS keyword | ||
| // 7. the column alias, can be more than one, e.g. AS key, value | ||
| // An concrete example: "tbl LATERAL VIEW EXPLODE(map_col) sub_q AS key, value", and the builder | ||
| // will put it in FROM clause later. | ||
| build( | ||
| childSQL, | ||
| "LATERAL VIEW", | ||
| if (g.outer) "OUTER" else "", | ||
| g.generator.sql, | ||
| SQLBuilder.newSubqueryName, | ||
| "AS", | ||
| columnAliases | ||
| ) | ||
| } | ||
|
|
||
| private def sameOutput(output1: Seq[Attribute], output2: Seq[Attribute]): Boolean = | ||
| output1.size == output2.size && | ||
| output1.zip(output2).forall(pair => pair._1.semanticEquals(pair._2)) | ||
|
|
@@ -394,6 +433,17 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi | |
| case j: Join => j.copy( | ||
| left = addSubqueryIfNeeded(j.left), | ||
| right = addSubqueryIfNeeded(j.right)) | ||
|
|
||
| // A special case for Generate. When we put UDTF in project list, followed by WHERE, e.g. | ||
| // SELECT EXPLODE(arr) FROM tbl WHERE id > 1, the Filter operator will be under Generate | ||
| // operator and we need to add a sub-query between them, as it's not allowed to have a WHERE | ||
| // before LATERAL VIEW, e.g. "... FROM tbl WHERE id > 2 EXPLODE(arr) ..." is illegal. | ||
| case g @ Generate(_, _, _, _, _, f: Filter) => | ||
| // Add an extra `Project` to make sure we can generate legal SQL string for sub-query, | ||
| // for example, Subquery -> Filter -> Table will generate "(tbl WHERE ...) AS name", which | ||
| // misses the SELECT part. | ||
| val proj = Project(f.output, f) | ||
| g.copy(child = addSubquery(proj)) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -402,13 +452,14 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi | |
| } | ||
|
|
||
| private def addSubqueryIfNeeded(plan: LogicalPlan): LogicalPlan = plan match { | ||
| case _: SubqueryAlias => plan | ||
| case _: Filter => plan | ||
| case _: Join => plan | ||
| case _: LocalLimit => plan | ||
| case _: GlobalLimit => plan | ||
| case _: SQLTable => plan | ||
| case OneRowRelation => plan | ||
| case _: SubqueryAlias | | ||
| _: Filter | | ||
| _: Join | | ||
| _: LocalLimit | | ||
| _: GlobalLimit | | ||
| _: SQLTable | | ||
| _: Generate | | ||
| OneRowRelation => plan | ||
| case _ => addSubquery(plan) | ||
| } | ||
| } | ||
|
|
||
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.
Nit: Please add a space after
,