Skip to content

Fix Arrow field metadata not available for queries with 0 rows - #1177

Merged
gopalldb merged 13 commits into
databricks:mainfrom
gopalldb:sea-roll
Jan 16, 2026
Merged

gopalldb merged 13 commits into
databricks:mainfrom
gopalldb:sea-roll

Conversation

@gopalldb

@gopalldb gopalldb commented Jan 12, 2026 •

Copy link
Copy Markdown
Collaborator

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:

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

gopalldb and others added 11 commits January 9, 2026 15:25
Problem:
The custom user agent from 'useragententry' parameter was not included
in the connector service feature flags request. This happened because
UserAgentManager.setUserAgent() was setting the custom user agent AFTER
triggering the client type determination (which fetches feature flags).

Root Cause:
In UserAgentManager.setUserAgent():
- Line 28 called getClientUserAgent() which triggered getClientType()
- getClientType() called DatabricksDriverFeatureFlagsContextFactory.getInstance()
- Feature flags were fetched WITHOUT the custom user agent
- Custom user agent was set afterwards (too late)

Solution:
Reordered UserAgentManager.setUserAgent() to set custom user agent
BEFORE calling getClientUserAgent():
1. Set base product
2. Set custom user agent FIRST via UserAgent.withOtherInfo()
3. THEN call getClientUserAgent() which triggers feature flags fetch

Result:
Custom user agent is now included in ALL HTTP requests including
the connector service request.

Testing:
- Added testCustomUserAgentIncludedBeforeClientTypeEvaluation() test
- Updated existing tests to be version-agnostic
- All UserAgentManagerTest tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
- Added buildUserAgentForConnectorService() to build separate UA for connector service
- Modified DatabricksDriverFeatureFlagsContext to use new method for feature flag requests
- Maintains proper order for backend requests: base -> client type -> custom
- Custom UA now included in both connector service and backend requests without circular dependency
- Updated test to verify proper order: client type before custom user agent
- Added DEBUG log when SEA circuit breaker is open
- Shows time remaining until circuit closes
- Helps users understand THRIFT fallback due to 429 rate limits
- Created parseCustomerUserAgent() helper method
- Eliminates code duplication in setUserAgent() and buildUserAgentForConnectorService()
- Added try-catch protection for UserAgent.sanitize() calls
- All tests pass (7/7)
Changed catch blocks to catch IllegalArgumentException instead of generic
Exception when calling UserAgent.sanitize(). This is the specific exception
thrown by sanitize() when it encounters invalid characters in user agent strings.

Addresses PR review comment.
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, MAP, STRUCT
- Expected: ARRAY<INT>, MAP<STRING,STRING>, STRUCT<field: TYPE>

Root Cause:
Arrow field metadata was only extracted inside the while(loadNextBatch())
loop. For queries with 0 rows, no batches are loaded, so the loop never
executes and metadata is never extracted.

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.

Impact:
- Fixes metadata availability for both SQL Exec API and Thrift Server
- All 2,692 unit tests pass
- Verified with queries returning 0 rows - metadata now correctly shows
  detailed type information for complex types

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Add testMetadataExtractionWithZeroRows() to ArrowResultChunkTest to
verify that Arrow field metadata is correctly extracted from
VectorSchemaRoot even when there are 0 rows in the result set.

This test complements the fix in AbstractArrowResultChunk.java where
metadata extraction was moved before the loadNextBatch() loop to
ensure metadata is available for queries returning 0 rows (e.g.,
WHERE 1=0).

Test coverage:
- Creates Arrow file with schema metadata but 0 rows
- Verifies getArrowMetadata() returns non-null metadata
- Verifies metadata contains detailed type information
  (ARRAY<INT>, MAP<STRING,STRING>) even with 0 rows

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@gopalldb gopalldb changed the title Changes for fixing Metadata for 0 rows Fix Arrow field metadata not available for queries with 0 rows Jan 12, 2026
@gopalldb
gopalldb enabled auto-merge (squash) January 16, 2026 11:31
@gopalldb
gopalldb disabled auto-merge January 16, 2026 13:49
@gopalldb
gopalldb merged commit 3e4f21c into databricks:main Jan 16, 2026
10 of 13 checks passed
gopalldb added a commit that referenced this pull request Jan 19, 2026
…istency (#1182)

## Summary
This PR adds TIMESTAMP_NTZ normalization in the Thrift path to ensure
consistent metadata behavior across both SEA and Thrift API paths.

## Background
PR #1177 moved Arrow metadata extraction earlier in the processing
pipeline, which exposed an inconsistency: the Thrift path started
returning the correct "TIMESTAMP_NTZ" from server metadata, while the
SEA path was already normalizing it to "TIMESTAMP" for backward
compatibility.

## Changes
- Added TIMESTAMP_NTZ → TIMESTAMP normalization in
`DatabricksResultSetMetaData.java` Thrift constructor (lines 205-208)
- This brings Thrift path behavior in line with existing SEA path
normalization
- Fixes test failure in
`PreparedStatementIntegrationTests.testGetMetaData_NoResultSet`

## Testing
- ✅ Local test run:
`PreparedStatementIntegrationTests.testGetMetaData_NoResultSet` passes
- ✅ Metadata now consistent before and after `executeQuery()` for
TIMESTAMP_NTZ columns
- ✅ Both SEA and Thrift paths return "TIMESTAMP" for TIMESTAMP_NTZ
columns

## Related
- Builds on PR #1177 (Fix Arrow field metadata not available for queries
with 0 rows)
- Fixes issue introduced by early metadata extraction in PR #1177
- Maintains backward compatibility with existing behavior

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants