Skip to content

Commit

Permalink
Support counter type in ESQL
Browse files Browse the repository at this point in the history
  • Loading branch information
dnhatn committed Apr 25, 2024
1 parent 0ac10c9 commit 84cd4ee
Show file tree
Hide file tree
Showing 25 changed files with 218 additions and 55 deletions.
12 changes: 12 additions & 0 deletions docs/reference/esql/functions/kibana/definition/to_double.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
"variadic" : false,
"returnType" : "double"
},
{
"params" : [
{
"name" : "field",
"type" : "counter_double",
"optional" : false,
"description" : "Input value. The input can be a single- or multi-valued column or an expression."
}
],
"variadic" : false,
"returnType" : "double"
},
{
"params" : [
{
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/esql/functions/kibana/definition/to_integer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
"variadic" : false,
"returnType" : "integer"
},
{
"params" : [
{
"name" : "field",
"type" : "counter_integer",
"optional" : false,
"description" : "Input value. The input can be a single- or multi-valued column or an expression."
}
],
"variadic" : false,
"returnType" : "integer"
},
{
"params" : [
{
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/esql/functions/kibana/definition/to_long.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
"variadic" : false,
"returnType" : "long"
},
{
"params" : [
{
"name" : "field",
"type" : "counter_long",
"optional" : false,
"description" : "Input value. The input can be a single- or multi-valued column or an expression."
}
],
"variadic" : false,
"returnType" : "long"
},
{
"params" : [
{
Expand Down
1 change: 1 addition & 0 deletions docs/reference/esql/functions/types/to_double.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|===
field | result
boolean | double
counter_double | double
datetime | double
double | double
integer | double
Expand Down
1 change: 1 addition & 0 deletions docs/reference/esql/functions/types/to_integer.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|===
field | result
boolean | integer
counter_integer | integer
datetime | integer
double | integer
integer | integer
Expand Down
1 change: 1 addition & 0 deletions docs/reference/esql/functions/types/to_long.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|===
field | result
boolean | long
counter_long | long
datetime | long
double | long
integer | long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1709,10 +1709,6 @@ public Function<byte[], Number> pointReaderIfPossible() {

@Override
public BlockLoader blockLoader(BlockLoaderContext blContext) {
if (indexMode == IndexMode.TIME_SERIES && metricType == TimeSeriesParams.MetricType.COUNTER) {
// Counters are not supported by ESQL so we load them in null
return BlockLoader.CONSTANT_NULLS;
}
if (hasDocValues()) {
return type.blockLoaderFromDocValues(name());
}
Expand Down
20 changes: 10 additions & 10 deletions x-pack/plugin/esql/qa/testFixtures/src/main/resources/meta.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ double tau()
"cartesian_point to_cartesianpoint(field:cartesian_point|keyword|text)"
"cartesian_shape to_cartesianshape(field:cartesian_point|cartesian_shape|keyword|text)"
"date to_datetime(field:date|keyword|text|double|long|unsigned_long|integer)"
"double to_dbl(field:boolean|date|keyword|text|double|long|unsigned_long|integer)"
"double to_dbl(field:boolean|date|keyword|text|double|long|unsigned_long|integer|counter_double)"
"double to_degrees(number:double|integer|long|unsigned_long)"
"double to_double(field:boolean|date|keyword|text|double|long|unsigned_long|integer)"
"double to_double(field:boolean|date|keyword|text|double|long|unsigned_long|integer|counter_double)"
"date to_dt(field:date|keyword|text|double|long|unsigned_long|integer)"
"geo_point to_geopoint(field:geo_point|keyword|text)"
"geo_shape to_geoshape(field:geo_point|geo_shape|keyword|text)"
"integer to_int(field:boolean|date|keyword|text|double|long|unsigned_long|integer)"
"integer to_integer(field:boolean|date|keyword|text|double|long|unsigned_long|integer)"
"integer to_int(field:boolean|date|keyword|text|double|long|unsigned_long|integer|counter_integer)"
"integer to_integer(field:boolean|date|keyword|text|double|long|unsigned_long|integer|counter_integer)"
"ip to_ip(field:ip|keyword|text)"
"long to_long(field:boolean|date|keyword|text|double|long|unsigned_long|integer)"
"long to_long(field:boolean|date|keyword|text|double|long|unsigned_long|integer|counter_long)"
"keyword|text to_lower(str:keyword|text)"
"double to_radians(number:double|integer|long|unsigned_long)"
"keyword to_str(field:boolean|cartesian_point|cartesian_shape|date|double|geo_point|geo_shape|integer|ip|keyword|long|text|unsigned_long|version)"
Expand Down Expand Up @@ -198,16 +198,16 @@ to_boolean |field |"boolean|keyword|text|double
to_cartesianpo|field |"cartesian_point|keyword|text" |Input value. The input can be a single- or multi-valued column or an expression.
to_cartesiansh|field |"cartesian_point|cartesian_shape|keyword|text" |Input value. The input can be a single- or multi-valued column or an expression.
to_datetime |field |"date|keyword|text|double|long|unsigned_long|integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_dbl |field |"boolean|date|keyword|text|double|long|unsigned_long|integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_dbl |field |"boolean|date|keyword|text|double|long|unsigned_long|integer|counter_double" |Input value. The input can be a single- or multi-valued column or an expression.
to_degrees |number |"double|integer|long|unsigned_long" |Input value. The input can be a single- or multi-valued column or an expression.
to_double |field |"boolean|date|keyword|text|double|long|unsigned_long|integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_double |field |"boolean|date|keyword|text|double|long|unsigned_long|integer|counter_double" |Input value. The input can be a single- or multi-valued column or an expression.
to_dt |field |"date|keyword|text|double|long|unsigned_long|integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_geopoint |field |"geo_point|keyword|text" |Input value. The input can be a single- or multi-valued column or an expression.
to_geoshape |field |"geo_point|geo_shape|keyword|text" |Input value. The input can be a single- or multi-valued column or an expression.
to_int |field |"boolean|date|keyword|text|double|long|unsigned_long|integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_integer |field |"boolean|date|keyword|text|double|long|unsigned_long|integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_int |field |"boolean|date|keyword|text|double|long|unsigned_long|integer|counter_integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_integer |field |"boolean|date|keyword|text|double|long|unsigned_long|integer|counter_integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_ip |field |"ip|keyword|text" |Input value. The input can be a single- or multi-valued column or an expression.
to_long |field |"boolean|date|keyword|text|double|long|unsigned_long|integer" |Input value. The input can be a single- or multi-valued column or an expression.
to_long |field |"boolean|date|keyword|text|double|long|unsigned_long|integer|counter_long" |Input value. The input can be a single- or multi-valued column or an expression.
to_lower |str |"keyword|text" |String expression. If `null`, the function returns `null`.
to_radians |number |"double|integer|long|unsigned_long" |Input value. The input can be a single- or multi-valued column or an expression.
to_str |field |"boolean|cartesian_point|cartesian_shape|date|double|geo_point|geo_shape|integer|ip|keyword|long|text|unsigned_long|version" |Input value. The input can be a single- or multi-valued column or an expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ protected abstract XContentBuilder valueToXContent(XContentBuilder builder, ToXC

public static PositionToXContent positionToXContent(ColumnInfo columnInfo, Block block, BytesRef scratch) {
return switch (columnInfo.type()) {
case "long" -> new PositionToXContent(block) {
case "long", "counter_long" -> new PositionToXContent(block) {
@Override
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
throws IOException {
return builder.value(((LongBlock) block).getLong(valueIndex));
}
};
case "integer" -> new PositionToXContent(block) {
case "integer", "counter_integer" -> new PositionToXContent(block) {
@Override
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
throws IOException {
return builder.value(((IntBlock) block).getInt(valueIndex));
}
};
case "double" -> new PositionToXContent(block) {
case "double", "counter_double" -> new PositionToXContent(block) {
@Override
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ static Object valueAtPosition(Block block, int position, String dataType, BytesR
private static Object valueAt(String dataType, Block block, int offset, BytesRef scratch) {
return switch (dataType) {
case "unsigned_long" -> unsignedLongAsNumber(((LongBlock) block).getLong(offset));
case "long" -> ((LongBlock) block).getLong(offset);
case "integer" -> ((IntBlock) block).getInt(offset);
case "double" -> ((DoubleBlock) block).getDouble(offset);
case "long", "counter_long" -> ((LongBlock) block).getLong(offset);
case "integer", "counter_integer" -> ((IntBlock) block).getInt(offset);
case "double", "counter_double" -> ((DoubleBlock) block).getDouble(offset);
case "keyword", "text" -> ((BytesRefBlock) block).getBytesRef(offset, scratch).utf8ToString();
case "ip" -> {
BytesRef val = ((BytesRefBlock) block).getBytesRef(offset, scratch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.xpack.esql.expression.function.Example;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.InvalidArgumentException;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
Expand Down Expand Up @@ -42,7 +43,8 @@ public class ToDouble extends AbstractConvertFunction {
Map.entry(TEXT, ToDoubleFromStringEvaluator.Factory::new),
Map.entry(UNSIGNED_LONG, ToDoubleFromUnsignedLongEvaluator.Factory::new),
Map.entry(LONG, ToDoubleFromLongEvaluator.Factory::new), // CastLongToDoubleEvaluator would be a candidate, but not MV'd
Map.entry(INTEGER, ToDoubleFromIntEvaluator.Factory::new) // CastIntToDoubleEvaluator would be a candidate, but not MV'd
Map.entry(INTEGER, ToDoubleFromIntEvaluator.Factory::new), // CastIntToDoubleEvaluator would be a candidate, but not MV'd
Map.entry(EsqlDataTypes.COUNTER_DOUBLE, (field, source) -> field)
);

@FunctionInfo(
Expand All @@ -65,7 +67,7 @@ public ToDouble(
Source source,
@Param(
name = "field",
type = { "boolean", "date", "keyword", "text", "double", "long", "unsigned_long", "integer" },
type = { "boolean", "date", "keyword", "text", "double", "long", "unsigned_long", "integer", "counter_double" },
description = "Input value. The input can be a single- or multi-valued column or an expression."
) Expression field
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.xpack.esql.expression.function.Example;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.InvalidArgumentException;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
Expand Down Expand Up @@ -43,7 +44,8 @@ public class ToInteger extends AbstractConvertFunction {
Map.entry(TEXT, ToIntegerFromStringEvaluator.Factory::new),
Map.entry(DOUBLE, ToIntegerFromDoubleEvaluator.Factory::new),
Map.entry(UNSIGNED_LONG, ToIntegerFromUnsignedLongEvaluator.Factory::new),
Map.entry(LONG, ToIntegerFromLongEvaluator.Factory::new)
Map.entry(LONG, ToIntegerFromLongEvaluator.Factory::new),
Map.entry(EsqlDataTypes.COUNTER_INTEGER, (fieldEval, source) -> fieldEval)
);

@FunctionInfo(
Expand All @@ -68,7 +70,7 @@ public ToInteger(
Source source,
@Param(
name = "field",
type = { "boolean", "date", "keyword", "text", "double", "long", "unsigned_long", "integer" },
type = { "boolean", "date", "keyword", "text", "double", "long", "unsigned_long", "integer", "counter_integer" },
description = "Input value. The input can be a single- or multi-valued column or an expression."
) Expression field
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.xpack.esql.expression.function.Example;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.InvalidArgumentException;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
Expand Down Expand Up @@ -43,7 +44,8 @@ public class ToLong extends AbstractConvertFunction {
Map.entry(TEXT, ToLongFromStringEvaluator.Factory::new),
Map.entry(DOUBLE, ToLongFromDoubleEvaluator.Factory::new),
Map.entry(UNSIGNED_LONG, ToLongFromUnsignedLongEvaluator.Factory::new),
Map.entry(INTEGER, ToLongFromIntEvaluator.Factory::new) // CastIntToLongEvaluator would be a candidate, but not MV'd
Map.entry(INTEGER, ToLongFromIntEvaluator.Factory::new), // CastIntToLongEvaluator would be a candidate, but not MV'd
Map.entry(EsqlDataTypes.COUNTER_LONG, (field, source) -> field)
);

@FunctionInfo(
Expand All @@ -67,7 +69,7 @@ public ToLong(
Source source,
@Param(
name = "field",
type = { "boolean", "date", "keyword", "text", "double", "long", "unsigned_long", "integer" },
type = { "boolean", "date", "keyword", "text", "double", "long", "unsigned_long", "integer", "counter_long" },
description = "Input value. The input can be a single- or multi-valued column or an expression."
) Expression field
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ private PhysicalOperation planTopN(TopNExec topNExec, LocalExecutionPlannerConte
case "text", "keyword" -> TopNEncoder.UTF8;
case "version" -> TopNEncoder.VERSION;
case "boolean", "null", "byte", "short", "integer", "long", "double", "float", "half_float", "datetime", "date_period",
"time_duration", "object", "nested", "scaled_float", "unsigned_long", "_doc" -> TopNEncoder.DEFAULT_SORTABLE;
"time_duration", "object", "nested", "scaled_float", "unsigned_long", "_doc", "counter_integer", "counter_long",
"counter_double" -> TopNEncoder.DEFAULT_SORTABLE;
case "geo_point", "cartesian_point", "geo_shape", "cartesian_shape" -> TopNEncoder.DEFAULT_UNSORTABLE;
// unsupported fields are encoded as BytesRef, we'll use the same encoder; all values should be null at this point
case "unsupported" -> TopNEncoder.UNSUPPORTED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,16 @@ public static ElementType toElementType(DataType dataType) {
* For example, spatial types can be extracted into doc-values under specific conditions, otherwise they extract as BytesRef.
*/
public static ElementType toElementType(DataType dataType, MappedFieldType.FieldExtractPreference fieldExtractPreference) {
if (dataType == DataTypes.LONG || dataType == DataTypes.DATETIME || dataType == DataTypes.UNSIGNED_LONG) {
if (dataType == DataTypes.LONG
|| dataType == DataTypes.DATETIME
|| dataType == DataTypes.UNSIGNED_LONG
|| dataType == EsqlDataTypes.COUNTER_LONG) {
return ElementType.LONG;
}
if (dataType == DataTypes.INTEGER) {
if (dataType == DataTypes.INTEGER || dataType == EsqlDataTypes.COUNTER_INTEGER) {
return ElementType.INT;
}
if (dataType == DataTypes.DOUBLE) {
if (dataType == DataTypes.DOUBLE || dataType == EsqlDataTypes.COUNTER_DOUBLE) {
return ElementType.DOUBLE;
}
// unsupported fields are passed through as a BytesRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public class EsqlFeatures implements FeatureSpecification {
*/
public static final NodeFeature MV_ORDERING_SORTED_ASCENDING = new NodeFeature("esql.mv_ordering_sorted_ascending");

/**
* Support for metrics counter fields
*/
public static final NodeFeature METRICS_COUNTER_FIELDS = new NodeFeature("esql.metrics_counter_fields");

@Override
public Set<NodeFeature> getFeatures() {
return Set.of(
Expand All @@ -139,7 +144,8 @@ public Set<NodeFeature> getFeatures() {
ST_DISJOINT,
STRING_LITERAL_AUTO_CASTING,
CASTING_OPERATOR,
MV_ORDERING_SORTED_ASCENDING
MV_ORDERING_SORTED_ASCENDING,
METRICS_COUNTER_FIELDS
);
}

Expand Down

0 comments on commit 84cd4ee

Please sign in to comment.