Search in sources :

Example 26 with SqlParser

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

the class PlanBuilder method rowExpression.

public RowExpression rowExpression(String sql) {
    Expression expression = expression(sql);
    Map<NodeRef<Expression>, Type> expressionTypes = ExpressionAnalyzer.analyzeExpressions(TEST_SESSION, metadata, new SqlParser(), getTypes(), ImmutableList.of(expression), ImmutableList.of(), WarningCollector.NOOP, false).getExpressionTypes();
    return SqlToRowExpressionTranslator.translate(expression, SCALAR, expressionTypes, ImmutableMap.of(), metadata.getFunctionAndTypeManager(), TEST_SESSION, false);
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) CallExpression(io.prestosql.spi.relation.CallExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) Expression(io.prestosql.sql.tree.Expression) RowExpression(io.prestosql.spi.relation.RowExpression) SqlParser(io.prestosql.sql.parser.SqlParser)

Example 27 with SqlParser

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

the class CubeFinishOperator method mergePredicates.

private CubeFilter mergePredicates(CubeFilter existing, String newPredicateString) {
    String sourceTablePredicate = existing == null ? null : existing.getSourceTablePredicate();
    if (newPredicateString == null && sourceTablePredicate == null) {
        return null;
    } else if (newPredicateString == null) {
        return new CubeFilter(sourceTablePredicate, null);
    }
    SqlParser sqlParser = new SqlParser();
    Expression newPredicate = sqlParser.createExpression(newPredicateString, new ParsingOptions());
    if (!updateMetadata.isOverwrite() && existing != null && existing.getCubePredicate() != null) {
        newPredicate = ExpressionUtils.or(sqlParser.createExpression(existing.getCubePredicate(), new ParsingOptions()), newPredicate);
    }
    // Merge new data predicate with existing predicate string
    CubeRangeCanonicalizer canonicalizer = new CubeRangeCanonicalizer(metadata, session, types);
    newPredicate = canonicalizer.mergePredicates(newPredicate);
    String formatExpression = newPredicate.equals(BooleanLiteral.TRUE_LITERAL) ? null : ExpressionFormatter.formatExpression(newPredicate, Optional.empty());
    return formatExpression == null && sourceTablePredicate == null ? null : new CubeFilter(sourceTablePredicate, formatExpression);
}
Also used : Expression(io.prestosql.sql.tree.Expression) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) CubeFilter(io.hetu.core.spi.cube.CubeFilter) SqlParser(io.prestosql.sql.parser.SqlParser)

Example 28 with SqlParser

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

the class AbstractTestQueryFramework method init.

@BeforeClass
public void init() throws Exception {
    queryRunner = queryRunnerSupplier.get();
    h2QueryRunner = new H2QueryRunner();
    sqlParser = new SqlParser();
}
Also used : SqlParser(io.prestosql.sql.parser.SqlParser) BeforeClass(org.testng.annotations.BeforeClass)

Example 29 with SqlParser

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

the class TestShadowing method testInsert.

@Test
public void testInsert() throws Exception {
    handle.execute("CREATE TABLE \"test_insert_table\" (a BIGINT, b DOUBLE, c VARCHAR)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "INSERT INTO test_insert_table (b, a, c) values (1.1, 1, 'a'), (2.0, 2, 'b'), (3.1, 3, 'c')", ImmutableList.of(), null, null, ImmutableMap.of());
    QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("other_catalog", "other_schema", "tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
    Query rewrittenQuery = rewriter.shadowQuery(query);
    assertEquals(rewrittenQuery.getPreQueries().size(), 2);
    CreateTable createTable = (CreateTable) parser.createStatement(rewrittenQuery.getPreQueries().get(0));
    assertEquals(createTable.getName().getParts().size(), 3);
    assertEquals(createTable.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema"));
    assertTrue(createTable.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTable.getName().getSuffix().contains("test_insert_table"));
    Insert insert = (Insert) parser.createStatement(rewrittenQuery.getPreQueries().get(1));
    assertEquals(insert.getTarget(), createTable.getName());
    assertEquals(insert.getColumns(), Optional.of(ImmutableList.of(identifier("b"), identifier("a"), identifier("c"))));
    Table table = new Table(createTable.getName());
    SingleColumn columnA = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("A"))));
    SingleColumn columnB = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("B"), new LongLiteral("1"))))));
    SingleColumn columnC = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("C"))));
    Select select = new Select(false, ImmutableList.of(columnA, columnB, columnC));
    QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new io.prestosql.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty(), Optional.empty()));
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTable.getName(), true));
}
Also used : Table(io.prestosql.sql.tree.Table) DropTable(io.prestosql.sql.tree.DropTable) CreateTable(io.prestosql.sql.tree.CreateTable) LongLiteral(io.prestosql.sql.tree.LongLiteral) SqlParser(io.prestosql.sql.parser.SqlParser) CreateTable(io.prestosql.sql.tree.CreateTable) Duration(io.airlift.units.Duration) SingleColumn(io.prestosql.sql.tree.SingleColumn) Insert(io.prestosql.sql.tree.Insert) DropTable(io.prestosql.sql.tree.DropTable) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) Identifier(io.prestosql.sql.tree.Identifier) Select(io.prestosql.sql.tree.Select) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) FunctionCall(io.prestosql.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Aggregations

SqlParser (io.prestosql.sql.parser.SqlParser)29 Expression (io.prestosql.sql.tree.Expression)11 ParsingOptions (io.prestosql.sql.parser.ParsingOptions)10 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)6 Optional (java.util.Optional)6 BeforeClass (org.testng.annotations.BeforeClass)6 CubeFilter (io.hetu.core.spi.cube.CubeFilter)5 Metadata (io.prestosql.metadata.Metadata)5 Symbol (io.prestosql.spi.plan.Symbol)5 FeaturesConfig (io.prestosql.sql.analyzer.FeaturesConfig)5 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)5 FunctionCall (io.prestosql.sql.tree.FunctionCall)5 List (java.util.List)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Session (io.prestosql.Session)4 CubeManager (io.prestosql.cube.CubeManager)4 CallExpression (io.prestosql.spi.relation.CallExpression)4 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)4 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)4