use of io.prestosql.sql.tree.LogicalBinaryExpression in project hetu-core by openlookeng.
the class TestSqlParser method testShowStatsForQuery.
@Test
public void testShowStatsForQuery() {
final String[] tableNames = { "t", "s.t", "c.s.t" };
for (String fullName : tableNames) {
QualifiedName qualifiedName = makeQualifiedName(fullName);
assertStatement(format("SHOW STATS FOR (SELECT * FROM %s)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.empty()));
assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("field"), new LongLiteral("0")))));
assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0 or field < 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new ComparisonExpression(GREATER_THAN, new Identifier("field"), new LongLiteral("0")), new ComparisonExpression(LESS_THAN, new Identifier("field"), new LongLiteral("0"))))));
}
}
use of io.prestosql.sql.tree.LogicalBinaryExpression in project hetu-core by openlookeng.
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, 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"))));
}
use of io.prestosql.sql.tree.LogicalBinaryExpression in project hetu-core by openlookeng.
the class HeuristicIndexUtilsTest method testExtractPartitions.
@Test
public void testExtractPartitions() {
Expression equalExpName = new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1"));
System.out.println(equalExpName);
assertEquals(HeuristicIndexUtils.extractPartitions(equalExpName), ImmutableList.of("a=1"));
Expression equalExpNameExp = new ComparisonExpression(EQUAL, nameExp("a"), new LongLiteral("1"));
System.out.println(equalExpNameExp);
assertEquals(HeuristicIndexUtils.extractPartitions(equalExpName), ImmutableList.of("a=1"));
Expression equalExp2Name = new ComparisonExpression(EQUAL, name("a"), new LongLiteral("2"));
System.out.println(equalExp2Name);
Expression orExp = new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, equalExpName, equalExp2Name);
System.out.println(orExp);
assertEquals(HeuristicIndexUtils.extractPartitions(orExp), ImmutableList.of("a=1", "a=2"));
Expression inExpInteger = new InPredicate(name("a"), new InListExpression(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"), new LongLiteral("3"), new LongLiteral("4"), new LongLiteral("5"), new LongLiteral("6"))));
System.out.println(inExpInteger);
assertEquals(HeuristicIndexUtils.extractPartitions(inExpInteger), ImmutableList.of("a=1", "a=2", "a=3", "a=4", "a=5", "a=6"));
Expression inExpBigInt = new InPredicate(name("a"), new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2), bigintLiteral(3), bigintLiteral(4), bigintLiteral(5), bigintLiteral(6))));
System.out.println(inExpBigInt);
assertEquals(HeuristicIndexUtils.extractPartitions(inExpInteger), ImmutableList.of("a=1", "a=2", "a=3", "a=4", "a=5", "a=6"));
}
use of io.prestosql.sql.tree.LogicalBinaryExpression 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)");
}
use of io.prestosql.sql.tree.LogicalBinaryExpression in project hetu-core by openlookeng.
the class ExpressionUtils method binaryExpression.
public static Expression binaryExpression(LogicalBinaryExpression.Operator operator, Collection<Expression> expressions) {
requireNonNull(operator, "operator is null");
requireNonNull(expressions, "expressions is null");
if (expressions.isEmpty()) {
switch(operator) {
case AND:
return TRUE_LITERAL;
case OR:
return FALSE_LITERAL;
default:
throw new IllegalArgumentException("Unsupported LogicalBinaryExpression operator");
}
}
// Build balanced tree for efficient recursive processing that
// preserves the evaluation order of the input expressions.
//
// The tree is built bottom up by combining pairs of elements into
// binary AND expressions.
//
// Example:
//
// Initial state:
// a b c d e
//
// First iteration:
//
// /\ /\ e
// a b c d
//
// Second iteration:
//
// / \ e
// /\ /\
// a b c d
//
//
// Last iteration:
//
// / \
// / \ e
// /\ /\
// a b c d
Queue<Expression> queue = new ArrayDeque<>(expressions);
while (queue.size() > 1) {
Queue<Expression> buffer = new ArrayDeque<>();
// combine pairs of elements
while (queue.size() >= 2) {
buffer.add(new LogicalBinaryExpression(operator, queue.remove(), queue.remove()));
}
// if there's and odd number of elements, just append the last one
if (!queue.isEmpty()) {
buffer.add(queue.remove());
}
// continue processing the pairs that were just built
queue = buffer;
}
return queue.remove();
}
Aggregations