-
Notifications
You must be signed in to change notification settings - Fork 45
[2/7] Add native batching configuration and parameter model #1621
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
sreekanth-db
merged 3 commits into
main
from
sreekanth-db/stack/native-batch-foundation
Aug 22, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
77 changes: 77 additions & 0 deletions
77
src/main/java/com/databricks/jdbc/api/impl/BatchParameterSet.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,77 @@ | ||
| package com.databricks.jdbc.api.impl; | ||
|
|
||
| import java.sql.Date; | ||
| import java.sql.Time; | ||
| import java.sql.Timestamp; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Immutable, position-ordered snapshot of one prepared-statement parameter set. | ||
| * | ||
| * <p>This model normalizes JDBC's one-based parameter indexes to zero-based wire ordinals. It does | ||
| * not validate parameter completeness, index continuity, or consistency with other parameter sets; | ||
| * those validations remain the backend's responsibility. | ||
| */ | ||
| public final class BatchParameterSet { | ||
|
|
||
| private final List<ImmutableSqlParameter> parameters; | ||
|
|
||
| private BatchParameterSet(List<ImmutableSqlParameter> parameters) { | ||
| this.parameters = List.copyOf(parameters); | ||
| } | ||
|
|
||
| public static BatchParameterSet from(Map<Integer, ImmutableSqlParameter> parameterBindings) { | ||
| Objects.requireNonNull(parameterBindings, "parameterBindings"); | ||
| List<ImmutableSqlParameter> orderedParameters = | ||
| parameterBindings.entrySet().stream() | ||
| .sorted(Comparator.comparingInt(Map.Entry::getKey)) | ||
| .map(BatchParameterSet::snapshotParameter) | ||
| .collect(Collectors.toList()); | ||
| return new BatchParameterSet(orderedParameters); | ||
| } | ||
|
|
||
| public List<ImmutableSqlParameter> getParameters() { | ||
| return parameters; | ||
| } | ||
|
|
||
| public int size() { | ||
| return parameters.size(); | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return parameters.isEmpty(); | ||
| } | ||
|
|
||
| private static ImmutableSqlParameter snapshotParameter( | ||
| Map.Entry<Integer, ImmutableSqlParameter> entry) { | ||
| ImmutableSqlParameter parameter = entry.getValue(); | ||
| return ImmutableSqlParameter.builder() | ||
| .cardinal(entry.getKey() - 1) | ||
| .type(parameter.type()) | ||
| .value(snapshotValue(parameter.value())) | ||
| .build(); | ||
| } | ||
|
|
||
| private static Object snapshotValue(Object value) { | ||
| if (value instanceof Timestamp) { | ||
| Timestamp timestamp = (Timestamp) value; | ||
| Timestamp copy = new Timestamp(timestamp.getTime()); | ||
| copy.setNanos(timestamp.getNanos()); | ||
| return copy; | ||
| } | ||
| if (value instanceof Date) { | ||
| return new Date(((Date) value).getTime()); | ||
| } | ||
| if (value instanceof Time) { | ||
| return new Time(((Time) value).getTime()); | ||
| } | ||
| if (value instanceof byte[]) { | ||
| return ((byte[]) value).clone(); | ||
| } | ||
| return value; | ||
| } | ||
| } | ||
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
106 changes: 106 additions & 0 deletions
106
src/test/java/com/databricks/jdbc/api/impl/BatchParameterSetTest.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,106 @@ | ||
| package com.databricks.jdbc.api.impl; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.databricks.jdbc.model.core.ColumnInfoTypeName; | ||
| import java.sql.Timestamp; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class BatchParameterSetTest { | ||
|
|
||
| @Test | ||
| void ordersParametersByJdbcIndexAndUsesZeroBasedOrdinals() { | ||
| Map<Integer, ImmutableSqlParameter> bindings = new HashMap<>(); | ||
| bindings.put(3, parameter(99, "third", ColumnInfoTypeName.STRING)); | ||
| bindings.put(1, parameter(99, "first", ColumnInfoTypeName.STRING)); | ||
| bindings.put(2, parameter(99, "second", ColumnInfoTypeName.STRING)); | ||
|
|
||
| BatchParameterSet parameterSet = BatchParameterSet.from(bindings); | ||
|
|
||
| assertEquals(List.of("first", "second", "third"), values(parameterSet)); | ||
| assertEquals(List.of(0, 1, 2), ordinals(parameterSet)); | ||
| } | ||
|
|
||
| @Test | ||
| void preservesSparseIndexesWithoutValidation() { | ||
| Map<Integer, ImmutableSqlParameter> bindings = new HashMap<>(); | ||
| bindings.put(3, parameter(3, "third", ColumnInfoTypeName.STRING)); | ||
| bindings.put(1, parameter(1, "first", ColumnInfoTypeName.STRING)); | ||
|
|
||
| BatchParameterSet parameterSet = BatchParameterSet.from(bindings); | ||
|
|
||
| assertEquals(List.of("first", "third"), values(parameterSet)); | ||
| assertEquals(List.of(0, 2), ordinals(parameterSet)); | ||
| } | ||
|
|
||
| @Test | ||
| void allowsEmptyParameterSet() { | ||
| BatchParameterSet parameterSet = BatchParameterSet.from(Map.of()); | ||
|
|
||
| assertTrue(parameterSet.isEmpty()); | ||
| assertEquals(0, parameterSet.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void snapshotsBindingsAndMutableValues() { | ||
| Timestamp timestamp = Timestamp.valueOf("2026-08-10 12:34:56.123456789"); | ||
| byte[] bytes = new byte[] {1, 2, 3}; | ||
| Map<Integer, ImmutableSqlParameter> bindings = new HashMap<>(); | ||
| bindings.put(1, parameter(1, timestamp, ColumnInfoTypeName.TIMESTAMP)); | ||
| bindings.put(2, parameter(2, bytes, ColumnInfoTypeName.BINARY)); | ||
|
|
||
| BatchParameterSet parameterSet = BatchParameterSet.from(bindings); | ||
| bindings.clear(); | ||
| timestamp.setTime(0); | ||
| bytes[0] = 9; | ||
|
|
||
| assertFalse(parameterSet.isEmpty()); | ||
| assertEquals( | ||
| Timestamp.valueOf("2026-08-10 12:34:56.123456789"), | ||
| parameterSet.getParameters().get(0).value()); | ||
| assertArrayEquals(new byte[] {1, 2, 3}, (byte[]) parameterSet.getParameters().get(1).value()); | ||
| assertThrows( | ||
| UnsupportedOperationException.class, | ||
| () -> parameterSet.getParameters().add(parameter(3, "extra", ColumnInfoTypeName.STRING))); | ||
| } | ||
|
|
||
| @Test | ||
| void preservesNullValueAndType() { | ||
| BatchParameterSet parameterSet = | ||
| BatchParameterSet.from(Map.of(1, parameter(1, null, ColumnInfoTypeName.DECIMAL))); | ||
|
|
||
| ImmutableSqlParameter parameter = parameterSet.getParameters().get(0); | ||
| assertNull(parameter.value()); | ||
| assertEquals(ColumnInfoTypeName.DECIMAL, parameter.type()); | ||
| assertEquals(0, parameter.cardinal()); | ||
| } | ||
|
|
||
| private ImmutableSqlParameter parameter( | ||
| int cardinal, Object value, ColumnInfoTypeName columnInfoTypeName) { | ||
| return ImmutableSqlParameter.builder() | ||
| .cardinal(cardinal) | ||
| .value(value) | ||
| .type(columnInfoTypeName) | ||
| .build(); | ||
| } | ||
|
|
||
| private List<Object> values(BatchParameterSet parameterSet) { | ||
| return parameterSet.getParameters().stream() | ||
| .map(ImmutableSqlParameter::value) | ||
| .collect(java.util.stream.Collectors.toList()); | ||
| } | ||
|
|
||
| private List<Integer> ordinals(BatchParameterSet parameterSet) { | ||
| return parameterSet.getParameters().stream() | ||
| .map(ImmutableSqlParameter::cardinal) | ||
| .collect(java.util.stream.Collectors.toList()); | ||
| } | ||
| } |
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.