Search in sources :

Example 41 with AbstractExpression

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

the class AggregatePlanNode method updateAggregate.

public void updateAggregate(int index, ExpressionType aggType) {
    // Create a new aggregate expression which we'll use to update the
    // output schema (whose exprs are TVEs).
    AggregateExpression aggExpr = new AggregateExpression(aggType);
    aggExpr.finalizeValueTypes();
    int outputSchemaIndex = m_aggregateOutputColumns.get(index);
    SchemaColumn schemaCol = m_outputSchema.getColumns().get(outputSchemaIndex);
    AbstractExpression schemaExpr = schemaCol.getExpression();
    schemaExpr.setValueType(aggExpr.getValueType());
    schemaExpr.setValueSize(aggExpr.getValueSize());
    m_aggregateTypes.set(index, aggType);
}
Also used : AbstractExpression(org.voltdb.expressions.AbstractExpression) AggregateExpression(org.voltdb.expressions.AggregateExpression)

Example 42 with AbstractExpression

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

the class AggregatePlanNode method isTableNonDistinctCountConstant.

public boolean isTableNonDistinctCountConstant() {
    if (!isTableNonDistinctCount()) {
        return false;
    }
    AbstractExpression aggArgument = m_aggregateExpressions.get(0);
    ExpressionType argumentType = aggArgument.getExpressionType();
    // Is the expression a constant?
    return argumentType.equals(ExpressionType.VALUE_PARAMETER) || argumentType.equals(ExpressionType.VALUE_CONSTANT);
}
Also used : AbstractExpression(org.voltdb.expressions.AbstractExpression) ExpressionType(org.voltdb.types.ExpressionType)

Example 43 with AbstractExpression

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

the class IndexCountPlanNode method createOrNull.

