-
Notifications
You must be signed in to change notification settings - Fork 41
Implement getSchemas() for SEA #802
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
Merged
jayantsing-db
merged 10 commits into
databricks:main
from
databricks:jayantsing-db/show-schemas-client
May 22, 2025
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c33b19b
Implement getSchemas() for SEA
jayantsing-db 3f8f349
Merge remote-tracking branch 'databricks/main' into jayantsing-db/sho…
jayantsing-db 8c722fd
Close result set after consuming
jayantsing-db 69afe1b
Merge remote-tracking branch 'databricks/main' into jayantsing-db/sho…
jayantsing-db fd0ed2f
Address review comments
jayantsing-db 2cc82ef
Merge remote-tracking branch 'databricks/main' into jayantsing-db/sho…
jayantsing-db 095360b
Update changelog
jayantsing-db 2c14d4a
fmt
jayantsing-db 0277ce0
nit
jayantsing-db 61fdab5
Merge branch 'main' into jayantsing-db/show-schemas-client
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
152 changes: 152 additions & 0 deletions
152
src/main/java/com/databricks/jdbc/common/util/JdbcThreadUtils.java
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 |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package com.databricks.jdbc.common.util; | ||
|
|
||
| import com.databricks.jdbc.api.internal.IDatabricksConnectionContext; | ||
| import com.databricks.jdbc.exception.DatabricksSQLException; | ||
| import com.databricks.jdbc.model.telemetry.enums.DatabricksDriverErrorCode; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.concurrent.*; | ||
| import java.util.function.Function; | ||
|
|
||
| /** Utility class for executing tasks in parallel with proper context handling. */ | ||
| public class JdbcThreadUtils { | ||
|
|
||
| /** | ||
| * Executes tasks in parallel with proper context handling. | ||
| * | ||
| * @param items The items to process | ||
| * @param connectionContext The connection context to propagate to worker threads | ||
| * @param maxThreads Maximum number of threads to use | ||
| * @param timeoutSeconds Timeout in seconds | ||
| * @param task The task to execute for each item | ||
| * @param <T> Type of input items | ||
| * @param <R> Type of result | ||
| * @return List of results from all tasks | ||
| * @throws SQLException If an error occurs during execution | ||
| */ | ||
| public static <T, R> List<R> parallelMap( | ||
| Collection<T> items, | ||
| IDatabricksConnectionContext connectionContext, | ||
| int maxThreads, | ||
| int timeoutSeconds, | ||
| Function<T, R> task) | ||
| throws SQLException { | ||
|
|
||
| if (items.isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| // Use a reasonable thread pool size | ||
| int threadCount = Math.min(items.size(), maxThreads); | ||
| ExecutorService executor = Executors.newFixedThreadPool(threadCount); | ||
|
jayantsing-db marked this conversation as resolved.
Outdated
|
||
|
|
||
| try { | ||
| List<Future<R>> futures = new ArrayList<>(); | ||
|
|
||
| // Submit tasks to the executor | ||
| for (T item : items) { | ||
| futures.add( | ||
| executor.submit( | ||
| () -> { | ||
| // Set connection context for this thread | ||
| DatabricksThreadContextHolder.setConnectionContext(connectionContext); | ||
| try { | ||
| // Execute the task | ||
| return task.apply(item); | ||
| } finally { | ||
| // Clear connection context | ||
| DatabricksThreadContextHolder.clearConnectionContext(); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| // Collect results | ||
| List<R> results = new ArrayList<>(items.size()); | ||
| for (Future<R> future : futures) { | ||
| try { | ||
| results.add(future.get(timeoutSeconds, TimeUnit.SECONDS)); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new DatabricksSQLException( | ||
| "Parallel execution interrupted", | ||
| e, | ||
| DatabricksDriverErrorCode.THREAD_INTERRUPTED_ERROR); | ||
| } catch (ExecutionException e) { | ||
| SQLException sqlEx = findSQLExceptionInCauseChain(e); | ||
| if (sqlEx != null) { | ||
| throw sqlEx; | ||
| } else { | ||
| throw new DatabricksSQLException( | ||
| "Error in parallel execution", e, DatabricksDriverErrorCode.INVALID_STATE); | ||
| } | ||
| } catch (TimeoutException e) { | ||
| throw new DatabricksSQLException( | ||
| "Parallel execution timed out after " + timeoutSeconds + " seconds", | ||
| e, | ||
| DatabricksDriverErrorCode.OPERATION_TIMEOUT_ERROR); | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } finally { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Executes tasks in parallel, collecting and flattening all results. Useful when each task | ||
| * produces multiple results. | ||
| * | ||
| * @param items The items to process | ||
| * @param connectionContext The connection context to propagate to worker threads | ||
| * @param maxThreads Maximum number of threads to use | ||
| * @param timeoutSeconds Timeout in seconds | ||
| * @param task The task to execute for each item, producing a collection of results | ||
| * @param <T> Type of input items | ||
| * @param <R> Type of result | ||
| * @return Flattened list of all results | ||
| * @throws SQLException If an error occurs during execution | ||
| */ | ||
| public static <T, R> List<R> parallelFlatMap( | ||
| Collection<T> items, | ||
| IDatabricksConnectionContext connectionContext, | ||
| int maxThreads, | ||
| int timeoutSeconds, | ||
| Function<T, Collection<R>> task) | ||
| throws SQLException { | ||
|
|
||
| List<Collection<R>> collections = | ||
| parallelMap(items, connectionContext, maxThreads, timeoutSeconds, task); | ||
|
|
||
| // Flatten the results | ||
| List<R> allResults = new ArrayList<>(); | ||
| for (Collection<R> collection : collections) { | ||
| if (collection != null) { | ||
| allResults.addAll(collection); | ||
| } | ||
| } | ||
|
|
||
| return allResults; | ||
| } | ||
|
|
||
| /** | ||
| * Recursively searches for a SQLException in the exception cause chain. | ||
| * | ||
| * @param throwable The exception to search | ||
| * @return The first SQLException found in the cause chain, or null if none | ||
| */ | ||
| private static SQLException findSQLExceptionInCauseChain(Throwable throwable) { | ||
| if (throwable == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (throwable instanceof SQLException) { | ||
| return (SQLException) throwable; | ||
| } | ||
|
|
||
| return findSQLExceptionInCauseChain(throwable.getCause()); | ||
| } | ||
| } | ||
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.
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.