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;
}
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"))));
}
Aggregations