Search in sources :

Example 66 with TupleValueExpression

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

the class PlanAssembler method canPushDownDistinctAggregation.

private boolean canPushDownDistinctAggregation(AggregateExpression aggExpr) {
    assert (m_parsedSelect != null);
    assert (aggExpr != null);
    assert (aggExpr.isDistinct());
    if (aggExpr.getExpressionType() == ExpressionType.AGGREGATE_COUNT_STAR) {
        return true;
    }
    AbstractExpression aggArg = aggExpr.getLeft();
    // constant
    if (aggArg instanceof ConstantValueExpression || aggArg instanceof ParameterValueExpression) {
        return true;
    }
    if (!(aggArg instanceof TupleValueExpression)) {
        return false;
    }
    TupleValueExpression tve = (TupleValueExpression) aggArg;
    String tableAlias = tve.getTableAlias();
    StmtTableScan scanTable = m_parsedSelect.getStmtTableScanByAlias(tableAlias);
    // table alias may be from AbstractParsedStmt.TEMP_TABLE_NAME.
    if (scanTable == null || scanTable.getPartitioningColumns() == null) {
        return false;
    }
    for (SchemaColumn pcol : scanTable.getPartitioningColumns()) {
        if (pcol != null && pcol.getColumnName().equals(tve.getColumnName())) {
            return true;
        }
    }
    return false;
}
Also used : TupleValueExpression(org.voltdb.expressions.TupleValueExpression) AbstractExpression(org.voltdb.expressions.AbstractExpression) ConstantValueExpression(org.voltdb.expressions.ConstantValueExpression) SchemaColumn(org.voltdb.plannodes.SchemaColumn) ParameterValueExpression(org.voltdb.expressions.ParameterValueExpression) StmtTableScan(org.voltdb.planner.parseinfo.StmtTableScan)

Example 67 with TupleValueExpression

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

the class PlanAssembler method addProjection.

/**
     * Given a relatively complete plan-sub-graph, apply a trivial projection
     * (filter) to it. If the root node can embed the projection do so. If not,
     * add a new projection node.
     *
     * @param rootNode
     *            The root of the plan-sub-graph to add the projection to.
     * @return The new root of the plan-sub-graph (might be the same as the
     *         input).
     */
private AbstractPlanNode addProjection(AbstractPlanNode rootNode) {
    assert (m_parsedSelect != null);
    assert (m_parsedSelect.m_displayColumns != null);
    // Build the output schema for the projection based on the display columns
    NodeSchema proj_schema = m_parsedSelect.getFinalProjectionSchema();
    for (SchemaColumn col : proj_schema.getColumns()) {
        // Adjust the differentiator fields of TVEs, since they need to
        // reflect the inlined projection node in scan nodes.
        AbstractExpression colExpr = col.getExpression();
        Collection<TupleValueExpression> allTves = ExpressionUtil.getTupleValueExpressions(colExpr);
        for (TupleValueExpression tve : allTves) {
            if (!tve.needsDifferentiation()) {
                // so we just ignore it here.
                continue;
            }
            rootNode.adjustDifferentiatorField(tve);
        }
    }
    ProjectionPlanNode projectionNode = new ProjectionPlanNode();
    projectionNode.setOutputSchemaWithoutClone(proj_schema);
    // projection node inline.
    if (rootNode instanceof AbstractScanPlanNode) {
        rootNode.addInlinePlanNode(projectionNode);
        return rootNode;
    }
    projectionNode.addAndLinkChild(rootNode);
    return projectionNode;
}
Also used : TupleValueExpression(org.voltdb.expressions.TupleValueExpression) AbstractScanPlanNode(org.voltdb.plannodes.AbstractScanPlanNode) AbstractExpression(org.voltdb.expressions.AbstractExpression) SchemaColumn(org.voltdb.plannodes.SchemaColumn) NodeSchema(org.voltdb.plannodes.NodeSchema) ProjectionPlanNode(org.voltdb.plannodes.ProjectionPlanNode)

Example 68 with TupleValueExpression

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

the class ParsedSelectStmt method processDistinct.

private VoltXMLElement processDistinct(VoltXMLElement displayElement, VoltXMLElement groupbyElement, VoltXMLElement havingElement) {
    // process DISTINCT clause
    if (!m_distinct) {
        return groupbyElement;
    }
    // DISTINCT without GROUP BY
    if (groupbyElement == null || groupbyElement.children.isEmpty()) {
        // Tricky: rewrote DISTINCT without GROUP BY with GROUP BY clause
        if (!m_hasAggregateExpression) {
            // attribute "id" is the only one that differs from a real
            // GROUP BY query
            groupbyElement = displayElement.duplicate();
        }
        // When it is table aggregate, it's also safe to drop DISTINCT.
        m_distinct = false;
        return groupbyElement;
    }
    // DISTINCT with GROUP BY
    m_distinctGroupByColumns = new ArrayList<>();
    m_distinctProjectSchema = new NodeSchema();
    // Iterate the Display columns
    for (ParsedColInfo col : m_displayColumns) {
        TupleValueExpression tve = new TupleValueExpression(col.tableName, col.tableAlias, col.columnName, col.alias, col.index, col.differentiator);
        tve.setTypeSizeAndInBytes(col.asSchemaColumn());
        ParsedColInfo pcol = new ParsedColInfo();
        pcol.tableName = col.tableName;
        pcol.tableAlias = col.tableAlias;
        pcol.columnName = col.columnName;
        pcol.alias = col.alias;
        pcol.expression = tve;
        m_distinctGroupByColumns.add(pcol);
        m_distinctProjectSchema.addColumn(col.tableName, col.tableAlias, col.columnName, col.alias, tve, col.differentiator);
    }
    return groupbyElement;
}
Also used : TupleValueExpression(org.voltdb.expressions.TupleValueExpression) NodeSchema(org.voltdb.plannodes.NodeSchema)

Example 69 with TupleValueExpression

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

the class ParsedSelectStmt method insertTVEsToAggResultColumns.

private void insertTVEsToAggResultColumns(List<TupleValueExpression> colCollection) {
    // TVEs do not need to take care
    for (TupleValueExpression tve : colCollection) {
        ParsedColInfo col = new ParsedColInfo();
        col.alias = tve.getColumnAlias();
        col.columnName = tve.getColumnName();
        col.tableName = tve.getTableName();
        col.tableAlias = tve.getTableAlias();
        col.expression = tve;
        if (!m_aggResultColumns.contains(col)) {
            m_aggResultColumns.add(col);
        }
    }
}
Also used : TupleValueExpression(org.voltdb.expressions.TupleValueExpression)

Example 70 with TupleValueExpression

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

the class TestPlansJoin method testDisplayColumnFromUsingCondition.

