Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Updated

- `WITH ... INSERT/UPDATE/DELETE/MERGE` statements are now treated as updates: `executeUpdate()` and `executeLargeUpdate()` return the affected-row count instead of throwing after the write. This changes behavior for existing callers: `execute()` now returns `false`, and `executeQuery()` throws `RESULT_SET_ERROR` after the statement has run. To keep receiving the affected-row ResultSet, set `NonRowcountQueryPrefixes=WITH`. Prefixes match the start of the statement, so `NonRowcountQueryPrefixes=INSERT` alone does not opt out for a statement beginning with `WITH`.
- SQL classification and client-side placeholder detection recognize backslash-escaped quotes, raw string literals, and backslash-continued line comments.
- Updated JSON-java to 20260814 to address CVE-2026-59171.
- Reduced typed result getter CPU by resolving each column's converter once when its metadata is built and reusing it across rows.
- Reduced driver CPU when reading Arrow results: `ResultSet.next()` and the `getXxx`/`getObject` getters no longer re-read connection settings, re-parse the column type name, or record telemetry on every row or value. Reading narrow primitive columns (for example a single `BOOLEAN` or `DOUBLE` column) now takes several times less CPU per row.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.entity.InputStreamEntity;

public class DatabricksStatement implements IDatabricksStatement, IDatabricksStatementInternal {

private static final JdbcLogger LOGGER = JdbcLoggerFactory.getLogger(DatabricksStatement.class);
private static final Pattern CTE_STATEMENT_PATTERN =
Pattern.compile("\\)\\s*(\\w+)", Pattern.UNICODE_CHARACTER_CLASS);
private static final Pattern DML_PATTERN =
Pattern.compile("INSERT|UPDATE|DELETE|MERGE", Pattern.CASE_INSENSITIVE);

/**
* Sentinel value indicating update count has not yet been evaluated. Used for lazy evaluation to
Expand Down Expand Up @@ -806,7 +812,7 @@ static boolean shouldReturnResultSet(String query, List<String> nonRowcountQuery
|| SHOW_PATTERN.matcher(trimmedQuery).find()
|| DESCRIBE_PATTERN.matcher(trimmedQuery).find()
|| EXPLAIN_PATTERN.matcher(trimmedQuery).find()
|| WITH_PATTERN.matcher(trimmedQuery).find()
|| (WITH_PATTERN.matcher(trimmedQuery).find() && !isCteDml(trimmedQuery))
|| SET_PATTERN.matcher(trimmedQuery).find()
|| MAP_PATTERN.matcher(trimmedQuery).find()
|| FROM_PATTERN.matcher(trimmedQuery).find()
Expand All @@ -828,6 +834,37 @@ protected boolean shouldReturnResultSetWithConfig(String query) {
query, connection.getConnectionContext().getNonRowcountQueryPrefixes());
}

private static boolean isCteDml(String query) {
// Only the words outside CTE bodies and column lists determine the outer statement.
StringBuilder topLevel = new StringBuilder();
int[] depth = {0};
SqlCommentParser.forEachNonCommentChar(
query,
(state, c) -> {
if (state != SqlCommentParser.State.NORMAL) {
return;
}
if (c == '(') {
depth[0]++;
} else if (c == ')') {
depth[0]--;
}
if (depth[0] == 0) {
topLevel.append(c);
}
});

Matcher statement = CTE_STATEMENT_PATTERN.matcher(topLevel);
while (statement.find()) {
String keyword = statement.group(1);
// A column list can be followed by AS or MAX RECURSION LEVEL before the CTE body.
if (!keyword.equalsIgnoreCase("AS") && !keyword.equalsIgnoreCase("MAX")) {
return DML_PATTERN.matcher(keyword).matches();
}
}
return false;
}

static boolean isSelectQuery(String query) {
String trimmedQuery = trimCommentsAndWhitespaces(query);
return SELECT_PATTERN.matcher(trimmedQuery).find();
Expand All @@ -836,17 +873,17 @@ static boolean isSelectQuery(String query) {
/**
* Returns whether the query can have its result-set metadata retrieved via {@code DESCRIBE QUERY}
* without executing it. {@code DESCRIBE QUERY} accepts the query forms that produce a result set
* from a top-level query expression: {@code SELECT}, {@code WITH} (CTEs), {@code VALUES}, {@code
* FROM (...)} and parenthesized selects. Other result-set-producing statements such as {@code
* SHOW}, {@code DESCRIBE} and {@code EXPLAIN} are not valid inputs to {@code DESCRIBE QUERY} and
* must be excluded.
* from a top-level query expression: {@code SELECT}, CTE queries, {@code VALUES}, {@code FROM
* (...)} queries and parenthesized selects. CTE-prefixed DML is excluded, as are other
* result-set-producing statements such as {@code SHOW}, {@code DESCRIBE} and {@code EXPLAIN} that
* are not valid inputs to {@code DESCRIBE QUERY}.
*/
static boolean isDescribableQuery(String query) {
String trimmedQuery = trimCommentsAndWhitespaces(query);
return SELECT_PATTERN.matcher(trimmedQuery).find()
|| WITH_PATTERN.matcher(trimmedQuery).find()
|| VALUES_PATTERN.matcher(trimmedQuery).find()
|| FROM_PATTERN.matcher(trimmedQuery).find();
|| (WITH_PATTERN.matcher(trimmedQuery).find() && !isCteDml(trimmedQuery))
|| FROM_PATTERN.matcher(trimmedQuery).find()
|| VALUES_PATTERN.matcher(trimmedQuery).find();
}

static boolean isInsertQuery(String query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface IndexedSqlCharConsumer {
* Iterates over each character in the SQL string while keeping track of comment, string literal,
* and identifier state. Each character that is not part of a comment calls the consumer with the
* current state and character. Emits a ' ' character after each comment to avoid token fusion.
* Quote boundaries follow Spark's {@code SqlBaseLexer}; parser settings that change how token
* contents are interpreted do not change those boundaries.
*
* @param sql the SQL string to parse
* @param consumer called for each visible character with its parsing state
Expand All @@ -54,6 +56,7 @@ public static void forEachNonCommentChar(String sql, IndexedSqlCharConsumer cons

State state = State.NORMAL;
int blockCommentDepth = 0;
boolean rawString = false;

for (int i = 0; i < sql.length(); i++) {
char c = sql.charAt(i);
Expand All @@ -69,9 +72,11 @@ public static void forEachNonCommentChar(String sql, IndexedSqlCharConsumer cons
blockCommentDepth = 1;
i++; // skip '*'
} else if (c == '\'') {
rawString = hasRawStringPrefix(sql, i);
state = State.IN_SINGLE_QUOTE;
consumer.accept(state, c, i);
} else if (c == '"') {
rawString = hasRawStringPrefix(sql, i);
state = State.IN_DOUBLE_QUOTE;
consumer.accept(state, c, i);
} else if (c == '`') {
Expand All @@ -84,21 +89,29 @@ public static void forEachNonCommentChar(String sql, IndexedSqlCharConsumer cons

case IN_SINGLE_QUOTE:
consumer.accept(state, c, i);
if (c == '\'' && next == '\'') {
if (!rawString && c == '\\' && next != '\0') {
consumer.accept(state, next, i + 1);
i++;
} else if (!rawString && c == '\'' && next == '\'') {
consumer.accept(state, next, i + 1);
i++; // skip escaped quote
} else if (c == '\'') {
state = State.NORMAL;
rawString = false;
}
break;

case IN_DOUBLE_QUOTE:
consumer.accept(state, c, i);
if (c == '"' && next == '"') {
if (!rawString && c == '\\' && next != '\0') {
consumer.accept(state, next, i + 1);
i++;
} else if (!rawString && c == '"' && next == '"') {
consumer.accept(state, next, i + 1);
i++; // skip escaped quote
} else if (c == '"') {
state = State.NORMAL;
rawString = false;
}
break;

Expand All @@ -113,7 +126,9 @@ public static void forEachNonCommentChar(String sql, IndexedSqlCharConsumer cons
break;

case IN_LINE_COMMENT:
if (c == '\n' || c == '\r') {
if (c == '\\' && next == '\n') {
i++;
} else if (c == '\n' || c == '\r') {
state = State.NORMAL;
consumer.accept(state, ' ', i);
if (c == '\r' && next == '\n') {
Expand Down Expand Up @@ -141,6 +156,17 @@ public static void forEachNonCommentChar(String sql, IndexedSqlCharConsumer cons
}
}

private static boolean hasRawStringPrefix(String sql, int quoteIndex) {
if (quoteIndex == 0) {
return false;
}
char prefix = sql.charAt(quoteIndex - 1);
if (prefix != 'r' && prefix != 'R') {
return false;
}
return quoteIndex == 1 || !Character.isJavaIdentifierPart(sql.charAt(quoteIndex - 2));
}

/**
* Finds the source-string indices of placeholder ('?') characters that appear in {@link
* State#NORMAL} state — i.e. not inside comments, string literals, or quoted identifiers. Used by
Expand Down
Loading
Loading