-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[ESQL] Adds wriring for compute side of LIMIT BY command #143458
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d893856
[ESQL] Adds wriring for compute side of LIMIT BY command
ncordon 03d0830
Adds serialization tests for LIMIT with groupings
ncordon 57a6798
Adds more tests and tweaks
ncordon d7ed4e1
[CI] Auto commit changes from spotless
b232ec0
Adds transport version tests
ncordon 1bef15c
Merge remote-tracking branch 'upstream/main' into esql-limit-by
ncordon 6a24032
Undoes any logical or physical plan part
ncordon e0d2c30
Rename
ncordon 10b97f4
Adds code for the edge case limitPerGroup = 0
ncordon 3fbeb25
Adds some explanatory comments
ncordon e53190b
Merge branch 'main' into esql-limit-by
ncordon cb5bf9a
Uses javadoc instead
ncordon d2bd9f3
Addresses pr review feedback
ncordon f3f05ab
Adds more multivalue tests
ncordon a45aef8
Last nit
ncordon 88664e7
Addresses last bits of feedback, hopefully
ncordon e28884e
Merge branch 'main' into esql-limit-by
ncordon 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
120 changes: 120 additions & 0 deletions
120
...plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/GroupKeyEncoder.java
This file contains hidden or 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,120 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.compute.operator; | ||
|
|
||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.compute.data.Block; | ||
| import org.elasticsearch.compute.data.BooleanBlock; | ||
| import org.elasticsearch.compute.data.BytesRefBlock; | ||
| import org.elasticsearch.compute.data.DoubleBlock; | ||
| import org.elasticsearch.compute.data.ElementType; | ||
| import org.elasticsearch.compute.data.FloatBlock; | ||
| import org.elasticsearch.compute.data.IntBlock; | ||
| import org.elasticsearch.compute.data.LongBlock; | ||
| import org.elasticsearch.compute.data.Page; | ||
| import org.elasticsearch.compute.operator.topn.DefaultUnsortableTopNEncoder; | ||
| import org.elasticsearch.compute.operator.topn.TopNEncoder; | ||
| import org.elasticsearch.core.Releasable; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Encodes the values at a given position across multiple blocks into a single {@link BytesRef} composite key. | ||
| * Multivalued positions are serialized with list semantics: the value count is written first, then each value | ||
| * in block iteration order. This means {@code [1, 2]} and {@code [2, 1]} produce different keys. | ||
| * Null positions are encoded as a value count of zero. | ||
| */ | ||
| public class GroupKeyEncoder implements Releasable { | ||
|
|
||
| private static final DefaultUnsortableTopNEncoder encoder = TopNEncoder.DEFAULT_UNSORTABLE; | ||
|
|
||
| private final int[] groupChannels; | ||
| private final ElementType[] elementTypes; | ||
| private final BreakingBytesRefBuilder scratch; | ||
| private final BytesRef scratchBytesRef = new BytesRef(); | ||
|
|
||
| public GroupKeyEncoder(int[] groupChannels, List<ElementType> elementTypes, BreakingBytesRefBuilder scratch) { | ||
| this.groupChannels = groupChannels; | ||
| this.elementTypes = new ElementType[groupChannels.length]; | ||
| for (int i = 0; i < groupChannels.length; i++) { | ||
| this.elementTypes[i] = elementTypes.get(groupChannels[i]); | ||
| } | ||
| this.scratch = scratch; | ||
| } | ||
|
|
||
| /** | ||
| * Encode the group key for the given position from the page into a {@link BytesRef}. | ||
| * The returned reference is only valid until the next call to {@code encode}. | ||
| */ | ||
| public BytesRef encode(Page page, int position) { | ||
| scratch.clear(); | ||
| for (int i = 0; i < groupChannels.length; i++) { | ||
| Block block = page.getBlock(groupChannels[i]); | ||
| encodeBlock(block, elementTypes[i], position); | ||
| } | ||
| return scratch.bytesRefView(); | ||
| } | ||
|
|
||
| private void encodeBlock(Block block, ElementType type, int position) { | ||
| if (block.isNull(position)) { | ||
| encoder.encodeVInt(0, scratch); | ||
| return; | ||
| } | ||
| int firstValueIndex = block.getFirstValueIndex(position); | ||
| int valueCount = block.getValueCount(position); | ||
| encoder.encodeVInt(valueCount, scratch); | ||
| switch (type) { | ||
| case INT -> { | ||
| IntBlock b = (IntBlock) block; | ||
| for (int v = 0; v < valueCount; v++) { | ||
| encoder.encodeInt(b.getInt(firstValueIndex + v), scratch); | ||
| } | ||
| } | ||
| case LONG -> { | ||
| LongBlock b = (LongBlock) block; | ||
| for (int v = 0; v < valueCount; v++) { | ||
| encoder.encodeLong(b.getLong(firstValueIndex + v), scratch); | ||
| } | ||
| } | ||
| case DOUBLE -> { | ||
| DoubleBlock b = (DoubleBlock) block; | ||
| for (int v = 0; v < valueCount; v++) { | ||
| encoder.encodeDouble(b.getDouble(firstValueIndex + v), scratch); | ||
| } | ||
| } | ||
| case FLOAT -> { | ||
| FloatBlock b = (FloatBlock) block; | ||
| for (int v = 0; v < valueCount; v++) { | ||
| encoder.encodeFloat(b.getFloat(firstValueIndex + v), scratch); | ||
| } | ||
| } | ||
| case BOOLEAN -> { | ||
| BooleanBlock b = (BooleanBlock) block; | ||
| for (int v = 0; v < valueCount; v++) { | ||
| encoder.encodeBoolean(b.getBoolean(firstValueIndex + v), scratch); | ||
| } | ||
| } | ||
| case BYTES_REF -> { | ||
| BytesRefBlock b = (BytesRefBlock) block; | ||
| for (int v = 0; v < valueCount; v++) { | ||
| BytesRef ref = b.getBytesRef(firstValueIndex + v, scratchBytesRef); | ||
| encoder.encodeBytesRef(ref, scratch); | ||
| } | ||
| } | ||
| case NULL -> { | ||
| // already handled by isNull above; nothing extra to write | ||
| } | ||
| default -> throw new IllegalArgumentException("unsupported element type for group key encoding: " + type); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| scratch.close(); | ||
| } | ||
| } | ||
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.
Not sure whether I like this
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.
Looks good to me, and we reuse some code. Plus, having the concrete class declared there should help with inlining.
Is there something you don't like specifically?
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.
The naming perhaps (we are calling something from TopN inside limit), but it's fine, I can live with it
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.
Uhm yep, the namespacing seems odd, but I would live with it. Ideally, if we decide to change to a BlockHash or similar, that would be "fixed" (Fun, because
BlockHashis in theaggregationspackage, so not perfect either! 😆)