public void testDisplayColumnFromUsingCondition() {
    String query;
    List<AbstractPlanNode> lpn;
    AbstractPlanNode pn;
    AbstractPlanNode node;
    NestLoopPlanNode nlj;
    AbstractExpression predicate;
    SeqScanPlanNode seqScan;
    SchemaColumn sc0;
    List<SchemaColumn> selectColumns;
    query = "SELECT max(A) FROM R1 JOIN R2 USING(A)";
    pn = compileToTopDownTree(query, 1, PlanNodeType.SEND, PlanNodeType.NESTLOOP, PlanNodeType.SEQSCAN, PlanNodeType.SEQSCAN);
    selectColumns = pn.getOutputSchema().getColumns();
    for (SchemaColumn sc : selectColumns) {
        AbstractExpression e = sc.getExpression();
        assertTrue(e instanceof TupleValueExpression);
        TupleValueExpression tve = (TupleValueExpression) e;
        assertNotSame(-1, tve.getColumnIndex());
    }
    node = followAssertedLeftChain(pn, PlanNodeType.SEND, PlanNodeType.NESTLOOP);
    assertNotNull(AggregatePlanNode.getInlineAggregationNode(node));
    query = "SELECT distinct(A) FROM R1 JOIN R2 USING(A)";
    pn = compileToTopDownTree(query, 1, PlanNodeType.SEND, PlanNodeType.NESTLOOP, PlanNodeType.SEQSCAN, PlanNodeType.SEQSCAN);
    selectColumns = pn.getOutputSchema().getColumns();
    for (SchemaColumn sc : selectColumns) {
        AbstractExpression e = sc.getExpression();
        assertTrue(e instanceof TupleValueExpression);
        TupleValueExpression tve = (TupleValueExpression) e;
        assertNotSame(-1, tve.getColumnIndex());
    }
    query = "SELECT A FROM R1 JOIN R2 USING(A) ORDER BY A";
    pn = compileToTopDownTree(query, 1, PlanNodeType.SEND, PlanNodeType.PROJECTION, PlanNodeType.ORDERBY, PlanNodeType.NESTLOOP, PlanNodeType.SEQSCAN, PlanNodeType.SEQSCAN);
    selectColumns = pn.getOutputSchema().getColumns();
    for (SchemaColumn sc : selectColumns) {
        AbstractExpression e = sc.getExpression();
        assertTrue(e instanceof TupleValueExpression);
        TupleValueExpression tve = (TupleValueExpression) e;
        assertNotSame(-1, tve.getColumnIndex());
    }
    query = "SELECT * FROM P1 LABEL JOIN R2 USING(A) " + "WHERE A > 0 AND R2.C >= 5";
    lpn = compileToFragments(query);
    assertProjectingCoordinator(lpn);
    pn = lpn.get(1);
    assertTopDownTree(pn, PlanNodeType.SEND, PlanNodeType.NESTLOOP, PlanNodeType.SEQSCAN, PlanNodeType.SEQSCAN);
    node = followAssertedLeftChain(lpn.get(1), PlanNodeType.SEND, PlanNodeType.NESTLOOP);
    nlj = (NestLoopPlanNode) node;
    assertNull(nlj.getPreJoinPredicate());
    predicate = nlj.getJoinPredicate();
    assertExprTopDownTree(predicate, ExpressionType.COMPARE_EQUAL, ExpressionType.VALUE_TUPLE, ExpressionType.VALUE_TUPLE);
    assertNull(nlj.getWherePredicate());
    seqScan = (SeqScanPlanNode) nlj.getChild(0);
    predicate = seqScan.getPredicate();
    assertExprTopDownTree(predicate, ExpressionType.CONJUNCTION_AND, ExpressionType.COMPARE_GREATERTHANOREQUALTO, ExpressionType.VALUE_TUPLE, ExpressionType.VALUE_CONSTANT, ExpressionType.COMPARE_GREATERTHAN, ExpressionType.VALUE_TUPLE, ExpressionType.VALUE_CONSTANT);
    seqScan = (SeqScanPlanNode) nlj.getChild(1);
    assertNull(seqScan.getPredicate());
    query = "SELECT * FROM P1 LABEL LEFT JOIN R2 USING(A) WHERE A > 0";
    lpn = compileToFragments(query);
    node = followAssertedLeftChain(lpn.get(1), PlanNodeType.SEND, PlanNodeType.NESTLOOP);
    nlj = (NestLoopPlanNode) node;
    assertTrue(JoinType.LEFT == nlj.getJoinType());
    assertNull(nlj.getPreJoinPredicate());
    predicate = nlj.getJoinPredicate();
    assertExprTopDownTree(predicate, ExpressionType.COMPARE_EQUAL, ExpressionType.VALUE_TUPLE, ExpressionType.VALUE_TUPLE);
    assertNull(nlj.getWherePredicate());
    seqScan = (SeqScanPlanNode) nlj.getChild(0);
    predicate = seqScan.getPredicate();
    assertExprTopDownTree(predicate, ExpressionType.COMPARE_GREATERTHAN, ExpressionType.VALUE_TUPLE, ExpressionType.VALUE_CONSTANT);
    query = "SELECT A FROM R2 LABEL RIGHT JOIN P1 AP1 USING(A) WHERE A > 0";
    lpn = compileToFragments(query);
    assertProjectingCoordinator(lpn);
    pn = lpn.get(0);
    selectColumns = pn.getOutputSchema().getColumns();
    assertEquals(1, selectColumns.size());
    sc0 = selectColumns.get(0);
    assertEquals("AP1", sc0.getTableAlias());
    assertEquals("P1", sc0.getTableName());
    pn = lpn.get(1);
    assertTopDownTree(pn, PlanNodeType.SEND, PlanNodeType.NESTLOOP, PlanNodeType.SEQSCAN, PlanNodeType.SEQSCAN);
    node = followAssertedLeftChain(lpn.get(1), PlanNodeType.SEND, PlanNodeType.NESTLOOP);
    nlj = (NestLoopPlanNode) node;
    assertEquals(JoinType.LEFT, nlj.getJoinType());
    assertNull(nlj.getPreJoinPredicate());
    predicate = nlj.getJoinPredicate();
    assertExprTopDownTree(predicate, ExpressionType.COMPARE_EQUAL, ExpressionType.VALUE_TUPLE, ExpressionType.VALUE_TUPLE);
    assertNull(nlj.getWherePredicate());
    seqScan = (SeqScanPlanNode) nlj.getChild(0);
    predicate = seqScan.getPredicate();
    assertExprTopDownTree(predicate, ExpressionType.COMPARE_GREATERTHAN, ExpressionType.VALUE_TUPLE, ExpressionType.VALUE_CONSTANT);
    selectColumns = seqScan.getOutputSchema().getColumns();
    assertEquals(1, selectColumns.size());
    sc0 = selectColumns.get(0);
    assertEquals("AP1", sc0.getTableAlias());
    assertEquals("P1", sc0.getTableName());
}
Also used : AbstractPlanNode(org.voltdb.plannodes.AbstractPlanNode) SeqScanPlanNode(org.voltdb.plannodes.SeqScanPlanNode) TupleValueExpression(org.voltdb.expressions.TupleValueExpression) AbstractExpression(org.voltdb.expressions.AbstractExpression) SchemaColumn(org.voltdb.plannodes.SchemaColumn) NestLoopPlanNode(org.voltdb.plannodes.NestLoopPlanNode)

Aggregations

TupleValueExpression (org.voltdb.expressions.TupleValueExpression)83 AbstractExpression (org.voltdb.expressions.AbstractExpression)59 SchemaColumn (org.voltdb.plannodes.SchemaColumn)19 Column (org.voltdb.catalog.Column)17 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)14 Constraint (org.voltdb.catalog.Constraint)13 Table (org.voltdb.catalog.Table)10 ColumnRef (org.voltdb.catalog.ColumnRef)9 ParameterValueExpression (org.voltdb.expressions.ParameterValueExpression)9 StmtTableScan (org.voltdb.planner.parseinfo.StmtTableScan)9 JSONException (org.json_voltpatches.JSONException)8 AbstractPlanNode (org.voltdb.plannodes.AbstractPlanNode)8 NodeSchema (org.voltdb.plannodes.NodeSchema)8 ConstantValueExpression (org.voltdb.expressions.ConstantValueExpression)7 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)6 Set (java.util.Set)5 Index (org.voltdb.catalog.Index)5 StmtTargetTableScan (org.voltdb.planner.parseinfo.StmtTargetTableScan)5 ProjectionPlanNode (org.voltdb.plannodes.ProjectionPlanNode)5