-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: serialize expressions Adds serialization modules for serializing/parsing Expression, ColumnRef, SelectExpression, and WindowExpression into/from ksql snippets. ColumnRef is serialized to / parsed from column reference expressions. SelectExpression is serialized to / parsed from SingleColumn AST nodes.
- Loading branch information
Showing
32 changed files
with
1,159 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
ksql-parser/src/main/java/io/confluent/ksql/parser/ExpressionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.parser; | ||
|
||
import io.confluent.ksql.execution.expression.tree.Expression; | ||
import io.confluent.ksql.execution.plan.SelectExpression; | ||
import io.confluent.ksql.execution.windows.KsqlWindowExpression; | ||
import io.confluent.ksql.metastore.TypeRegistry; | ||
import io.confluent.ksql.name.ColumnName; | ||
import io.confluent.ksql.parser.tree.WindowExpression; | ||
import io.confluent.ksql.util.ParserUtil; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
|
||
public final class ExpressionParser { | ||
private ExpressionParser() { | ||
} | ||
|
||
public static SelectExpression parseSelectExpression(final String expressionText) { | ||
final SqlBaseParser.SelectItemContext parseCtx = GrammarParseUtil.getParseTree( | ||
expressionText, | ||
SqlBaseParser::selectItem | ||
); | ||
if (!(parseCtx instanceof SqlBaseParser.SelectSingleContext)) { | ||
throw new IllegalArgumentException("Illegal select item type in: " + expressionText); | ||
} | ||
final SqlBaseParser.SelectSingleContext selectSingleContext = | ||
(SqlBaseParser.SelectSingleContext) parseCtx; | ||
if (selectSingleContext.identifier() == null) { | ||
throw new IllegalArgumentException("Select item must have identifier in: " + expressionText); | ||
} | ||
return SelectExpression.of( | ||
ColumnName.of(ParserUtil.getIdentifierText(selectSingleContext.identifier())), | ||
new AstBuilder(TypeRegistry.EMPTY).buildExpression(selectSingleContext.expression()) | ||
); | ||
} | ||
|
||
public static Expression parseExpression(final String expressionText) { | ||
final ParserRuleContext parseTree = GrammarParseUtil.getParseTree( | ||
expressionText, | ||
SqlBaseParser::singleExpression | ||
); | ||
return new AstBuilder(TypeRegistry.EMPTY).buildExpression(parseTree); | ||
} | ||
|
||
public static KsqlWindowExpression parseWindowExpression(final String expressionText) { | ||
final ParserRuleContext parseTree = GrammarParseUtil.getParseTree( | ||
expressionText, | ||
SqlBaseParser::windowExpression | ||
); | ||
final WindowExpression windowExpression = | ||
new AstBuilder(TypeRegistry.EMPTY).buildWindowExpression(parseTree); | ||
return windowExpression.getKsqlWindowExpression(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
ksql-parser/src/main/java/io/confluent/ksql/parser/GrammarParseUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.parser; | ||
|
||
import java.util.function.Function; | ||
import org.antlr.v4.runtime.BaseErrorListener; | ||
import org.antlr.v4.runtime.CharStreams; | ||
import org.antlr.v4.runtime.CommonTokenStream; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
import org.antlr.v4.runtime.RecognitionException; | ||
import org.antlr.v4.runtime.Recognizer; | ||
import org.antlr.v4.runtime.atn.PredictionMode; | ||
import org.antlr.v4.runtime.misc.ParseCancellationException; | ||
|
||
final class GrammarParseUtil { | ||
private static final BaseErrorListener ERROR_LISTENER = new BaseErrorListener() { | ||
@Override | ||
public void syntaxError( | ||
final Recognizer<?, ?> recognizer, | ||
final Object offendingSymbol, | ||
final int line, | ||
final int charPositionInLine, | ||
final String message, | ||
final RecognitionException e | ||
) { | ||
throw new ParsingException(message, e, line, charPositionInLine); | ||
} | ||
}; | ||
|
||
private GrammarParseUtil() { | ||
} | ||
|
||
static <T extends ParserRuleContext> T getParseTree( | ||
final String text, | ||
final Function<SqlBaseParser, T> parseFunction) { | ||
final SqlBaseLexer sqlBaseLexer = new SqlBaseLexer( | ||
new CaseInsensitiveStream(CharStreams.fromString(text))); | ||
final CommonTokenStream tokenStream = new CommonTokenStream(sqlBaseLexer); | ||
final SqlBaseParser sqlBaseParser = new SqlBaseParser(tokenStream); | ||
|
||
sqlBaseLexer.removeErrorListeners(); | ||
sqlBaseLexer.addErrorListener(ERROR_LISTENER); | ||
|
||
sqlBaseParser.removeErrorListeners(); | ||
sqlBaseParser.addErrorListener(ERROR_LISTENER); | ||
|
||
try { | ||
// first, try parsing w/ potentially faster SLL mode | ||
sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.SLL); | ||
return castContext(parseFunction.apply(sqlBaseParser)); | ||
} catch (final ParseCancellationException ex) { | ||
// if we fail, parse with LL mode | ||
tokenStream.seek(0); // rewind input stream | ||
sqlBaseParser.reset(); | ||
|
||
sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.LL); | ||
return castContext(parseFunction.apply(sqlBaseParser)); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T extends ParserRuleContext> T castContext(final ParserRuleContext ctx) { | ||
return (T) ctx; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
ksql-parser/src/main/java/io/confluent/ksql/parser/json/ColumnRefDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.parser.json; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import io.confluent.ksql.execution.expression.tree.ColumnReferenceExp; | ||
import io.confluent.ksql.execution.expression.tree.Expression; | ||
import io.confluent.ksql.parser.ExpressionParser; | ||
import io.confluent.ksql.schema.ksql.ColumnRef; | ||
import java.io.IOException; | ||
|
||
class ColumnRefDeserializer extends JsonDeserializer<ColumnRef> { | ||
@Override | ||
public ColumnRef deserialize(final JsonParser parser, final DeserializationContext ctx) | ||
throws IOException { | ||
final Expression expression | ||
= ExpressionParser.parseExpression(parser.readValueAs(String.class)); | ||
if (expression instanceof ColumnReferenceExp) { | ||
return ((ColumnReferenceExp) expression).getReference(); | ||
} | ||
throw new IllegalArgumentException("Passed JSON is not a column reference: " + expression); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
ksql-parser/src/main/java/io/confluent/ksql/parser/json/ColumnRefSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.parser.json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import io.confluent.ksql.schema.ksql.ColumnRef; | ||
import io.confluent.ksql.schema.ksql.FormatOptions; | ||
import io.confluent.ksql.util.IdentifierUtil; | ||
import java.io.IOException; | ||
|
||
class ColumnRefSerializer extends JsonSerializer<ColumnRef> { | ||
@Override | ||
public void serialize( | ||
final ColumnRef columnRef, | ||
final JsonGenerator gen, | ||
final SerializerProvider provider) throws IOException { | ||
gen.writeString(columnRef.toString(FormatOptions.of(IdentifierUtil::needsQuotes))); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
ksql-parser/src/main/java/io/confluent/ksql/parser/json/ExpressionDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.parser.json; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import io.confluent.ksql.execution.expression.tree.Expression; | ||
import io.confluent.ksql.parser.ExpressionParser; | ||
import java.io.IOException; | ||
|
||
class ExpressionDeserializer<T extends Expression> extends JsonDeserializer<T> { | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public T deserialize(final JsonParser parser, final DeserializationContext ctx) | ||
throws IOException { | ||
return (T) ExpressionParser.parseExpression(parser.readValueAs(String.class)); | ||
} | ||
} |
Oops, something went wrong.