Skip to content

Commit

Permalink
limit offset (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
yliuuuu authored Feb 20, 2024
1 parent 0155e6a commit f1aeb6f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
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 @@ -260,7 +261,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 @@ -148,8 +148,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

0 comments on commit f1aeb6f

Please sign in to comment.