Skip to content

Commit 6ed3a59

Browse files
committed
test: align protocol 3.1 partition key fixture
1 parent a913331 commit 6ed3a59

1 file changed

Lines changed: 44 additions & 3 deletions

File tree

‎vgi-example-worker/src/main/java/farm/query/vgi/example/table/SplitFunctions.java‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import farm.query.vgi.internal.BatchUtil;
99
import farm.query.vgi.internal.EmitMetadata;
1010
import farm.query.vgi.protocol.ScanSplit;
11+
import farm.query.vgi.protocol.FieldPath;
12+
import farm.query.vgi.protocol.KeyGrouping;
13+
import farm.query.vgi.protocol.TransformSpec;
1114
import farm.query.vgi.pushdown.PushdownFilter;
1215
import farm.query.vgi.pushdown.PushdownFilters;
1316
import farm.query.vgi.table.PlanRequest;
@@ -19,6 +22,7 @@
1922
import farm.query.vgi.types.Schemas;
2023
import farm.query.vgirpc.CallContext;
2124
import farm.query.vgirpc.OutputCollector;
25+
import farm.query.vgirpc.marshal.RecordCodec;
2226
import org.apache.arrow.vector.BigIntVector;
2327
import org.apache.arrow.vector.VarCharVector;
2428
import org.apache.arrow.vector.types.pojo.Schema;
@@ -913,6 +917,7 @@ public static final class SplitPartitioned implements TableFunction {
913917
.withSplits()
914918
.withPartitionKind(FunctionMetadata.PartitionKind.SINGLE_VALUE_PARTITIONS))
915919
.named("rows_per_country", Schemas.INT64, "5")
920+
.named("key_mode", Schemas.UTF8, "valid")
916921
.build();
917922

918923
@Override public farm.query.vgi.function.FunctionSpec spec() { return SPEC; }
@@ -925,12 +930,48 @@ public static final class SplitPartitioned implements TableFunction {
925930
/** Names each partition by INDEX, so a redemption reads the same
926931
* partition however many times it runs and in whichever process. */
927932
@Override public PlanResult plan(TableBindParams params, PlanRequest request) {
933+
String keyMode = params.arguments().namedString("key_mode", "valid");
928934
List<ScanSplit> splits = new ArrayList<>(COUNTRIES.length);
929935
for (int i = 0; i < COUNTRIES.length; i++) {
930-
splits.add(new ScanSplit(encode(new Range(i, i)), new byte[0],
931-
null, false, null, null, null, null, null, null));
936+
ScanSplit split = ScanSplit.of(encode(new Range(i, i)));
937+
byte[] keyValue = keyValue(COUNTRIES[i], keyMode);
938+
splits.add(keyValue == null ? split : split.withKeyValue(keyValue));
939+
}
940+
byte[] path = RecordCodec.serializeToBytes(new FieldPath(List.of("country")));
941+
byte[] transform = RecordCodec.serializeToBytes(
942+
new TransformSpec("vgi.identity/1", List.of(path), null));
943+
return PlanResult.of(splits).withKeyGrouping(
944+
RecordCodec.serializeToBytes(new KeyGrouping(List.of(transform))));
945+
}
946+
947+
private static byte[] keyValue(String country, String mode) {
948+
if ("missing".equals(mode)) return null;
949+
if (!List.of("valid", "two_rows", "wrong_type").contains(mode)) {
950+
throw new IllegalArgumentException(
951+
"unknown split_partitioned key_mode: " + mode);
952+
}
953+
boolean wrongType = "wrong_type".equals(mode);
954+
int rows = "two_rows".equals(mode) ? 2 : 1;
955+
Schema schema = wrongType
956+
? Schemas.of(Schemas.nullable("country", Schemas.INT64))
957+
: Schemas.of(Schemas.nullable("country", Schemas.UTF8));
958+
try (org.apache.arrow.vector.VectorSchemaRoot root =
959+
org.apache.arrow.vector.VectorSchemaRoot.create(
960+
schema, farm.query.vgirpc.wire.Allocators.root())) {
961+
root.allocateNew();
962+
if (wrongType) {
963+
BigIntVector values = (BigIntVector) root.getVector("country");
964+
values.setSafe(0, 1L);
965+
values.setValueCount(1);
966+
} else {
967+
VarCharVector values = (VarCharVector) root.getVector("country");
968+
byte[] text = country.getBytes(StandardCharsets.UTF_8);
969+
for (int i = 0; i < rows; i++) values.setSafe(i, text);
970+
values.setValueCount(rows);
971+
}
972+
root.setRowCount(rows);
973+
return BatchUtil.writeSingleBatch(root);
932974
}
933-
return PlanResult.of(splits);
934975
}
935976

936977
@Override public TableProducerState createProducer(TableInitParams params) {

0 commit comments

Comments
 (0)