|
| 1 | +// Copyright 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +package farm.query.vgi.example.table; |
| 4 | + |
| 5 | +import farm.query.vgi.function.FunctionMetadata; |
| 6 | +import farm.query.vgi.function.FunctionSpec; |
| 7 | +import farm.query.vgi.internal.BatchUtil; |
| 8 | +import farm.query.vgi.protocol.FunctionExample; |
| 9 | +import farm.query.vgi.table.SimpleTableFunction; |
| 10 | +import farm.query.vgi.table.TableInitParams; |
| 11 | +import farm.query.vgi.table.TableProducerState; |
| 12 | +import farm.query.vgi.types.Schemas; |
| 13 | +import farm.query.vgirpc.CallContext; |
| 14 | +import farm.query.vgirpc.OutputCollector; |
| 15 | +import org.apache.arrow.vector.VarCharVector; |
| 16 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 17 | +import org.apache.arrow.vector.util.Text; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +/** Same-named scan functions backing same-named declarative tables in two schemas. */ |
| 22 | +public final class SameNameTableFunctions { |
| 23 | + |
| 24 | + private SameNameTableFunctions() {} |
| 25 | + |
| 26 | + /** Deliberately colliding scan-function name. */ |
| 27 | + public static final String FUNCTION_NAME = "test_same_name_table_scan"; |
| 28 | + /** Deliberately colliding declarative table name. */ |
| 29 | + public static final String TABLE_NAME = "test_same_name_table"; |
| 30 | + /** Shared one-column output schema. */ |
| 31 | + public static final Schema OUTPUT = Schemas.of(Schemas.nullable("tag", Schemas.UTF8)); |
| 32 | + |
| 33 | + abstract static class Tagging extends SimpleTableFunction { |
| 34 | + abstract String owningSchema(); |
| 35 | + |
| 36 | + @Override protected Schema outputSchema() { return OUTPUT; } |
| 37 | + |
| 38 | + @Override public TableProducerState createProducer(TableInitParams params) { |
| 39 | + return new State(owningSchema()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** One-shot schema-tagged producer. */ |
| 44 | + public static final class State extends TableProducerState { |
| 45 | + /** Value identifying the function's owning schema. */ |
| 46 | + public String tag; |
| 47 | + /** Whether the row has already been emitted. */ |
| 48 | + public boolean done; |
| 49 | + |
| 50 | + /** Required for continuation-state deserialization. */ |
| 51 | + public State() {} |
| 52 | + |
| 53 | + State(String tag) { this.tag = tag; } |
| 54 | + |
| 55 | + @Override public void produceTick(OutputCollector out, CallContext ctx) { |
| 56 | + if (done) { out.finish(); return; } |
| 57 | + done = true; |
| 58 | + BatchUtil.emit(OUTPUT, 1, out, (root, rows, ignored) -> |
| 59 | + ((VarCharVector) root.getVector("tag")).setSafe(0, new Text(tag))); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** Main-schema implementation. */ |
| 64 | + public static final class MainSchema extends Tagging { |
| 65 | + private static final FunctionSpec SPEC = FunctionSpec.builder(FUNCTION_NAME) |
| 66 | + .metadata(FunctionMetadata.describe( |
| 67 | + "Schema-disambiguation probe; the main-schema table producer") |
| 68 | + .withCategories("generator", "testing") |
| 69 | + .withExamples(List.of(new FunctionExample( |
| 70 | + "SELECT * FROM example.main.test_same_name_table", |
| 71 | + "One row tagged 'main'", null)))) |
| 72 | + .build(); |
| 73 | + |
| 74 | + @Override public FunctionSpec spec() { return SPEC; } |
| 75 | + @Override String owningSchema() { return "main"; } |
| 76 | + } |
| 77 | + |
| 78 | + /** Data-schema implementation. */ |
| 79 | + public static final class DataSchema extends Tagging { |
| 80 | + private static final FunctionSpec SPEC = FunctionSpec.builder(FUNCTION_NAME) |
| 81 | + .metadata(FunctionMetadata.describe( |
| 82 | + "Schema-disambiguation probe; the data-schema table producer") |
| 83 | + .withCategories("generator", "testing") |
| 84 | + .withExamples(List.of(new FunctionExample( |
| 85 | + "SELECT * FROM example.data.test_same_name_table", |
| 86 | + "One row tagged 'data'", null)))) |
| 87 | + .build(); |
| 88 | + |
| 89 | + @Override public FunctionSpec spec() { return SPEC; } |
| 90 | + @Override String owningSchema() { return "data"; } |
| 91 | + } |
| 92 | +} |
0 commit comments