Commit 3e4f21c
Fix Arrow field metadata not available for queries with 0 rows (databricks#1177)
### Problem
When executing queries that return 0 rows (e.g., `WHERE 1=0`), complex
types (ARRAY, MAP, STRUCT) showed only generic type names instead of
detailed type information:
**Before:**
- `ARRAY` instead of `ARRAY<INT>`
- `MAP` instead of `MAP<STRING,STRING>`
- `STRUCT` instead of `STRUCT<field: TYPE>`
**After:**
- Detailed type information is correctly preserved for all row counts
### Root Cause
In `AbstractArrowResultChunk.java`, Arrow field metadata was only
extracted inside the `while(arrowStreamReader.loadNextBatch())` loop.
For queries with 0 rows, no batches are loaded, so the loop never
executes and metadata is never extracted.
**Code location:**
`/src/main/java/com/databricks/jdbc/api/impl/arrow/AbstractArrowResultChunk.java:338-359`
### Solution
Extract metadata from `VectorSchemaRoot` immediately after obtaining it,
**before** the `loadNextBatch()` loop.
The Arrow IPC format always sends the schema message first (before any
record batches), so field metadata is available even when there are 0
rows. `VectorSchemaRoot` contains field vectors with metadata regardless
of row count.
**Key changes:**
1. Moved metadata extraction from inside the while loop to before it
2. Added defensive null checks for `VectorSchemaRoot` and field vectors
3. Added debug logging to track metadata extraction
### Testing
#### Unit Test Coverage
- ✅ Added `testMetadataExtractionWithZeroRows()` to
`ArrowResultChunkTest`
- ✅ Verifies Arrow field metadata is extracted correctly with 0 rows
- ✅ Tests complex types: `ARRAY<INT>`, `MAP<STRING,STRING>`
- ✅ All 2,693 unit tests pass
#### Manual Verification
Tested with queries returning 0 rows:
```sql
SELECT array_col, map_col, struct_col
FROM table
WHERE 1=0
Result: Metadata now correctly shows detailed type information
Impact
- Scope: Both SQL Exec API and Thrift Server (shared code path)
- Risk: Low - backward compatible change, only affects metadata
extraction timing
- Benefits:
- Fixes schema discovery for WHERE 1=0 pattern
- Improves metadata availability for empty result sets
- Aligns with Arrow IPC specification behavior
Additional Context
- Arrow IPC specification guarantees schema is sent before record
batches
- VectorSchemaRoot.getFieldVectors() is available immediately after
ArrowStreamReader.getVectorSchemaRoot()
- No performance impact: metadata extraction is now done once upfront
instead of conditionally on first batch
---------
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 0e8b1ad commit 3e4f21c
3 files changed
Lines changed: 70 additions & 5 deletions
File tree
- src
- main/java/com/databricks/jdbc/api/impl/arrow
- test/java/com/databricks/jdbc/api/impl/arrow
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
Lines changed: 15 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
340 | | - | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
341 | 355 | | |
342 | 356 | | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | 357 | | |
348 | 358 | | |
349 | 359 | | |
| |||
Lines changed: 54 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
261 | 263 | | |
262 | 264 | | |
263 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
264 | 318 | | |
0 commit comments