Search in sources :

Example 36 with Cast

use of io.prestosql.sql.tree.Cast in project hetu-core by openlookeng.

the class TestEffectivePredicateExtractor method testValues.

@Test
public void testValues() {
    TypeProvider types = TypeProvider.copyOf(ImmutableMap.<Symbol, Type>builder().put(A, BIGINT).put(B, BIGINT).build());
    // one column
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)))), types, typeAnalyzer), new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))));
    // one column with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), or(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new IsNullPredicate(AE)));
    // all nulls
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), new IsNullPredicate(AE));
    // many rows
    List<List<RowExpression>> rows = IntStream.range(0, 500).mapToObj(TestEffectivePredicateExtractor::bigintLiteralRowExpression).map(ImmutableList::of).collect(toImmutableList());
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), rows), types, typeAnalyzer), new BetweenPredicate(AE, bigintLiteral(0), bigintLiteral(499)));
    // multiple columns
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), bigintLiteralRowExpression(100)), ImmutableList.of(bigintLiteralRowExpression(2), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new InPredicate(BE, new InListExpression(ImmutableList.of(bigintLiteral(100), bigintLiteral(200))))));
    // multiple columns with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString())), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(or(new ComparisonExpression(EQUAL, AE, bigintLiteral(1)), new IsNullPredicate(AE)), or(new ComparisonExpression(EQUAL, BE, bigintLiteral(200)), new IsNullPredicate(BE))));
    // non-deterministic
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()))))), types, typeAnalyzer), new ComparisonExpression(EQUAL, AE, bigintLiteral(1)));
    // non-constant
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(castToRowExpression(BE)))), types, typeAnalyzer), TRUE_LITERAL);
}
Also used : Cast(io.prestosql.sql.tree.Cast) ValuesNode(io.prestosql.spi.plan.ValuesNode) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) Symbol(io.prestosql.spi.plan.Symbol) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) NullLiteral(io.prestosql.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Example 37 with Cast

use of io.prestosql.sql.tree.Cast in project hetu-core by openlookeng.

the class TestTypeValidator method testValidTypeOnlyCoercion.

@Test
public void testValidTypeOnlyCoercion() {
    Expression expression = new Cast(toSymbolReference(columnB), StandardTypes.BIGINT);
    Assignments assignments = Assignments.builder().put(planSymbolAllocator.newSymbol(expression, BIGINT), castToRowExpression(expression)).put(planSymbolAllocator.newSymbol(toSymbolReference(columnE), VARCHAR), // implicit coercion from varchar(3) to varchar
    castToRowExpression(toSymbolReference(columnE))).build();
    PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
    assertTypesValid(node);
}
Also used : Cast(io.prestosql.sql.tree.Cast) PlanNode(io.prestosql.spi.plan.PlanNode) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Expression(io.prestosql.sql.tree.Expression) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 38 with Cast

use of io.prestosql.sql.tree.Cast in project hetu-core by openlookeng.

the class TestTypeValidator method testValidProject.

@Test
public void testValidProject() {
    Expression expression1 = new Cast(toSymbolReference(columnB), StandardTypes.BIGINT);
    Expression expression2 = new Cast(toSymbolReference(columnC), StandardTypes.BIGINT);
    Assignments assignments = Assignments.builder().put(planSymbolAllocator.newSymbol(expression1, BIGINT), castToRowExpression(expression1)).put(planSymbolAllocator.newSymbol(expression2, BIGINT), castToRowExpression(expression2)).build();
    PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
    assertTypesValid(node);
}
Also used : Cast(io.prestosql.sql.tree.Cast) PlanNode(io.prestosql.spi.plan.PlanNode) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Expression(io.prestosql.sql.tree.Expression) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 39 with Cast

use of io.prestosql.sql.tree.Cast in project boostkit-bigdata by kunpengcompute.

the class OmniRowExpressionUtil method generateOmniExpr.

/**
 * Generates a RowExpression to ensure like query compatibility with omniruntime
 *
 * @param staticExpr Expression from openLookeng
 * @param translatedExpr RowExpression from staticFilter conversion
 * @return new Optional<RowExpression> with new, regex-syntax arguments for like
 * queries
 */
public static Optional<RowExpression> generateOmniExpr(Expression staticExpr, RowExpression translatedExpr) {
    // parse the expr
    if (staticExpr instanceof SearchedCaseExpression && translatedExpr instanceof SpecialForm) {
        return getWhenExpr((SearchedCaseExpression) staticExpr, (SpecialForm) translatedExpr);
    }
    if (translatedExpr instanceof SpecialForm) {
        SpecialForm specialExpr = (SpecialForm) translatedExpr;
        List<RowExpression> newArguments = new ArrayList<RowExpression>();
        for (int i = 0; i < specialExpr.getArguments().size(); i++) {
            RowExpression nestedExpr = specialExpr.getArguments().get(i);
            if (nestedExpr instanceof SpecialForm || nestedExpr instanceof CallExpression) {
                newArguments.add(generateOmniExpr((Expression) staticExpr.getChildren().get(i), nestedExpr).get());
            } else {
                newArguments.add(specialExpr.getArguments().get(i));
            }
        }
        Optional<RowExpression> newOmniFilter = Optional.of(new SpecialForm(specialExpr.getForm(), specialExpr.getType(), newArguments));
        return newOmniFilter;
    } else if (translatedExpr instanceof CallExpression) {
        CallExpression callExpr = (CallExpression) translatedExpr;
        List<RowExpression> newArguments = new ArrayList<RowExpression>();
        for (int i = 0; i < callExpr.getArguments().size(); i++) {
            RowExpression nestedExpr = callExpr.getArguments().get(i);
            if (nestedExpr instanceof SpecialForm || nestedExpr instanceof CallExpression) {
                newArguments.add(generateOmniExpr((Expression) staticExpr.getChildren().get(i), nestedExpr).get());
            } else {
                newArguments.add(callExpr.getArguments().get(i));
            }
        }
        Optional<RowExpression> newOmniFilter = Optional.of(new CallExpression(callExpr.getDisplayName(), callExpr.getFunctionHandle(), callExpr.getType(), newArguments));
        if ("LIKE".equals(((CallExpression) newOmniFilter.get()).getDisplayName().toUpperCase(Locale.ROOT))) {
            String sqlString = "";
            if (staticExpr.getChildren().get(1) instanceof Cast) {
                sqlString = ((StringLiteral) ((Cast) staticExpr.getChildren().get(1)).getExpression()).getValue();
            } else {
                sqlString = ((StringLiteral) staticExpr.getChildren().get(1)).getValue();
            }
            return generateLikeExpr(sqlString, newOmniFilter);
        }
        return newOmniFilter;
    }
    return Optional.of(translatedExpr);
}
Also used : Cast(io.prestosql.sql.tree.Cast) Optional(java.util.Optional) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CallExpression(io.prestosql.spi.relation.CallExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression) InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Aggregations

Cast (io.prestosql.sql.tree.Cast)39 Expression (io.prestosql.sql.tree.Expression)24 Symbol (io.prestosql.spi.plan.Symbol)23 ProjectNode (io.prestosql.spi.plan.ProjectNode)18 Type (io.prestosql.spi.type.Type)18 SymbolReference (io.prestosql.sql.tree.SymbolReference)18 Assignments (io.prestosql.spi.plan.Assignments)16 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)16 Test (org.testng.annotations.Test)16 RowExpression (io.prestosql.spi.relation.RowExpression)14 PlanNode (io.prestosql.spi.plan.PlanNode)13 ImmutableList (com.google.common.collect.ImmutableList)12 SymbolUtils.toSymbolReference (io.prestosql.sql.planner.SymbolUtils.toSymbolReference)12 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 RelationType (io.prestosql.sql.analyzer.RelationType)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 ArrayList (java.util.ArrayList)10 AggregationNode (io.prestosql.spi.plan.AggregationNode)9 HashMap (java.util.HashMap)9