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);
}
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;
}
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;
}
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;
}
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);
}
Aggregations