Skip to content

Commit

Permalink
Merge pull request #1435 from johnedquinn/v1-conformance-nullif-coalesce
Browse files Browse the repository at this point in the history
Adds support for NULLIF and COALESCE
  • Loading branch information
johnedquinn authored Apr 18, 2024
2 parents 41c00fd + 0bcc3e2 commit b1244cc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions partiql-eval/src/main/kotlin/org/partiql/eval/internal/Compiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import org.partiql.eval.internal.operator.rex.ExprCallDynamic
import org.partiql.eval.internal.operator.rex.ExprCallStatic
import org.partiql.eval.internal.operator.rex.ExprCase
import org.partiql.eval.internal.operator.rex.ExprCast
import org.partiql.eval.internal.operator.rex.ExprCoalesce
import org.partiql.eval.internal.operator.rex.ExprCollection
import org.partiql.eval.internal.operator.rex.ExprLiteral
import org.partiql.eval.internal.operator.rex.ExprNullIf
import org.partiql.eval.internal.operator.rex.ExprPathIndex
import org.partiql.eval.internal.operator.rex.ExprPathKey
import org.partiql.eval.internal.operator.rex.ExprPathSymbol
Expand Down Expand Up @@ -132,6 +134,17 @@ internal class Compiler(
}
}

override fun visitRexOpCoalesce(node: Rex.Op.Coalesce, ctx: StaticType?): Operator {
val args = Array(node.args.size) { visitRex(node.args[it], node.args[it].type) }
return ExprCoalesce(args)
}

override fun visitRexOpNullif(node: Rex.Op.Nullif, ctx: StaticType?): Operator {
val value = visitRex(node.value, node.value.type)
val nullifier = visitRex(node.nullifier, node.value.type)
return ExprNullIf(value, nullifier)
}

/**
* All variables from the local scope have a depth of 0.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.operator.Operator
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.PartiQLValueType
import org.partiql.value.nullValue

internal class ExprCoalesce(
private val args: Array<Operator.Expr>
) : Operator.Expr {

@PartiQLValueExperimental
override fun eval(env: Environment): PartiQLValue {
for (arg in args) {
val result = arg.eval(env)
if (!result.isNull && result.type != PartiQLValueType.MISSING) {
return result
}
}
return nullValue()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.operator.Operator
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.nullValue

internal class ExprNullIf(
private val valueExpr: Operator.Expr,
private val nullifierExpr: Operator.Expr
) : Operator.Expr {

@OptIn(PartiQLValueExperimental::class)
private val comparator = PartiQLValue.comparator()

@PartiQLValueExperimental
override fun eval(env: Environment): PartiQLValue {
val value = valueExpr.eval(env)
val nullifier = nullifierExpr.eval(env)
return when (comparator.compare(value, nullifier)) {
0 -> nullValue()
else -> value
}
}
}

0 comments on commit b1244cc

Please sign in to comment.