Skip to content

Commit

Permalink
Change outer bag op behavior following discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed Aug 15, 2024
1 parent ab5cf7b commit 359f674
Show file tree
Hide file tree
Showing 33 changed files with 662 additions and 1,358 deletions.
199 changes: 78 additions & 121 deletions partiql-ast/api/partiql-ast.api

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToLegacyAst.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.partiql.ast.Let
import org.partiql.ast.OnConflict
import org.partiql.ast.OrderBy
import org.partiql.ast.Path
import org.partiql.ast.QueryExpr
import org.partiql.ast.QueryBody
import org.partiql.ast.Returning
import org.partiql.ast.Select
import org.partiql.ast.SetOp
Expand Down Expand Up @@ -622,19 +622,6 @@ private class AstTranslator(val metas: Map<String, MetaContainer>) : AstBaseVisi
call("date_diff", operands, metas)
}

override fun visitExprBagOp(node: Expr.BagOp, ctx: Ctx) = translate(node) { metas ->
val lhs = visitExpr(node.lhs, ctx)
val rhs = visitExpr(node.rhs, ctx)
val op = when (node.type.type) {
SetOp.Type.UNION -> outerUnion()
SetOp.Type.INTERSECT -> outerIntersect()
SetOp.Type.EXCEPT -> outerExcept()
}
val setq = node.type.setq?.toLegacySetQuantifier() ?: distinct()
val operands = listOf(lhs, rhs)
bagOp(op, setq, operands, metas)
}

