|
| 1 | +// Copyright 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +package farm.query.vgi.example.table; |
| 4 | + |
| 5 | +import farm.query.vgi.Secrets; |
| 6 | +import farm.query.vgi.function.FunctionMetadata; |
| 7 | +import farm.query.vgi.function.FunctionSpec; |
| 8 | +import farm.query.vgi.internal.BatchUtil; |
| 9 | +import farm.query.vgi.internal.SchemaUtil; |
| 10 | +import farm.query.vgi.protocol.BindResponse; |
| 11 | +import farm.query.vgi.protocol.FunctionRequiredSecret; |
| 12 | +import farm.query.vgi.table.SimpleTableFunction; |
| 13 | +import farm.query.vgi.table.TableBindParams; |
| 14 | +import farm.query.vgi.table.TableFunction; |
| 15 | +import farm.query.vgi.table.TableInitParams; |
| 16 | +import farm.query.vgi.table.TableProducerState; |
| 17 | +import farm.query.vgi.types.Schemas; |
| 18 | +import farm.query.vgirpc.CallContext; |
| 19 | +import farm.query.vgirpc.OutputCollector; |
| 20 | +import org.apache.arrow.vector.BitVector; |
| 21 | +import org.apache.arrow.vector.VarCharVector; |
| 22 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 23 | +import org.apache.arrow.vector.util.Text; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * Secret-lookup conformance fixtures: the five table functions the shared |
| 31 | + * {@code test/sql/integration/secret/secret_lookup_*.test} files run against |
| 32 | + * every SDK's example worker. The contract (names, declared requirements, |
| 33 | + * output columns, rendering) is the vgi repo's |
| 34 | + * {@code docs/secret_lookup_fixtures.md}; this mirrors vgi-python's |
| 35 | + * {@code vgi/_test_fixtures/secret_lookup.py}. |
| 36 | + * |
| 37 | + * <p>Every function reads {@code vgi_example} secrets and selects them through |
| 38 | + * the public {@link Secrets} selectors (by type / by name / by scope), never by |
| 39 | + * indexing the batch's columns: the columns are keyed by secret name.</p> |
| 40 | + */ |
| 41 | +public final class SecretLookupFixtures { |
| 42 | + |
| 43 | + private SecretLookupFixtures() {} |
| 44 | + |
| 45 | + /** The secret type every value fixture reads (registered by the example worker). */ |
| 46 | + public static final String SECRET_TYPE = "vgi_example"; |
| 47 | + /** Outer static scope of {@code secret_value_for_scope}. */ |
| 48 | + public static final String SCOPE_A = "vgi-conformance://a/"; |
| 49 | + /** Inner static scope of {@code secret_value_for_scope}. */ |
| 50 | + public static final String SCOPE_AB = "vgi-conformance://a/b/"; |
| 51 | + /** Scope of the {@code s3} requirement of {@code secret_types_seen}. */ |
| 52 | + public static final String S3_SCOPE = "s3://vgi-conformance/"; |
| 53 | + |
| 54 | + private static final Schema VALUE_SCHEMA = Schemas.of( |
| 55 | + Schemas.nullable("found", Schemas.BOOL), |
| 56 | + Schemas.nullable("secret_name", Schemas.UTF8), |
| 57 | + Schemas.nullable("value", Schemas.UTF8)); |
| 58 | + private static final byte[] VALUE_SCHEMA_IPC = SchemaUtil.serializeSchema(VALUE_SCHEMA); |
| 59 | + |
| 60 | + private static final Schema SEEN_SCHEMA = Schemas.of( |
| 61 | + Schemas.nullable("secret_name", Schemas.UTF8), |
| 62 | + Schemas.nullable("secret_type", Schemas.UTF8), |
| 63 | + Schemas.nullable("scope", Schemas.UTF8)); |
| 64 | + |
| 65 | + /** Every fixture, for registration in the example catalog. */ |
| 66 | + public static List<TableFunction> functions() { |
| 67 | + return List.of(new ByType(), new ByName(), new ForScope(), new Required(), new TypesSeen()); |
| 68 | + } |
| 69 | + |
| 70 | + private static String arg(TableInitParams p, int i) { |
| 71 | + Object o = p.arguments().positional().size() > i ? p.arguments().positionalAt(i) : null; |
| 72 | + return o == null ? null : o.toString(); |
| 73 | + } |
| 74 | + |
| 75 | + private static String arg(TableBindParams p, int i) { |
| 76 | + Object o = p.arguments().positional().size() > i ? p.arguments().positionalAt(i) : null; |
| 77 | + return o == null ? null : o.toString(); |
| 78 | + } |
| 79 | + |
| 80 | + private static FunctionSpec.Builder specBuilder(String name, String description) { |
| 81 | + return FunctionSpec.builder(name) |
| 82 | + .metadata(FunctionMetadata.describe(description).withCategories("secret", "testing")); |
| 83 | + } |
| 84 | + |
| 85 | + /** The output row for a selected secret (or its absence). */ |
| 86 | + static ValueState stateFor(Map<String, String> secret, String field) { |
| 87 | + if (secret == null) return new ValueState(); |
| 88 | + ValueState s = new ValueState(); |
| 89 | + s.found = true; |
| 90 | + s.secretName = secret.get("name"); |
| 91 | + s.value = field == null ? null : secret.get(field); |
| 92 | + return s; |
| 93 | + } |
| 94 | + |
| 95 | + /** One {@code (found, secret_name, value)} row. Public fields for the HTTP state token. */ |
| 96 | + public static final class ValueState extends TableProducerState { |
| 97 | + /** Whether a secret was selected. */ |
| 98 | + public boolean found; |
| 99 | + /** The selected secret's name, or null. */ |
| 100 | + public String secretName; |
| 101 | + /** The selected field rendered as text, or null. */ |
| 102 | + public String value; |
| 103 | + /** Whether the row has been emitted. */ |
| 104 | + public boolean done; |
| 105 | + |
| 106 | + /** Required no-arg constructor for state deserialization. */ |
| 107 | + public ValueState() {} |
| 108 | + |
| 109 | + @Override public void produceTick(OutputCollector out, CallContext ctx) { |
| 110 | + if (done) { out.finish(); return; } |
| 111 | + done = true; |
| 112 | + boolean f = found; |
| 113 | + String n = secretName; |
| 114 | + String v = value; |
| 115 | + BatchUtil.emit(VALUE_SCHEMA, 1, out, (root, rows, ignored) -> { |
| 116 | + ((BitVector) root.getVector("found")).setSafe(0, f ? 1 : 0); |
| 117 | + VarCharVector nv = (VarCharVector) root.getVector("secret_name"); |
| 118 | + if (n == null) nv.setNull(0); else nv.setSafe(0, new Text(n)); |
| 119 | + VarCharVector vv = (VarCharVector) root.getVector("value"); |
| 120 | + if (v == null) vv.setNull(0); else vv.setSafe(0, new Text(v)); |
| 121 | + }); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** {@code secret_value_by_type(field)}: static simplest-form requirement, selected by type. */ |
| 126 | + public static final class ByType extends SimpleTableFunction { |
| 127 | + private static final FunctionSpec SPEC = specBuilder("secret_value_by_type", |
| 128 | + "Conformance: a field of the vgi_example secret, selected by type") |
| 129 | + .arg("field", Schemas.UTF8).build(); |
| 130 | + |
| 131 | + @Override public FunctionSpec spec() { return SPEC; } |
| 132 | + @Override protected Schema outputSchema() { return VALUE_SCHEMA; } |
| 133 | + @Override public List<FunctionRequiredSecret> requiredSecrets() { |
| 134 | + return List.of(FunctionRequiredSecret.ofType(SECRET_TYPE)); |
| 135 | + } |
| 136 | + @Override public TableProducerState createProducer(TableInitParams p) { |
| 137 | + return stateFor(Secrets.parse(p.secrets()).lookup(SECRET_TYPE).orElse(null), arg(p, 0)); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** {@code secret_value_required(field)}: as by-type, but the bind fails when absent. */ |
| 142 | + public static final class Required implements TableFunction { |
| 143 | + private static final FunctionSpec SPEC = specBuilder("secret_value_required", |
| 144 | + "Conformance: a field of the required vgi_example secret; errors when absent") |
| 145 | + .arg("field", Schemas.UTF8).build(); |
| 146 | + |
| 147 | + @Override public FunctionSpec spec() { return SPEC; } |
| 148 | + @Override public List<FunctionRequiredSecret> requiredSecrets() { |
| 149 | + return List.of(FunctionRequiredSecret.ofType(SECRET_TYPE)); |
| 150 | + } |
| 151 | + @Override public BindResponse onBind(TableBindParams p) { |
| 152 | + Secrets.parse(p.secrets()).require(SECRET_TYPE); |
| 153 | + return BindResponse.forSchema(VALUE_SCHEMA_IPC); |
| 154 | + } |
| 155 | + @Override public TableProducerState createProducer(TableInitParams p) { |
| 156 | + return stateFor(Secrets.parse(p.secrets()).require(SECRET_TYPE), arg(p, 0)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** {@code secret_value_by_name(name, field)}: requested by name in a two-phase bind. */ |
| 161 | + public static final class ByName implements TableFunction { |
| 162 | + private static final FunctionSpec SPEC = specBuilder("secret_value_by_name", |
| 163 | + "Conformance: a field of the named vgi_example secret") |
| 164 | + .arg("name", Schemas.UTF8).arg("field", Schemas.UTF8).build(); |
| 165 | + |
| 166 | + @Override public FunctionSpec spec() { return SPEC; } |
| 167 | + @Override public BindResponse onBind(TableBindParams p) { |
| 168 | + String name = arg(p, 0); |
| 169 | + if (!p.resolvedSecretsProvided() && name != null) { |
| 170 | + return new BindResponse(VALUE_SCHEMA_IPC, new byte[0], |
| 171 | + List.of(SECRET_TYPE), List.of(""), List.of(name)); |
| 172 | + } |
| 173 | + return BindResponse.forSchema(VALUE_SCHEMA_IPC); |
| 174 | + } |
| 175 | + @Override public TableProducerState createProducer(TableInitParams p) { |
| 176 | + String name = arg(p, 0); |
| 177 | + Map<String, String> secret = name == null ? null |
| 178 | + : Secrets.parse(p.secrets()).lookupNamed(SECRET_TYPE, name).orElse(null); |
| 179 | + return stateFor(secret, arg(p, 1)); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** {@code secret_value_for_scope(path, field)}: three static same-type requirements, longest prefix. */ |
| 184 | + public static final class ForScope extends SimpleTableFunction { |
| 185 | + private static final FunctionSpec SPEC = specBuilder("secret_value_for_scope", |
| 186 | + "Conformance: a field of the vgi_example secret whose scope best matches a path") |
| 187 | + .arg("path", Schemas.UTF8).arg("field", Schemas.UTF8).build(); |
| 188 | + |
| 189 | + @Override public FunctionSpec spec() { return SPEC; } |
| 190 | + @Override protected Schema outputSchema() { return VALUE_SCHEMA; } |
| 191 | + @Override public List<FunctionRequiredSecret> requiredSecrets() { |
| 192 | + return List.of( |
| 193 | + FunctionRequiredSecret.ofType(SECRET_TYPE), |
| 194 | + FunctionRequiredSecret.scoped(SECRET_TYPE, SCOPE_A), |
| 195 | + FunctionRequiredSecret.scoped(SECRET_TYPE, SCOPE_AB)); |
| 196 | + } |
| 197 | + @Override public TableProducerState createProducer(TableInitParams p) { |
| 198 | + String path = arg(p, 0); |
| 199 | + Map<String, String> secret = path == null ? null |
| 200 | + : Secrets.parse(p.secrets()).forScopeOfType(path, SECRET_TYPE).orElse(null); |
| 201 | + return stateFor(secret, arg(p, 1)); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /** {@code secret_types_seen()}: requires vgi_example and scoped s3; one row per delivered secret. */ |
| 206 | + public static final class TypesSeen extends SimpleTableFunction { |
| 207 | + private static final FunctionSpec SPEC = specBuilder("secret_types_seen", |
| 208 | + "Conformance: the secrets delivered for a vgi_example + s3 requirement").build(); |
| 209 | + |
| 210 | + @Override public FunctionSpec spec() { return SPEC; } |
| 211 | + @Override protected Schema outputSchema() { return SEEN_SCHEMA; } |
| 212 | + @Override public List<FunctionRequiredSecret> requiredSecrets() { |
| 213 | + return List.of( |
| 214 | + FunctionRequiredSecret.ofType(SECRET_TYPE), |
| 215 | + FunctionRequiredSecret.scoped("s3", S3_SCOPE)); |
| 216 | + } |
| 217 | + @Override public TableProducerState createProducer(TableInitParams p) { |
| 218 | + Secrets secrets = Secrets.parse(p.secrets()); |
| 219 | + SeenState s = new SeenState(); |
| 220 | + for (String name : new java.util.TreeSet<>(secrets.byName().keySet())) { |
| 221 | + s.names.add(name); |
| 222 | + s.types.add(secrets.secretType(name).orElse(null)); |
| 223 | + s.scopes.add(secrets.namedField(name, "scope").orElse("")); |
| 224 | + } |
| 225 | + return s; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + /** One {@code (secret_name, secret_type, scope)} row per delivered secret. */ |
| 230 | + public static final class SeenState extends TableProducerState { |
| 231 | + /** Delivered secret names. */ |
| 232 | + public List<String> names = new ArrayList<>(); |
| 233 | + /** Their {@code type} fields. */ |
| 234 | + public List<String> types = new ArrayList<>(); |
| 235 | + /** Their {@code scope} fields. */ |
| 236 | + public List<String> scopes = new ArrayList<>(); |
| 237 | + /** Whether the rows have been emitted. */ |
| 238 | + public boolean done; |
| 239 | + |
| 240 | + /** Required no-arg constructor for state deserialization. */ |
| 241 | + public SeenState() {} |
| 242 | + |
| 243 | + @Override public void produceTick(OutputCollector out, CallContext ctx) { |
| 244 | + if (done || names.isEmpty()) { out.finish(); return; } |
| 245 | + done = true; |
| 246 | + List<String> n = names, t = types, sc = scopes; |
| 247 | + BatchUtil.emit(SEEN_SCHEMA, n.size(), out, (root, rows, ignored) -> { |
| 248 | + VarCharVector nv = (VarCharVector) root.getVector("secret_name"); |
| 249 | + VarCharVector tv = (VarCharVector) root.getVector("secret_type"); |
| 250 | + VarCharVector sv = (VarCharVector) root.getVector("scope"); |
| 251 | + for (int i = 0; i < rows; i++) { |
| 252 | + nv.setSafe(i, new Text(n.get(i))); |
| 253 | + if (t.get(i) == null) tv.setNull(i); else tv.setSafe(i, new Text(t.get(i))); |
| 254 | + sv.setSafe(i, new Text(sc.get(i) == null ? "" : sc.get(i))); |
| 255 | + } |
| 256 | + }); |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments