Search in sources :

Example 11 with ComparisonExpression

use of org.voltdb.expressions.ComparisonExpression in project voltdb by VoltDB.

the class ParsedSelectStmt method rewriteInSubqueryAsExists.

/**
     * Converts an IN expression into the equivalent EXISTS one
     * IN (SELECT" forms e.g. "(A, B) IN (SELECT X, Y, FROM ...) ==
     * EXISTS (SELECT 42 FROM ... AND|WHERE|HAVING A=X AND|WHERE|HAVING B=Y)
     *
     * @param selectStmt select subquery from the IN expression
     * @param inListExpr TVE for the columns from the IN list
     * @return modified subquery
     */
protected static void rewriteInSubqueryAsExists(ParsedSelectStmt selectStmt, AbstractExpression inListExpr) {
    List<AbstractExpression> whereList = new ArrayList<>();
    List<AbstractExpression> havingList = new ArrayList<>();
    // multi-column IN expression is a RowSubqueryExpression
    // where each arg represents an individual column
    List<AbstractExpression> inExprList = null;
    if (inListExpr instanceof RowSubqueryExpression) {
        inExprList = inListExpr.getArgs();
    } else {
        inExprList = new ArrayList<>();
        inExprList.add(inListExpr);
    }
    int idx = 0;
    assert (inExprList.size() == selectStmt.m_displayColumns.size());
    selectStmt.m_aggregationList = new ArrayList<>();
    // If not, it should be added to the WHERE expressions
    for (AbstractExpression expr : inExprList) {
        ParsedColInfo colInfo = selectStmt.m_displayColumns.get(idx++);
        assert (colInfo.expression != null);
        // The TVE and the aggregated expressions from the IN clause will
        // be parameters to the child select statement once the IN
        // expression is replaced with the EXISTS one.
        expr = selectStmt.replaceExpressionsWithPve(expr);
        // Finalize the expression. The subquery's own expressions
        // are already finalized but not the expressions from the IN list
        ExpressionUtil.finalizeValueTypes(expr);
        // Create new compare equal expression
        AbstractExpression equalityExpr = new ComparisonExpression(ExpressionType.COMPARE_EQUAL, expr, colInfo.expression.clone());
        // Check if this column contains aggregate expression
        if (ExpressionUtil.containsAggregateExpression(colInfo.expression)) {
            // We are not creating any new aggregate expressions so
            // the aggregation list doen't need to be updated.
            // Only the HAVING expression itself
            havingList.add(equalityExpr);
        } else {
            whereList.add(equalityExpr);
        }
    }
    // Add new WHERE expressions
    if (!whereList.isEmpty()) {
        if (selectStmt.m_joinTree.getWhereExpression() != null) {
            whereList.add(selectStmt.m_joinTree.getWhereExpression());
        }
        selectStmt.m_joinTree.setWhereExpression(ExpressionUtil.combinePredicates(whereList));
    }
    // Add new HAVING expressions
    if (!havingList.isEmpty()) {
        if (selectStmt.m_having != null) {
            havingList.add(selectStmt.m_having);
        }
        selectStmt.m_having = ExpressionUtil.combinePredicates(havingList);
        // reprocess HAVING expressions
        ExpressionUtil.finalizeValueTypes(selectStmt.m_having);
    }
    selectStmt.m_aggregationList = null;
    if (selectStmt.needComplexAggregation()) {
        selectStmt.fillUpAggResultColumns();
    } else {
        selectStmt.m_aggResultColumns = selectStmt.m_displayColumns;
    }
    selectStmt.placeTVEsinColumns();
}
Also used : ComparisonExpression(org.voltdb.expressions.ComparisonExpression) AbstractExpression(org.voltdb.expressions.AbstractExpression) RowSubqueryExpression(org.voltdb.expressions.RowSubqueryExpression) ArrayList(java.util.ArrayList)

Example 12 with ComparisonExpression

use of org.voltdb.expressions.ComparisonExpression in project voltdb by VoltDB.

the class TestPlansSubQueries method checkPredicateComparisonExpression.

private void checkPredicateComparisonExpression(AbstractPlanNode pn, String tableAlias) {
    AbstractExpression expr = ((SeqScanPlanNode) pn).getPredicate();
    assertTrue(expr instanceof ComparisonExpression);
    expr = expr.getLeft();
    assertTrue(expr instanceof TupleValueExpression);
    assertEquals(tableAlias, ((TupleValueExpression) expr).getTableAlias());
}
Also used : SeqScanPlanNode(org.voltdb.plannodes.SeqScanPlanNode) ComparisonExpression(org.voltdb.expressions.ComparisonExpression) TupleValueExpression(org.voltdb.expressions.TupleValueExpression) AbstractExpression(org.voltdb.expressions.AbstractExpression)

Aggregations

AbstractExpression (org.voltdb.expressions.AbstractExpression)12 ComparisonExpression (org.voltdb.expressions.ComparisonExpression)12 ExpressionType (org.voltdb.types.ExpressionType)6 ArrayList (java.util.ArrayList)4 JSONException (org.json_voltpatches.JSONException)4 TupleValueExpression (org.voltdb.expressions.TupleValueExpression)3 SeqScanPlanNode (org.voltdb.plannodes.SeqScanPlanNode)3 OperatorExpression (org.voltdb.expressions.OperatorExpression)2 ParameterValueExpression (org.voltdb.expressions.ParameterValueExpression)2 RowSubqueryExpression (org.voltdb.expressions.RowSubqueryExpression)2 SelectSubqueryExpression (org.voltdb.expressions.SelectSubqueryExpression)2 AbstractPlanNode (org.voltdb.plannodes.AbstractPlanNode)2 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)1 VoltType (org.voltdb.VoltType)1 Column (org.voltdb.catalog.Column)1 ColumnRef (org.voltdb.catalog.ColumnRef)1 Constraint (org.voltdb.catalog.Constraint)1 Index (org.voltdb.catalog.Index)1 ConjunctionExpression (org.voltdb.expressions.ConjunctionExpression)1 ConstantValueExpression (org.voltdb.expressions.ConstantValueExpression)1