Sfoglia il codice sorgente

ES|QL enable FloatBlock serialization (#109858)

This commit enables ES|QL FloatBlock serialization.
Chris Hegarty 1 anno fa
parent
commit
d5488ad78d

+ 1 - 0
x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/Block.java

@@ -276,6 +276,7 @@ public interface Block extends Accountable, BlockLoader.Block, NamedWriteable, R
         return List.of(
             IntBlock.ENTRY,
             LongBlock.ENTRY,
+            FloatBlock.ENTRY,
             DoubleBlock.ENTRY,
             BytesRefBlock.ENTRY,
             BooleanBlock.ENTRY,

+ 14 - 6
x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BasicPageTests.java

@@ -136,14 +136,15 @@ public class BasicPageTests extends SerializationTestCase {
         int blockCount = randomIntBetween(1, 256);
         Block[] blocks = new Block[blockCount];
         for (int blockIndex = 0; blockIndex < blockCount; blockIndex++) {
-            blocks[blockIndex] = switch (randomInt(6)) {
+            blocks[blockIndex] = switch (randomInt(7)) {
                 case 0 -> blockFactory.newIntArrayVector(randomInts(positions).toArray(), positions).asBlock();
                 case 1 -> blockFactory.newLongArrayVector(randomLongs(positions).toArray(), positions).asBlock();
-                case 2 -> blockFactory.newDoubleArrayVector(randomDoubles(positions).toArray(), positions).asBlock();
-                case 3 -> blockFactory.newConstantIntBlockWith(randomInt(), positions);
-                case 4 -> blockFactory.newConstantLongBlockWith(randomLong(), positions);
-                case 5 -> blockFactory.newConstantDoubleBlockWith(randomDouble(), positions);
-                case 6 -> blockFactory.newConstantBytesRefBlockWith(new BytesRef(Integer.toHexString(randomInt())), positions);
+                case 2 -> blockFactory.newFloatArrayVector(randomFloats(positions), positions).asBlock();
+                case 3 -> blockFactory.newDoubleArrayVector(randomDoubles(positions).toArray(), positions).asBlock();
+                case 4 -> blockFactory.newConstantIntBlockWith(randomInt(), positions);
+                case 5 -> blockFactory.newConstantLongBlockWith(randomLong(), positions);
+                case 6 -> blockFactory.newConstantDoubleBlockWith(randomDouble(), positions);
+                case 7 -> blockFactory.newConstantBytesRefBlockWith(new BytesRef(Integer.toHexString(randomInt())), positions);
                 default -> throw new AssertionError();
             };
         }
@@ -184,6 +185,7 @@ public class BasicPageTests extends SerializationTestCase {
         Page origPage = new Page(
             blockFactory.newIntArrayVector(IntStream.range(0, 10).toArray(), 10).asBlock(),
             blockFactory.newLongArrayVector(LongStream.range(10, 20).toArray(), 10).asBlock(),
+            blockFactory.newFloatArrayVector(randomFloats(10), 10).asBlock(),
             blockFactory.newDoubleArrayVector(LongStream.range(30, 40).mapToDouble(i -> i).toArray(), 10).asBlock(),
             blockFactory.newBytesRefArrayVector(bytesRefArrayOf("0a", "1b", "2c", "3d", "4e", "5f", "6g", "7h", "8i", "9j"), 10).asBlock(),
             blockFactory.newConstantIntBlockWith(randomInt(), 10),
@@ -248,4 +250,10 @@ public class BasicPageTests extends SerializationTestCase {
         Arrays.stream(values).map(BytesRef::new).forEach(array::append);
         return array;
     }
+
+    float[] randomFloats(int size) {
+        float[] fa = new float[size];
+        IntStream.range(0, size).forEach(i -> fa[i] = randomFloat());
+        return fa;
+    }
 }

+ 31 - 0
x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BlockSerializationTests.java

@@ -39,6 +39,10 @@ public class BlockSerializationTests extends SerializationTestCase {
         assertConstantBlockImpl(blockFactory.newConstantLongBlockWith(randomLong(), randomIntBetween(1, 8192)));
     }
 
+    public void testConstantFloatBlock() throws IOException {
+        assertConstantBlockImpl(blockFactory.newConstantFloatBlockWith(randomFloat(), randomIntBetween(1, 8192)));
+    }
+
     public void testConstantDoubleBlock() throws IOException {
         assertConstantBlockImpl(blockFactory.newConstantDoubleBlockWith(randomDouble(), randomIntBetween(1, 8192)));
     }
@@ -81,6 +85,17 @@ public class BlockSerializationTests extends SerializationTestCase {
         }
     }
 
+    public void testEmptyFloatBlock() throws IOException {
+        assertEmptyBlock(blockFactory.newFloatBlockBuilder(0).build());
+        try (FloatBlock toFilter = blockFactory.newFloatBlockBuilder(0).appendNull().build()) {
+            assertEmptyBlock(toFilter.filter());
+        }
+        assertEmptyBlock(blockFactory.newFloatVectorBuilder(0).build().asBlock());
+        try (FloatVector toFilter = blockFactory.newFloatVectorBuilder(0).appendFloat(randomFloat()).build()) {
+            assertEmptyBlock(toFilter.filter().asBlock());
+        }
+    }
+
     public void testEmptyDoubleBlock() throws IOException {
         assertEmptyBlock(blockFactory.newDoubleBlockBuilder(0).build());
         try (DoubleBlock toFilter = blockFactory.newDoubleBlockBuilder(0).appendNull().build()) {
@@ -140,6 +155,22 @@ public class BlockSerializationTests extends SerializationTestCase {
         }
     }
 
+    public void testFilterFloatBlock() throws IOException {
+        try (FloatBlock toFilter = blockFactory.newFloatBlockBuilder(0).appendFloat(1).appendFloat(2).build()) {
+            assertFilterBlock(toFilter.filter(1));
+        }
+        try (FloatBlock toFilter = blockFactory.newFloatBlockBuilder(1).appendFloat(randomFloat()).appendNull().build()) {
+            assertFilterBlock(toFilter.filter(0));
+        }
+        try (FloatVector toFilter = blockFactory.newFloatVectorBuilder(1).appendFloat(randomFloat()).build()) {
+            assertFilterBlock(toFilter.filter(0).asBlock());
+
+        }
+        try (FloatVector toFilter = blockFactory.newFloatVectorBuilder(1).appendFloat(randomFloat()).appendFloat(randomFloat()).build()) {
+            assertFilterBlock(toFilter.filter(0).asBlock());
+        }
+    }
+
     public void testFilterDoubleBlock() throws IOException {
         try (DoubleBlock toFilter = blockFactory.newDoubleBlockBuilder(0).appendDouble(1).appendDouble(2).build()) {
             assertFilterBlock(toFilter.filter(1));