Search in sources :

Example 21 with Constant

use of org.teiid.query.sql.symbol.Constant 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 22 with Constant

use of org.teiid.query.sql.symbol.Constant in project teiid by teiid.

the class ProjectIntoNode method convertValuesToConstants.

private List<Constant> convertValuesToConstants(List<?> values, List<ElementSymbol> elements) {
    ArrayList<Constant> constants = new ArrayList<Constant>(values.size());
    for (int i = 0; i < elements.size(); i++) {
        ElementSymbol es = elements.get(i);
        Class<?> type = es.getType();
        constants.add(new Constant(values.get(i), type));
    }
    return constants;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) SourceHint(org.teiid.query.sql.lang.SourceHint)

Example 23 with Constant

use of org.teiid.query.sql.symbol.Constant in project teiid by teiid.

the class RelationalNodeUtil method shouldExecute.

public static boolean shouldExecute(Command command, boolean simplifyCriteria, boolean duringPlanning) throws TeiidComponentException, ExpressionEvaluationException {
    int cmdType = command.getType();
    Criteria criteria = null;
    switch(cmdType) {
        case Command.TYPE_QUERY:
            QueryCommand queryCommand = (QueryCommand) command;
            Limit limit = queryCommand.getLimit();
            if (limit != null && limit.getRowLimit() instanceof Constant) {
                Constant rowLimit = (Constant) limit.getRowLimit();
                if (Integer.valueOf(0).equals(rowLimit.getValue())) {
                    return false;
                }
            }
            if (queryCommand instanceof SetQuery) {
                SetQuery union = (SetQuery) queryCommand;
                boolean shouldExecute = false;
                for (QueryCommand innerQuery : union.getQueryCommands()) {
                    boolean shouldInner = shouldExecute(innerQuery, simplifyCriteria, duringPlanning);
                    if (shouldInner) {
                        shouldExecute = true;
                        break;
                    }
                }
                return shouldExecute;
            }
            // Else this is a query
            Query query = (Query) queryCommand;
            criteria = query.getCriteria();
            boolean shouldEvaluate = shouldEvaluate(simplifyCriteria, duringPlanning, criteria, query, false);
            if (shouldEvaluate) {
                // check for false having as well
                shouldEvaluate = shouldEvaluate(simplifyCriteria, duringPlanning, query.getHaving(), query, true);
            }
            if (shouldEvaluate) {
                return true;
            }
            if (query.hasAggregates() && query.getGroupBy() == null) {
                return true;
            }
            break;
        case Command.TYPE_INSERT:
            Insert insert = (Insert) command;
            QueryCommand expr = insert.getQueryExpression();
            if (expr != null) {
                return shouldExecute(expr, simplifyCriteria);
            }
            return true;
        case Command.TYPE_UPDATE:
            Update update = (Update) command;
            if (update.getChangeList().isEmpty()) {
                return false;
            }
            criteria = update.getCriteria();
            // then we don't know the result, so assume we need to execute
            if (criteria == null) {
                return true;
            }
            if (!EvaluatableVisitor.isFullyEvaluatable(criteria, duringPlanning)) {
                return true;
            } else if (Evaluator.evaluate(criteria)) {
                if (simplifyCriteria) {
                    update.setCriteria(null);
                }
                return true;
            }
            break;
        case Command.TYPE_DELETE:
            Delete delete = (Delete) command;
            criteria = delete.getCriteria();
            // then we don't know the result, so assume we need to execute
            if (criteria == null) {
                return true;
            }
            if (!EvaluatableVisitor.isFullyEvaluatable(criteria, duringPlanning)) {
                return true;
            } else if (Evaluator.evaluate(criteria)) {
                if (simplifyCriteria) {
                    delete.setCriteria(null);
                }
                return true;
            }
            break;
        default:
            return true;
    }
    return false;
}
Also used : Constant(org.teiid.query.sql.symbol.Constant)

Example 24 with Constant

use of org.teiid.query.sql.symbol.Constant in project teiid by teiid.

the class ExecDynamicSqlInstruction method createVariableValuesMap.

/**
 * @param localContext
 * @return
 */
private Map<ElementSymbol, Expression> createVariableValuesMap(VariableContext localContext) {
    Map<ElementSymbol, Object> variableMap = new HashMap<ElementSymbol, Object>();
    localContext.getFlattenedContextMap(variableMap);
    Map<ElementSymbol, Expression> nameValueMap = new HashMap<ElementSymbol, Expression>(variableMap.size());
    for (Map.Entry<ElementSymbol, Object> entry : variableMap.entrySet()) {
        if (entry.getKey() instanceof ElementSymbol) {
            if (entry.getValue() instanceof Expression) {
                nameValueMap.put(entry.getKey(), (Expression) entry.getValue());
            } else {
                nameValueMap.put(entry.getKey(), new Constant(entry.getValue(), entry.getKey().getType()));
            }
        }
    }
    return nameValueMap;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) HashMap(java.util.HashMap) Expression(org.teiid.query.sql.symbol.Expression) Constant(org.teiid.query.sql.symbol.Constant) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with Constant

use of org.teiid.query.sql.symbol.Constant in project teiid by teiid.

the class TestQueryRewriter method testRewriteCase1954b.

@Test
public void testRewriteCase1954b() throws Exception {
    QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
    CompareCriteria expected = new CompareCriteria();
    // $NON-NLS-1$
    ElementSymbol leftElement = new ElementSymbol("pm1.g1.e4");
    Constant constant = new Constant(new Double(3.0), DataTypeManager.DefaultDataClasses.DOUBLE);
    expected.setLeftExpression(leftElement);
    expected.setRightExpression(constant);
    // resolve against metadata
    QueryResolver.resolveCriteria(expected, metadata);
    // $NON-NLS-1$
    helpTestRewriteCriteria("convert(pm1.g1.e4, string) = '3.0'", expected, metadata);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Test(org.junit.Test)

Aggregations

Constant (org.teiid.query.sql.symbol.Constant)203 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)94 Test (org.junit.Test)88 ArrayList (java.util.ArrayList)61 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)48 List (java.util.List)38 Expression (org.teiid.query.sql.symbol.Expression)38 Function (org.teiid.query.sql.symbol.Function)31 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)25 Query (org.teiid.query.sql.lang.Query)22 Select (org.teiid.query.sql.lang.Select)15 Reference (org.teiid.query.sql.symbol.Reference)14 From (org.teiid.query.sql.lang.From)12 HashMap (java.util.HashMap)11 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)11 Criteria (org.teiid.query.sql.lang.Criteria)11 SetQuery (org.teiid.query.sql.lang.SetQuery)11 LinkedList (java.util.LinkedList)10 Limit (org.teiid.query.sql.lang.Limit)10 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)9