// Create an IndexCountPlanNode that replaces the parent aggregate and child
// indexscan IF the indexscan's end expressions are a form that can be
// modeled with an end key.
// The supported forms for end expression are:
//   - null
//   - one filter expression per index key component (ANDed together)
//     as "combined" for the IndexScan.
//   - fewer filter expressions than index key components with one of the
//     (the last) being a LT comparison.
//   - 1 fewer filter expressions than index key components,
//     but all ANDed equality filters
// The LT restriction comes because when index key prefixes are identical
// to the prefix-only end key, the entire index key sorts greater than the
// prefix-only end-key, because it is always longer.
// These prefix-equal cases would be missed in an EQ or LTE filter,
// causing undercounts.
// A prefix-only LT filter discards prefix-equal cases, so it is allowed.
// @return the IndexCountPlanNode or null if one is not possible.
public static IndexCountPlanNode createOrNull(IndexScanPlanNode isp, AggregatePlanNode apn) {
    // add support for reverse scan
    // for ASC scan, check endExpression;
    // for DESC scan (isReverseScan()), check the searchkeys
    List<AbstractExpression> endKeys = new ArrayList<>();
    // Translate the index scan's end condition into a list of end key
    // expressions and note the comparison operand of the last one.
    // Initially assume it to be an equality filter.
    IndexLookupType endType = IndexLookupType.EQ;
    List<AbstractExpression> endComparisons = ExpressionUtil.uncombinePredicate(isp.getEndExpression());
    for (AbstractExpression ae : endComparisons) {
        // LT or LTE expression that resets the end type.
        assert (endType == IndexLookupType.EQ);
        ExpressionType exprType = ae.getExpressionType();
        if (exprType == ExpressionType.COMPARE_LESSTHAN) {
            endType = IndexLookupType.LT;
        } else if (exprType == ExpressionType.COMPARE_LESSTHANOREQUALTO) {
            endType = IndexLookupType.LTE;
        } else {
            assert (exprType == ExpressionType.COMPARE_EQUAL || exprType == ExpressionType.COMPARE_NOTDISTINCT);
        }
        // PlanNodes all need private deep copies of expressions
        // so that the resolveColumnIndexes results
        // don't get bashed by other nodes or subsequent planner runs
        endKeys.add(ae.getRight().clone());
    }
    int indexSize = 0;
    String jsonstring = isp.getCatalogIndex().getExpressionsjson();
    List<ColumnRef> indexedColRefs = null;
    List<AbstractExpression> indexedExprs = null;
    if (jsonstring.isEmpty()) {
        indexedColRefs = CatalogUtil.getSortedCatalogItems(isp.getCatalogIndex().getColumns(), "index");
        indexSize = indexedColRefs.size();
    } else {
        try {
            indexedExprs = AbstractExpression.fromJSONArrayString(jsonstring, isp.getTableScan());
            indexSize = indexedExprs.size();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    int searchKeySize = isp.getSearchKeyExpressions().size();
    int endKeySize = endKeys.size();
    if (!isp.isReverseScan() && endType != IndexLookupType.LT && endKeySize > 0 && endKeySize < indexSize) {
        // That is, when a prefix-only key exists and does not use LT.
        if (endType != IndexLookupType.EQ || searchKeySize != indexSize || endKeySize < indexSize - 1) {
            return null;
        }
        // To use an index count for an equality search of da compound key,
        // both the search key and end key must have a component for each
        // index component.
        // If the search key is long enough but the end key is one component
        // short, it can be patched with a type-appropriate max key value
        // (if one exists for the type), but the end key comparison needs to
        // change from EQ to LTE to compensate.
        VoltType missingEndKeyType;
        // and get the missing key component's indexed expression.
        if (jsonstring.isEmpty()) {
            int lastIndex = indexedColRefs.get(endKeySize).getColumn().getIndex();
            for (AbstractExpression expr : endComparisons) {
                if (((TupleValueExpression) (expr.getLeft())).getColumnIndex() == lastIndex) {
                    return null;
                }
            }
            int catalogTypeCode = indexedColRefs.get(endKeySize).getColumn().getType();
            missingEndKeyType = VoltType.get((byte) catalogTypeCode);
        } else {
            AbstractExpression lastIndexedExpr = indexedExprs.get(endKeySize);
            for (AbstractExpression expr : endComparisons) {
                if (expr.getLeft().bindingToIndexedExpression(lastIndexedExpr) != null) {
                    return null;
                }
            }
            missingEndKeyType = lastIndexedExpr.getValueType();
        }
        String maxValueForType = missingEndKeyType.getMaxValueForKeyPadding();
        // for which all legal values are less than or equal to it.
        if (maxValueForType == null) {
            return null;
        }
        ConstantValueExpression maxKey = new ConstantValueExpression();
        maxKey.setValueType(missingEndKeyType);
        maxKey.setValue(maxValueForType);
        maxKey.setValueSize(missingEndKeyType.getLengthInBytesForFixedTypes());
        endType = IndexLookupType.LTE;
        endKeys.add(maxKey);
    }
    // DESC case
    if (searchKeySize > 0 && searchKeySize < indexSize) {
        return null;
    }
    return new IndexCountPlanNode(isp, apn, endType, endKeys);
}
Also used : IndexLookupType(org.voltdb.types.IndexLookupType) ArrayList(java.util.ArrayList) JSONException(org.json_voltpatches.JSONException) AbstractExpression(org.voltdb.expressions.AbstractExpression) VoltType(org.voltdb.VoltType) ConstantValueExpression(org.voltdb.expressions.ConstantValueExpression) ColumnRef(org.voltdb.catalog.ColumnRef) ExpressionType(org.voltdb.types.ExpressionType)

Example 44 with AbstractExpression

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

the class IndexScanPlanNode method buildSkipNullPredicate.

public static AbstractExpression buildSkipNullPredicate(int nullExprIndex, Index catalogIndex, StmtTableScan tableScan, List<AbstractExpression> searchkeyExpressions, List<Boolean> compareNotDistinct) {
    String exprsjson = catalogIndex.getExpressionsjson();
    List<AbstractExpression> indexedExprs = null;
    if (exprsjson.isEmpty()) {
        indexedExprs = new ArrayList<>();
        List<ColumnRef> indexedColRefs = CatalogUtil.getSortedCatalogItems(catalogIndex.getColumns(), "index");
        assert (nullExprIndex < indexedColRefs.size());
        for (int i = 0; i <= nullExprIndex; i++) {
            ColumnRef colRef = indexedColRefs.get(i);
            Column col = colRef.getColumn();
            TupleValueExpression tve = new TupleValueExpression(tableScan.getTableName(), tableScan.getTableAlias(), col, col.getIndex());
            indexedExprs.add(tve);
        }
    } else {
        try {
            indexedExprs = AbstractExpression.fromJSONArrayString(exprsjson, tableScan);
            assert (nullExprIndex < indexedExprs.size());
        } catch (JSONException e) {
            e.printStackTrace();
            assert (false);
        }
    }
    // For a partial index extract all TVE expressions from it predicate if it's NULL-rejecting expression
    // These TVEs do not need to be added to the skipNUll predicate because it's redundant.
    AbstractExpression indexPredicate = null;
    Set<TupleValueExpression> notNullTves = null;
    String indexPredicateJson = catalogIndex.getPredicatejson();
    if (!StringUtil.isEmpty(indexPredicateJson)) {
        try {
            indexPredicate = AbstractExpression.fromJSONString(indexPredicateJson, tableScan);
            assert (indexPredicate != null);
        } catch (JSONException e) {
            e.printStackTrace();
            assert (false);
        }
        if (ExpressionUtil.isNullRejectingExpression(indexPredicate, tableScan.getTableAlias())) {
            notNullTves = new HashSet<>();
            notNullTves.addAll(ExpressionUtil.getTupleValueExpressions(indexPredicate));
        }
    }
    AbstractExpression nullExpr = indexedExprs.get(nullExprIndex);
    AbstractExpression skipNullPredicate = null;
    if (notNullTves == null || !notNullTves.contains(nullExpr)) {
        List<AbstractExpression> exprs = new ArrayList<>();
        for (int i = 0; i < nullExprIndex; i++) {
            AbstractExpression idxExpr = indexedExprs.get(i);
            ExpressionType exprType = ExpressionType.COMPARE_EQUAL;
            if (i < compareNotDistinct.size() && compareNotDistinct.get(i)) {
                exprType = ExpressionType.COMPARE_NOTDISTINCT;
            }
            AbstractExpression expr = new ComparisonExpression(exprType, idxExpr, searchkeyExpressions.get(i).clone());
            exprs.add(expr);
        }
        // then we add "nullExpr IS NULL" to the expression for matching tuples to skip. (ENG-11096)
        if (nullExprIndex == searchkeyExpressions.size() || compareNotDistinct.get(nullExprIndex) == false) {
            // nullExprIndex == m_searchkeyExpressions.size() - 1
            AbstractExpression expr = new OperatorExpression(ExpressionType.OPERATOR_IS_NULL, nullExpr, null);
            exprs.add(expr);
        } else {
            return null;
        }
        skipNullPredicate = ExpressionUtil.combinePredicates(exprs);
        skipNullPredicate.finalizeValueTypes();
    }
    return skipNullPredicate;
}
Also used : TupleValueExpression(org.voltdb.expressions.TupleValueExpression) ArrayList(java.util.ArrayList) JSONException(org.json_voltpatches.JSONException) ComparisonExpression(org.voltdb.expressions.ComparisonExpression) AbstractExpression(org.voltdb.expressions.AbstractExpression) Column(org.voltdb.catalog.Column) OperatorExpression(org.voltdb.expressions.OperatorExpression) ColumnRef(org.voltdb.catalog.ColumnRef) ExpressionType(org.voltdb.types.ExpressionType)

Example 45 with AbstractExpression

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

the class IndexScanPlanNode method isSortExpressionCovered.

private boolean isSortExpressionCovered(AbstractExpression sortExpression, List<AbstractExpression> indexedExprs, int idxToCover) {
    assert (idxToCover < indexedExprs.size());
    AbstractExpression indexExpression = indexedExprs.get(idxToCover);
    List<AbstractExpression> bindings = sortExpression.bindingToIndexedExpression(indexExpression);
    if (bindings != null) {
        m_bindings.addAll(bindings);
        return true;
    }
    return false;
}
Also used : AbstractExpression(org.voltdb.expressions.AbstractExpression)

Aggregations

AbstractExpression (org.voltdb.expressions.AbstractExpression)215 TupleValueExpression (org.voltdb.expressions.TupleValueExpression)59 AbstractPlanNode (org.voltdb.plannodes.AbstractPlanNode)55 ArrayList (java.util.ArrayList)43 SeqScanPlanNode (org.voltdb.plannodes.SeqScanPlanNode)26 SchemaColumn (org.voltdb.plannodes.SchemaColumn)25 NestLoopPlanNode (org.voltdb.plannodes.NestLoopPlanNode)23 Constraint (org.voltdb.catalog.Constraint)22 IndexScanPlanNode (org.voltdb.plannodes.IndexScanPlanNode)22 HashSet (java.util.HashSet)21 Column (org.voltdb.catalog.Column)21 AbstractScanPlanNode (org.voltdb.plannodes.AbstractScanPlanNode)21 JSONException (org.json_voltpatches.JSONException)19 ColumnRef (org.voltdb.catalog.ColumnRef)19 Table (org.voltdb.catalog.Table)17 AbstractSubqueryExpression (org.voltdb.expressions.AbstractSubqueryExpression)16 ParameterValueExpression (org.voltdb.expressions.ParameterValueExpression)16 StmtTableScan (org.voltdb.planner.parseinfo.StmtTableScan)16 ExpressionType (org.voltdb.types.ExpressionType)16 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)14