Skip to content

Commit

Permalink
Refactor modeling of EXCLUDE in partiql-ast and partiql-plan (#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 authored Dec 15, 2023
1 parent 0a75bd3 commit dafdac5
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 190 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Thank you to all who have contributed!
- Changes the return type of `filter_distinct` to a list if input collection is list
- Changes the `PartiQLValue` collections to implement Iterable rather than Sequence, allowing for multiple consumption.
- **BREAKING** Moves PartiQLParserBuilder.standard().build() to be PartiQLParser.default().
- **BREAKING** Changed modeling of `EXCLUDE` in `partiql-ast`

### Deprecated

Expand All @@ -72,6 +73,7 @@ Thank you to all who have contributed!
Thank you to all who have contributed!
- @rchowell
- @johnedquinn
- @alancai98

## [0.13.2-alpha] - 2023-09-29

Expand Down
22 changes: 11 additions & 11 deletions partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToLegacyAst.kt
Original file line number Diff line number Diff line change
Expand Up @@ -756,42 +756,42 @@ private class AstTranslator(val metas: Map<String, MetaContainer>) : AstBaseVisi
}

override fun visitExclude(node: Exclude, ctx: Ctx): PartiqlAst.ExcludeOp = translate(node) { metas ->
val excludeExprs = node.exprs.translate<PartiqlAst.ExcludeExpr>(ctx)
val excludeExprs = node.items.translate<PartiqlAst.ExcludeExpr>(ctx)
excludeOp(excludeExprs, metas)
}

override fun visitExcludeExcludeExpr(node: Exclude.ExcludeExpr, ctx: Ctx) = translate(node) { metas ->
val root = visitIdentifierSymbol(node.root, ctx)
override fun visitExcludeItem(node: Exclude.Item, ctx: Ctx) = translate(node) { metas ->
val root = visitExprVar(node.root, ctx)
val steps = node.steps.translate<PartiqlAst.ExcludeStep>(ctx)
excludeExpr(root = root, steps = steps, metas)
excludeExpr(root = identifier_(root.name, root.case), steps = steps, metas)
}

override fun visitExcludeStep(node: Exclude.Step, ctx: Ctx) =
super.visitExcludeStep(node, ctx) as PartiqlAst.ExcludeStep

override fun visitExcludeStepExcludeTupleAttr(node: Exclude.Step.ExcludeTupleAttr, ctx: Ctx) = translate(node) { metas ->
override fun visitExcludeStepStructField(node: Exclude.Step.StructField, ctx: Ctx) = translate(node) { metas ->
val attr = node.symbol.symbol
val case = node.symbol.caseSensitivity.toLegacyCaseSensitivity()
excludeTupleAttr(identifier(attr, case), metas)
}

override fun visitExcludeStepExcludeCollectionIndex(
node: Exclude.Step.ExcludeCollectionIndex,
override fun visitExcludeStepCollIndex(
node: Exclude.Step.CollIndex,
ctx: Ctx
) = translate(node) { metas ->
val index = node.index.toLong()
excludeCollectionIndex(index, metas)
}

override fun visitExcludeStepExcludeTupleWildcard(
node: Exclude.Step.ExcludeTupleWildcard,
override fun visitExcludeStepStructWildcard(
node: Exclude.Step.StructWildcard,
ctx: Ctx
) = translate(node) { metas ->
excludeTupleWildcard(metas)
}

override fun visitExcludeStepExcludeCollectionWildcard(
node: Exclude.Step.ExcludeCollectionWildcard,
override fun visitExcludeStepCollWildcard(
node: Exclude.Step.CollWildcard,
ctx: Ctx
) = translate(node) { metas ->
excludeCollectionWildcard(metas)
Expand Down
14 changes: 7 additions & 7 deletions partiql-ast/src/main/kotlin/org/partiql/ast/sql/SqlDialect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,32 @@ public abstract class SqlDialect : AstBaseVisitor<SqlBlock, SqlBlock>() {
override fun visitExclude(node: Exclude, head: SqlBlock): SqlBlock {
var h = head
h = h concat " EXCLUDE "
h = h concat list(start = null, end = null) { node.exprs }
h = h concat list(start = null, end = null) { node.items }
return h
}

override fun visitExcludeExcludeExpr(node: Exclude.ExcludeExpr, head: SqlBlock): SqlBlock {
override fun visitExcludeItem(node: Exclude.Item, head: SqlBlock): SqlBlock {
var h = head
h = h concat visitIdentifierSymbol(node.root, SqlBlock.Nil)
h = h concat visitExprVar(node.root, SqlBlock.Nil)
h = h concat list(delimiter = null, start = null, end = null) { node.steps }
return h
}

override fun visitExcludeStepExcludeCollectionIndex(node: Exclude.Step.ExcludeCollectionIndex, head: SqlBlock): SqlBlock {
override fun visitExcludeStepCollIndex(node: Exclude.Step.CollIndex, head: SqlBlock): SqlBlock {
return head concat r("[${node.index}]")
}

override fun visitExcludeStepExcludeTupleWildcard(node: Exclude.Step.ExcludeTupleWildcard, head: SqlBlock): SqlBlock {
override fun visitExcludeStepStructWildcard(node: Exclude.Step.StructWildcard, head: SqlBlock): SqlBlock {
return head concat r(".*")
}

override fun visitExcludeStepExcludeTupleAttr(node: Exclude.Step.ExcludeTupleAttr, head: SqlBlock): SqlBlock {
override fun visitExcludeStepStructField(node: Exclude.Step.StructField, head: SqlBlock): SqlBlock {
var h = head concat r(".")
h = h concat visitIdentifierSymbol(node.symbol, SqlBlock.Nil)
return h
}

override fun visitExcludeStepExcludeCollectionWildcard(node: Exclude.Step.ExcludeCollectionWildcard, head: SqlBlock): SqlBlock {
override fun visitExcludeStepCollWildcard(node: Exclude.Step.CollWildcard, head: SqlBlock): SqlBlock {
return head concat r("[*]")
}

Expand Down
14 changes: 7 additions & 7 deletions partiql-ast/src/main/resources/partiql_ast.ion
Original file line number Diff line number Diff line change
Expand Up @@ -563,17 +563,17 @@ select::[
]

exclude::{
exprs: list::[exclude_expr],
items: list::[item],
_: [
exclude_expr::{
root: '.identifier.symbol',
item::{
root: '.expr.var',
steps: list::[step],
},
step::[
exclude_tuple_attr::{ symbol: '.identifier.symbol' },
exclude_collection_index::{ index: int },
exclude_tuple_wildcard::{},
exclude_collection_wildcard::{},
struct_field::{ symbol: '.identifier.symbol' },
coll_index::{ index: int },
struct_wildcard::{},
coll_wildcard::{},
]
]
}
Expand Down
66 changes: 32 additions & 34 deletions partiql-ast/src/test/kotlin/org/partiql/ast/sql/SqlDialectTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,9 @@ class SqlDialectTest {
type = From.Value.Type.SCAN
}
exclude = exclude {
exprs += excludeExcludeExpr {
root = id("t", Identifier.CaseSensitivity.INSENSITIVE)
steps += excludeStepExcludeTupleAttr {
symbol = id("a", Identifier.CaseSensitivity.INSENSITIVE)
}
items += excludeItem {
root = v("t")
steps += insensitiveExcludeStructField("a")
}
}
}
Expand All @@ -1090,21 +1088,21 @@ class SqlDialectTest {
type = From.Value.Type.SCAN
}
exclude = exclude {
exprs += excludeExcludeExpr {
root = id("a", Identifier.CaseSensitivity.INSENSITIVE)
steps += insensitiveExcludeTupleAttr("b")
items += excludeItem {
root = v("a")
steps += insensitiveExcludeStructField("b")
}
exprs += excludeExcludeExpr {
root = id("c", Identifier.CaseSensitivity.INSENSITIVE)
steps += insensitiveExcludeTupleAttr("d")
items += excludeItem {
root = v("c")
steps += insensitiveExcludeStructField("d")
}
exprs += excludeExcludeExpr {
root = id("e", Identifier.CaseSensitivity.INSENSITIVE)
steps += insensitiveExcludeTupleAttr("f")
items += excludeItem {
root = v("e")
steps += insensitiveExcludeStructField("f")
}
exprs += excludeExcludeExpr {
root = id("g", Identifier.CaseSensitivity.INSENSITIVE)
steps += insensitiveExcludeTupleAttr("h")
items += excludeItem {
root = v("g")
steps += insensitiveExcludeStructField("h")
}
}
}
Expand All @@ -1117,37 +1115,37 @@ class SqlDialectTest {
type = From.Value.Type.SCAN
}
exclude = exclude {
exprs += excludeExcludeExpr {
root = id("t", Identifier.CaseSensitivity.INSENSITIVE)
items += excludeItem {
root = v("t")
steps += mutableListOf(
insensitiveExcludeTupleAttr("a"),
sensitiveExcludeTupleAttr("b"),
excludeStepExcludeTupleWildcard(),
excludeStepExcludeCollectionWildcard(),
insensitiveExcludeTupleAttr("c"),
insensitiveExcludeStructField("a"),
sensitiveExcludeStructField("b"),
excludeStepStructWildcard(),
excludeStepCollWildcard(),
insensitiveExcludeStructField("c"),
)
}
exprs += excludeExcludeExpr {
root = id("s", Identifier.CaseSensitivity.SENSITIVE)
items += excludeItem {
root = exprVar(id("s", Identifier.CaseSensitivity.SENSITIVE), Expr.Var.Scope.DEFAULT)
steps += mutableListOf(
excludeStepExcludeCollectionIndex(0),
insensitiveExcludeTupleAttr("d"),
sensitiveExcludeTupleAttr("e"),
excludeStepExcludeCollectionWildcard(),
insensitiveExcludeTupleAttr("f"),
excludeStepExcludeTupleWildcard(),
excludeStepCollIndex(0),
insensitiveExcludeStructField("d"),
sensitiveExcludeStructField("e"),
excludeStepCollWildcard(),
insensitiveExcludeStructField("f"),
excludeStepStructWildcard(),
)
}
}
}
},
)

private fun AstBuilder.insensitiveExcludeTupleAttr(str: String) = excludeStepExcludeTupleAttr {
private fun AstBuilder.insensitiveExcludeStructField(str: String) = excludeStepStructField {
symbol = id(str, Identifier.CaseSensitivity.INSENSITIVE)
}

private fun AstBuilder.sensitiveExcludeTupleAttr(str: String) = excludeStepExcludeTupleAttr {
private fun AstBuilder.sensitiveExcludeStructField(str: String) = excludeStepStructField {
symbol = id(str, Identifier.CaseSensitivity.SENSITIVE)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ import org.partiql.ast.Statement
import org.partiql.ast.TableDefinition
import org.partiql.ast.Type
import org.partiql.ast.exclude
import org.partiql.ast.excludeExcludeExpr
import org.partiql.ast.excludeStepExcludeCollectionIndex
import org.partiql.ast.excludeStepExcludeCollectionWildcard
import org.partiql.ast.excludeStepExcludeTupleAttr
import org.partiql.ast.excludeStepExcludeTupleWildcard
import org.partiql.ast.excludeItem
import org.partiql.ast.excludeStepCollIndex
import org.partiql.ast.excludeStepCollWildcard
import org.partiql.ast.excludeStepStructField
import org.partiql.ast.excludeStepStructWildcard
import org.partiql.ast.exprAgg
import org.partiql.ast.exprBagOp
import org.partiql.ast.exprBetween
Expand Down Expand Up @@ -1058,20 +1058,21 @@ internal class PartiQLParserDefault : PartiQLParser {
}

override fun visitExcludeExpr(ctx: GeneratedParser.ExcludeExprContext) = translate(ctx) {
val root = visitSymbolPrimitive(ctx.symbolPrimitive())
val rootId = visitSymbolPrimitive(ctx.symbolPrimitive())
val root = exprVar(rootId, Expr.Var.Scope.DEFAULT)
val steps = visitOrEmpty<Exclude.Step>(ctx.excludeExprSteps())
excludeExcludeExpr(root, steps)
excludeItem(root, steps)
}

override fun visitExcludeExprTupleAttr(ctx: GeneratedParser.ExcludeExprTupleAttrContext) = translate(ctx) {
val identifier = visitSymbolPrimitive(ctx.symbolPrimitive())
excludeStepExcludeTupleAttr(identifier)
excludeStepStructField(identifier)
}

override fun visitExcludeExprCollectionIndex(ctx: GeneratedParser.ExcludeExprCollectionIndexContext) =
translate(ctx) {
val index = ctx.index.text.toInt()
excludeStepExcludeCollectionIndex(index)
excludeStepCollIndex(index)
}

override fun visitExcludeExprCollectionAttr(ctx: GeneratedParser.ExcludeExprCollectionAttrContext) =
Expand All @@ -1081,17 +1082,17 @@ internal class PartiQLParserDefault : PartiQLParser {
attr,
Identifier.CaseSensitivity.SENSITIVE,
)
excludeStepExcludeTupleAttr(identifier)
excludeStepStructField(identifier)
}

override fun visitExcludeExprCollectionWildcard(ctx: org.partiql.parser.antlr.PartiQLParser.ExcludeExprCollectionWildcardContext) =
translate(ctx) {
excludeStepExcludeCollectionWildcard()
excludeStepCollWildcard()
}

override fun visitExcludeExprTupleWildcard(ctx: org.partiql.parser.antlr.PartiQLParser.ExcludeExprTupleWildcardContext) =
translate(ctx) {
excludeStepExcludeTupleWildcard()
excludeStepStructWildcard()
}

/**
Expand Down
12 changes: 4 additions & 8 deletions partiql-plan/src/main/resources/partiql_plan.ion
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,14 @@ rel::{
items: list::[item],
_: [
item::{
root: '.identifier.symbol',
root: '.rex.op.var',
steps: list::[step],
},
step::[
attr::{
symbol: '.identifier.symbol',
},
pos::{
index: int,
},
struct_field::{ symbol: '.identifier.symbol' },
coll_index::{ index: int },
struct_wildcard::{},
collection_wildcard::{},
coll_wildcard::{},
],
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ internal class Env(
/**
* Check locals, else search structs.
*/
private fun resolveLocalBind(path: BindingPath, locals: List<Rel.Binding>): ResolvedVar? {
internal fun resolveLocalBind(path: BindingPath, locals: List<Rel.Binding>): ResolvedVar? {
if (path.steps.isEmpty()) {
return null
}
Expand Down
Loading

0 comments on commit dafdac5

Please sign in to comment.