|
| 1 | +// Copyright 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +package farm.query.vgi.internal; |
| 4 | + |
| 5 | +import farm.query.vgi.buffering.TableBufferingCombineParams; |
| 6 | +import farm.query.vgi.buffering.TableBufferingFinalizeParams; |
| 7 | +import farm.query.vgi.buffering.TableBufferingFunction; |
| 8 | +import farm.query.vgi.buffering.TableBufferingProcessParams; |
| 9 | +import farm.query.vgi.function.ArgSpec; |
| 10 | +import farm.query.vgi.function.FunctionMetadata; |
| 11 | +import farm.query.vgi.protocol.BindResponse; |
| 12 | +import farm.query.vgi.protocol.FunctionInfo; |
| 13 | +import farm.query.vgi.protocol.FunctionRequiredSecret; |
| 14 | +import farm.query.vgi.scalar.ScalarBindParams; |
| 15 | +import farm.query.vgi.scalar.ScalarFunction; |
| 16 | +import farm.query.vgi.scalar.ScalarProcessParams; |
| 17 | +import farm.query.vgi.table.TableProducerState; |
| 18 | +import farm.query.vgi.tableinout.TableInOutBindParams; |
| 19 | +import farm.query.vgi.tableinout.TableInOutExchangeState; |
| 20 | +import farm.query.vgi.tableinout.TableInOutFunction; |
| 21 | +import farm.query.vgi.tableinout.TableInOutInitParams; |
| 22 | +import org.apache.arrow.memory.BufferAllocator; |
| 23 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +/** |
| 34 | + * Every function kind, not only table and aggregate, can declare required |
| 35 | + * secrets, and the declaration reaches {@link FunctionInfo#required_secrets()} |
| 36 | + * (the extension resolves them before bind for every kind). Before, scalar, |
| 37 | + * table-in-out and buffering functions had no hook and always advertised an |
| 38 | + * empty list. |
| 39 | + */ |
| 40 | +class FunctionInfoRequiredSecretsAllKindsTest { |
| 41 | + |
| 42 | + private static final List<FunctionRequiredSecret> DECLARED = List.of( |
| 43 | + FunctionRequiredSecret.ofType("vgi_example"), |
| 44 | + FunctionRequiredSecret.scoped("s3", "s3://bucket/"), |
| 45 | + FunctionRequiredSecret.named("vgi_example", "mine")); |
| 46 | + |
| 47 | + private static List<FunctionRequiredSecret> withNull() { |
| 48 | + List<FunctionRequiredSecret> l = new ArrayList<>(DECLARED); |
| 49 | + l.add(null); |
| 50 | + return l; |
| 51 | + } |
| 52 | + |
| 53 | + static class Scalar implements ScalarFunction { |
| 54 | + final List<FunctionRequiredSecret> req; |
| 55 | + Scalar(List<FunctionRequiredSecret> req) { this.req = req; } |
| 56 | + @Override public String name() { return "scalar_secret"; } |
| 57 | + @Override public FunctionMetadata metadata() { return FunctionMetadata.describe("doc"); } |
| 58 | + @Override public List<ArgSpec> argumentSpecs() { return List.of(); } |
| 59 | + @Override public List<FunctionRequiredSecret> requiredSecrets() { return req; } |
| 60 | + @Override public BindResponse onBind(ScalarBindParams p) { throw new UnsupportedOperationException(); } |
| 61 | + @Override public VectorSchemaRoot process(ScalarProcessParams p, VectorSchemaRoot in, BufferAllocator a) { |
| 62 | + throw new UnsupportedOperationException(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static class InOut implements TableInOutFunction { |
| 67 | + final List<FunctionRequiredSecret> req; |
| 68 | + InOut(List<FunctionRequiredSecret> req) { this.req = req; } |
| 69 | + @Override public String name() { return "tio_secret"; } |
| 70 | + @Override public FunctionMetadata metadata() { return FunctionMetadata.describe("doc"); } |
| 71 | + @Override public List<ArgSpec> argumentSpecs() { return List.of(); } |
| 72 | + @Override public List<FunctionRequiredSecret> requiredSecrets() { return req; } |
| 73 | + @Override public BindResponse onBind(TableInOutBindParams p) { throw new UnsupportedOperationException(); } |
| 74 | + @Override public TableInOutExchangeState createExchange(TableInOutInitParams p) { |
| 75 | + throw new UnsupportedOperationException(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static class Buffering implements TableBufferingFunction { |
| 80 | + final List<FunctionRequiredSecret> req; |
| 81 | + Buffering(List<FunctionRequiredSecret> req) { this.req = req; } |
| 82 | + @Override public String name() { return "buffering_secret"; } |
| 83 | + @Override public FunctionMetadata metadata() { return FunctionMetadata.describe("doc"); } |
| 84 | + @Override public List<ArgSpec> argumentSpecs() { return List.of(); } |
| 85 | + @Override public List<FunctionRequiredSecret> requiredSecrets() { return req; } |
| 86 | + @Override public BindResponse onBind(TableInOutBindParams p) { throw new UnsupportedOperationException(); } |
| 87 | + @Override public byte[] process(VectorSchemaRoot b, TableBufferingProcessParams p) { |
| 88 | + throw new UnsupportedOperationException(); |
| 89 | + } |
| 90 | + @Override public List<byte[]> combine(List<byte[]> ids, TableBufferingCombineParams p) { |
| 91 | + throw new UnsupportedOperationException(); |
| 92 | + } |
| 93 | + @Override public TableProducerState createFinalizeProducer(TableBufferingFinalizeParams p) { |
| 94 | + throw new UnsupportedOperationException(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void scalarDeclarationReachesFunctionInfo() { |
| 100 | + assertEquals(DECLARED, VgiServiceImpl.scalarFunctionInfo(new Scalar(DECLARED), "main").required_secrets()); |
| 101 | + assertTrue(VgiServiceImpl.scalarFunctionInfo(new Scalar(List.of()), "main").required_secrets().isEmpty()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void tableInOutDeclarationReachesFunctionInfo() { |
| 106 | + assertEquals(DECLARED, VgiServiceImpl.toTableInOutFunctionInfo(new InOut(DECLARED), "main").required_secrets()); |
| 107 | + assertTrue(VgiServiceImpl.toTableInOutFunctionInfo(new InOut(List.of()), "main").required_secrets().isEmpty()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void bufferingDeclarationReachesFunctionInfo() { |
| 112 | + assertEquals(DECLARED, VgiServiceImpl.toBufferingFunctionInfo(new Buffering(DECLARED), "main").required_secrets()); |
| 113 | + assertTrue(VgiServiceImpl.toBufferingFunctionInfo(new Buffering(List.of()), "main").required_secrets().isEmpty()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void nullEntryIsAnErrorForEveryKind() { |
| 118 | + assertThrows(IllegalArgumentException.class, |
| 119 | + () -> VgiServiceImpl.scalarFunctionInfo(new Scalar(withNull()), "main")); |
| 120 | + assertThrows(IllegalArgumentException.class, |
| 121 | + () -> VgiServiceImpl.toTableInOutFunctionInfo(new InOut(withNull()), "main")); |
| 122 | + assertThrows(IllegalArgumentException.class, |
| 123 | + () -> VgiServiceImpl.toBufferingFunctionInfo(new Buffering(withNull()), "main")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void declarationSurvivesSerializationForEveryKind() { |
| 128 | + for (FunctionInfo info : List.of( |
| 129 | + VgiServiceImpl.scalarFunctionInfo(new Scalar(DECLARED), "main"), |
| 130 | + VgiServiceImpl.toTableInOutFunctionInfo(new InOut(DECLARED), "main"), |
| 131 | + VgiServiceImpl.toBufferingFunctionInfo(new Buffering(DECLARED), "main"))) { |
| 132 | + try (VectorSchemaRoot encoded = BatchUtil.readSingleBatch( |
| 133 | + FunctionInfoSerializer.serialize(info), farm.query.vgirpc.wire.Allocators.root())) { |
| 134 | + List<?> items = ((org.apache.arrow.vector.complex.ListVector) |
| 135 | + encoded.getVector("required_secrets")).getObject(0); |
| 136 | + assertEquals(3, items.size(), info.name()); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments