-
Notifications
You must be signed in to change notification settings - Fork 27
Add StreamingChunkProvider for result fetching #1111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jayantsing-db
wants to merge
7
commits into
main
Choose a base branch
from
jayantsing-db/chunk-download
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57ac936
Add StreamingChunkProvider for result fetching
jayantsing-db 51b0dee
fmt
jayantsing-db cd14529
Add Thrift implementation
jayantsing-db d23eed4
fmt
jayantsing-db 5bffdb5
Address review comments
jayantsing-db 5818265
Merge branch 'main' into jayantsing-db/chunk-download
jayantsing-db 22fb391
fmt
jayantsing-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| package com.databricks.jdbc.api.impl.arrow; | ||
|
|
||
| import static com.databricks.jdbc.common.util.DatabricksThriftUtil.createExternalLink; | ||
| import static com.databricks.jdbc.common.util.DatabricksThriftUtil.getColumnInfoFromTColumnDesc; | ||
|
|
||
| import com.databricks.jdbc.api.impl.ComplexDataTypeParser; | ||
| import com.databricks.jdbc.api.impl.IExecutionResult; | ||
| import com.databricks.jdbc.api.internal.IDatabricksConnectionContext; | ||
| import com.databricks.jdbc.api.internal.IDatabricksSession; | ||
| import com.databricks.jdbc.api.internal.IDatabricksStatementInternal; | ||
| import com.databricks.jdbc.common.CompressionCodec; | ||
|
|
@@ -16,12 +18,16 @@ | |
| import com.databricks.jdbc.model.client.thrift.generated.TColumnDesc; | ||
| import com.databricks.jdbc.model.client.thrift.generated.TFetchResultsResp; | ||
| import com.databricks.jdbc.model.client.thrift.generated.TGetResultSetMetadataResp; | ||
| import com.databricks.jdbc.model.client.thrift.generated.TSparkArrowResultLink; | ||
| import com.databricks.jdbc.model.core.ChunkLinkFetchResult; | ||
| import com.databricks.jdbc.model.core.ColumnInfo; | ||
| import com.databricks.jdbc.model.core.ColumnInfoTypeName; | ||
| import com.databricks.jdbc.model.core.ExternalLink; | ||
| import com.databricks.jdbc.model.core.ResultData; | ||
| import com.databricks.jdbc.model.core.ResultManifest; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| /** Result container for Arrow-based query results. */ | ||
|
|
@@ -69,20 +75,71 @@ public ArrowStreamResult( | |
| "Creating ArrowStreamResult with remote links for statementId: {}", | ||
| statementId.toSQLExecStatementId()); | ||
| this.chunkProvider = | ||
| new RemoteChunkProvider( | ||
| statementId, | ||
| resultManifest, | ||
| resultData, | ||
| session, | ||
| httpClient, | ||
| session.getConnectionContext().getCloudFetchThreadPoolSize()); | ||
| createRemoteChunkProvider(statementId, resultManifest, resultData, session, httpClient); | ||
| } | ||
| this.columnInfos = | ||
| resultManifest.getSchema().getColumnCount() == 0 | ||
| ? new ArrayList<>() | ||
| : new ArrayList<>(resultManifest.getSchema().getColumns()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates the appropriate remote chunk provider based on configuration. | ||
| * | ||
| * @param statementId The statement ID | ||
| * @param resultManifest The result manifest containing chunk metadata | ||
| * @param resultData The result data containing initial external links | ||
| * @param session The session for fetching additional chunks | ||
| * @param httpClient The HTTP client for downloading chunk data | ||
| * @return A ChunkProvider instance | ||
| */ | ||
| private static ChunkProvider createRemoteChunkProvider( | ||
| StatementId statementId, | ||
| ResultManifest resultManifest, | ||
| ResultData resultData, | ||
| IDatabricksSession session, | ||
| IDatabricksHttpClient httpClient) | ||
| throws DatabricksSQLException { | ||
|
|
||
| IDatabricksConnectionContext connectionContext = session.getConnectionContext(); | ||
|
|
||
| if (connectionContext.isStreamingChunkProviderEnabled()) { | ||
| LOGGER.info( | ||
| "Using StreamingChunkProvider for statementId: {}", statementId.toSQLExecStatementId()); | ||
|
|
||
| ChunkLinkFetcher linkFetcher = new SeaChunkLinkFetcher(session, statementId); | ||
| CompressionCodec compressionCodec = resultManifest.getResultCompression(); | ||
| int maxChunksInMemory = connectionContext.getCloudFetchThreadPoolSize(); | ||
| int linkPrefetchWindow = connectionContext.getLinkPrefetchWindow(); | ||
| int chunkReadyTimeoutSeconds = connectionContext.getChunkReadyTimeoutSeconds(); | ||
| double cloudFetchSpeedThreshold = connectionContext.getCloudFetchSpeedThreshold(); | ||
|
|
||
| // Convert ExternalLinks to ChunkLinkFetchResult for the provider | ||
| ChunkLinkFetchResult initialLinks = | ||
| convertToChunkLinkFetchResult(resultData.getExternalLinks()); | ||
|
|
||
| return new StreamingChunkProvider( | ||
| linkFetcher, | ||
| httpClient, | ||
| compressionCodec, | ||
| statementId, | ||
| maxChunksInMemory, | ||
| linkPrefetchWindow, | ||
| chunkReadyTimeoutSeconds, | ||
| cloudFetchSpeedThreshold, | ||
| initialLinks); | ||
| } else { | ||
| // Use the original RemoteChunkProvider | ||
| return new RemoteChunkProvider( | ||
| statementId, | ||
| resultManifest, | ||
| resultData, | ||
| session, | ||
| httpClient, | ||
| connectionContext.getCloudFetchThreadPoolSize()); | ||
| } | ||
| } | ||
|
|
||
| public ArrowStreamResult( | ||
| TFetchResultsResp resultsResp, | ||
| boolean isInlineArrow, | ||
|
|
@@ -110,16 +167,63 @@ public ArrowStreamResult( | |
| if (isInlineArrow) { | ||
| this.chunkProvider = new InlineChunkProvider(resultsResp, parentStatement, session); | ||
| } else { | ||
| CompressionCodec compressionCodec = | ||
| CompressionCodec.getCompressionMapping(resultsResp.getResultSetMetadata()); | ||
| this.chunkProvider = | ||
| new RemoteChunkProvider( | ||
| parentStatement, | ||
| resultsResp, | ||
| session, | ||
| httpClient, | ||
| session.getConnectionContext().getCloudFetchThreadPoolSize(), | ||
| compressionCodec); | ||
| createThriftRemoteChunkProvider(resultsResp, parentStatement, session, httpClient); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates the appropriate remote chunk provider for Thrift based on configuration. | ||
| * | ||
| * @param resultsResp The Thrift fetch results response | ||
| * @param parentStatement The parent statement for fetching additional chunks | ||
| * @param session The session for fetching additional chunks | ||
| * @param httpClient The HTTP client for downloading chunk data | ||
| * @return A ChunkProvider instance | ||
| */ | ||
| private static ChunkProvider createThriftRemoteChunkProvider( | ||
| TFetchResultsResp resultsResp, | ||
| IDatabricksStatementInternal parentStatement, | ||
| IDatabricksSession session, | ||
| IDatabricksHttpClient httpClient) | ||
| throws DatabricksSQLException { | ||
|
|
||
| IDatabricksConnectionContext connectionContext = session.getConnectionContext(); | ||
| CompressionCodec compressionCodec = | ||
| CompressionCodec.getCompressionMapping(resultsResp.getResultSetMetadata()); | ||
|
|
||
| if (connectionContext.isStreamingChunkProviderEnabled()) { | ||
| StatementId statementId = parentStatement.getStatementId(); | ||
| LOGGER.info("Using StreamingChunkProvider for Thrift statementId: {}", statementId); | ||
|
|
||
| ChunkLinkFetcher linkFetcher = new ThriftChunkLinkFetcher(session, statementId); | ||
| int maxChunksInMemory = connectionContext.getCloudFetchThreadPoolSize(); | ||
| int linkPrefetchWindow = connectionContext.getLinkPrefetchWindow(); | ||
| int chunkReadyTimeoutSeconds = connectionContext.getChunkReadyTimeoutSeconds(); | ||
| double cloudFetchSpeedThreshold = connectionContext.getCloudFetchSpeedThreshold(); | ||
|
|
||
| // Convert initial Thrift links to ChunkLinkFetchResult | ||
| ChunkLinkFetchResult initialLinks = convertThriftLinksToChunkLinkFetchResult(resultsResp); | ||
|
|
||
| return new StreamingChunkProvider( | ||
| linkFetcher, | ||
| httpClient, | ||
| compressionCodec, | ||
| statementId, | ||
| maxChunksInMemory, | ||
| linkPrefetchWindow, | ||
| chunkReadyTimeoutSeconds, | ||
| cloudFetchSpeedThreshold, | ||
| initialLinks); | ||
| } else { | ||
| // Use the original RemoteChunkProvider | ||
| return new RemoteChunkProvider( | ||
| parentStatement, | ||
| resultsResp, | ||
| session, | ||
| httpClient, | ||
| connectionContext.getCloudFetchThreadPoolSize(), | ||
| compressionCodec); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -268,4 +372,79 @@ private void setColumnInfo(TGetResultSetMetadataResp resultManifest) { | |
| columnInfos.add(getColumnInfoFromTColumnDesc(tColumnDesc)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a collection of ExternalLinks to a ChunkLinkFetchResult. | ||
| * | ||
| * @param externalLinks The external links to convert, may be null | ||
| * @return A ChunkLinkFetchResult, or null if input is null or empty | ||
| */ | ||
| private static ChunkLinkFetchResult convertToChunkLinkFetchResult( | ||
| Collection<ExternalLink> externalLinks) { | ||
| if (externalLinks == null || externalLinks.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| List<ExternalLink> linkList = | ||
| externalLinks instanceof List | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why converting to ArrayList explicitly? can't you do collection.stream directly? |
||
| ? (List<ExternalLink>) externalLinks | ||
| : new ArrayList<>(externalLinks); | ||
|
|
||
| // Derive hasMore and nextRowOffset from last link (SEA style) | ||
| ExternalLink lastLink = linkList.get(linkList.size() - 1); | ||
| boolean hasMore = lastLink.getNextChunkIndex() != null; | ||
| long nextFetchIndex = hasMore ? lastLink.getNextChunkIndex() : -1; | ||
| long nextRowOffset = lastLink.getRowOffset() + lastLink.getRowCount(); | ||
|
|
||
| return ChunkLinkFetchResult.of(linkList, hasMore, nextFetchIndex, nextRowOffset); | ||
| } | ||
|
|
||
| /** | ||
| * Converts Thrift result links to a ChunkLinkFetchResult. | ||
| * | ||
| * <p>This method converts TSparkArrowResultLink from the Thrift response to the unified | ||
| * ChunkLinkFetchResult format used by StreamingChunkProvider. | ||
| * | ||
| * @param resultsResp The Thrift fetch results response containing initial links | ||
| * @return A ChunkLinkFetchResult, or null if no links | ||
| */ | ||
| private static ChunkLinkFetchResult convertThriftLinksToChunkLinkFetchResult( | ||
| TFetchResultsResp resultsResp) { | ||
| List<TSparkArrowResultLink> resultLinks = resultsResp.getResults().getResultLinks(); | ||
| if (resultLinks == null || resultLinks.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| List<ExternalLink> chunkLinks = new ArrayList<>(); | ||
| int lastIndex = resultLinks.size() - 1; | ||
| boolean hasMoreRows = resultsResp.hasMoreRows; | ||
|
|
||
| for (int i = 0; i < resultLinks.size(); i++) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of i, use readable names |
||
| TSparkArrowResultLink thriftLink = resultLinks.get(i); | ||
|
|
||
| // Convert Thrift link to ExternalLink (sets chunkIndex, rowOffset, rowCount, etc.) | ||
| ExternalLink externalLink = createExternalLink(thriftLink, i); | ||
|
|
||
| // For the last link, set nextChunkIndex based on hasMoreRows | ||
| if (i == lastIndex) { | ||
| if (hasMoreRows) { | ||
| // More chunks available - next fetch should start from lastIndex + 1 | ||
| externalLink.setNextChunkIndex((long) i + 1); | ||
| } | ||
| // If hasMoreRows is false, nextChunkIndex remains null (end of stream) | ||
| } else { | ||
| // Not the last link - next chunk follows immediately | ||
| externalLink.setNextChunkIndex((long) i + 1); | ||
| } | ||
|
|
||
| chunkLinks.add(externalLink); | ||
| } | ||
|
|
||
| // Calculate next fetch positions from last link | ||
| TSparkArrowResultLink lastThriftLink = resultLinks.get(lastIndex); | ||
| long nextFetchIndex = hasMoreRows ? lastIndex + 1 : -1; | ||
| long nextRowOffset = lastThriftLink.getStartRowOffset() + lastThriftLink.getRowCount(); | ||
|
|
||
| return ChunkLinkFetchResult.of(chunkLinks, hasMoreRows, nextFetchIndex, nextRowOffset); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.