Skip to content

Commit

Permalink
Adds support for DISTINCT in the Planner
Browse files Browse the repository at this point in the history
  • Loading branch information
johnedquinn committed Dec 8, 2023
1 parent ddd642d commit f505d29
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.partiql.ast.GroupBy
import org.partiql.ast.OrderBy
import org.partiql.ast.Select
import org.partiql.ast.SetOp
import org.partiql.ast.SetQuantifier
import org.partiql.ast.Sort
import org.partiql.ast.builder.ast
import org.partiql.ast.helpers.toBinder
Expand All @@ -37,6 +38,7 @@ import org.partiql.planner.internal.ir.rel
import org.partiql.planner.internal.ir.relBinding
import org.partiql.planner.internal.ir.relOpAggregate
import org.partiql.planner.internal.ir.relOpAggregateCall
import org.partiql.planner.internal.ir.relOpDistinct
import org.partiql.planner.internal.ir.relOpErr
import org.partiql.planner.internal.ir.relOpExcept
import org.partiql.planner.internal.ir.relOpExclude
Expand Down Expand Up @@ -146,14 +148,31 @@ internal object RelConverter {
rel = convertExclude(rel, sel.exclude)
// append SQL projection if present
rel = when (val projection = sel.select) {
is Select.Project -> visitSelectProject(projection, rel)
is Select.Value -> visitSelectValue(projection, rel)
is Select.Project -> {
val project = visitSelectProject(projection, rel)
visitSetQuantifier(projection.setq, project)
}
is Select.Value -> {
val project = visitSelectValue(projection, rel)
visitSetQuantifier(projection.setq, project)
}
is Select.Star -> error("AST not normalized, found project star")
else -> rel // skip PIVOT and SELECT VALUE
is Select.Pivot -> rel // Skip PIVOT
}
return rel
}

/**
* Given a non-null [setQuantifier], this will return a [Rel] of [Rel.Op.Distinct] wrapping the [input].
* If [setQuantifier] is null or ALL, this will return the [input].
*/
private fun visitSetQuantifier(setQuantifier: SetQuantifier?, input: Rel): Rel {
return when (setQuantifier) {
SetQuantifier.DISTINCT -> rel(input.type, relOpDistinct(input))
SetQuantifier.ALL, null -> input
}
}

override fun visitSelectProject(node: Select.Project, input: Rel): Rel {
// this ignores aggregations
val schema = mutableListOf<Rel.Binding>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.partiql.planner.internal.ir.rel
import org.partiql.planner.internal.ir.relBinding
import org.partiql.planner.internal.ir.relOpAggregate
import org.partiql.planner.internal.ir.relOpAggregateCall
import org.partiql.planner.internal.ir.relOpDistinct
import org.partiql.planner.internal.ir.relOpErr
import org.partiql.planner.internal.ir.relOpFilter
import org.partiql.planner.internal.ir.relOpJoin
Expand Down Expand Up @@ -197,7 +198,8 @@ internal class PlanTyper(
}

override fun visitRelOpDistinct(node: Rel.Op.Distinct, ctx: Rel.Type?): Rel {
TODO("Type RelOp Distinct")
val input = visitRel(node.input, ctx)
return rel(input.type, relOpDistinct(input))
}

override fun visitRelOpFilter(node: Rel.Op.Filter, ctx: Rel.Type?): Rel {
Expand Down

0 comments on commit f505d29

Please sign in to comment.