Search in sources :

Example 1 with LogicalBinaryExpression

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"))))));
    }
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) Identifier(io.prestosql.sql.tree.Identifier) QueryUtil.quotedIdentifier(io.prestosql.sql.QueryUtil.quotedIdentifier) LongLiteral(io.prestosql.sql.tree.LongLiteral) QualifiedName(io.prestosql.sql.tree.QualifiedName) AllColumns(io.prestosql.sql.tree.AllColumns) Test(org.testng.annotations.Test)

Example 2 with LogicalBinaryExpression

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"))));
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) LongLiteral(io.prestosql.sql.tree.LongLiteral) NotExpression(io.prestosql.sql.tree.NotExpression) Test(org.testng.annotations.Test)

Example 3 with LogicalBinaryExpression

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"));
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) InListExpression(io.prestosql.sql.tree.InListExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) LongLiteral(io.prestosql.sql.tree.LongLiteral) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) Test(org.testng.annotations.Test)

Example 4 with LogicalBinaryExpression

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)");
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) InListExpression(io.prestosql.sql.tree.InListExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ParsingException(io.prestosql.sql.parser.ParsingException) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) LinkedList(java.util.LinkedList)

Example 5 with LogicalBinaryExpression

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();
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) Expression(io.prestosql.sql.tree.Expression) ArrayDeque(java.util.ArrayDeque)

Aggregations

LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)8 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)7 Expression (io.prestosql.sql.tree.Expression)4 LongLiteral (io.prestosql.sql.tree.LongLiteral)4 InPredicate (io.prestosql.sql.tree.InPredicate)3 Test (org.testng.annotations.Test)3 AllColumns (io.prestosql.sql.tree.AllColumns)2 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)2 Identifier (io.prestosql.sql.tree.Identifier)2 InListExpression (io.prestosql.sql.tree.InListExpression)2 NotExpression (io.prestosql.sql.tree.NotExpression)2 QualifiedName (io.prestosql.sql.tree.QualifiedName)2 SymbolReference (io.prestosql.sql.tree.SymbolReference)2 CTEScanNode (io.prestosql.spi.plan.CTEScanNode)1 PlanNode (io.prestosql.spi.plan.PlanNode)1 ProjectNode (io.prestosql.spi.plan.ProjectNode)1 Symbol (io.prestosql.spi.plan.Symbol)1 TableScanNode (io.prestosql.spi.plan.TableScanNode)1 RowExpression (io.prestosql.spi.relation.RowExpression)1 QueryUtil.quotedIdentifier (io.prestosql.sql.QueryUtil.quotedIdentifier)1