Search in sources :

Example 6 with LogicalBinaryExpression

use of com.facebook.presto.sql.tree.LogicalBinaryExpression in project presto by prestodb.

the class MaterializedViewUtils method generateBaseTablePredicates.

public static Map<SchemaTableName, Expression> generateBaseTablePredicates(Map<SchemaTableName, MaterializedViewStatus.MaterializedDataPredicates> predicatesFromBaseTables, Metadata metadata) {
    Map<SchemaTableName, Expression> baseTablePredicates = new HashMap<>();
    for (SchemaTableName baseTable : predicatesFromBaseTables.keySet()) {
        MaterializedViewStatus.MaterializedDataPredicates predicatesInfo = predicatesFromBaseTables.get(baseTable);
        List<String> partitionKeys = predicatesInfo.getColumnNames();
        ImmutableList<Expression> keyExpressions = partitionKeys.stream().map(Identifier::new).collect(toImmutableList());
        List<TupleDomain<String>> predicateDisjuncts = predicatesInfo.getPredicateDisjuncts();
        Expression disjunct = null;
        for (TupleDomain<String> predicateDisjunct : predicateDisjuncts) {
            Expression conjunct = null;
            Iterator<Expression> keyExpressionsIterator = keyExpressions.stream().iterator();
            Map<String, NullableValue> predicateKeyValue = extractFixedValues(predicateDisjunct).orElseThrow(() -> new IllegalStateException("predicateKeyValue is not present!"));
            for (String key : partitionKeys) {
                NullableValue nullableValue = predicateKeyValue.get(key);
                Expression expression;
                if (nullableValue.isNull()) {
                    expression = new IsNullPredicate(keyExpressionsIterator.next());
                } else {
                    LiteralEncoder literalEncoder = new LiteralEncoder(metadata.getBlockEncodingSerde());
                    Expression valueExpression = literalEncoder.toExpression(nullableValue.getValue(), nullableValue.getType(), false);
                    expression = new ComparisonExpression(EQUAL, keyExpressionsIterator.next(), valueExpression);
                }
                conjunct = conjunct == null ? expression : new LogicalBinaryExpression(AND, conjunct, expression);
            }
            disjunct = conjunct == null ? disjunct : disjunct == null ? conjunct : new LogicalBinaryExpression(OR, disjunct, conjunct);
        }
        // If no (fresh) partitions are found for table, that means we should not select from it
        if (disjunct == null) {
            disjunct = FALSE_LITERAL;
        }
        baseTablePredicates.put(baseTable, disjunct);
    }
    return baseTablePredicates;
}
Also used : LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) HashMap(java.util.HashMap) NullableValue(com.facebook.presto.common.predicate.NullableValue) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) LiteralEncoder(com.facebook.presto.sql.planner.LiteralEncoder) IsNullPredicate(com.facebook.presto.sql.tree.IsNullPredicate) MaterializedViewStatus(com.facebook.presto.spi.MaterializedViewStatus)

Example 7 with LogicalBinaryExpression

use of com.facebook.presto.sql.tree.LogicalBinaryExpression in project presto by prestodb.

the class TestSqlParser method testPrecedenceAndAssociativity.

@Test
public void testPrecedenceAndAssociativity() {
    assertExpression("1 AND 2 OR 3", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new LogicalBinaryExpression(LogicalBinaryExpression.Operator.AND, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 OR 2 AND 3", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new LongLiteral("1"), new LogicalBinaryExpression(LogicalBinaryExpression.Operator.AND, new LongLiteral("2"), new LongLiteral("3"))));
    assertExpression("NOT 1 AND 2", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.AND, new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
    assertExpression("NOT 1 OR 2", new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
    assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, negative(new LongLiteral("1")), new LongLiteral("2")));
    assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(DIVIDE, new ArithmeticBinaryExpression(DIVIDE, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new LongLiteral("2"), new LongLiteral("3"))));
}
Also used : LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) NotExpression(com.facebook.presto.sql.tree.NotExpression) Test(org.testng.annotations.Test)

Aggregations

LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)7 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)6 Expression (com.facebook.presto.sql.tree.Expression)5 NotExpression (com.facebook.presto.sql.tree.NotExpression)4 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)3 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)3 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)3 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)3 AllColumns (com.facebook.presto.sql.tree.AllColumns)2 Identifier (com.facebook.presto.sql.tree.Identifier)2 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)2 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)2 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)2 ArrayDeque (java.util.ArrayDeque)2 NullableValue (com.facebook.presto.common.predicate.NullableValue)1 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)1 MaterializedViewStatus (com.facebook.presto.spi.MaterializedViewStatus)1 SchemaTableName (com.facebook.presto.spi.SchemaTableName)1 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)1 LiteralEncoder (com.facebook.presto.sql.planner.LiteralEncoder)1