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

[Test] testing extend SparkSQL #1

Merged
merged 1 commit into from May 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Large diffs are not rendered by default.

1,781 changes: 1,781 additions & 0 deletions core/trino-parser/src/main/antlr4/io/trino/sql/sparkSQL/SparkSQLParser.g4

Large diffs are not rendered by default.

Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import io.trino.sql.tree.AllColumns;
import io.trino.sql.tree.AllRows;
import io.trino.sql.tree.AnyValue;
import io.trino.sql.tree.ArithmeticBinaryExpression;
import io.trino.sql.tree.ArithmeticUnaryExpression;
import io.trino.sql.tree.Array;
Expand Down Expand Up @@ -178,6 +179,15 @@ protected String visitExpression(Expression node, Void context)
throw new UnsupportedOperationException(format("not yet implemented: %s.visit%s", getClass().getName(), node.getClass().getSimpleName()));
}

@Override
protected String visitAnyValue(AnyValue node, Void context)
{
return new StringBuilder()
.append("ANY_VALUE(")
.append(process(node.getExpression(), context))
.append(")").toString();
}

@Override
protected String visitAtTimeZone(AtTimeZone node, Void context)
{
Expand Down
Expand Up @@ -24,20 +24,38 @@ public enum DecimalLiteralTreatment
REJECT
}

public enum SqlDialect
{
SPARKSQL,
TRINO
}

private final DecimalLiteralTreatment decimalLiteralTreatment;
private final SqlDialect sqlDialect;

public ParsingOptions()
{
this(DecimalLiteralTreatment.REJECT);
}

public ParsingOptions(DecimalLiteralTreatment decimalLiteralTreatment)
{
this(decimalLiteralTreatment, SqlDialect.TRINO);
}

public ParsingOptions(DecimalLiteralTreatment decimalLiteralTreatment, SqlDialect sqlDialect)
{
this.decimalLiteralTreatment = requireNonNull(decimalLiteralTreatment, "decimalLiteralTreatment is null");
this.sqlDialect = requireNonNull(sqlDialect, "sqlDialect is null");
}

public DecimalLiteralTreatment getDecimalLiteralTreatment()
{
return decimalLiteralTreatment;
}

public SqlDialect getSqlDialect()
{
return sqlDialect;
}
}
@@ -0,0 +1,95 @@
/*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.sql.parser;
import io.trino.sql.sparkSQL.SparkSQLParser;
import io.trino.sql.sparkSQL.SparkSQLParserBaseVisitor;
import io.trino.sql.tree.AnyValue;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.Node;
import io.trino.sql.tree.NodeLocation;
import io.trino.sql.tree.StringLiteral;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.Objects;

import static java.util.Objects.requireNonNull;

/**
* @author jiefei
* @version : SparkSQLAstBuilder.java, v 0.1 2023-04-28 15:56 jiefei
*/
public class SparkSQLAstBuilder
extends SparkSQLParserBaseVisitor<Node>
{
private int parameterPosition;
private final ParsingOptions parsingOptions;

SparkSQLAstBuilder(ParsingOptions parsingOptions)
{
this.parsingOptions = requireNonNull(parsingOptions, "parsingOptions is null");
}

@Override
public Node visitSingleStatement(SparkSQLParser.SingleStatementContext context)
{
return visit(context.statement());
}

@Override
public Node visitSingleExpression(SparkSQLParser.SingleExpressionContext context)
{
return visit(context.namedExpression());
}

@Override
public Node visitAny_value(SparkSQLParser.Any_valueContext context)
{
return new AnyValue(
getLocation(context),
(Expression) visit(context.expression()),
Objects.nonNull(context.IGNORE()));
}

@Override
public Node visitStringLiteral(SparkSQLParser.StringLiteralContext context)
{
return new StringLiteral(getLocation(context), unquote(context.getText()));
}

private static String unquote(String value)
{
return value.substring(1, value.length() - 1)
.replace("''", "'");
}

public static NodeLocation getLocation(TerminalNode terminalNode)
{
requireNonNull(terminalNode, "terminalNode is null");
return getLocation(terminalNode.getSymbol());
}

public static NodeLocation getLocation(ParserRuleContext parserRuleContext)
{
requireNonNull(parserRuleContext, "parserRuleContext is null");
return getLocation(parserRuleContext.getStart());
}

public static NodeLocation getLocation(Token token)
{
requireNonNull(token, "token is null");
return new NodeLocation(token.getLine(), token.getCharPositionInLine() + 1);
}
}
58 changes: 58 additions & 0 deletions core/trino-parser/src/main/java/io/trino/sql/parser/SqlParser.java
Expand Up @@ -13,6 +13,8 @@
*/
package io.trino.sql.parser;

import io.trino.sql.sparkSQL.SparkSQLLexer;
import io.trino.sql.sparkSQL.SparkSQLParser;
import io.trino.sql.tree.DataType;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.Node;
Expand Down Expand Up @@ -86,6 +88,9 @@ public Statement createStatement(String sql, ParsingOptions parsingOptions)

