Skip to content
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

With Recursive #1000

Merged
merged 21 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3329,14 +3329,11 @@ switchSection: WHEN expr THEN expr
queryStatement: withExpression? selectStatement setOperators*
;

withExpression: WITH commonTableExpression (COMMA commonTableExpression)*
withExpression: WITH RECURSIVE? commonTableExpression (COMMA commonTableExpression)*
;

commonTableExpression
: tableName = id (L_PAREN columns += id (COMMA columns += id)* R_PAREN)? AS L_PAREN (
(selectStatement setOperators*)
| expr
) R_PAREN
: tableName = id (L_PAREN columnList R_PAREN)? AS L_PAREN ((selectStatement setOperators*) | expr) R_PAREN
;

selectStatement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,19 @@ class SnowflakeAstBuilder(override val vc: SnowflakeVisitorCoordinator)
errorCheck(ctx) match {
case Some(errorResult) => errorResult
case None =>
val ctes = vc.relationBuilder.visitMany(ctx.commonTableExpression())
ir.WithCTE(ctes, relation)
if (ctx.RECURSIVE() == null) {
val ctes = vc.relationBuilder.visitMany(ctx.commonTableExpression())
ir.WithCTE(ctes, relation)
} else {
ir.UnresolvedCommand(
ruleText = contextText(ctx),
message = "*** WITH RECURSIVE IS NOT SUPPORTED ***",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are reports that WITH RECURSIVE could be implemented with various slow-performance hacks in PySpark - see https://stackoverflow.com/questions/52562607/recursive-cte-in-spark-sql

given that, we should emit new WithRecursive node and try to translate it in PySpark later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for review

ruleName = vc.ruleName(ctx),
tokenName = Some(tokenName(ctx.getStart)))
}

}

}

private def buildSetOperator(left: ir.LogicalPlan, ctx: SetOperatorsContext): ir.LogicalPlan = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,14 @@ class SnowflakeRelationBuilder(override val vc: SnowflakeVisitorCoordinator)
case Some(errorResult) => errorResult
case None =>
val tableName = vc.expressionBuilder.buildId(ctx.tableName)
val columns = ctx.columns.asScala.map(vc.expressionBuilder.buildId)
val columns = ctx.columnList() match {
case null => Seq.empty[ir.Id]
case c => c.columnName().asScala.flatMap(_.id.asScala.map(vc.expressionBuilder.buildId))
}

val query = ctx.selectStatement().accept(this)
ir.SubqueryAlias(query, tableName, columns)

}

private def buildNum(ctx: NumContext): BigDecimal = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- snowflake sql:
WITH employee_hierarchy AS (
SELECT
employee_id,
manager_id,
employee_name
FROM
employees
WHERE
manager_id IS NULL
)
SELECT *
FROM employee_hierarchy;

-- databricks sql:
WITH employee_hierarchy AS (SELECT employee_id, manager_id, employee_name FROM employees WHERE manager_id IS NULL) SELECT * FROM employee_hierarchy;
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- snowflake sql:
WITH RECURSIVE employee_hierarchy AS (
SELECT
employee_id,
manager_id,
employee_name,
1 AS level
FROM
employees
WHERE
manager_id IS NULL
UNION ALL
SELECT
e.employee_id,
e.manager_id,
e.employee_name,
eh.level + 1 AS level
FROM
employees e
INNER JOIN
employee_hierarchy eh ON e.manager_id = eh.employee_id
)
SELECT *
FROM employee_hierarchy
ORDER BY level, employee_id;

-- databricks sql:
/* The following issues were detected:

*** WITH RECURSIVE IS NOT SUPPORTED ***
WITH RECURSIVE employee_hierarchy AS (
SELECT
employee_id,
manager_id,
employee_name,
1 AS level
FROM
employees
WHERE
manager_id IS NULL
UNION ALL
SELECT
e.employee_id,
e.manager_id,
e.employee_name,
eh.level + 1 AS level
FROM
employees e
INNER JOIN
employee_hierarchy eh ON e.manager_id = eh.employee_id
)
*/
Loading