Skip to content

Commit

Permalink
Update eval implementations to create iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed Aug 8, 2024
1 parent 28428bf commit fb73da5
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.helpers.IteratorPeeking
import org.partiql.eval.internal.helpers.asIterator
import org.partiql.eval.internal.operator.Operator
import org.partiql.eval.value.Datum
Expand All @@ -13,30 +14,35 @@ internal class ExprExceptAll(
private val rhs: Operator.Expr,
private val coerce: Boolean,
) : Operator.Expr {

// TODO: Add support for equals/hashcode in Datum
private val counts: MutableMap<PartiQLValue, Int> = mutableMapOf()

override fun eval(env: Environment): Datum {
val lIter = lhs.eval(env).asIterator(coerce)
val rIter = rhs.eval(env).asIterator(coerce)
// read in RHS first
for (d in rIter) {
val value = d.toPartiQLValue()
val count = counts[value] ?: 0
counts[value] = count + 1
}
val excepted = sequence {
for (d in lIter) {
val value = d.toPartiQLValue()
val count = counts[value] ?: 0
if (count > 0) {
counts[value] = count - 1
continue
val lDatum = lhs.eval(env)
val rDatum = rhs.eval(env)
val iter = Iterable {
// TODO: Add support for equals/hashcode in Datum
val counts: MutableMap<PartiQLValue, Int> = mutableMapOf()
val lIter = lDatum.asIterator(coerce)
val rIter = rDatum.asIterator(coerce)
object : IteratorPeeking<Datum>() {
override fun peek(): Datum? {
// read in RHS first
for (r in rIter) {
val value = r.toPartiQLValue()
val count = counts[value] ?: 0
counts[value] = count + 1
}
for (l in lIter) {
val value = l.toPartiQLValue()
val count = counts[value] ?: 0
if (count > 0) {
counts[value] = count - 1
continue
}
return l
}
return null
}
yield(d)
}
}
return Datum.bagValue(excepted.asIterable())
return Datum.bagValue(iter)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.helpers.IteratorPeeking
import org.partiql.eval.internal.helpers.asIterator
import org.partiql.eval.internal.operator.Operator
import org.partiql.eval.value.Datum
Expand All @@ -13,25 +14,31 @@ internal class ExprExceptDistinct(
private val rhs: Operator.Expr,
private val coerce: Boolean,
) : Operator.Expr {
// TODO: Add support for equals/hashcode in Datum
private val seen: MutableSet<PartiQLValue> = mutableSetOf()

override fun eval(env: Environment): Datum {
val lIter = lhs.eval(env).asIterator(coerce)
val rIter = rhs.eval(env).asIterator(coerce)
// read in RHS first
for (d in rIter) {
val value = d.toPartiQLValue()
seen.add(value)
}
val excepted = sequence {
for (d in lIter) {
val value = d.toPartiQLValue()
if (!seen.contains(value)) {
yield(d)
val lDatum = lhs.eval(env)
val rDatum = rhs.eval(env)
val iter = Iterable {
// TODO: Add support for equals/hashcode in Datum
val seen: MutableSet<PartiQLValue> = mutableSetOf()
val lIter = lDatum.asIterator(coerce)
val rIter = rDatum.asIterator(coerce)
object : IteratorPeeking<Datum>() {
override fun peek(): Datum? {
// read in RHS first
for (r in rIter) {
val value = r.toPartiQLValue()
seen.add(value)
}
for (l in lIter) {
val value = l.toPartiQLValue()
if (!seen.contains(value)) {
return l
}
}
return null
}
}
}
return Datum.bagValue(excepted.asIterable())
return Datum.bagValue(iter)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.helpers.IteratorPeeking
import org.partiql.eval.internal.helpers.asIterator
import org.partiql.eval.internal.operator.Operator
import org.partiql.eval.value.Datum
Expand All @@ -13,28 +14,34 @@ internal class ExprIntersectAll(
private val rhs: Operator.Expr,
private val coerce: Boolean,
) : Operator.Expr {
// TODO: Add support for equals/hashcode in Datum
private val counts: MutableMap<PartiQLValue, Int> = mutableMapOf()

override fun eval(env: Environment): Datum {
val lIter = lhs.eval(env).asIterator(coerce)
val rIter = rhs.eval(env).asIterator(coerce)
// read in LHS first
for (d in lIter) {
val value = d.toPartiQLValue()
val count = counts[value] ?: 0
counts[value] = count + 1
}
val intersected = sequence {
for (d in rIter) {
val value = d.toPartiQLValue()
val count = counts[value] ?: 0
if (count > 0) {
counts[value] = count - 1
yield(d)
val lDatum = lhs.eval(env)
val rDatum = rhs.eval(env)
val iter = Iterable {
// TODO: Add support for equals/hashcode in Datum
val counts: MutableMap<PartiQLValue, Int> = mutableMapOf()
val lIter = lDatum.asIterator(coerce)
val rIter = rDatum.asIterator(coerce)
object : IteratorPeeking<Datum>() {
override fun peek(): Datum? {
// read in LHS first
for (l in lIter) {
val value = l.toPartiQLValue()
val count = counts[value] ?: 0
counts[value] = count + 1
}
for (r in rIter) {
val value = r.toPartiQLValue()
val count = counts[value] ?: 0
if (count > 0) {
counts[value] = count - 1
return r
}
}
return null
}
}
}
return Datum.bagValue(intersected.asIterable())
return Datum.bagValue(iter)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.helpers.IteratorPeeking
import org.partiql.eval.internal.helpers.asIterator
import org.partiql.eval.internal.operator.Operator
import org.partiql.eval.value.Datum
Expand All @@ -13,25 +14,31 @@ internal class ExprIntersectDistinct(
private val rhs: Operator.Expr,
private val coerce: Boolean,
) : Operator.Expr {
// TODO: Add support for equals/hashcode in Datum
private val seen: MutableSet<PartiQLValue> = mutableSetOf()

override fun eval(env: Environment): Datum {
val lIter = lhs.eval(env).asIterator(coerce)
val rIter = rhs.eval(env).asIterator(coerce)
// read in LHS first
for (d in lIter) {
val value = d.toPartiQLValue()
seen.add(value)
}
val intersected = sequence {
for (d in rIter) {
val value = d.toPartiQLValue()
if (seen.remove(value)) {
yield(d)
val lDatum = lhs.eval(env)
val rDatum = rhs.eval(env)
val iter = Iterable {
// TODO: Add support for equals/hashcode in Datum
val seen: MutableSet<PartiQLValue> = mutableSetOf()
val lIter = lDatum.asIterator(coerce)
val rIter = rDatum.asIterator(coerce)
object : IteratorPeeking<Datum>() {
override fun peek(): Datum? {
// read in LHS first
for (l in lIter) {
val value = l.toPartiQLValue()
seen.add(value)
}
for (r in rIter) {
val value = r.toPartiQLValue()
if (seen.remove(value)) {
return r
}
}
return null
}
}
}
return Datum.bagValue(intersected.asIterable())
return Datum.bagValue(iter)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.helpers.IteratorChain
import org.partiql.eval.internal.helpers.asIterator
import org.partiql.eval.internal.operator.Operator
import org.partiql.eval.value.Datum
Expand All @@ -11,9 +12,13 @@ internal class ExprUnionAll(
private val coerce: Boolean,
) : Operator.Expr {
override fun eval(env: Environment): Datum {
val lIter = lhs.eval(env).asIterator(coerce)
val rIter = rhs.eval(env).asIterator(coerce)
val unioned = lIter.asSequence() + rIter.asSequence()
return Datum.bagValue(unioned.asIterable())
val lDatum = lhs.eval(env)
val rDatum = rhs.eval(env)
val iter = Iterable {
val lIter = lDatum.asIterator(coerce)
val rIter = rDatum.asIterator(coerce)
IteratorChain(arrayOf(lIter, rIter))
}
return Datum.bagValue(iter)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.partiql.eval.internal.operator.rex

import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.helpers.IteratorChain
import org.partiql.eval.internal.helpers.IteratorPeeking
import org.partiql.eval.internal.helpers.asIterator
import org.partiql.eval.internal.operator.Operator
import org.partiql.eval.value.Datum
Expand All @@ -13,22 +15,28 @@ internal class ExprUnionDistinct(
private val rhs: Operator.Expr,
private val coerce: Boolean,
) : Operator.Expr {
// TODO: Add support for equals/hashcode in Datum
private val seen: MutableSet<PartiQLValue> = mutableSetOf()

override fun eval(env: Environment): Datum {
val lIter = lhs.eval(env).asIterator(coerce)
val rIter = rhs.eval(env).asIterator(coerce)
val unioned = lIter.asSequence() + rIter.asSequence()
val distinct = sequence {
for (d in unioned) {
val value = d.toPartiQLValue()
if (!seen.contains(value)) {
seen.add(value)
yield(d)
val lDatum = lhs.eval(env)
val rDatum = rhs.eval(env)
val iter = Iterable {
// TODO: Add support for equals/hashcode in Datum
val seen: MutableSet<PartiQLValue> = mutableSetOf()
val lIter = lDatum.asIterator(coerce)
val rIter = rDatum.asIterator(coerce)
val combined = IteratorChain(arrayOf(lIter, rIter))
object : IteratorPeeking<Datum>() {
override fun peek(): Datum? {
for (d in combined) {
val value = d.toPartiQLValue()
if (!seen.contains(value)) {
seen.add(value)
return d
}
}
return null
}
}
}
return Datum.bagValue(distinct.asIterable())
return Datum.bagValue(iter)
}
}

0 comments on commit fb73da5

Please sign in to comment.