-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds basic aggregations to the partiql-planner (#1247)
- Loading branch information
Showing
19 changed files
with
1,268 additions
and
340 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
partiql-ast/src/main/kotlin/org/partiql/ast/normalize/AstPass.kt
This file contains 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 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
14 changes: 14 additions & 0 deletions
14
partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt
This file contains 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
46 changes: 46 additions & 0 deletions
46
partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeGroupBy.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at: | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.partiql.ast.normalize | ||
|
||
import org.partiql.ast.Expr | ||
import org.partiql.ast.GroupBy | ||
import org.partiql.ast.Statement | ||
import org.partiql.ast.groupByKey | ||
import org.partiql.ast.helpers.toBinder | ||
import org.partiql.ast.util.AstRewriter | ||
|
||
/** | ||
* Adds a unique binder to each group key. | ||
*/ | ||
object NormalizeGroupBy : AstPass { | ||
|
||
override fun apply(statement: Statement) = Visitor.visitStatement(statement, 0) as Statement | ||
|
||
private object Visitor : AstRewriter<Int>() { | ||
|
||
override fun visitGroupByKey(node: GroupBy.Key, ctx: Int): GroupBy.Key { | ||
val expr = visitExpr(node.expr, 0) as Expr | ||
val alias = when (node.asAlias) { | ||
null -> expr.toBinder(ctx) | ||
else -> node.asAlias | ||
} | ||
return if (expr !== node.expr || alias !== node.asAlias) { | ||
groupByKey(expr, alias) | ||
} else { | ||
node | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeSelectList.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at: | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.partiql.ast.normalize | ||
|
||
import org.partiql.ast.Expr | ||
import org.partiql.ast.Select | ||
import org.partiql.ast.Statement | ||
import org.partiql.ast.builder.ast | ||
import org.partiql.ast.helpers.toBinder | ||
import org.partiql.ast.util.AstRewriter | ||
|
||
/** | ||
* Adds an `as` alias to every select-list item. | ||
* | ||
* - [org.partiql.ast.helpers.toBinder] | ||
* - https://partiql.org/assets/PartiQL-Specification.pdf#page=28 | ||
* - https://web.cecs.pdx.edu/~len/sql1999.pdf#page=287 | ||
*/ | ||
internal object NormalizeSelectList : AstPass { | ||
|
||
override fun apply(statement: Statement) = Visitor.visitStatement(statement, 0) as Statement | ||
|
||
private object Visitor : AstRewriter<Int>() { | ||
|
||
override fun visitSelectProject(node: Select.Project, ctx: Int) = ast { | ||
if (node.items.isEmpty()) { | ||
return@ast node | ||
} | ||
var diff = false | ||
val transformed = ArrayList<Select.Project.Item>(node.items.size) | ||
node.items.forEachIndexed { i, n -> | ||
val item = visitSelectProjectItem(n, i) as Select.Project.Item | ||
if (item !== n) diff = true | ||
transformed.add(item) | ||
} | ||
// We don't want to create a new list unless we have to, as to not trigger further rewrites up the tree. | ||
if (diff) selectProject(transformed) else node | ||
} | ||
|
||
override fun visitSelectProjectItemAll(node: Select.Project.Item.All, ctx: Int) = node.copy() | ||
|
||
override fun visitSelectProjectItemExpression(node: Select.Project.Item.Expression, ctx: Int) = ast { | ||
val expr = visitExpr(node.expr, 0) as Expr | ||
val alias = when (node.asAlias) { | ||
null -> expr.toBinder(ctx) | ||
else -> node.asAlias | ||
} | ||
if (expr != node.expr || alias != node.asAlias) { | ||
selectProjectItemExpression(expr, alias) | ||
} else { | ||
node | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeSelectStar.kt
This file contains 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 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 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
Oops, something went wrong.