-
Notifications
You must be signed in to change notification settings - Fork 25.6k
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
d138266
f437a40
525ffbe
e347009
7574caa
4cd2a07
731be5b
17e9a69
734b677
ba22060
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,3 +223,22 @@ TS k8s | |
|
||
; | ||
|
||
sum_rate_per_region | ||
required_capability: ts_command_v0 | ||
required_capability: pack_dimensions_in_ts | ||
TS k8s | ||
| STATS rate_bytes_in=avg(rate(network.total_bytes_in)) BY cluster, region, time_bucket = bucket(@timestamp,5minute) | ||
| SORT rate_bytes_in DESC, time_bucket, cluster | LIMIT 10; | ||
|
||
rate_bytes_in:double | cluster:keyword | region:keyword | time_bucket:datetime | ||
15.025267167998313 | qa | null | 2024-05-10T00:15:00.000Z | ||
13.638384356589611 | qa | null | 2024-05-10T00:05:00.000Z | ||
11.761724575728252 | prod | [eu, us] | 2024-05-10T00:15:00.000Z | ||
7.453275209904956 | qa | null | 2024-05-10T00:10:00.000Z | ||
7.307225056633641 | staging | us | 2024-05-10T00:05:00.000Z | ||
7.203958127639015 | prod | [eu, us] | 2024-05-10T00:10:00.000Z | ||
6.34494062999877 | staging | us | 2024-05-10T00:10:00.000Z | ||
5.700488689624205 | prod | [eu, us] | 2024-05-10T00:20:00.000Z | ||
5.4539153439153445 | prod | [eu, us] | 2024-05-10T00:00:00.000Z | ||
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. We return arrays for grouping dimensions. |
||
5.241187469367376 | staging | us | 2024-05-10T00:00:00.000Z | ||
; |
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(); | ||
} | ||
} | ||
} |
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 will open a separate PR for this fix.