-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Pack dimension values in time-series aggregation #136216
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
10 commits
Select commit
Hold shift + click to select a range
d138266
Pack dimension values in time-series aggregation
dnhatn f437a40
[CI] Auto commit changes from spotless
525ffbe
all labels
dnhatn e347009
Merge remote-tracking branch 'elastic/main' into pack-dimensions
dnhatn 7574caa
Merge remote-tracking branch 'dnhatn/pack-dimensions' into pack-dimen…
dnhatn 4cd2a07
Fix tests
dnhatn 731be5b
Merge remote-tracking branch 'elastic/main' into pack-dimensions
dnhatn 17e9a69
Fix tests
dnhatn 734b677
Merge remote-tracking branch 'elastic/main' into pack-dimensions
dnhatn ba22060
Merge remote-tracking branch 'elastic/main' into pack-dimensions
dnhatn 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
402 changes: 201 additions & 201 deletions
402
x-pack/plugin/esql/qa/testFixtures/src/main/resources/data/k8s.csv
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
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
272 changes: 272 additions & 0 deletions
272
.../java/org/elasticsearch/xpack/esql/expression/function/scalar/internal/InternalPacks.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,272 @@ | ||
/* | ||
* 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.xpack.esql.expression.function.scalar.internal; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.compute.data.BooleanBlock; | ||
import org.elasticsearch.compute.data.BytesRefBlock; | ||
import org.elasticsearch.compute.data.BytesRefVector; | ||
import org.elasticsearch.compute.data.IntBlock; | ||
import org.elasticsearch.compute.data.LongBlock; | ||
import org.elasticsearch.compute.data.OrdinalBytesRefVector; | ||
import org.elasticsearch.compute.operator.BreakingBytesRefBuilder; | ||
import org.elasticsearch.compute.operator.DriverContext; | ||
import org.elasticsearch.compute.operator.topn.TopNEncoder; | ||
|
||
final class InternalPacks { | ||
private static final TopNEncoder ENCODER = TopNEncoder.DEFAULT_UNSORTABLE; | ||
public static final int INITIAL_SIZE_IN_BYTES = 6 * 1024; | ||
|
||
static int estimateForBytesBuilder(int positionCount) { | ||
// allocate at least one page for the bytes block builder to avoid copying during resizing | ||
return Math.max(INITIAL_SIZE_IN_BYTES, positionCount); | ||
} | ||
|
||
static BytesRefBlock packBytesValues(DriverContext driverContext, BytesRefBlock raw) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where we encode multi-valued block into a single value. |
||
BytesRefVector vector = raw.asVector(); | ||
if (vector != null) { | ||
OrdinalBytesRefVector ordinals = vector.asOrdinals(); | ||
if (ordinals != null) { | ||
var encoded = packBytesVector(driverContext, ordinals.getDictionaryVector()); | ||
ordinals.getOrdinalsVector().incRef(); | ||
return new OrdinalBytesRefVector(ordinals.getOrdinalsVector(), encoded).asBlock(); | ||
} else { | ||
return packBytesVector(driverContext, vector).asBlock(); | ||
} | ||
} | ||
int positionCount = raw.getPositionCount(); | ||
try ( | ||
var builder = driverContext.blockFactory().newBytesRefBlockBuilder(estimateForBytesBuilder(positionCount)); | ||
var work = new BreakingBytesRefBuilder(driverContext.breaker(), "pack_dimensions", 1024) | ||
) { | ||
BytesRef scratch = new BytesRef(); | ||
for (int p = 0; p < positionCount; p++) { | ||
int valueCount = raw.getValueCount(p); | ||
if (valueCount == 0) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
work.clear(); | ||
int first = raw.getFirstValueIndex(p); | ||
int end = first + valueCount; | ||
for (int i = first; i < end; i++) { | ||
raw.getBytesRef(i, scratch); | ||
ENCODER.encodeBytesRef(scratch, work); | ||
} | ||
builder.appendBytesRef(work.bytesRefView()); | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static BytesRefVector packBytesVector(DriverContext driverContext, BytesRefVector encode) { | ||
int positionCount = encode.getPositionCount(); | ||
try ( | ||
var builder = driverContext.blockFactory().newBytesRefVectorBuilder(estimateForBytesBuilder(positionCount)); | ||
var work = new BreakingBytesRefBuilder(driverContext.breaker(), "pack_values", 1024) | ||
) { | ||
BytesRef scratch = new BytesRef(); | ||
for (int p = 0; p < positionCount; p++) { | ||
ENCODER.encodeBytesRef(encode.getBytesRef(p, scratch), work); | ||
builder.appendBytesRef(work.bytesRefView()); | ||
work.clear(); | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static BytesRefBlock unpackBytesValues(DriverContext driverContext, BytesRefBlock encoded) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And decode here. |
||
int positionCount = encoded.getPositionCount(); | ||
try (var builder = driverContext.blockFactory().newBytesRefBlockBuilder(estimateForBytesBuilder(positionCount));) { | ||
BytesRef inScratch = new BytesRef(); | ||
BytesRef outScratch = new BytesRef(); | ||
for (int p = 0; p < positionCount; p++) { | ||
if (encoded.isNull(p)) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
BytesRef row = encoded.getBytesRef(p, inScratch); | ||
var v = ENCODER.decodeBytesRef(row, outScratch); | ||
if (row.length == 0) { | ||
builder.appendBytesRef(v); | ||
} else { | ||
builder.beginPositionEntry(); | ||
builder.appendBytesRef(v); | ||
while (row.length > 0) { | ||
v = ENCODER.decodeBytesRef(row, outScratch); | ||
builder.appendBytesRef(v); | ||
} | ||
builder.endPositionEntry(); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static BytesRefBlock packLongValues(DriverContext driverContext, LongBlock raw) { | ||
int positionCount = raw.getPositionCount(); | ||
try ( | ||
var builder = driverContext.blockFactory().newBytesRefBlockBuilder(estimateForBytesBuilder(positionCount)); | ||
var work = new BreakingBytesRefBuilder(driverContext.breaker(), "pack_values", 32) | ||
) { | ||
for (int p = 0; p < positionCount; p++) { | ||
int valueCount = raw.getValueCount(p); | ||
if (valueCount == 0) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
work.clear(); | ||
int first = raw.getFirstValueIndex(p); | ||
if (valueCount == 1) { | ||
ENCODER.encodeLong(raw.getLong(first), work); | ||
} else { | ||
int end = first + valueCount; | ||
for (int i = first; i < end; i++) { | ||
ENCODER.encodeLong(raw.getLong(i), work); | ||
} | ||
} | ||
builder.appendBytesRef(work.bytesRefView()); | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static LongBlock unpackLongValues(DriverContext driverContext, BytesRefBlock encoded) { | ||
int positionCount = encoded.getPositionCount(); | ||
try (var builder = driverContext.blockFactory().newLongBlockBuilder(positionCount)) { | ||
BytesRef inScratch = new BytesRef(); | ||
for (int p = 0; p < positionCount; p++) { | ||
if (encoded.isNull(p)) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
BytesRef row = encoded.getBytesRef(p, inScratch); | ||
var v = ENCODER.decodeLong(row); | ||
if (row.length == 0) { | ||
builder.appendLong(v); | ||
} else { | ||
builder.beginPositionEntry(); | ||
builder.appendLong(v); | ||
while (row.length > 0) { | ||
builder.appendLong(ENCODER.decodeLong(row)); | ||
} | ||
builder.endPositionEntry(); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static BytesRefBlock packIntValues(DriverContext driverContext, IntBlock raw) { | ||
int positionCount = raw.getPositionCount(); | ||
try ( | ||
var builder = driverContext.blockFactory().newBytesRefBlockBuilder(estimateForBytesBuilder(positionCount)); | ||
var work = new BreakingBytesRefBuilder(driverContext.breaker(), "pack_values", 32) | ||
) { | ||
for (int p = 0; p < positionCount; p++) { | ||
int valueCount = raw.getValueCount(p); | ||
if (valueCount == 0) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
work.clear(); | ||
int first = raw.getFirstValueIndex(p); | ||
if (valueCount == 1) { | ||
ENCODER.encodeInt(raw.getInt(first), work); | ||
} else { | ||
int end = first + valueCount; | ||
for (int i = first; i < end; i++) { | ||
ENCODER.encodeInt(raw.getInt(i), work); | ||
} | ||
} | ||
builder.appendBytesRef(work.bytesRefView()); | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static IntBlock unpackIntValues(DriverContext driverContext, BytesRefBlock encoded) { | ||
int positionCount = encoded.getPositionCount(); | ||
try (IntBlock.Builder builder = driverContext.blockFactory().newIntBlockBuilder(positionCount)) { | ||
BytesRef inScratch = new BytesRef(); | ||
for (int p = 0; p < positionCount; p++) { | ||
if (encoded.isNull(p)) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
BytesRef row = encoded.getBytesRef(p, inScratch); | ||
int v = ENCODER.decodeInt(row); | ||
if (row.length == 0) { | ||
builder.appendInt(v); | ||
} else { | ||
builder.beginPositionEntry(); | ||
builder.appendInt(v); | ||
while (row.length > 0) { | ||
builder.appendInt(ENCODER.decodeInt(row)); | ||
} | ||
builder.endPositionEntry(); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static BytesRefBlock packBooleanValues(DriverContext driverContext, BooleanBlock raw) { | ||
int positionCount = raw.getPositionCount(); | ||
try ( | ||
var builder = driverContext.blockFactory().newBytesRefBlockBuilder(positionCount); | ||
var work = new BreakingBytesRefBuilder(driverContext.breaker(), "pack_values", 32) | ||
) { | ||
for (int p = 0; p < positionCount; p++) { | ||
work.clear(); | ||
int valueCount = raw.getValueCount(p); | ||
if (valueCount == 0) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
int first = raw.getFirstValueIndex(p); | ||
if (valueCount == 1) { | ||
ENCODER.encodeBoolean(raw.getBoolean(first), work); | ||
} else { | ||
int end = first + valueCount; | ||
for (int i = first; i < end; i++) { | ||
ENCODER.encodeBoolean(raw.getBoolean(i), work); | ||
} | ||
} | ||
builder.appendBytesRef(work.bytesRefView()); | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
|
||
static BooleanBlock unpackBooleanValues(DriverContext driverContext, BytesRefBlock encoded) { | ||
int positionCount = encoded.getPositionCount(); | ||
try (var builder = driverContext.blockFactory().newBooleanBlockBuilder(positionCount)) { | ||
BytesRef inScratch = new BytesRef(); | ||
for (int p = 0; p < positionCount; p++) { | ||
if (encoded.isNull(p)) { | ||
builder.appendNull(); | ||
continue; | ||
} | ||
BytesRef row = encoded.getBytesRef(p, inScratch); | ||
boolean v = ENCODER.decodeBoolean(row); | ||
if (row.length == 0) { | ||
builder.appendBoolean(v); | ||
} else { | ||
builder.beginPositionEntry(); | ||
builder.appendBoolean(v); | ||
while (row.length > 0) { | ||
builder.appendBoolean(ENCODER.decodeBoolean(row)); | ||
} | ||
builder.endPositionEntry(); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
} |
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.
We return arrays for grouping dimensions.