Search in sources :

Example 1 with AbstractSetCriteria

use of org.teiid.query.sql.lang.AbstractSetCriteria in project teiid by teiid.

the class DependentCriteriaProcessor method replaceDependentCriteria.

public Criteria replaceDependentCriteria(AbstractSetCriteria crit, SetState state) throws TeiidComponentException {
    if (state.overMax) {
        DependentValueSource originalVs = (DependentValueSource) dependentNode.getContext().getVariableContext().getGlobalValue(((DependentSetCriteria) crit).getContextSymbol());
        originalVs.setUnused(true);
        return QueryRewriter.TRUE_CRITERIA;
    }
    if (state.replacement.isEmpty()) {
        // No values - return criteria that is always false
        return QueryRewriter.FALSE_CRITERIA;
    }
    int numberOfSets = 1;
    int setSize = Integer.MAX_VALUE;
    if (this.maxSetSize > 0) {
        setSize = (int) Math.max(1, this.maxSetSize / state.valueCount);
        numberOfSets = state.replacement.size() / setSize + (state.replacement.size() % setSize != 0 ? 1 : 0);
    }
    Iterator<Constant> iter = state.replacement.iterator();
    ArrayList<Criteria> orCrits = new ArrayList<Criteria>(numberOfSets);
    for (int i = 0; i < numberOfSets; i++) {
        if (setSize == 1 || i + 1 == state.replacement.size()) {
            orCrits.add(new CompareCriteria(crit.getExpression(), CompareCriteria.EQ, iter.next()));
        } else {
            List<Constant> vals = new ArrayList<Constant>(Math.min(state.replacement.size(), setSize));
            for (int j = 0; j < setSize && iter.hasNext(); j++) {
                Constant val = iter.next();
                vals.add(val);
            }
            SetCriteria sc = new SetCriteria();
            sc.setExpression(crit.getExpression());
            sc.setValues(vals);
            orCrits.add(sc);
        }
    }
    if (orCrits.size() == 1) {
        return orCrits.get(0);
    }
    return new CompoundCriteria(CompoundCriteria.OR, orCrits);
}
Also used : DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) Constant(org.teiid.query.sql.symbol.Constant) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) AbstractSetCriteria(org.teiid.query.sql.lang.AbstractSetCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) Criteria(org.teiid.query.sql.lang.Criteria) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) AbstractSetCriteria(org.teiid.query.sql.lang.AbstractSetCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 2 with AbstractSetCriteria

use of org.teiid.query.sql.lang.AbstractSetCriteria in project teiid by teiid.

the class DependentCriteriaProcessor method prepareCriteria.

public Criteria prepareCriteria() throws TeiidComponentException, TeiidProcessingException {
    if (phase == SORT) {
        for (TupleState state : dependentState.values()) {
            state.sort();
            if (state.dvs.getTupleBuffer().getRowCount() == 0) {
                return QueryRewriter.FALSE_CRITERIA;
            }
        }
        // init total predicates and max size
        totalPredicates = setStates.size();
        if (this.maxPredicates > 0) {
            // We have a bin packing problem if totalPredicates < sources - We'll address that case later.
            // TODO: better handling for the correlated composite case
            totalPredicates = Math.max(totalPredicates, this.maxPredicates);
        }
        long maxParams = this.maxPredicates * this.maxSetSize;
        maxSize = Integer.MAX_VALUE;
        if (this.maxSetSize > 0) {
            maxSize = this.maxSetSize;
            if (this.maxPredicates > 0 && totalPredicates > this.maxPredicates) {
                // scale the max based upon the number of predicates - this is not perfect, but sufficient for most situations
                maxSize = Math.max(1, maxParams / totalPredicates);
            }
        }
        // determine push down handling
        if (pushdown) {
            List<Criteria> newCriteria = new ArrayList<Criteria>();
            long params = 0;
            int sets = 0;
            for (Criteria criteria : queryCriteria) {
                if (!(criteria instanceof DependentSetCriteria)) {
                    newCriteria.add(criteria);
                    continue;
                }
                sets++;
                DependentSetCriteria dsc = (DependentSetCriteria) criteria;
                TupleState ts = dependentState.get(dsc.getContextSymbol());
                DependentValueSource dvs = ts.dvs;
                // check if this has more rows than we want to push
                if ((dsc.getMaxNdv() != -1 && dvs.getTupleBuffer().getRowCount() > dsc.getMaxNdv()) || (dsc.getMakeDepOptions() != null && dsc.getMakeDepOptions().getMax() != null && dvs.getTupleBuffer().getRowCount() > dsc.getMakeDepOptions().getMax())) {
                    // don't try to pushdown
                    continue;
                }
                int cols = 1;
                if (dsc.getExpression() instanceof Array) {
                    cols = ((Array) dsc.getExpression()).getExpressions().size();
                }
                dsc = dsc.clone();
                // determine if this will be more than 1 source query
                params += cols * dvs.getTupleBuffer().getRowCount();
                // TODO: this assumes that if any one of the dependent
                // joins are pushed, then they all are
                dsc.setDependentValueSource(dvs);
                newCriteria.add(dsc);
            }
            // TODO: see if this should be a source tunable parameter
            int maxParamThreshold = 3;
            // generally this value accounts for the additional overhead of temp table creation
            if (params > maxParams && (sets > 1 || complexQuery || params > maxParams * maxParamThreshold)) {
                // and only if the we could produce a cross set or have a complex query
                return Criteria.combineCriteria(newCriteria);
            }
        }
        // proceed with set based processing
        phase = SET_PROCESSING;
    }
    replaceDependentValueIterators();
    LinkedList<Criteria> crits = new LinkedList<Criteria>();
    for (int i = 0; i < queryCriteria.size(); i++) {
        SetState state = this.setStates.get(i);
        Criteria criteria = queryCriteria.get(i);
        if (state == null) {
            if (criteria != QueryRewriter.TRUE_CRITERIA) {
                crits.add((Criteria) criteria.clone());
            }
        } else {
            Criteria crit = replaceDependentCriteria((AbstractSetCriteria) criteria, state);
            if (crit == QueryRewriter.FALSE_CRITERIA) {
                return QueryRewriter.FALSE_CRITERIA;
            }
            if (crit != QueryRewriter.TRUE_CRITERIA) {
                crits.add(crit);
            }
        }
    }
    if (crits.size() == 1) {
        return crits.get(0);
    }
    return new CompoundCriteria(CompoundCriteria.AND, crits);
}
Also used : CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) Criteria(org.teiid.query.sql.lang.Criteria) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) AbstractSetCriteria(org.teiid.query.sql.lang.AbstractSetCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) Array(org.teiid.query.sql.symbol.Array) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria)

Aggregations

AbstractSetCriteria (org.teiid.query.sql.lang.AbstractSetCriteria)2 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)2 CompoundCriteria (org.teiid.query.sql.lang.CompoundCriteria)2 Criteria (org.teiid.query.sql.lang.Criteria)2 DependentSetCriteria (org.teiid.query.sql.lang.DependentSetCriteria)2 SetCriteria (org.teiid.query.sql.lang.SetCriteria)2 Array (org.teiid.query.sql.symbol.Array)1 Constant (org.teiid.query.sql.symbol.Constant)1