Skip to content

Commit 87ad0fa

Browse files
committed
Basic change + things requires for the XContent parser
1 parent 68eff34 commit 87ad0fa

File tree

12 files changed

+66
-38
lines changed

12 files changed

+66
-38
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/DataType.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
* </ul>
141141
*/
142142
public enum DataType {
143+
143144
/**
144145
* Fields of this type are unsupported by any functions and are always
145146
* rendered as {@code null} in the response.
@@ -182,7 +183,15 @@ public enum DataType {
182183
* aggregation, and casting to their parent numeric type.
183184
*/
184185
COUNTER_DOUBLE(builder().esType("counter_double").estimatedSize(Double.BYTES).docValues().counter()),
185-
186+
/**
187+
* 32-bit floating point numbers labeled as metric counters in time-series indices.
188+
* Although stored internally as numeric fields, they represent cumulative
189+
* metrics and must not be treated as regular numeric fields. Therefore,
190+
* we define them differently and separately from their parent numeric field.
191+
* These fields are strictly for use in retrieval from indices, rate
192+
* aggregation, and casting to their parent numeric type.
193+
*/
194+
COUNTER_FLOAT(builder().esType("counter_float").estimatedSize(Float.BYTES).docValues().counter()),
186195
/**
187196
* 64-bit signed numbers loaded as a java {@code long}.
188197
*/
@@ -217,7 +226,9 @@ public enum DataType {
217226
* Values of this type never escape type resolution and functions,
218227
* operators, and results should never encounter one.
219228
*/
220-
FLOAT(builder().esType("float").estimatedSize(Float.BYTES).rationalNumber().docValues().widenSmallNumeric(DOUBLE)),
229+
FLOAT(
230+
builder().esType("float").estimatedSize(Float.BYTES).rationalNumber().docValues().counter(COUNTER_FLOAT).widenSmallNumeric(DOUBLE)
231+
),
221232
/**
222233
* 16-bit floating point numbers widened on load to {@link #DOUBLE}.
223234
* Values of this type never escape type resolution and functions,
@@ -559,7 +570,7 @@ public static boolean isRepresentable(DataType t) {
559570
}
560571

561572
public static boolean isCounter(DataType t) {
562-
return t == COUNTER_DOUBLE || t == COUNTER_INTEGER || t == COUNTER_LONG;
573+
return Set.of(COUNTER_DOUBLE, COUNTER_FLOAT, COUNTER_INTEGER, COUNTER_LONG).contains(t);
563574
}
564575

565576
public static boolean isSpatialPoint(DataType t) {
@@ -654,7 +665,10 @@ public boolean isCounter() {
654665
* widen it into, otherwise this returns {@code this}.
655666
*/
656667
public DataType widenSmallNumeric() {
657-
return widenSmallNumeric == null ? this : widenSmallNumeric;
668+
return switch (this) {
669+
case FLOAT -> NATIVE_FLOATS_FEATURE_FLAG ? FLOAT : DOUBLE;
670+
default -> widenSmallNumeric == null ? this : widenSmallNumeric;
671+
};
658672
}
659673

660674
/**
@@ -843,4 +857,6 @@ Builder counter(DataType counter) {
843857
return this;
844858
}
845859
}
860+
861+
public static final boolean NATIVE_FLOATS_FEATURE_FLAG = true;
846862
}

x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/EvaluatorImplementer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static org.elasticsearch.compute.gen.Types.DRIVER_CONTEXT;
4545
import static org.elasticsearch.compute.gen.Types.EXPRESSION_EVALUATOR;
4646
import static org.elasticsearch.compute.gen.Types.EXPRESSION_EVALUATOR_FACTORY;
47+
import static org.elasticsearch.compute.gen.Types.FLOAT_BLOCK;
4748
import static org.elasticsearch.compute.gen.Types.INT_BLOCK;
4849
import static org.elasticsearch.compute.gen.Types.LONG_BLOCK;
4950
import static org.elasticsearch.compute.gen.Types.PAGE;
@@ -1091,6 +1092,7 @@ static boolean isBlockType(TypeName type) {
10911092
return type.equals(INT_BLOCK)
10921093
|| type.equals(LONG_BLOCK)
10931094
|| type.equals(DOUBLE_BLOCK)
1095+
|| type.equals(FLOAT_BLOCK)
10941096
|| type.equals(BOOLEAN_BLOCK)
10951097
|| type.equals(BYTES_REF_BLOCK);
10961098
}

x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/Methods.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import static org.elasticsearch.compute.gen.Types.DOUBLE_VECTOR_BUILDER;
4040
import static org.elasticsearch.compute.gen.Types.DOUBLE_VECTOR_FIXED_BUILDER;
4141
import static org.elasticsearch.compute.gen.Types.FLOAT_BLOCK_BUILDER;
42+
import static org.elasticsearch.compute.gen.Types.FLOAT_VECTOR;
4243
import static org.elasticsearch.compute.gen.Types.FLOAT_VECTOR_BUILDER;
4344
import static org.elasticsearch.compute.gen.Types.FLOAT_VECTOR_FIXED_BUILDER;
4445
import static org.elasticsearch.compute.gen.Types.INT_BLOCK;
@@ -226,7 +227,7 @@ static String appendMethod(TypeName t) {
226227
if (t.equals(TypeName.DOUBLE) || t.equals(DOUBLE_BLOCK) || t.equals(DOUBLE_VECTOR)) {
227228
return "appendDouble";
228229
}
229-
if (t.equals(TypeName.FLOAT) || t.equals(FLOAT_BLOCK_BUILDER)) {
230+
if (t.equals(TypeName.FLOAT) || t.equals(FLOAT_BLOCK_BUILDER) || t.equals(FLOAT_VECTOR)) {
230231
return "appendFloat";
231232
}
232233
throw new IllegalArgumentException("unknown append method for [" + t + "]");

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/EsqlTestUtils.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@
165165

166166
public final class EsqlTestUtils {
167167

168-
public static final Literal ONE = new Literal(Source.EMPTY, 1, DataType.INTEGER);
169-
public static final Literal TWO = new Literal(Source.EMPTY, 2, DataType.INTEGER);
170-
public static final Literal THREE = new Literal(Source.EMPTY, 3, DataType.INTEGER);
171-
public static final Literal FOUR = new Literal(Source.EMPTY, 4, DataType.INTEGER);
172-
public static final Literal FIVE = new Literal(Source.EMPTY, 5, DataType.INTEGER);
173-
public static final Literal SIX = new Literal(Source.EMPTY, 6, DataType.INTEGER);
168+
public static final Literal ONE = new Literal(EMPTY, 1, INTEGER);
169+
public static final Literal TWO = new Literal(EMPTY, 2, INTEGER);
170+
public static final Literal THREE = new Literal(EMPTY, 3, INTEGER);
171+
public static final Literal FOUR = new Literal(EMPTY, 4, INTEGER);
172+
public static final Literal FIVE = new Literal(EMPTY, 5, INTEGER);
173+
public static final Literal SIX = new Literal(EMPTY, 6, INTEGER);
174174

175175
public static Equals equalsOf(Expression left, Expression right) {
176176
return new Equals(EMPTY, left, right, null);
@@ -181,19 +181,19 @@ public static LessThan lessThanOf(Expression left, Expression right) {
181181
}
182182

183183
public static GreaterThan greaterThanOf(Expression left, Expression right) {
184-
return new GreaterThan(EMPTY, left, right, ESTestCase.randomZone());
184+
return new GreaterThan(EMPTY, left, right, randomZone());
185185
}
186186

187187
public static NotEquals notEqualsOf(Expression left, Expression right) {
188-
return new NotEquals(EMPTY, left, right, ESTestCase.randomZone());
188+
return new NotEquals(EMPTY, left, right, randomZone());
189189
}
190190

191191
public static LessThanOrEqual lessThanOrEqualOf(Expression left, Expression right) {
192-
return new LessThanOrEqual(EMPTY, left, right, ESTestCase.randomZone());
192+
return new LessThanOrEqual(EMPTY, left, right, randomZone());
193193
}
194194

195195
public static GreaterThanOrEqual greaterThanOrEqualOf(Expression left, Expression right) {
196-
return new GreaterThanOrEqual(EMPTY, left, right, ESTestCase.randomZone());
196+
return new GreaterThanOrEqual(EMPTY, left, right, randomZone());
197197
}
198198

199199
public static FieldAttribute getFieldAttribute() {
@@ -217,7 +217,7 @@ public static FieldAttribute fieldAttribute(String name, DataType type) {
217217
}
218218

219219
public static Literal of(Object value) {
220-
return of(Source.EMPTY, value);
220+
return of(EMPTY, value);
221221
}
222222

223223
/**
@@ -455,11 +455,11 @@ public static Literal L(Object value) {
455455
}
456456

457457
public static LogicalPlan emptySource() {
458-
return new LocalRelation(Source.EMPTY, emptyList(), EmptyLocalSupplier.EMPTY);
458+
return new LocalRelation(EMPTY, emptyList(), EmptyLocalSupplier.EMPTY);
459459
}
460460

461461
public static LogicalPlan localSource(BlockFactory blockFactory, List<Attribute> fields, List<Object> row) {
462-
return new LocalRelation(Source.EMPTY, fields, LocalSupplier.of(BlockUtils.fromListRow(blockFactory, row)));
462+
return new LocalRelation(EMPTY, fields, LocalSupplier.of(BlockUtils.fromListRow(blockFactory, row)));
463463
}
464464

465465
public static <T> T as(Object node, Class<T> type) {
@@ -619,10 +619,7 @@ public static Map<String, Map<String, Column>> tables() {
619619
BytesRefBlock namesBlock = names.build();
620620
tables.put(
621621
"int_number_names",
622-
table(
623-
Map.entry("int", new Column(DataType.INTEGER, intsBlock)),
624-
Map.entry("name", new Column(DataType.KEYWORD, namesBlock))
625-
)
622+
table(Map.entry("int", new Column(INTEGER, intsBlock)), Map.entry("name", new Column(DataType.KEYWORD, namesBlock)))
626623
);
627624
tables.put(
628625
"long_number_names",
@@ -682,8 +679,8 @@ public static Map<String, Map<String, Column>> tables() {
682679
table(
683680
Map.entry("aa", new Column(DataType.KEYWORD, aa.build())),
684681
Map.entry("ab", new Column(DataType.KEYWORD, ab.build())),
685-
Map.entry("na", new Column(DataType.INTEGER, na.build())),
686-
Map.entry("nb", new Column(DataType.INTEGER, nb.build()))
682+
Map.entry("na", new Column(INTEGER, na.build())),
683+
Map.entry("nb", new Column(INTEGER, nb.build()))
687684
)
688685
);
689686
}
@@ -816,7 +813,7 @@ public static Tuple<String, String> pathAndName(String string) {
816813
* Generate a random value of the appropriate type to fit into blocks of {@code e}.
817814
*/
818815
public static Literal randomLiteral(DataType type) {
819-
return new Literal(Source.EMPTY, switch (type) {
816+
return new Literal(EMPTY, switch (type) {
820817
case BOOLEAN -> randomBoolean();
821818
case BYTE -> randomByte();
822819
case SHORT -> randomShort();
@@ -827,7 +824,7 @@ public static Literal randomLiteral(DataType type) {
827824
case DATETIME -> randomMillisUpToYear9999();
828825
case DATE_NANOS -> randomLongBetween(0, Long.MAX_VALUE);
829826
case DOUBLE, SCALED_FLOAT, COUNTER_DOUBLE -> randomDouble();
830-
case FLOAT -> randomFloat();
827+
case FLOAT, COUNTER_FLOAT -> randomFloat();
831828
case HALF_FLOAT -> HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat()));
832829
case KEYWORD -> new BytesRef(randomAlphaOfLength(5));
833830
case IP -> new BytesRef(InetAddressPoint.encode(randomIp(randomBoolean())));

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/PositionToXContent.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Pa
8484
return builder.value(((DoubleBlock) block).getDouble(valueIndex));
8585
}
8686
};
87+
case FLOAT, COUNTER_FLOAT -> new PositionToXContent(block) {
88+
@Override
89+
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
90+
throws IOException {
91+
return builder.value(((FloatBlock) block).getFloat(valueIndex));
92+
}
93+
};
8794
case UNSIGNED_LONG -> new PositionToXContent(block) {
8895
@Override
8996
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
@@ -190,8 +197,8 @@ protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Pa
190197
return builder.value(((FloatBlock) block).getFloat(valueIndex));
191198
}
192199
};
193-
case DATE_PERIOD, TIME_DURATION, DOC_DATA_TYPE, TSID_DATA_TYPE, SHORT, BYTE, OBJECT, FLOAT, HALF_FLOAT, SCALED_FLOAT,
194-
PARTIAL_AGG -> throw new IllegalArgumentException("can't convert values of type [" + columnInfo.type() + "]");
200+
case DATE_PERIOD, TIME_DURATION, DOC_DATA_TYPE, TSID_DATA_TYPE, SHORT, BYTE, OBJECT, HALF_FLOAT, SCALED_FLOAT, PARTIAL_AGG ->
201+
throw new IllegalArgumentException("can't convert values of type [" + columnInfo.type() + "]");
195202
};
196203
}
197204
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/ResponseValueUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ private static Object valueAt(DataType dataType, Block block, int offset, BytesR
117117
case LONG, COUNTER_LONG -> ((LongBlock) block).getLong(offset);
118118
case INTEGER, COUNTER_INTEGER -> ((IntBlock) block).getInt(offset);
119119
case DOUBLE, COUNTER_DOUBLE -> ((DoubleBlock) block).getDouble(offset);
120+
case FLOAT, COUNTER_FLOAT -> ((FloatBlock) block).getFloat(offset);
120121
case KEYWORD, TEXT -> ((BytesRefBlock) block).getBytesRef(offset, scratch).utf8ToString();
121122
case IP -> {
122123
BytesRef val = ((BytesRefBlock) block).getBytesRef(offset, scratch);
@@ -149,7 +150,7 @@ private static Object valueAt(DataType dataType, Block block, int offset, BytesR
149150
}
150151
}
151152
case DENSE_VECTOR -> ((FloatBlock) block).getFloat(offset);
152-
case SHORT, BYTE, FLOAT, HALF_FLOAT, SCALED_FLOAT, OBJECT, DATE_PERIOD, TIME_DURATION, DOC_DATA_TYPE, TSID_DATA_TYPE, NULL,
153+
case SHORT, BYTE, HALF_FLOAT, SCALED_FLOAT, OBJECT, DATE_PERIOD, TIME_DURATION, DOC_DATA_TYPE, TSID_DATA_TYPE, NULL,
153154
PARTIAL_AGG -> throw EsqlIllegalArgumentException.illegalDataType(dataType);
154155
};
155156
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/nulls/Coalesce.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
209209
case KEYWORD, TEXT, CARTESIAN_POINT, CARTESIAN_SHAPE, GEO_POINT, GEO_SHAPE, IP, VERSION -> CoalesceBytesRefEvaluator
210210
.toEvaluator(toEvaluator, children());
211211
case NULL -> EvalOperator.CONSTANT_NULL_FACTORY;
212-
case UNSUPPORTED, SHORT, BYTE, DATE_PERIOD, OBJECT, DOC_DATA_TYPE, SOURCE, TIME_DURATION, FLOAT, HALF_FLOAT, TSID_DATA_TYPE,
213-
SCALED_FLOAT, PARTIAL_AGG, AGGREGATE_METRIC_DOUBLE, DENSE_VECTOR -> throw new UnsupportedOperationException(
212+
case UNSUPPORTED, SHORT, BYTE, DATE_PERIOD, OBJECT, DOC_DATA_TYPE, SOURCE, TIME_DURATION, FLOAT, HALF_FLOAT, COUNTER_FLOAT,
213+
TSID_DATA_TYPE, SCALED_FLOAT, PARTIAL_AGG, AGGREGATE_METRIC_DOUBLE, DENSE_VECTOR -> throw new UnsupportedOperationException(
214214
dataType() + " can’t be coalesced"
215215
);
216216
};

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/LocalExecutionPlanner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ private PhysicalOperation planTopN(TopNExec topNExec, LocalExecutionPlannerConte
490490
case VERSION -> TopNEncoder.VERSION;
491491
case BOOLEAN, NULL, BYTE, SHORT, INTEGER, LONG, DOUBLE, FLOAT, HALF_FLOAT, DATETIME, DATE_NANOS, DATE_PERIOD, TIME_DURATION,
492492
OBJECT, SCALED_FLOAT, UNSIGNED_LONG, DOC_DATA_TYPE, TSID_DATA_TYPE -> TopNEncoder.DEFAULT_SORTABLE;
493-
case GEO_POINT, CARTESIAN_POINT, GEO_SHAPE, CARTESIAN_SHAPE, COUNTER_LONG, COUNTER_INTEGER, COUNTER_DOUBLE, SOURCE,
494-
AGGREGATE_METRIC_DOUBLE, DENSE_VECTOR -> TopNEncoder.DEFAULT_UNSORTABLE;
493+
case GEO_POINT, CARTESIAN_POINT, GEO_SHAPE, CARTESIAN_SHAPE, COUNTER_LONG, COUNTER_INTEGER, COUNTER_DOUBLE, COUNTER_FLOAT,
494+
SOURCE, AGGREGATE_METRIC_DOUBLE, DENSE_VECTOR -> TopNEncoder.DEFAULT_UNSORTABLE;
495495
// unsupported fields are encoded as BytesRef, we'll use the same encoder; all values should be null at this point
496496
case PARTIAL_AGG, UNSUPPORTED -> TopNEncoder.UNSUPPORTED;
497497
};

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/PlannerUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ public static ElementType toElementType(DataType dataType, MappedFieldType.Field
320320
case LONG, DATETIME, DATE_NANOS, UNSIGNED_LONG, COUNTER_LONG -> ElementType.LONG;
321321
case INTEGER, COUNTER_INTEGER -> ElementType.INT;
322322
case DOUBLE, COUNTER_DOUBLE -> ElementType.DOUBLE;
323+
case FLOAT, COUNTER_FLOAT -> ElementType.FLOAT;
323324
// unsupported fields are passed through as a BytesRef
324325
case KEYWORD, TEXT, IP, SOURCE, VERSION, UNSUPPORTED -> ElementType.BYTES_REF;
325326
case NULL -> ElementType.NULL;
@@ -331,7 +332,7 @@ public static ElementType toElementType(DataType dataType, MappedFieldType.Field
331332
case PARTIAL_AGG -> ElementType.COMPOSITE;
332333
case AGGREGATE_METRIC_DOUBLE -> ElementType.AGGREGATE_METRIC_DOUBLE;
333334
case DENSE_VECTOR -> ElementType.FLOAT;
334-
case SHORT, BYTE, DATE_PERIOD, TIME_DURATION, OBJECT, FLOAT, HALF_FLOAT, SCALED_FLOAT -> throw EsqlIllegalArgumentException
335+
case SHORT, BYTE, DATE_PERIOD, TIME_DURATION, OBJECT, HALF_FLOAT, SCALED_FLOAT -> throw EsqlIllegalArgumentException
335336
.illegalDataType(dataType);
336337
};
337338
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ private Page randomPage(List<ColumnInfoImpl> columns) {
213213
case UNSIGNED_LONG, LONG, COUNTER_LONG -> ((LongBlock.Builder) builder).appendLong(randomLong());
214214
case INTEGER, COUNTER_INTEGER -> ((IntBlock.Builder) builder).appendInt(randomInt());
215215
case DOUBLE, COUNTER_DOUBLE -> ((DoubleBlock.Builder) builder).appendDouble(randomDouble());
216+
case FLOAT, COUNTER_FLOAT -> ((FloatBlock.Builder) builder).appendFloat(randomFloat());
216217
case KEYWORD -> ((BytesRefBlock.Builder) builder).appendBytesRef(new BytesRef(randomAlphaOfLength(10)));
217218
case TEXT -> ((BytesRefBlock.Builder) builder).appendBytesRef(new BytesRef(randomAlphaOfLength(10000)));
218219
case IP -> ((BytesRefBlock.Builder) builder).appendBytesRef(
@@ -1175,6 +1176,7 @@ static Page valuesToPage(BlockFactory blockFactory, List<ColumnInfoImpl> columns
11751176
case LONG, COUNTER_LONG -> ((LongBlock.Builder) builder).appendLong(((Number) value).longValue());
11761177
case INTEGER, COUNTER_INTEGER -> ((IntBlock.Builder) builder).appendInt(((Number) value).intValue());
11771178
case DOUBLE, COUNTER_DOUBLE -> ((DoubleBlock.Builder) builder).appendDouble(((Number) value).doubleValue());
1179+
case FLOAT -> ((FloatBlock.Builder) builder).appendFloat(((Number) value).floatValue());
11781180
case KEYWORD, TEXT -> ((BytesRefBlock.Builder) builder).appendBytesRef(new BytesRef(value.toString()));
11791181
case UNSUPPORTED -> ((BytesRefBlock.Builder) builder).appendNull();
11801182
case IP -> ((BytesRefBlock.Builder) builder).appendBytesRef(stringToIP(value.toString()));

0 commit comments

Comments
 (0)