Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create persistent queries fail with reserved words #3216

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,16 @@ public String visitDereferenceExpression(
final Context context) {
final String baseString = process(node.getBase(), context);
if (node.getBase() instanceof QualifiedNameReference) {
return baseString + KsqlConstants.DOT + formatIdentifier(node.getFieldName());
return baseString + KsqlConstants.DOT
+ quoteReservedWord(formatIdentifier(node.getFieldName()), context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we only quoting reserved here and not elsewhere? why not just do it in formatIdentifier?

}
return baseString + KsqlConstants.STRUCT_FIELD_REF + formatIdentifier(node.getFieldName());
}

private String quoteReservedWord(final String s, final Context context) {
return context.isReserved.test(s) ? "`" + s + "`" : s;
}

private static String formatQualifiedName(final QualifiedName name) {
final List<String> parts = new ArrayList<>();
for (final String part : name.getParts()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public class SqlFormatterTest {
.valueField("ARRAYCOL", SqlTypes.array(SqlTypes.DOUBLE))
.valueField("MAPCOL", SqlTypes.map(SqlTypes.DOUBLE))
.valueField("ADDRESS", addressSchema)
.valueField("SIZE", SqlTypes.INTEGER) // Reserved word
.build();

private static final CreateSourceProperties SOME_WITH_PROPS = CreateSourceProperties.from(
Expand Down Expand Up @@ -358,6 +359,15 @@ public void shouldFormatSelectStarCorrectly() {
+ "FROM ADDRESS ADDRESS"));
}

@Test
public void shouldFormatCSASWithReservedWords() {
final String statementString = "CREATE STREAM S AS SELECT ITEMID, \"SIZE\" FROM address;";
final Statement statement = parseSingle(statementString);
assertThat(SqlFormatter.formatSql(statement),
equalTo("CREATE STREAM S AS SELECT\n" +
" ADDRESS.ITEMID \"ITEMID\",\n ADDRESS.`SIZE` \"SIZE\"\nFROM ADDRESS ADDRESS"));
}

@Test
public void shouldFormatSelectStarCorrectlyWithOtherFields() {
final String statementString = "CREATE STREAM S AS SELECT *, address AS city FROM address;";
Expand Down