Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Younes Berianebdb37f0ca3e3
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SqlParameterParser.java b/src/main/java/io/github/hectorvent/floci/core/common/SqlParameterParser.java
index ff68f17a3c..a91df7d5e2 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/SqlParameterParser.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SqlParameterParser.java
@@ -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) {
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SqlParameterParserTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SqlParameterParserTest.java
index 9421d77b1b..1eb6836439 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/SqlParameterParserTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/SqlParameterParserTest.java
@@ -39,6 +39,23 @@ class SqlParameterParserTest {
assertEquals(List.of("id"), parsed.parameterOrder());
}
+ @Test
+ void rdsAcceptsParameterNamesThatStartWithADigit() {
+ ParsedSql parsed = SqlParameterParser.parse(
+ "select :1::int as x, :2 as y, :1", Options.RDS_POSTGRESQL);
+
+ assertEquals("select ?::int as x, ? as y, ?", parsed.sql());
+ assertEquals(List.of("1", "2", "1"), parsed.parameterOrder());
+ }
+
+ @Test
+ void redshiftKeepsColonDigitAsLiteralText() {
+ ParsedSql parsed = SqlParameterParser.parse("select :1", Options.REDSHIFT);
+
+ assertEquals("select :1", parsed.sql());
+ assertEquals(List.of(), parsed.parameterOrder());
+ }
+
@Test
void redshiftDoesNotTreatBackticksAsQuotedIdentifiers() {
ParsedSql parsed = SqlParameterParser.parse(
@@ -146,4 +163,71 @@ class SqlParameterParserTest {
assertTrue(SqlParameterParser.isMultiStatement("select `a;b`", Options.REDSHIFT));
assertFalse(SqlParameterParser.isMultiStatement("select `a;b`", Options.RDS_MYSQL));
}
+
+ @Test
+ void numericNamesAreNotArraySliceBounds() {
+ for (String sql : new String[] {
+ "select arr[1:2] from t", "select arr[:2] from t", "select arr[1:] from t",
+ "select arr[a:2] from t", "select arr[ :2] from t", "select a[b[1]:2] from t",
+ "select a[f(x):2] from t", "select a[1:2][3:4] from t", "select arr[1 :2] from t",
+ "select arr[1: 2] from t", "select f(x)[1 :2] from t", "select a[1][2 :3] from t",
+ "select a[b[1] :2] from t", "select arr /* note */ [1:2] from t",
+ "select arr -- note\n [1:2] from t", "select f(x) /* note */ [1 :2] from t",
+ "select arr[1/* note */:2] from t", "select arr[:1] from t",
+ "select set[1:2] from t", "select by[1:2] from t", "select values[1:2] from t"}) {
+ SqlParameterParser.ParsedSql parsed =
+ SqlParameterParser.parse(sql, SqlParameterParser.Options.RDS_POSTGRESQL);
+ assertEquals(sql, parsed.sql());
+ assertTrue(parsed.parameterOrder().isEmpty(), sql);
+ }
+ }
+
+ @Test
+ void numericNamesStillParametersOutsideSlices() {
+ SqlParameterParser.Options o = SqlParameterParser.Options.RDS_POSTGRESQL;
+ SqlParameterParser.ParsedSql p =
+ SqlParameterParser.parse("select :1, (:2), a+:3 from t where id = :4 and x in (:5,:6)", o);
+ assertEquals("select ?, (?), a+? from t where id = ? and x in (?,?)", p.sql());
+ assertEquals(List.of("1", "2", "3", "4", "5", "6"), p.parameterOrder());
+ SqlParameterParser.ParsedSql q = SqlParameterParser.parse("select arr[1:2] from t where id = :1", o);
+ assertEquals("select arr[1:2] from t where id = ?", q.sql());
+ assertEquals(List.of("1"), q.parameterOrder());
+ }
+
+ @Test
+ void numericNamesInsideArrayConstructorAreParameters() {
+ SqlParameterParser.Options o = SqlParameterParser.Options.RDS_POSTGRESQL;
+ SqlParameterParser.ParsedSql p = SqlParameterParser.parse("select ARRAY[:1] , array [ :2, :3 ]", o);
+ assertEquals("select ARRAY[?] , array [ ?, ? ]", p.sql());
+ assertEquals(List.of("1", "2", "3"), p.parameterOrder());
+ }
+
+ @Test
+ void numericNamesNestedInsideSubscriptsFollowTheirOwnBracket() {
+ SqlParameterParser.Options o = SqlParameterParser.Options.RDS_POSTGRESQL;
+ SqlParameterParser.ParsedSql p = SqlParameterParser.parse(
+ "select a[f(:1):2], b[(:3) :4], ARRAY[c[1:2], :5], d[ARRAY[:6][1]:2] from t where id = :7", o);
+ assertEquals("select a[f(?):2], b[(?) :4], ARRAY[c[1:2], ?], d[ARRAY[?][1]:2] from t where id = ?",
+ p.sql());
+ assertEquals(List.of("1", "3", "5", "6", "7"), p.parameterOrder());
+ }
+
+ @Test
+ void numericNamesAfterAnOperatorInsideASubscriptAreParameters() {
+ SqlParameterParser.Options o = SqlParameterParser.Options.RDS_POSTGRESQL;
+ SqlParameterParser.ParsedSql p = SqlParameterParser.parse(
+ "select arr[1 + :1], b[2 * :2 : 3], c[x, :3], d[(1) - :4], e[1:2 + :5] from t", o);
+ assertEquals("select arr[1 + ?], b[2 * ? : 3], c[x, ?], d[(1) - ?], e[1:2 + ?] from t", p.sql());
+ assertEquals(List.of("1", "2", "3", "4", "5"), p.parameterOrder());
+ }
+
+ @Test
+ void numericNamesInASubscriptAfterACommentAreSliceBoundsOrParametersByPosition() {
+ SqlParameterParser.Options o = SqlParameterParser.Options.RDS_POSTGRESQL;
+ SqlParameterParser.ParsedSql p = SqlParameterParser.parse(
+ "select arr /* a */ [1:2], ARRAY /* b */ [:3], arr /* c */ [1 /* d */ + :4] from t", o);
+ assertEquals("select arr /* a */ [1:2], ARRAY /* b */ [?], arr /* c */ [1 /* d */ + ?] from t",
+ p.sql());
+ assertEquals(List.of("3", "4"), p.parameterOrder());
+ }
}