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: quoted identifiers for source names #3695

Merged
merged 3 commits into from
Oct 30, 2019
Merged
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 @@ -89,6 +89,19 @@
"outputs": [
{"topic": "JOINED", "key": "1", "value": { "field!": "A"}}
]
},
{
"name": "source names requiring quotes",
"statements": [
"CREATE STREAM `foo-source` (id VARCHAR) WITH (kafka_topic='foo-source', value_format='JSON');",
"CREATE STREAM `foo-too` AS SELECT `foo-source`.id FROM `foo-source`;"
],
"inputs": [
{"topic": "foo-source", "value": {"id": "1"}}
],
"outputs": [
{"topic": "foo-too", "value": {"ID": "1"}}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@
"outputs": [
{"topic": "test_topic", "key": null, "value": {"I": -1, "A": [1, 2, 3]}}
]
},
{
"name": "should handle quoted identifiers",
"statements": [
"CREATE STREAM `test` (`id!` INT) WITH (kafka_topic='test_topic', value_format='JSON');",
"INSERT INTO `test` (ROWTIME, ROWKEY, `id!`) VALUES (1234, 'key', 10);"
],
"inputs": [
],
"outputs": [
{"topic": "test_topic", "timestamp": 1234, "key": "key", "value": {"id!": 10}}
],
"responses": [
{"@type": "currentStatus", "statementText": "{STATEMENT}"}
]
}
]
}
26 changes: 15 additions & 11 deletions ksql-parser/src/main/antlr4/io/confluent/ksql/parser/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ statement
| (LIST | SHOW) FUNCTIONS #listFunctions
| (LIST | SHOW) (SOURCE | SINK)? CONNECTORS #listConnectors
| (LIST | SHOW) TYPES #listTypes
| DESCRIBE EXTENDED? identifier #showColumns
| DESCRIBE EXTENDED? sourceName #showColumns
| DESCRIBE FUNCTION identifier #describeFunction
| DESCRIBE CONNECTOR identifier #describeConnector
| PRINT (identifier| STRING) printClause #printTopic
| (LIST | SHOW) QUERIES EXTENDED? #listQueries
| TERMINATE QUERY? identifier #terminateQuery
| SET STRING EQ STRING #setProperty
| UNSET STRING #unsetProperty
| CREATE STREAM (IF NOT EXISTS)? identifier
| CREATE STREAM (IF NOT EXISTS)? sourceName
(tableElements)?
(WITH tableProperties)? #createStream
| CREATE STREAM (IF NOT EXISTS)? identifier
| CREATE STREAM (IF NOT EXISTS)? sourceName
(WITH tableProperties)? AS query
(PARTITION BY identifier)? #createStreamAs
| CREATE TABLE (IF NOT EXISTS)? identifier
| CREATE TABLE (IF NOT EXISTS)? sourceName
(tableElements)?
(WITH tableProperties)? #createTable
| CREATE TABLE (IF NOT EXISTS)? identifier
| CREATE TABLE (IF NOT EXISTS)? sourceName
(WITH tableProperties)? AS query #createTableAs
| CREATE (SINK | SOURCE) CONNECTOR identifier WITH tableProperties #createConnector
| INSERT INTO identifier query (PARTITION BY identifier)? #insertInto
| INSERT INTO identifier (columns)? VALUES values #insertValues
| DROP STREAM (IF EXISTS)? identifier (DELETE TOPIC)? #dropStream
| DROP TABLE (IF EXISTS)? identifier (DELETE TOPIC)? #dropTable
| INSERT INTO sourceName query (PARTITION BY identifier)? #insertInto
| INSERT INTO sourceName (columns)? VALUES values #insertValues
| DROP STREAM (IF EXISTS)? sourceName (DELETE TOPIC)? #dropStream
| DROP TABLE (IF EXISTS)? sourceName (DELETE TOPIC)? #dropTable
| DROP CONNECTOR identifier #dropConnector
| EXPLAIN (statement | identifier) #explain
| RUN SCRIPT STRING #runScript
Expand Down Expand Up @@ -200,15 +200,15 @@ joinCriteria
;

aliasedRelation
: relationPrimary (AS? identifier)?
: relationPrimary (AS? sourceName)?
;

columns
: '(' identifier (',' identifier)* ')'
;

relationPrimary
: identifier #tableName
: sourceName #tableName
;

expression
Expand Down Expand Up @@ -304,6 +304,10 @@ identifier
| DIGIT_IDENTIFIER #digitIdentifier
;

sourceName
: identifier
;

number
: DECIMAL_VALUE #decimalLiteral
| INTEGER_VALUE #integerLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import io.confluent.ksql.parser.SqlBaseParser.ListTypesContext;
import io.confluent.ksql.parser.SqlBaseParser.RegisterTypeContext;
import io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext;
import io.confluent.ksql.parser.SqlBaseParser.SourceNameContext;
import io.confluent.ksql.parser.SqlBaseParser.TablePropertiesContext;
import io.confluent.ksql.parser.SqlBaseParser.TablePropertyContext;
import io.confluent.ksql.parser.properties.with.CreateSourceAsProperties;
Expand Down Expand Up @@ -240,7 +241,7 @@ public Node visitCreateTable(final SqlBaseParser.CreateTableContext context) {

return new CreateTable(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier())),
ParserUtil.getSourceName(context.sourceName()),
TableElements.of(elements),
context.EXISTS() != null,
CreateSourceProperties.from(properties)
Expand All @@ -257,7 +258,7 @@ public Node visitCreateStream(final SqlBaseParser.CreateStreamContext context) {

return new CreateStream(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier())),
ParserUtil.getSourceName(context.sourceName()),
TableElements.of(elements),
context.EXISTS() != null,
CreateSourceProperties.from(properties)
Expand All @@ -272,11 +273,11 @@ public Node visitCreateStreamAs(final SqlBaseParser.CreateStreamAsContext contex

return new CreateStreamAsSelect(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier(0))),
ParserUtil.getSourceName(context.sourceName()),
query,
context.EXISTS() != null,
CreateSourceAsProperties.from(properties),
getPartitionBy(context.identifier(1))
getPartitionBy(context.identifier())
);
}

Expand All @@ -288,7 +289,7 @@ public Node visitCreateTableAs(final SqlBaseParser.CreateTableAsContext context)

return new CreateTableAsSelect(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier())),
ParserUtil.getSourceName(context.sourceName()),
query,
context.EXISTS() != null,
CreateSourceAsProperties.from(properties)
Expand All @@ -312,8 +313,8 @@ public Node visitCreateConnector(final CreateConnectorContext context) {
@Override
public Node visitInsertInto(final SqlBaseParser.InsertIntoContext context) {

final SourceName targetName = SourceName.of(getIdentifierText(context.identifier(0)));
final Optional<NodeLocation> targetLocation = getLocation(context.identifier(0));
final SourceName targetName = ParserUtil.getSourceName(context.sourceName());
final Optional<NodeLocation> targetLocation = getLocation(context.sourceName());

final DataSource<?> target = getSource(targetName, targetLocation);

Expand All @@ -329,13 +330,13 @@ public Node visitInsertInto(final SqlBaseParser.InsertIntoContext context) {
getLocation(context),
targetName,
query,
getPartitionBy(context.identifier(1)));
getPartitionBy(context.identifier()));
}

@Override
public Node visitInsertValues(final InsertValuesContext context) {
final String targetName = getIdentifierText(context.identifier());
final Optional<NodeLocation> targetLocation = getLocation(context.identifier());
final SourceName targetName = ParserUtil.getSourceName(context.sourceName());
final Optional<NodeLocation> targetLocation = getLocation(context.sourceName());

final List<ColumnName> columns;
if (context.columns() != null) {
Expand All @@ -350,7 +351,7 @@ public Node visitInsertValues(final InsertValuesContext context) {

return new InsertValues(
targetLocation,
SourceName.of(targetName),
targetName,
columns,
visit(context.values().valueExpression(), Expression.class));
}
Expand All @@ -359,7 +360,7 @@ public Node visitInsertValues(final InsertValuesContext context) {
public Node visitDropTable(final SqlBaseParser.DropTableContext context) {
return new DropTable(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier())),
ParserUtil.getSourceName(context.sourceName()),
context.EXISTS() != null,
context.DELETE() != null
);
Expand All @@ -369,7 +370,7 @@ public Node visitDropTable(final SqlBaseParser.DropTableContext context) {
public Node visitDropStream(final SqlBaseParser.DropStreamContext context) {
return new DropStream(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier())),
ParserUtil.getSourceName(context.sourceName()),
context.EXISTS() != null,
context.DELETE() != null
);
Expand Down Expand Up @@ -670,15 +671,16 @@ public Node visitListTypes(final ListTypesContext ctx) {
public Node visitTerminateQuery(final SqlBaseParser.TerminateQueryContext context) {
return new TerminateQuery(
getLocation(context),
context.identifier().getText()
// use case sensitive parsing here to maintain backwards compatibility
ParserUtil.getIdentifierText(true, context.identifier())
);
}

@Override
public Node visitShowColumns(final SqlBaseParser.ShowColumnsContext context) {
return new ShowColumns(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier())),
ParserUtil.getSourceName(context.sourceName()),
context.EXTENDED() != null
);
}
Expand Down Expand Up @@ -781,19 +783,19 @@ public Node visitJoinRelation(final SqlBaseParser.JoinRelationContext context) {
public Node visitAliasedRelation(final SqlBaseParser.AliasedRelationContext context) {
final Relation child = (Relation) visit(context.relationPrimary());

final String alias;
final SourceName alias;
switch (context.children.size()) {
case 1:
final Table table = (Table) visit(context.relationPrimary());
alias = table.getName().name();
alias = table.getName();
break;

case 2:
alias = context.children.get(1).getText();
alias = ParserUtil.getSourceName((SourceNameContext) context.children.get(1));
break;

case 3:
alias = context.children.get(2).getText();
alias = ParserUtil.getSourceName((SourceNameContext) context.children.get(2));
break;

default:
Expand All @@ -803,14 +805,14 @@ public Node visitAliasedRelation(final SqlBaseParser.AliasedRelationContext cont
);
}

return new AliasedRelation(getLocation(context), child, SourceName.of(alias.toUpperCase()));
return new AliasedRelation(getLocation(context), child, alias);
}

@Override
public Node visitTableName(final SqlBaseParser.TableNameContext context) {
return new Table(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier()))
ParserUtil.getSourceName(context.sourceName())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.confluent.ksql.parser.NodeLocation;
import io.confluent.ksql.parser.SqlBaseBaseVisitor;
import io.confluent.ksql.parser.SqlBaseParser;
import io.confluent.ksql.parser.SqlBaseParser.SourceNameContext;
import io.confluent.ksql.parser.tree.AliasedRelation;
import io.confluent.ksql.parser.tree.AstNode;
import io.confluent.ksql.parser.tree.Table;
Expand Down Expand Up @@ -117,25 +118,26 @@ public AstNode visitQuery(final SqlBaseParser.QueryContext ctx) {
public AstNode visitTableName(final SqlBaseParser.TableNameContext context) {
return new Table(
getLocation(context),
SourceName.of(ParserUtil.getIdentifierText(context.identifier())));
ParserUtil.getSourceName(context.sourceName())
);
}

@Override
public AstNode visitAliasedRelation(final SqlBaseParser.AliasedRelationContext context) {
final Table table = (Table) visit(context.relationPrimary());

final String alias;
final SourceName alias;
switch (context.children.size()) {
case 1:
alias = table.getName().name().toUpperCase();
alias = table.getName();
break;

case 2:
alias = context.children.get(1).getText().toUpperCase();
alias = ParserUtil.getSourceName((SourceNameContext) context.children.get(1));
break;

case 3:
alias = context.children.get(2).getText().toUpperCase();
alias = ParserUtil.getSourceName((SourceNameContext) context.children.get(2));
break;

default:
Expand All @@ -146,16 +148,16 @@ public AstNode visitAliasedRelation(final SqlBaseParser.AliasedRelationContext c
}

if (!isJoin) {
fromAlias = SourceName.of(alias);
fromName = SourceName.of(table.getName().name().toUpperCase());
fromAlias = alias;
fromName = table.getName();
if (metaStore.getSource(fromName) == null) {
throw new KsqlException(table.getName().name() + " does not exist.");
}

return null;
}

return new AliasedRelation(getLocation(context), table, SourceName.of(alias.toUpperCase()));
return new AliasedRelation(getLocation(context), table, alias);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,45 @@
import io.confluent.ksql.execution.expression.tree.IntegerLiteral;
import io.confluent.ksql.execution.expression.tree.Literal;
import io.confluent.ksql.execution.expression.tree.LongLiteral;
import io.confluent.ksql.name.SourceName;
import io.confluent.ksql.parser.NodeLocation;
import io.confluent.ksql.parser.ParsingException;
import io.confluent.ksql.parser.SqlBaseParser;
import io.confluent.ksql.parser.SqlBaseParser.IntegerLiteralContext;
import io.confluent.ksql.parser.SqlBaseParser.NumberContext;
import io.confluent.ksql.parser.SqlBaseParser.SourceNameContext;
import io.confluent.ksql.parser.exception.ParseFailedException;
import java.util.Optional;
import java.util.regex.Pattern;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;

public final class ParserUtil {

/**
* Source names must adhere to the kafka topic naming convention. We restrict
* it here instead of as a parser rule to allow for a more descriptive error
* message and to avoid duplicated rules.
*
* @see org.apache.kafka.streams.state.StoreBuilder#name
*/
private static final Pattern VALID_SOURCE_NAMES = Pattern.compile("[a-zA-Z0-9_-]*");

private ParserUtil() {
}

public static SourceName getSourceName(final SourceNameContext sourceName) {
final String text = getIdentifierText(sourceName.identifier());
if (!VALID_SOURCE_NAMES.matcher(text).matches()) {
throw new ParseFailedException(
"Illegal argument at " + getLocation(sourceName).map(NodeLocation::toString).orElse("?")
+ ". Source names may only contain alphanumeric values, '_' or '-'. Got: '"
+ text + "'");
}
return SourceName.of(text);
}

public static String getIdentifierText(final SqlBaseParser.IdentifierContext context) {
return getIdentifierText(false, context);
}
Expand Down
Loading