Search in sources :

Example 1 with RowSubqueryExpression

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

the class AbstractParsedStmt method rejectDisallowedRowOpExpressions.

private void rejectDisallowedRowOpExpressions(AbstractExpression expr) {
    ExpressionType exprType = expr.getExpressionType();
    // searching for row op subquery expressions.
    if (ExpressionType.CONJUNCTION_AND == exprType || ExpressionType.CONJUNCTION_OR == exprType) {
        rejectDisallowedRowOpExpressions(expr.getLeft());
        rejectDisallowedRowOpExpressions(expr.getRight());
        return;
    }
    if (ExpressionType.OPERATOR_NOT == exprType) {
        rejectDisallowedRowOpExpressions(expr.getLeft());
    }
    // The problem cases are all comparison ops.
    if (!(expr instanceof ComparisonExpression)) {
        return;
    }
    // The problem cases all have row expressions as an operand.
    AbstractExpression rowExpression = expr.getLeft();
    if (rowExpression instanceof RowSubqueryExpression) {
        rejectDisallowedRowColumns(rowExpression);
    }
    rowExpression = expr.getRight();
    if (rowExpression instanceof RowSubqueryExpression) {
        rejectDisallowedRowColumns(rowExpression);
    }
}
Also used : ComparisonExpression(org.voltdb.expressions.ComparisonExpression) AbstractExpression(org.voltdb.expressions.AbstractExpression) RowSubqueryExpression(org.voltdb.expressions.RowSubqueryExpression) ExpressionType(org.voltdb.types.ExpressionType)

Example 2 with RowSubqueryExpression

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

the class AbstractParsedStmt method parseRowExpression.

/**
     *
     * @param exprNode
     * @return
     */
private AbstractExpression parseRowExpression(List<VoltXMLElement> exprNodes) {
    // Parse individual columnref expressions from the IN output schema
    List<AbstractExpression> exprs = new ArrayList<>();
    for (VoltXMLElement exprNode : exprNodes) {
        AbstractExpression expr = parseExpressionNode(exprNode);
        exprs.add(expr);
    }
    return new RowSubqueryExpression(exprs);
}
Also used : AbstractExpression(org.voltdb.expressions.AbstractExpression) RowSubqueryExpression(org.voltdb.expressions.RowSubqueryExpression) ArrayList(java.util.ArrayList) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement)

Example 3 with RowSubqueryExpression

use of org.voltdb.expressions.RowSubqueryExpression 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)

Aggregations

AbstractExpression (org.voltdb.expressions.AbstractExpression)3 RowSubqueryExpression (org.voltdb.expressions.RowSubqueryExpression)3 ArrayList (java.util.ArrayList)2 ComparisonExpression (org.voltdb.expressions.ComparisonExpression)2 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)1 ExpressionType (org.voltdb.types.ExpressionType)1