mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(redshift): tokenize streaming view JOIN and aggregate checks with SqlParameterParser (#8977)
The JOIN and aggregate checks on a streaming materialized view blanked string literals with their own regex, which paired the quotes of an escape string such as E'\'' wrongly: a JOIN or an aggregate after it could hide inside a bogus literal, and a keyword inside E'it\'s a join' was refused. The checks now run on the query as SqlParameterParser tokenizes it, through a new blankQuoted that empties quoted strings, quoted identifiers and dollar-quoted bodies and drops comments. The Redshift DDL rewrite masks its string literals through the same tokenizer's literalSpans instead of a second regex.
This commit is contained in:
1 parent
9ea2f038f7
commit
8db9542226
6 files changed
+120
-12
No files matched your search
@@ -12,6 +12,31 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class SqlParameterParserTest {
|
||||
|
||||
@Test
|
||||
void blankQuotedEmptiesLiteralsIdentifiersAndDollarBodiesAndDropsComments() {
|
||||
assertEquals("select '', '' from t where a = ''",
|
||||
SqlParameterParser.blankQuoted(
|
||||
"select 'a join b', \"group by\" from t where a = $$ x $$", Options.REDSHIFT));
|
||||
assertEquals("select 1 from t",
|
||||
SqlParameterParser.blankQuoted("select 1 /* join */ from t", Options.REDSHIFT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankQuotedReadsABackslashQuoteInAnEscapeStringAsPartOfTheLiteral() {
|
||||
assertEquals("select E'' from t join u",
|
||||
SqlParameterParser.blankQuoted("select E'\\' x' from t join u", Options.REDSHIFT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void literalSpansCoverStringsAndDollarBodiesButNotIdentifiersOrComments() {
|
||||
String sql = "select E'\\'' , \"a'b\", $t$ x $t$ /* 'c' */ -- 'd'\n, 'e''f'";
|
||||
|
||||
List<String> literals = SqlParameterParser.literalSpans(sql, Options.REDSHIFT).stream()
|
||||
.map(span -> sql.substring(span[0], span[1])).toList();
|
||||
|
||||
assertEquals(List.of("'\\''", "$t$ x $t$", "'e''f'"), literals);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rewritesNamedPlaceholdersToPositional() {
|
||||
ParsedSql parsed = SqlParameterParser.parse(
|
||||
|
||||
@@ -353,6 +353,17 @@ class RedshiftSqlInterceptorTest {
|
||||
assertSame(sql, RedshiftSqlInterceptor.rewrite(sql));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReadABackslashQuoteInAnEscapeStringAsPartOfTheLiteral() {
|
||||
String sql = "CREATE TABLE t (a VARCHAR(20) DEFAULT E'\\'', b INT DISTKEY, c VARCHAR(20) DEFAULT ' DISTKEY ');";
|
||||
|
||||
String rewritten = RedshiftSqlInterceptor.rewrite(sql);
|
||||
|
||||
assertTrue(!rewritten.contains("b INT DISTKEY"), "Should remove column-level DISTKEY: " + rewritten);
|
||||
assertTrue(rewritten.contains("DEFAULT E'\\''"), "Should keep the escape string: " + rewritten);
|
||||
assertTrue(rewritten.contains("' DISTKEY '"), "Should keep DISTKEY inside the literal: " + rewritten);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotTouchABinaryTypeOutsideTableDdl() {
|
||||
String sql = "SELECT CAST(x AS VARBYTE(10)) FROM t";
|
||||
|
||||
+19
@@ -136,6 +136,25 @@ class SpectrumKinesisViewPreparationTest {
|
||||
SESSION, backend);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEscapedQuoteInAnEscapeStringDoesNotHideAJoinOrAnAggregateFromTheChecks() {
|
||||
SpectrumSqlException join = assertThrows(SpectrumSqlException.class, () -> preparation.prepare(
|
||||
"CREATE MATERIALIZED VIEW v AS SELECT E'\\'' AS q FROM kds.events e JOIN public.t ON e.shard_id = 'a'",
|
||||
SESSION, backend));
|
||||
SpectrumSqlException aggregate = assertThrows(SpectrumSqlException.class, () -> preparation.prepare(
|
||||
"CREATE MATERIALIZED VIEW v AS SELECT E'\\'' AS q, count(*) FROM kds.events WHERE shard_id = 'a'",
|
||||
SESSION, backend));
|
||||
|
||||
assertThat(join.getMessage(), containsString("JOIN"));
|
||||
assertThat(aggregate.getMessage(), containsString("aggregate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeywordInsideAnEscapeStringWithAnEscapedQuoteIsNotTakenForAJoin() {
|
||||
preparation.prepare("CREATE MATERIALIZED VIEW v AS SELECT E'it\\'s a join' AS c FROM kds.events",
|
||||
SESSION, backend);
|
||||
}
|
||||
|
||||
@Test
|
||||
void droppingTheLastViewAlsoDropsTheStagingTable() {
|
||||
preparation.prepare(CREATE_VIEW, SESSION, backend);
|
||||
|
||||
Reference in new issue
Block a user