Skip to content

Partiql eval limit offset #1371

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
Feb 20, 2024
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 @@ -10,6 +10,7 @@ import org.partiql.eval.internal.operator.rel.RelJoinLeft
import org.partiql.eval.internal.operator.rel.RelJoinOuterFull
import org.partiql.eval.internal.operator.rel.RelJoinRight
import org.partiql.eval.internal.operator.rel.RelLimit
import org.partiql.eval.internal.operator.rel.RelOffset
import org.partiql.eval.internal.operator.rel.RelProject
import org.partiql.eval.internal.operator.rel.RelScan
import org.partiql.eval.internal.operator.rel.RelScanIndexed
Expand Down Expand Up @@ -216,7 +217,7 @@ internal class Compiler(
override fun visitRelOpOffset(node: Rel.Op.Offset, ctx: StaticType?): Operator {
val input = visitRel(node.input, ctx)
val offset = visitRex(node.offset, ctx)
return RelLimit(input, offset)
return RelOffset(input, offset)
}

override fun visitRexOpTupleUnion(node: Rex.Op.TupleUnion, ctx: StaticType?): Operator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ import org.partiql.eval.internal.Record
import org.partiql.eval.internal.operator.Operator
import org.partiql.value.NumericValue
import org.partiql.value.PartiQLValueExperimental
import java.math.BigInteger

@OptIn(PartiQLValueExperimental::class)
internal class RelLimit(
private val input: Operator.Relation,
private val limit: Operator.Expr,
) : Operator.Relation {

private var _seen: Long = 0
private var _limit: Long = 0
private var _seen: BigInteger = BigInteger.ZERO
private var _limit: BigInteger = BigInteger.ZERO

override fun open() {
input.open()
_seen = 0
_seen = BigInteger.ZERO

// TODO pass outer scope to limit expression
val l = limit.eval(Record.empty)
if (l is NumericValue<*>) {
_limit = l.toInt64().value ?: 0L
_limit = l.toInt().value!!
} else {
throw TypeCheckException()
}
Expand All @@ -31,7 +32,7 @@ internal class RelLimit(
override fun next(): Record? {
if (_seen < _limit) {
val row = input.next() ?: return null
_seen += 1
_seen = _seen.add(BigInteger.ONE)
return row
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.partiql.eval.internal.Record
import org.partiql.eval.internal.operator.Operator
import org.partiql.value.NumericValue
import org.partiql.value.PartiQLValueExperimental
import java.math.BigInteger

@OptIn(PartiQLValueExperimental::class)
internal class RelOffset(
Expand All @@ -13,18 +14,18 @@ internal class RelOffset(
) : Operator.Relation {

private var init = false
private var _seen: Long = 0
private var _offset: Long = 0
private var _seen: BigInteger = BigInteger.ZERO
private var _offset: BigInteger = BigInteger.ZERO

override fun open() {
input.open()
init = false
_seen = 0
_seen = BigInteger.ZERO

// TODO pass outer scope to offset expression
val o = offset.eval(Record.empty)
if (o is NumericValue<*>) {
_offset = o.toInt64().value ?: 0L
_offset = o.toInt().value!!
} else {
throw TypeCheckException()
}
Expand All @@ -34,7 +35,7 @@ internal class RelOffset(
if (!init) {
while (_seen < _offset) {
input.next() ?: return null
_seen += 1
_seen = _seen.add(BigInteger.ONE)
}
init = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ internal object RelConverter {
rel = convertHaving(rel, sel.having)
rel = convertSetOp(rel, sel.setOp)
rel = convertOrderBy(rel, sel.orderBy)
rel = convertLimit(rel, sel.limit)
// offset should precede limit
rel = convertOffset(rel, sel.offset)
rel = convertLimit(rel, sel.limit)
rel = convertExclude(rel, sel.exclude)
// append SQL projection if present
rel = when (val projection = sel.select) {
Expand Down