override fun visitExprMatch(node: Expr.Match, ctx: Ctx) = translate(node) { metas ->
val expr = visitExpr(node.expr, ctx)
val match = visitGraphMatch(node.pattern, ctx)
Expand Down Expand Up @@ -672,7 +659,7 @@ private class AstTranslator(val metas: Map<String, MetaContainer>) : AstBaseVisi
val limit = node.limit?.let { visitExpr(it, ctx) }
val offset = node.offset?.let { visitExpr(it, ctx) }
when (val body = node.body) {
is QueryExpr.SFW -> {
is QueryBody.SFW -> {
var setq = when (val s = body.select) {
is Select.Pivot -> null
is Select.Project -> s.setq?.toLegacySetQuantifier()
Expand All @@ -692,13 +679,26 @@ private class AstTranslator(val metas: Map<String, MetaContainer>) : AstBaseVisi
val having = body.having?.let { visitExpr(it, ctx) }
select(setq, project, exclude, from, fromLet, where, groupBy, having, orderBy, limit, offset, metas)
}
is QueryExpr.SetOp -> {
is QueryBody.SetOp -> {
val lhs = visitExpr(body.lhs, ctx)
val rhs = visitExpr(body.rhs, ctx)
val outer = body.isOuter
val op = when (body.type.type) {
SetOp.Type.UNION -> union()
SetOp.Type.INTERSECT -> intersect()
SetOp.Type.EXCEPT -> except()
SetOp.Type.UNION -> if (outer) {
outerUnion()
} else {
union()
}
SetOp.Type.INTERSECT -> if (outer) {
outerIntersect()
} else {
intersect()
}
SetOp.Type.EXCEPT -> if (outer) {
outerExcept()
} else {
except()
}
}
val setq = body.type.setq?.toLegacySetQuantifier() ?: distinct()
val operands = listOf(lhs, rhs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package org.partiql.ast.normalize
import org.partiql.ast.AstNode
import org.partiql.ast.Expr
import org.partiql.ast.From
import org.partiql.ast.QueryExpr
import org.partiql.ast.QueryBody
import org.partiql.ast.Statement
import org.partiql.ast.fromJoin
import org.partiql.ast.helpers.toBinder
Expand All @@ -33,7 +33,7 @@ internal object NormalizeFromSource : AstPass {
private object Visitor : AstRewriter<Int>() {

// Each SFW starts the ctx count again.
override fun visitQueryExprSFW(node: QueryExpr.SFW, ctx: Int): AstNode = super.visitQueryExprSFW(node, 0)
override fun visitQueryBodySFW(node: QueryBody.SFW, ctx: Int): AstNode = super.visitQueryBodySFW(node, 0)

override fun visitStatementDMLBatchLegacy(node: Statement.DML.BatchLegacy, ctx: Int): AstNode =
super.visitStatementDMLBatchLegacy(node, 0)
Expand Down
30 changes: 7 additions & 23 deletions partiql-ast/src/main/kotlin/org/partiql/ast/sql/SqlDialect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.partiql.ast.Identifier
import org.partiql.ast.Let
import org.partiql.ast.OrderBy
import org.partiql.ast.Path
import org.partiql.ast.QueryExpr
import org.partiql.ast.QueryBody
import org.partiql.ast.Select
import org.partiql.ast.SetOp
import org.partiql.ast.SetQuantifier
Expand Down Expand Up @@ -553,26 +553,6 @@ public abstract class SqlDialect : AstBaseVisitor<SqlBlock, SqlBlock>() {
return h
}

override fun visitExprBagOp(node: Expr.BagOp, head: SqlBlock): SqlBlock {
// [OUTER] [UNION|INTERSECT|EXCEPT] [ALL|DISTINCT]
val op = mutableListOf("OUTER")
when (node.type.type) {
SetOp.Type.UNION -> op.add("UNION")
SetOp.Type.INTERSECT -> op.add("INTERSECT")
SetOp.Type.EXCEPT -> op.add("EXCEPT")
}
when (node.type.setq) {
SetQuantifier.ALL -> op.add("ALL")
SetQuantifier.DISTINCT -> op.add("DISTINCT")
null -> {}
}
var h = head
h = visitExprWrapped(node.lhs, h)
h = h concat r(" ${op.joinToString(" ")} ")
h = visitExprWrapped(node.rhs, h)
return h
}

override fun visitExprQuerySet(node: Expr.QuerySet, head: SqlBlock): SqlBlock {
var h = head
// visit body (SFW or other SQL set op)
Expand All @@ -588,7 +568,7 @@ public abstract class SqlDialect : AstBaseVisitor<SqlBlock, SqlBlock>() {

// SELECT-FROM-WHERE

override fun visitQueryExprSFW(node: QueryExpr.SFW, head: SqlBlock): SqlBlock {
override fun visitQueryBodySFW(node: QueryBody.SFW, head: SqlBlock): SqlBlock {
var h = head
// SELECT
h = visit(node.select, h)
Expand All @@ -607,8 +587,12 @@ public abstract class SqlDialect : AstBaseVisitor<SqlBlock, SqlBlock>() {
return h
}

override fun visitQueryExprSetOp(node: QueryExpr.SetOp, head: SqlBlock): SqlBlock {
override fun visitQueryBodySetOp(node: QueryBody.SetOp, head: SqlBlock): SqlBlock {
val op = mutableListOf<String>()
when (node.isOuter) {
true -> op.add("OUTER")
else -> {}
}
when (node.type.type) {
SetOp.Type.UNION -> op.add("UNION")
SetOp.Type.INTERSECT -> op.add("INTERSECT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.partiql.ast.Identifier
import org.partiql.ast.Let
import org.partiql.ast.OrderBy
import org.partiql.ast.Path
import org.partiql.ast.QueryExpr
import org.partiql.ast.QueryBody
import org.partiql.ast.Select
import org.partiql.ast.SetOp
import org.partiql.ast.SetQuantifier
Expand Down Expand Up @@ -578,26 +578,6 @@ internal abstract class InternalSqlDialect : AstBaseVisitor<InternalSqlBlock, In
return t
}

override fun visitExprBagOp(node: Expr.BagOp, tail: InternalSqlBlock): InternalSqlBlock {
// [OUTER] [UNION|INTERSECT|EXCEPT] [ALL|DISTINCT]
val op = mutableListOf("OUTER")
when (node.type.type) {
SetOp.Type.UNION -> op.add("UNION")
SetOp.Type.INTERSECT -> op.add("INTERSECT")
SetOp.Type.EXCEPT -> op.add("EXCEPT")
}
when (node.type.setq) {
SetQuantifier.ALL -> op.add("ALL")
SetQuantifier.DISTINCT -> op.add("DISTINCT")
null -> {}
}
var t = tail
t = visitExprWrapped(node.lhs, t)
t = t concat " ${op.joinToString(" ")} "
t = visitExprWrapped(node.rhs, t)
return t
}

override fun visitExprQuerySet(node: Expr.QuerySet, tail: InternalSqlBlock): InternalSqlBlock {
var t = tail
// visit body (SFW or other SQL set op)
Expand All @@ -613,7 +593,7 @@ internal abstract class InternalSqlDialect : AstBaseVisitor<InternalSqlBlock, In

// SELECT-FROM-WHERE

override fun visitQueryExprSFW(node: QueryExpr.SFW, tail: InternalSqlBlock): InternalSqlBlock {
override fun visitQueryBodySFW(node: QueryBody.SFW, tail: InternalSqlBlock): InternalSqlBlock {
var t = tail
// SELECT
t = visit(node.select, t)
Expand All @@ -632,8 +612,12 @@ internal abstract class InternalSqlDialect : AstBaseVisitor<InternalSqlBlock, In
return t
}

override fun visitQueryExprSetOp(node: QueryExpr.SetOp, tail: InternalSqlBlock): InternalSqlBlock {
override fun visitQueryBodySetOp(node: QueryBody.SetOp, tail: InternalSqlBlock): InternalSqlBlock {
val op = mutableListOf<String>()
when (node.isOuter) {
true -> op.add("OUTER")
else -> {}
}
when (node.type.type) {
SetOp.Type.UNION -> op.add("UNION")
SetOp.Type.INTERSECT -> op.add("INTERSECT")
Expand Down
22 changes: 8 additions & 14 deletions partiql-ast/src/main/resources/partiql_ast.ion
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,8 @@ expr::[
rhs: expr,
},

// PartiQL special form `[OUTER] (UNION|INTERSECT|EXCEPT) [ALL|DISTINCT]`
bag_op::{
type: '.set_op',
lhs: expr,
rhs: expr,
},

query_set::{
body: query_expr,
body: query_body,
order_by: optional::order_by,
limit: optional::expr,
offset: optional::expr,
Expand All @@ -518,7 +511,7 @@ expr::[
},
]

query_expr::[
query_body::[
// The PartiQL `<sfw>` query expression, think SQL `<query specification>`
s_f_w::{
select: select, // oneof SELECT / SELECT VALUE / PIVOT
Expand All @@ -530,12 +523,13 @@ query_expr::[
having: optional::expr,
},

// SQL UNION (UNION|INTERSECT|EXCEPT) [ALL|DISTINCT]`
// PartiQL bag op / SQL set op [OUTER] (UNION|INTERSECT|EXCEPT) [ALL|DISTINCT]`
set_op::{
type: '.set_op',
lhs: '.expr.query_set',
rhs: '.expr.query_set',
},
type: '.set_op',
is_outer: bool,
lhs: expr,
rhs: expr,
}
]

// PartiQL SELECT Clause Variants — https://partiql.org/dql/select.html
Expand Down
Loading

0 comments on commit 359f674

Please sign in to comment.