Skip to content

Adds support for DISTINCT in the Planner #1293

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

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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))
Comment on lines +201 to +202
Copy link
Contributor

Choose a reason for hiding this comment

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

will need to update transpiler

}

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