fix(iot): parse exponent and large integer literals in rule SQL (#4441)

A number literal written with an exponent (1e5, 1E-3) or an integer beyond 64 bits failed to parse, so in the default lenient mode the rule fell back to a passthrough that ignored WHERE, and in strict mode CreateTopicRule was rejected.
The tokenizer now reads an optional exponent and such literals become decimals, following the JSON number syntax AWS IoT SQL uses for literals.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-09-25 22:27:02 -04:00
1 parent cee5b441d9
commit f51679c1f6
5 files changed
+56 -9

No files matched your search

+2
View File
@@ -282,6 +282,8 @@ Semantics:
one (`'10' > 9` is true); any other operand makes the comparison undefined.
- Payload numbers are read exactly, never through a double, so `9007199254740993.0`, `1e-400` and
`0.30000000000000004` compare as written, at any size or precision, as AWS's Decimal does.
- Number literals may carry an exponent (`1e5`, `1E-3`, `-2.411E247`), and an integer beyond 64
bits (`99999999999999999999`) is kept exact as a decimal.
- `AND`, `OR` and `NOT` take booleans or the strings `'true'` and `'false'` in any case. Any other
operand makes the result undefined.
- `startswith` and `endswith` convert numbers, booleans, arrays and objects to their string form
@@ -233,11 +233,15 @@ public final class RuleSqlParser {
return new ArrayLiteral(elements);
}
/** Only an exponent that a {@code BigDecimal} cannot hold, such as {@code 1e99999999999}, is out of range. */
private Literal numberLiteral(Token token) {
try {
return token.text().indexOf('.') < 0
? RuleSql.number(Long.parseLong(token.text()))
: RuleSql.decimal(new BigDecimal(token.text()));
String text = token.text();
BigDecimal value = new BigDecimal(text);
boolean integer = text.indexOf('.') < 0 && text.indexOf('e') < 0 && text.indexOf('E') < 0;
return integer && value.unscaledValue().bitLength() < 64
? RuleSql.number(value.longValue())
: RuleSql.decimal(value);
} catch (NumberFormatException e) {
throw fail("Number is out of range", token);
}
@@ -393,6 +397,15 @@ public final class RuleSqlParser {
break;
}
}
if (i + 1 < sql.length() && (sql.charAt(i) == 'e' || sql.charAt(i) == 'E')) {
int digit = sql.charAt(i + 1) == '+' || sql.charAt(i + 1) == '-' ? i + 2 : i + 1;
if (digit < sql.length() && Character.isDigit(sql.charAt(digit))) {
i = digit + 1;
while (i < sql.length() && Character.isDigit(sql.charAt(i))) {
i++;
}
}
}
tokens.add(new Token(Kind.NUMBER, sql.substring(start, i), start));
return i;
}
@@ -260,6 +260,29 @@ class RuleSqlEvaluatorTest {
"{\"level\":9007199254740993}").isPresent());
}
@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"level = 1e5 | {\"level\":100000} | true",
"level = 1e5 | {\"level\":1} | false",
"level > 1E-3 | {\"level\":0.01} | true",
"level > 1E-3 | {\"level\":0} | false",
"level < 2.5e+3 | {\"level\":2499.9} | true",
"level < 2.5e+3 | {\"level\":2500} | false",
"n = 99999999999999999999 | {\"n\":99999999999999999999} | true",
"n = 99999999999999999999 | {\"n\":1} | false",
"n > 99999999999999999999 | {\"n\":100000000000000000000} | true"
})
void filtersOnAnExponentOrBigIntegerLiteral(String predicate, String payload, boolean fires) {
assertEquals(fires, evaluate("SELECT * FROM 'a/b' WHERE " + predicate, "a/b", payload).isPresent());
}
@ParameterizedTest
@ValueSource(strings = {"1e5", "1E-3", "2.5e+3", "99999999999999999999"})
void projectsANumberLiteralAsTheSamePayloadNumberIsProjected(String number) {
assertEquals(text(evaluate("SELECT level FROM 'a/b'", "a/b", "{\"level\":" + number + "}")),
text(evaluate("SELECT " + number + " AS level FROM 'a/b'", "a/b", "{}")));
}
@Test
void skipsANonJsonPayloadWhenTheStatementNeedsFields() {
assertFalse(evaluate("SELECT *, topic() as topic FROM 'a/b'", "a/b", "plain text").isPresent());
@@ -103,7 +103,14 @@ class RuleSqlParserRobustnessTest {
"-17 | long:-17",
"1.50 | decimal:1.50",
"-0.001 | decimal:-0.001",
"9223372036854775807 | long:9223372036854775807"
"9223372036854775807 | long:9223372036854775807",
"-9223372036854775808 | long:-9223372036854775808",
"9223372036854775808 | decimal:9223372036854775808",
"99999999999999999999 | decimal:99999999999999999999",
"1e5 | decimal:1e5",
"1E-3 | decimal:1E-3",
"2.5e+3 | decimal:2.5e+3",
"-2.411E247 | decimal:-2.411E247"
})
void parsesEveryAcceptedLiteralForm(String literal, String expected) {
Comparison where = (Comparison) RuleSqlParser.parse("SELECT * FROM 't' WHERE a = " + literal).where();
@@ -120,7 +127,9 @@ class RuleSqlParserRobustnessTest {
@CsvSource(delimiter = '|', value = {
"1. | .",
".5 | .",
"1e5 | e5",
"1e | e",
"1e+ | +",
"1e5.5 | .",
"+1 | +",
"0x1F | x1F",
"1_000 | _000",
@@ -184,7 +193,7 @@ class RuleSqlParserRobustnessTest {
"SELECT * FROM 't' WHERE topic(a) = 'x' | topic",
"SELECT * FROM 't' WHERE topic(1.5) = 'x' | topic",
"SELECT * FROM 't' WHERE topic(-1) = 'x' | topic",
"SELECT * FROM 't' WHERE topic(99999999999999999999) = 'x' | 99999999999999999999",
"SELECT * FROM 't' WHERE topic(99999999999999999999) = 'x' | topic",
"SELECT * FROM '' | ''",
"SELECT * FROM ' ' | ' '",
"SELECT * FROM t | t",
@@ -201,11 +201,11 @@ class RuleSqlParserTest {
}
@Test
void rejectsANumberTooLargeToRepresent() {
void rejectsANumberWhoseExponentIsTooLargeToRepresent() {
RuleSqlParseException failure = assertThrows(RuleSqlParseException.class,
() -> RuleSqlParser.parse("SELECT * FROM 'a/b' WHERE level = 99999999999999999999999"));
() -> RuleSqlParser.parse("SELECT * FROM 'a/b' WHERE level = 1e99999999999"));
assertEquals("99999999999999999999999", failure.token());
assertEquals("1e99999999999", failure.token());
}
@Test