fix(rds-data): accept parameter names that start with a digit (#5119)

* fix(rds-data): accept parameter names that start with a digit

* fix(rds-data): do not treat array-slice bounds as numeric parameters

* fix(rds-data): decide array slices at the opening bracket

A [ right after a name other than ARRAY, a ] or a ) is a subscript, and a
:digit directly inside it is a slice bound however it is spaced. ARRAY[:1]
is a constructor, so :1 there stays a parameter. Tests use no var.

* fix(rds-data): track the last operand to tell slice bounds from parameters

Inside an array subscript a :digit is a slice bound only after an operand
(or directly after the opening bracket) and a parameter after an operator,
comma or paren, so arr[1 + :1] binds :1. The scan now remembers the last
significant token, skipping comments, so arr /* note */ [1:2] is still a
subscript.

* fix(rds-data): drop clause words from the non-operand keyword list

SET, BY, VALUES, SELECT, FROM, WHERE, ON and RETURNING never sit right
before a [ or a :digit inside a subscript, and a column may carry such a
name, so set[1:2] no longer turns :2 into a parameter. Make
isNonOperandWord a switch expression.
This commit is contained in:
Younes Beriane authored and GitHub committed 2026-10-07 11:29:50 -04:00
1 parent c6369b321a
commit bdb37f0ca3
2 files changed
+182 -14

No files matched your search

@@ -1,7 +1,10 @@
package io.github.hectorvent.floci.core.common;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Locale;
/**
* Translates SQL statements containing named placeholders ({@code :name}) into
@@ -26,11 +29,14 @@ public final class SqlParameterParser {
* @param backslashEscapes whether backslash escapes characters in string literals (e.g. MySQL)
* @param escapeStrings whether PostgreSQL escape strings ({@code E'...'}/{@code e'...'})
* honor backslash escapes (e.g. Redshift Data API)
* @param numericNames whether a parameter name may start with a digit, as in
* {@code :1} (RDS Data API)
*/
public record Options(boolean allowBackticks, boolean backslashEscapes, boolean escapeStrings) {
public static final Options REDSHIFT = new Options(false, false, true);
public static final Options RDS_MYSQL = new Options(true, true, false);
public static final Options RDS_POSTGRESQL = new Options(true, false, false);
public record Options(boolean allowBackticks, boolean backslashEscapes, boolean escapeStrings,
boolean numericNames) {
public static final Options REDSHIFT = new Options(false, false, true, false);
public static final Options RDS_MYSQL = new Options(true, true, false, true);
public static final Options RDS_POSTGRESQL = new Options(true, false, false, true);
}
/**
@@ -52,6 +58,13 @@ public final class SqlParameterParser {
List<String> order = new ArrayList<>();
int len = sql.length();
int i = 0;
// One entry per open bracket or parenthesis; true marks an array subscript.
Deque<Boolean> nesting = new ArrayDeque<>();
// Whether the last significant token (comments and whitespace excluded) ends an
// operand: a name, number, literal, quoted identifier, ']' or ')'.
boolean afterOperand = false;
// Whether the last significant token is the '[' of an array subscript.
boolean subscriptStart = false;
while (i < len) {
char c = sql.charAt(i);
@@ -75,6 +88,8 @@ public final class SqlParameterParser {
int end = skipQuoted(sql, i, c, options);
out.append(sql, i, end);
i = end;
afterOperand = true;
subscriptStart = false;
continue;
}
@@ -83,6 +98,8 @@ public final class SqlParameterParser {
if (consumed > i) {
out.append(sql, i, consumed);
i = consumed;
afterOperand = true;
subscriptStart = false;
continue;
}
out.append(c);
@@ -90,24 +107,76 @@ public final class SqlParameterParser {
continue;
}
if (isNamePart(c)) {
int j = i + 1;
while (j < len && isNamePart(sql.charAt(j))) {
j++;
}
afterOperand = !isNonOperandWord(sql.substring(i, j));
subscriptStart = false;
out.append(sql, i, j);
i = j;
continue;
}
if (c == '[') {
nesting.push(afterOperand);
subscriptStart = afterOperand;
afterOperand = false;
out.append(c);
i++;
continue;
}
if (c == '(') {
nesting.push(Boolean.FALSE);
afterOperand = false;
subscriptStart = false;
out.append(c);
i++;
continue;
}
if (c == ']' || c == ')') {
if (!nesting.isEmpty()) {
nesting.pop();
}
afterOperand = true;
subscriptStart = false;
out.append(c);
i++;
continue;
}
if (c == ':') {
if (i + 1 < len && sql.charAt(i + 1) == ':') {
out.append("::");
i += 2;
afterOperand = false;
subscriptStart = false;
continue;
}
if (i + 1 < len && isNameStart(sql.charAt(i + 1))) {
int j = i + 1;
while (j < len && isNamePart(sql.charAt(j))) {
j++;
if (i + 1 < len && isNameStart(sql.charAt(i + 1), options)) {
boolean sliceBound = Character.isDigit(sql.charAt(i + 1))
&& Boolean.TRUE.equals(nesting.peek())
&& (afterOperand || subscriptStart);
if (!sliceBound) {
int j = i + 1;
while (j < len && isNamePart(sql.charAt(j))) {
j++;
}
order.add(sql.substring(i + 1, j));
out.append('?');
i = j;
afterOperand = true;
subscriptStart = false;
continue;
}
order.add(sql.substring(i + 1, j));
out.append('?');
i = j;
continue;
}
}
if (!Character.isWhitespace(c)) {
afterOperand = false;
subscriptStart = false;
}
out.append(c);
i++;
}
@@ -211,8 +280,23 @@ public final class SqlParameterParser {
return quotePos - 1 == 0 || !isNamePart(sql.charAt(quotePos - 2));
}
private static boolean isNameStart(char c) {
return Character.isLetter(c) || c == '_';
/**
* Whether {@code word} is a keyword that cannot end an operand and can sit right before a
* {@code [} or a {@code :digit}, so what follows is not a subscript or slice bound.
* Clause words such as {@code SET}, {@code BY}, {@code VALUES} or {@code FROM} are
* left out: they never precede those, and a column may carry such a name. {@code ARRAY}
* is here because {@code ARRAY[...]} is a constructor, not a subscript.
*/
private static boolean isNonOperandWord(String word) {
return switch (word.toUpperCase(Locale.ROOT)) {
case "ARRAY", "AND", "OR", "NOT", "IN", "IS", "LIKE", "ILIKE", "BETWEEN",
"WHEN", "THEN", "ELSE", "CASE", "ANY", "ALL", "SOME", "DISTINCT" -> true;
default -> false;
};
}
private static boolean isNameStart(char c, Options options) {
return Character.isLetter(c) || c == '_' || (options.numericNames() && c >= '0' && c <= '9');
}
private static boolean isNamePart(char c) {