Search in sources :

Example 1 with ParsingException

use of io.prestosql.sql.parser.ParsingException in project hetu-core by openlookeng.

the class TypeCalculation method calculateLiteralValue.

public static Long calculateLiteralValue(String calculation, Map<String, Long> inputs) {
    try {
        ParserRuleContext tree = parseTypeCalculation(calculation);
        CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs);
        BigInteger result = visitor.visit(tree);
        return result.longValueExact();
    } catch (StackOverflowError e) {
        throw new ParsingException("Type calculation is too large (stack overflow while parsing)");
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParsingException(io.prestosql.sql.parser.ParsingException) BigInteger(java.math.BigInteger)

Example 2 with ParsingException

use of io.prestosql.sql.parser.ParsingException in project hetu-core by openlookeng.

the class HttpRequestSessionContext method parsePreparedStatementsHeaders.

private static Map<String, String> parsePreparedStatementsHeaders(HttpServletRequest servletRequest) {
    ImmutableMap.Builder<String, String> preparedStatementsBuilder = ImmutableMap.builder();
    parseProperty(servletRequest, PRESTO_PREPARED_STATEMENT).forEach((key, sqlString) -> {
        String statementName;
        try {
            statementName = urlDecode(key);
        } catch (IllegalArgumentException e) {
            throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
        }
        // Validate statement
        SqlParser sqlParser = new SqlParser();
        try {
            sqlParser.createStatement(sqlString, new ParsingOptions(AS_DOUBLE));
        } catch (ParsingException e) {
            throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
        }
        preparedStatementsBuilder.put(statementName, sqlString);
    });
    return preparedStatementsBuilder.build();
}
Also used : ParsingOptions(io.prestosql.sql.parser.ParsingOptions) ParsingException(io.prestosql.sql.parser.ParsingException) SqlParser(io.prestosql.sql.parser.SqlParser) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 3 with ParsingException

use of io.prestosql.sql.parser.ParsingException in project hetu-core by openlookeng.

the class HeuristicIndexUtils method extractPartitions.

/**
 * Given one of these expressions types:
 * - partition=1
 * - partition=1 or partition=2
 * - partition in (1,2)
 * - partition=1 or partition in (2)
 *
 * extract the partitions as a list of key=value pairs
 * @param expression
 * @return
 */
public static List<String> extractPartitions(Expression expression) {
    if (expression instanceof ComparisonExpression) {
        ComparisonExpression exp = (ComparisonExpression) expression;
        if (exp.getOperator() == ComparisonExpression.Operator.EQUAL) {
            return Collections.singletonList(parsePartitionName(exp.getLeft().toString()) + "=" + parsePartitionValue(exp.getRight().toString()));
        }
    } else if (expression instanceof LogicalBinaryExpression) {
        LogicalBinaryExpression exp = (LogicalBinaryExpression) expression;
        if (exp.getOperator() == LogicalBinaryExpression.Operator.OR) {
            Expression left = exp.getLeft();
            Expression right = exp.getRight();
            return Stream.concat(extractPartitions(left).stream(), extractPartitions(right).stream()).collect(Collectors.toList());
        }
    } else if (expression instanceof InPredicate) {
        Expression valueList = ((InPredicate) expression).getValueList();
        if (valueList instanceof InListExpression) {
            InListExpression inListExpression = (InListExpression) valueList;
            List<String> res = new LinkedList<>();
            for (Expression expr : inListExpression.getValues()) {
                res.add(parsePartitionName(((InPredicate) expression).getValue().toString()) + "=" + parsePartitionValue(expr.toString()));
            }
            if (res.size() > 0) {
                return res;
            }
        }
    }
    throw new ParsingException("Unsupported WHERE expression. Only in-predicate/equality-expressions are supported " + "e.g. partition=1 or partition=2/partition in (1,2)");
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) InListExpression(io.prestosql.sql.tree.InListExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ParsingException(io.prestosql.sql.parser.ParsingException) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) LinkedList(java.util.LinkedList)

Example 4 with ParsingException

use of io.prestosql.sql.parser.ParsingException in project hetu-core by openlookeng.

the class CubeConsole method createCubeCommand.

/**
 * Process the Create Cube Query
 *
 * @param queryRunner queryRunner
 * @param outputFormat outputFormat
 * @param schemaChanged schemaChanged
 * @param usePager usePager
 * @param schemaChanged schemaChanged
 * @param showProgress showProgress
 * @param terminal terminal
 * @param out out
 * @param errorChannel errorChannel
 * @return boolean after processing the create cube query command.
 */
public boolean createCubeCommand(String query, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel) {
    boolean success = true;
    SqlParser parser = new SqlParser();
    QualifiedName cubeName = null;
    try {
        CreateCube createCube = (CreateCube) parser.createStatement(query, new ParsingOptions(ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE));
        cubeName = createCube.getCubeName();
        QualifiedName sourceTableName = createCube.getSourceTableName();
        String whereClause = createCube.getWhere().get().toString();
        Set<FunctionCall> aggregations = createCube.getAggregations();
        List<Identifier> groupingSet = createCube.getGroupingSet();
        List<Property> properties = createCube.getProperties();
        boolean notExists = createCube.isNotExists();
        CreateCube modifiedCreateCube = new CreateCube(cubeName, sourceTableName, groupingSet, aggregations, notExists, properties, Optional.empty(), createCube.getSourceFilter().orElse(null));
        String queryCreateCube = SqlFormatter.formatSql(modifiedCreateCube, Optional.empty());
        if (!console.runQuery(queryRunner, queryCreateCube, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
            return false;
        }
        // we check whether the create cube expression can be processed
        if (isSupportedExpression(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
            if (createCube.getWhere().get() instanceof BetweenPredicate) {
                // we process the between predicate in the create cube query where clause
                success = processBetweenPredicate(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel, parser);
            }
            if (createCube.getWhere().get() instanceof ComparisonExpression) {
                // we process the comparison expression in the create cube query where clause
                success = processComparisonExpression(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel, parser);
            }
        } else {
            // if we donot support processing the create cube query with multiple inserts, then only a single insert is run internally.
            String queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, whereClause);
            success = console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
        if (!success) {
            // roll back mechanism for unsuccessful create cube query
            String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
            console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
    } catch (ParsingException e) {
        if (cubeName != null) {
            // roll back mechanism for unsuccessful create cube query
            String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
            console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
        System.out.println(e.getMessage());
        Query.renderErrorLocation(query, new ErrorLocation(e.getLineNumber(), e.getColumnNumber()), errorChannel);
        success = false;
    } catch (Exception e) {
        if (cubeName != null) {
            // roll back mechanism for unsuccessful create cube query
            String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
            console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
        // Add blank line after progress bar
        System.out.println();
        System.out.println(e.getMessage());
        success = false;
    }
    return success;
}
Also used : ErrorLocation(io.prestosql.client.ErrorLocation) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) QualifiedName(io.prestosql.sql.tree.QualifiedName) SqlParser(io.prestosql.sql.parser.SqlParser) ParsingException(io.prestosql.sql.parser.ParsingException) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Identifier(io.prestosql.sql.tree.Identifier) CreateCube(io.prestosql.sql.tree.CreateCube) ParsingException(io.prestosql.sql.parser.ParsingException) FunctionCall(io.prestosql.sql.tree.FunctionCall) Property(io.prestosql.sql.tree.Property)

Example 5 with ParsingException

use of io.prestosql.sql.parser.ParsingException in project hetu-core by openlookeng.

the class SqlFormatterUtil method getFormattedSql.

public static String getFormattedSql(Statement statement, SqlParser sqlParser, Optional<List<Expression>> parameters) {
    String sql = SqlFormatter.formatSql(statement, parameters);
    // verify round-trip
    Statement parsed;
    try {
        ParsingOptions parsingOptions = new ParsingOptions(REJECT);
        parsed = sqlParser.createStatement(sql, parsingOptions);
    } catch (ParsingException e) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Formatted query does not parse: " + statement);
    }
    if (!statement.equals(parsed)) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Query does not round-trip: " + statement);
    }
    return sql;
}
Also used : ParsingOptions(io.prestosql.sql.parser.ParsingOptions) Statement(io.prestosql.sql.tree.Statement) ParsingException(io.prestosql.sql.parser.ParsingException) PrestoException(io.prestosql.spi.PrestoException)

Aggregations

ParsingException (io.prestosql.sql.parser.ParsingException)10 JSONObject (org.codehaus.jettison.json.JSONObject)4 ParsingOptions (io.prestosql.sql.parser.ParsingOptions)3 Statement (io.prestosql.sql.tree.Statement)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 JSONArray (org.codehaus.jettison.json.JSONArray)3 SqlMigrationException (io.hetu.core.sql.migration.SqlMigrationException)2 ErrorLocation (io.prestosql.client.ErrorLocation)2 CaseInsensitiveStream (io.prestosql.sql.parser.CaseInsensitiveStream)2 SqlParser (io.prestosql.sql.parser.SqlParser)2 StatementSplitter (io.prestosql.sql.parser.StatementSplitter)2 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)2 IOException (java.io.IOException)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)2 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)2 Parser (org.antlr.v4.runtime.Parser)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 Token (org.antlr.v4.runtime.Token)2