-
Notifications
You must be signed in to change notification settings - Fork 63
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
Add RelSort
and comparator between PartiQLValue
#1343
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16931ad
Add RelSort and comparator between PartiQLValue
alancai98 50f1007
Apply commits from partiql-plugin-impl branch to fix build
alancai98 48455b7
Some test corrections; additional comments
alancai98 cbbdc33
Move PartiQLValueComparator to partiql-types and make a public fn
alancai98 792dabe
Change RelSort to use Iterator; move comparator to PartiQLValue
alancai98 6cbb87b
Merge branch 'partiql-eval' into partiql-eval-relsort
alancai98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
partiql-eval/src/main/kotlin/org/partiql/eval/internal/operator/rel/RelSort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.partiql.eval.internal.operator.rel | ||
|
||
import org.partiql.eval.internal.Record | ||
import org.partiql.eval.internal.operator.Operator | ||
import org.partiql.eval.internal.util.PartiQLValueComparator | ||
import org.partiql.plan.Rel | ||
import org.partiql.value.PartiQLValueExperimental | ||
|
||
internal class RelSort( | ||
val input: Operator.Relation, | ||
val specs: List<Pair<Operator.Expr, Rel.Op.Sort.Order>> | ||
alancai98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
) : Operator.Relation { | ||
private var records: MutableList<Record> = mutableListOf() | ||
alancai98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private var init: Boolean = false | ||
|
||
private val nullsFirstComparator = PartiQLValueComparator(nullOrder = PartiQLValueComparator.NullOrder.FIRST) | ||
private val nullsLastComparator = PartiQLValueComparator(nullOrder = PartiQLValueComparator.NullOrder.LAST) | ||
|
||
override fun open() { | ||
input.open() | ||
init = false | ||
records = mutableListOf() | ||
} | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
val comparator = object : Comparator<Record> { | ||
override fun compare(l: Record, r: Record): Int { | ||
specs.forEach { spec -> | ||
val lVal = spec.first.eval(l) | ||
val rVal = spec.first.eval(r) | ||
|
||
// DESC_NULLS_FIRST(l, r) == ASC_NULLS_LAST(r, l) | ||
// DESC_NULLS_LAST(l, r) == ASC_NULLS_FIRST(r, l) | ||
val cmpResult = when (spec.second) { | ||
Rel.Op.Sort.Order.ASC_NULLS_FIRST -> nullsFirstComparator.compare(lVal, rVal) | ||
Rel.Op.Sort.Order.ASC_NULLS_LAST -> nullsLastComparator.compare(lVal, rVal) | ||
Rel.Op.Sort.Order.DESC_NULLS_FIRST -> nullsLastComparator.compare(rVal, lVal) | ||
Rel.Op.Sort.Order.DESC_NULLS_LAST -> nullsFirstComparator.compare(rVal, lVal) | ||
} | ||
if (cmpResult != 0) { | ||
return cmpResult | ||
} | ||
} | ||
return 0 // Equal | ||
} | ||
} | ||
|
||
override fun next(): Record? { | ||
if (!init) { | ||
while (true) { | ||
val row = input.next() ?: break | ||
records.add(row) | ||
} | ||
records.sortWith(comparator) | ||
} | ||
return when (records.isEmpty()) { | ||
true -> null | ||
else -> records.removeAt(0) | ||
} | ||
} | ||
|
||
override fun close() { | ||
input.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the current plan, the rex node's type is only accessible on the
Rex
and not the inner classes (e.g.Rex.Op.Collection
). Passing it along in the context allows the visitor for the inner classes to access the type, which is needed by some nodes likeRex.Op.Collection
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would really like for our internal IRs to keep the types in the node by defining a type field as part of the PlanNode base class. This would require more codegen work which probably isn't worth the time at the moment. Please let me know if you have an additional ideas here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I'd need to play around with the generated code a bit to see if there's a better way. Only concern I'd have is that the
type
field forRex
is a different type (i.e.StaticType
) than thetype
field forRel
(i.e.Rel.Type
which has schema and props). Probably that representation in thePlanNode
base class would be an enum that could be either of thosetype
definitions?Anyways, perhaps we should tackle this in another issue/PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you check out PlanTyper, you'll see we use two different visitors since the type is parameterized. There are many ways around this, but really I wish we didn't have the union types. What would be better is for the code generator to support abstract fields.
The best situation however would be handwritten nodes with annotation based generation like Lombok. This would give us the most control.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see. The separate visitors for Rel and Rex doesn't seem too cumbersome.
Agree w/ a mix of code-generated nodes and handwritten nodes would give us the most flexibility when it comes to these interfaces.