Skip to content

Adds support for FILTER in Eval #1290

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 8, 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
@@ -1,6 +1,7 @@
package org.partiql.eval.internal

import org.partiql.eval.internal.operator.Operator
import org.partiql.eval.internal.operator.rel.RelFilter
import org.partiql.eval.internal.operator.rel.RelProject
import org.partiql.eval.internal.operator.rel.RelScan
import org.partiql.eval.internal.operator.rex.ExprCollection
Expand Down Expand Up @@ -69,6 +70,12 @@ internal object Compiler {
return super.visitRelOp(node.op, ctx) as Operator.Relation
}

override fun visitRelOpFilter(node: Rel.Op.Filter, ctx: Unit): Operator {
val input = visitRel(node.input, ctx)
val condition = visitRex(node.predicate, ctx)
return RelFilter(input, condition)
}

override fun visitRex(node: Rex, ctx: Unit): Operator.Expr {
return super.visitRexOp(node.op, ctx) as Operator.Expr
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.partiql.eval.internal.operator.rel

import org.partiql.eval.internal.Record
import org.partiql.eval.internal.operator.Operator
import org.partiql.value.BoolValue
import org.partiql.value.PartiQLValueExperimental

internal class RelFilter(
val input: Operator.Relation,
val expr: Operator.Expr
) : Operator.Relation {

override fun open() {
input.open()
}

override fun next(): Record? {
var inputRecord: Record? = input.next()
while (inputRecord != null) {
if (conditionIsTrue(inputRecord, expr)) {
return inputRecord
}
inputRecord = input.next()
}
return null
}

override fun close() {
input.close()
}

@OptIn(PartiQLValueExperimental::class)
private fun conditionIsTrue(record: Record, expr: Operator.Expr): Boolean {
val condition = expr.eval(record)
return condition is BoolValue && condition.value == true
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.partiql.eval.impl
package org.partiql.eval.internal

import org.junit.jupiter.api.Test
import org.partiql.eval.PartiQLEngine
Expand All @@ -9,9 +9,13 @@ import org.partiql.planner.PartiQLPlannerBuilder
import org.partiql.value.BagValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.bagValue
import org.partiql.value.boolValue
import org.partiql.value.int32Value
import kotlin.test.assertEquals

/**
* This holds sanity tests during the development of the [PartiQLEngine.default] implementation.
*/
class PartiQLEngineDefaultTest {

private val engine = PartiQLEngine.default()
Expand Down Expand Up @@ -47,4 +51,19 @@ class PartiQLEngineDefaultTest {
val expected = bagValue(sequenceOf(int32Value(10), int32Value(20), int32Value(30)))
assertEquals(expected, output)
}

@OptIn(PartiQLValueExperimental::class)
@Test
fun testFilter() {
val statement = parser.parse("SELECT VALUE t FROM <<true, false, true, false, false, false>> AS t WHERE t;").root
val session = PartiQLPlanner.Session("q", "u")
val plan = planner.plan(statement, session)

val prepared = engine.prepare(plan.plan)
val result = engine.execute(prepared) as PartiQLResult.Value
val output = result.value as BagValue<*>

val expected = bagValue(sequenceOf(boolValue(true), boolValue(true)))
assertEquals(expected, output)
}
}