|
21 | 21 |
|
22 | 22 | import java.util.ArrayList; |
23 | 23 | import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.OptionalLong; |
24 | 26 |
|
25 | 27 | import org.apache.arrow.c.ArrowArray; |
26 | 28 | import org.apache.arrow.c.ArrowArrayStream; |
|
33 | 35 | import org.apache.datafusion.ColumnarValue; |
34 | 36 | import org.apache.datafusion.ScalarFunction; |
35 | 37 | import org.apache.datafusion.ScalarFunctionArgs; |
| 38 | +import org.apache.datafusion.ScanTableProvider; |
36 | 39 | import org.apache.datafusion.TableProvider; |
| 40 | +import org.apache.datafusion.TableScan; |
| 41 | +import org.apache.datafusion.TableScanRequest; |
| 42 | +import org.apache.datafusion.protobuf.LogicalExprNode; |
| 43 | +import org.apache.datafusion.protobuf.TableScanOptionsProto; |
37 | 44 |
|
38 | | -/** Internal trampoline invoked from native code on every UDF call. Not part of the public API. */ |
| 45 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 46 | + |
| 47 | +/** Internal trampolines for native UDF and table-provider callbacks. Not part of the public API. */ |
39 | 48 | public final class JniBridge { |
40 | 49 | /** |
41 | | - * Shared allocator for UDF inputs/outputs. Created once at class-load time; never closed. |
42 | | - * Outstanding allocations are released by the FFI structs' release callbacks when the native side |
43 | | - * drops them after {@code from_ffi}. |
| 50 | + * Shared allocator for UDF inputs/outputs and table scans. Created once at class-load time; never |
| 51 | + * closed. Outstanding allocations are released by the FFI structs' release callbacks when the |
| 52 | + * native side drops them after {@code from_ffi}. |
44 | 53 | */ |
45 | 54 | private static final RootAllocator ALLOCATOR = new RootAllocator(); |
46 | 55 |
|
@@ -143,20 +152,74 @@ public static byte invokeScalarUdf( |
143 | 152 | } |
144 | 153 | } |
145 | 154 |
|
| 155 | + /** Plan one physical scan without opening its reader. Called from native code. */ |
| 156 | + public static TableScan planTableScan( |
| 157 | + TableProvider provider, byte[] optionsBytes, byte[][] filterProtos) |
| 158 | + throws InvalidProtocolBufferException { |
| 159 | + if (!(provider instanceof ScanTableProvider planned)) { |
| 160 | + return new TableScan(provider::scan); |
| 161 | + } |
| 162 | + TableScanOptionsProto options = TableScanOptionsProto.parseFrom(optionsBytes); |
| 163 | + Optional<List<Integer>> projection = |
| 164 | + options.hasProjection() |
| 165 | + ? Optional.of(options.getProjection().getIndicesList()) |
| 166 | + : Optional.empty(); |
| 167 | + TableScan scan = |
| 168 | + planned.planScan( |
| 169 | + new TableScanRequest( |
| 170 | + projection, |
| 171 | + parseFilters(filterProtos), |
| 172 | + options.hasLimit() ? OptionalLong.of(options.getLimit()) : OptionalLong.empty())); |
| 173 | + if (scan == null) { |
| 174 | + throw new IllegalStateException("ScanTableProvider.planScan returned null"); |
| 175 | + } |
| 176 | + return scan; |
| 177 | + } |
| 178 | + |
| 179 | + /** Report per-expression capabilities using the native adapter's closed numeric contract. */ |
| 180 | + public static int[] supportsTableFilterPushdown(TableProvider provider, byte[][] filterProtos) |
| 181 | + throws InvalidProtocolBufferException { |
| 182 | + List<ScanTableProvider.FilterPushdown> capabilities = |
| 183 | + ((ScanTableProvider) provider).supportsFiltersPushdown(parseFilters(filterProtos)); |
| 184 | + if (capabilities == null || capabilities.size() != filterProtos.length) { |
| 185 | + throw new IllegalArgumentException( |
| 186 | + "supportsFiltersPushdown must return one capability per filter"); |
| 187 | + } |
| 188 | + int[] result = new int[capabilities.size()]; |
| 189 | + for (int i = 0; i < result.length; i++) { |
| 190 | + result[i] = |
| 191 | + switch (capabilities.get(i)) { |
| 192 | + case UNSUPPORTED -> 1; |
| 193 | + case INEXACT -> 2; |
| 194 | + case EXACT -> 3; |
| 195 | + }; |
| 196 | + } |
| 197 | + return result; |
| 198 | + } |
| 199 | + |
| 200 | + private static List<LogicalExprNode> parseFilters(byte[][] filterProtos) |
| 201 | + throws InvalidProtocolBufferException { |
| 202 | + List<LogicalExprNode> filters = new ArrayList<>(filterProtos.length); |
| 203 | + for (byte[] filter : filterProtos) { |
| 204 | + filters.add(LogicalExprNode.parseFrom(filter)); |
| 205 | + } |
| 206 | + return filters; |
| 207 | + } |
| 208 | + |
146 | 209 | /** |
147 | 210 | * Open a fresh batch stream from a Java {@link TableProvider} and export it through the supplied |
148 | 211 | * Arrow C Data Interface address. Called from native code; not for application use. |
149 | 212 | * |
150 | | - * <p>{@link TableProvider#scan(org.apache.arrow.memory.BufferAllocator)} is called with {@link |
| 213 | + * <p>{@link TableScan#execute(org.apache.arrow.memory.BufferAllocator)} is called with {@link |
151 | 214 | * #ALLOCATOR} so that the reader's buffers share the same allocator root required by {@link |
152 | 215 | * Data#exportArrayStream}. |
153 | 216 | * |
154 | 217 | * <p>On success, ownership of the returned reader transfers to the FFI stream's release callback, |
155 | 218 | * so the native side closing the stream also closes the reader. On any failure during export, the |
156 | 219 | * reader is closed here before the exception propagates. |
157 | 220 | */ |
158 | | - public static void invokeTableScan(TableProvider provider, long ffiStreamAddr) { |
159 | | - ArrowReader reader = provider.scan(ALLOCATOR); |
| 221 | + public static void invokeTableScan(TableScan scan, long ffiStreamAddr) { |
| 222 | + ArrowReader reader = scan.execute(ALLOCATOR); |
160 | 223 | if (reader == null) { |
161 | 224 | throw new IllegalStateException("TableProvider.scan returned null"); |
162 | 225 | } |
|
0 commit comments