public Expression createExpression(String expression, ParsingOptions parsingOptions)
{
if (parsingOptions.getSqlDialect().equals(ParsingOptions.SqlDialect.SPARKSQL)) {
return (Expression) invokeSparkSQLParser("SparkSQL expression", expression, SparkSQLParser::singleExpression, parsingOptions);
}
return (Expression) invokeParser("expression", expression, SqlBaseParser::standaloneExpression, parsingOptions);
}

Expand Down Expand Up @@ -157,6 +162,59 @@ public Token recoverInline(Parser recognizer)
}
}

private Node invokeSparkSQLParser(String name, String sql, Function<SparkSQLParser, ParserRuleContext> parseFunction, ParsingOptions parsingOptions)
{
try {
SparkSQLLexer lexer = new SparkSQLLexer(new CaseInsensitiveStream(CharStreams.fromString(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SparkSQLParser parser = new SparkSQLParser(tokenStream);
// initializer.accept(lexer, parser);

// Override the default error strategy to not attempt inserting or deleting a token.
// Otherwise, it messes up error reporting
parser.setErrorHandler(new DefaultErrorStrategy()
{
@Override
public Token recoverInline(Parser recognizer)
throws RecognitionException
{
if (nextTokensContext == null) {
throw new InputMismatchException(recognizer);
}
throw new InputMismatchException(recognizer, nextTokensState, nextTokensContext);
}
});

// parser.addParseListener(new PostProcessor(Arrays.asList(parser.getRuleNames()), parser));
//
// lexer.removeErrorListeners();
// lexer.addErrorListener(LEXER_ERROR_LISTENER);
//
// parser.removeErrorListeners();
// parser.addErrorListener(PARSER_ERROR_HANDLER);

ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
}
catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
tokenStream.seek(0); // rewind input stream
parser.reset();

parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(parser);
}

return new SparkSQLAstBuilder(parsingOptions).visit(tree);
}
catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}

private static class PostProcessor
extends SqlBaseBaseListener
{
Expand Down
92 changes: 92 additions & 0 deletions core/trino-parser/src/main/java/io/trino/sql/tree/AnyValue.java
@@ -0,0 +1,92 @@
/*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.sql.tree;

import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* @author jiefei
* @version : AnyValue.java, v 0.1 2023-04-28 15:07 jiefei
*/
public class AnyValue
extends Expression
{
private final Expression expression;

private final Boolean isIgnoreNulls;

public AnyValue(Expression expression, Boolean isIgnoreNulls)
{
this(Optional.empty(), expression, isIgnoreNulls);
}

public AnyValue(NodeLocation location, Expression expression, Boolean isIgnoreNulls)
{
this(Optional.of(location), expression, isIgnoreNulls);
}

protected AnyValue(Optional<NodeLocation> location, Expression expression, Boolean isIgnoreNulls)
{
super(location);
this.expression = expression;
this.isIgnoreNulls = isIgnoreNulls;
}

public Expression getExpression()
{
return expression;
}

public Boolean getIsIgnoreNulls()
{
return isIgnoreNulls;
}

@Override
protected <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
return visitor.visitAnyValue(this, context);
}

@Override
public List<? extends Node> getChildren()
{
ImmutableList.Builder<Node> nodes = ImmutableList.builder();
nodes.add(expression);
return nodes.build();
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AnyValue anyValue = (AnyValue) o;
return Objects.equals(expression, anyValue.expression) && Objects.equals(isIgnoreNulls, anyValue.isIgnoreNulls);
}

@Override
public int hashCode()
{
return Objects.hash(expression, isIgnoreNulls);
}
}
Expand Up @@ -1166,4 +1166,9 @@ protected R visitEmptyTableTreatment(EmptyTableTreatment node, C context)
{
return visitNode(node, context);
}

protected R visitAnyValue(AnyValue node, C context)
{
return visitExpression(node, context);
}
}
Expand Up @@ -991,4 +991,11 @@ protected Void visitTableFunctionInvocation(TableFunctionInvocation node, C cont

return null;
}

@Override
protected Void visitAnyValue(AnyValue node, C context)
{
process(node.getExpression(), context);
return null;
}
}
Expand Up @@ -17,6 +17,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import io.trino.sql.ExpressionFormatter;
import io.trino.sql.tree.AddColumn;
import io.trino.sql.tree.AliasedRelation;
import io.trino.sql.tree.AllColumns;
Expand Down Expand Up @@ -295,6 +296,14 @@ public class TestSqlParser
{
private static final SqlParser SQL_PARSER = new SqlParser();

@Test
public void testAnyValue()
{
Expression expression = SQL_PARSER.createExpression("ANY_VALUE('test sparkSQL expression')",
new ParsingOptions(REJECT, ParsingOptions.SqlDialect.SPARKSQL));
System.out.println(ExpressionFormatter.formatExpression(expression));
}

@Test
public void testPosition()
{
Expand Down