use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.
the class Evaluator method evaluate.
private Boolean evaluate(MatchCriteria criteria, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
boolean result = false;
// Evaluate left expression
Object value = null;
try {
value = evaluate(criteria.getLeftExpression(), tuple);
} catch (ExpressionEvaluationException e) {
// $NON-NLS-1$
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30312, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30312, "left", criteria));
}
// Shortcut if null
if (value == null) {
return null;
}
CharSequence leftValue = null;
if (value instanceof CharSequence) {
leftValue = (CharSequence) value;
} else {
try {
leftValue = ((Sequencable) value).getCharSequence();
} catch (SQLException err) {
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30316, err, err.getMessage());
}
}
// Evaluate right expression
String rightValue = null;
try {
rightValue = (String) evaluate(criteria.getRightExpression(), tuple);
} catch (ExpressionEvaluationException e) {
// $NON-NLS-1$
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30312, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30312, "right", criteria));
}
// Shortcut if null
if (rightValue == null) {
return null;
}
result = match(rightValue, criteria.getEscapeChar(), leftValue, criteria.getMode());
return Boolean.valueOf(result ^ criteria.isNegated());
}
use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.
the class Evaluator method evaluateXMLParse.
private Object evaluateXMLParse(List<?> tuple, final XMLParse xp) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
Object value = internalEvaluate(xp.getExpression(), tuple);
if (value == null) {
return null;
}
XMLType.Type type = Type.DOCUMENT;
SQLXMLImpl result = null;
try {
if (value instanceof String) {
String string = (String) value;
result = new SQLXMLImpl(string);
result.setCharset(Streamable.CHARSET);
if (!xp.isWellFormed()) {
Reader r = new StringReader(string);
type = validate(xp, r);
}
} else if (value instanceof BinaryType) {
BinaryType string = (BinaryType) value;
result = new SQLXMLImpl(string.getBytesDirect());
result.setCharset(Streamable.CHARSET);
if (!xp.isWellFormed()) {
Reader r = result.getCharacterStream();
type = validate(xp, r);
}
} else {
InputStreamFactory isf = null;
Streamable<?> s = (Streamable<?>) value;
isf = getInputStreamFactory(s);
result = new SQLXMLImpl(isf);
if (!xp.isWellFormed()) {
Reader r = result.getCharacterStream();
type = validate(xp, r);
}
}
} catch (TransformationException e) {
throw new ExpressionEvaluationException(e);
} catch (SQLException e) {
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30331, e, e.getMessage());
}
if (!xp.isDocument()) {
type = Type.CONTENT;
}
XMLType xml = new XMLType(result);
xml.setType(type);
return xml;
}
use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.
the class Evaluator method internalEvaluate.
protected Object internalEvaluate(Expression expression, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
if (expression instanceof DerivedExpression) {
if (elements != null) {
// Try to evaluate by lookup in the elements map (may work for both ElementSymbol and ExpressionSymbol
Integer index = (Integer) elements.get(expression);
if (index != null) {
return tuple.get(index.intValue());
}
}
// Otherwise this should be an ExpressionSymbol and we just need to dive in and evaluate the expression itself
if (expression instanceof ExpressionSymbol) {
ExpressionSymbol exprSyb = (ExpressionSymbol) expression;
Expression expr = exprSyb.getExpression();
return internalEvaluate(expr, tuple);
}
return getContext(expression).getFromContext(expression);
}
if (expression instanceof Constant) {
Constant c = (Constant) expression;
if (c.isMultiValued()) {
// $NON-NLS-1$
throw new AssertionError("Multi-valued constant not allowed to be directly evaluated");
}
return c.getValue();
} else if (expression instanceof Function) {
return evaluate((Function) expression, tuple);
} else if (expression instanceof CaseExpression) {
return evaluate((CaseExpression) expression, tuple);
} else if (expression instanceof SearchedCaseExpression) {
return evaluate((SearchedCaseExpression) expression, tuple);
} else if (expression instanceof Reference) {
Reference ref = (Reference) expression;
if (ref.isPositional() && ref.getExpression() == null) {
return getContext(ref).getVariableContext().getGlobalValue(ref.getContextSymbol());
}
Object result = getContext(ref.getExpression()).getFromContext(ref.getExpression());
if (ref.getConstraint() != null) {
try {
ref.getConstraint().validate(result);
} catch (QueryValidatorException e) {
throw new ExpressionEvaluationException(e);
}
}
return result;
} else if (expression instanceof Criteria) {
return evaluate((Criteria) expression, tuple);
} else if (expression instanceof ScalarSubquery) {
return evaluate((ScalarSubquery) expression, tuple);
} else if (expression instanceof Criteria) {
return evaluate((Criteria) expression, tuple);
} else if (expression instanceof TextLine) {
return evaluateTextLine(tuple, (TextLine) expression);
} else if (expression instanceof XMLElement) {
return evaluateXMLElement(tuple, (XMLElement) expression);
} else if (expression instanceof XMLForest) {
return evaluateXMLForest(tuple, (XMLForest) expression);
} else if (expression instanceof JSONObject) {
return evaluateJSONObject(tuple, (JSONObject) expression, null);
} else if (expression instanceof XMLSerialize) {
return evaluateXMLSerialize(tuple, (XMLSerialize) expression);
} else if (expression instanceof XMLQuery) {
return evaluateXMLQuery(tuple, (XMLQuery) expression, false);
} else if (expression instanceof QueryString) {
return evaluateQueryString(tuple, (QueryString) expression);
} else if (expression instanceof XMLParse) {
return evaluateXMLParse(tuple, (XMLParse) expression);
} else if (expression instanceof Array) {
Array array = (Array) expression;
List<Expression> exprs = array.getExpressions();
Object[] result = (Object[]) java.lang.reflect.Array.newInstance(array.getComponentType(), exprs.size());
for (int i = 0; i < exprs.size(); i++) {
Object eval = internalEvaluate(exprs.get(i), tuple);
if (eval instanceof ArrayImpl) {
eval = ((ArrayImpl) eval).getValues();
}
result[i] = eval;
}
return new ArrayImpl(result);
} else if (expression instanceof ExceptionExpression) {
return evaluate(tuple, (ExceptionExpression) expression);
} else if (expression instanceof XMLCast) {
return evaluate(tuple, (XMLCast) expression);
} else {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30329, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30329, expression.getClass().getName()));
}
}
use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.
the class Evaluator method evaluateTextLine.
private Object evaluateTextLine(List<?> tuple, TextLine function) throws ExpressionEvaluationException, BlockedException, TeiidComponentException, FunctionExecutionException {
List<DerivedColumn> args = function.getExpressions();
Evaluator.NameValuePair<Object>[] nameValuePairs = getNameValuePairs(tuple, args, true, true);
try {
return new ArrayImpl(TextLine.evaluate(Arrays.asList(nameValuePairs), defaultExtractor, function));
} catch (TransformationException e) {
throw new ExpressionEvaluationException(e);
} catch (TeiidProcessingException e) {
throw new ExpressionEvaluationException(e);
}
}
use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.
the class Evaluator method evaluate.
private Boolean evaluate(AbstractSetCriteria criteria, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
// Evaluate expression
Object leftValue = null;
try {
leftValue = evaluate(criteria.getExpression(), tuple);
} catch (ExpressionEvaluationException e) {
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30323, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30323, criteria));
}
Boolean result = Boolean.FALSE;
ValueIterator valueIter = null;
if (criteria instanceof SetCriteria) {
SetCriteria set = (SetCriteria) criteria;
// Shortcut if null
if (leftValue == null) {
if (!set.getValues().isEmpty()) {
return null;
}
return criteria.isNegated();
}
if (set.isAllConstants()) {
boolean exists = set.getValues().contains(new Constant(leftValue, criteria.getExpression().getType()));
if (!exists) {
if (set.getValues().contains(Constant.NULL_CONSTANT)) {
return null;
}
return criteria.isNegated();
}
return !criteria.isNegated();
}
valueIter = new CollectionValueIterator(((SetCriteria) criteria).getValues());
} else if (criteria instanceof DependentSetCriteria) {
DependentSetCriteria ref = (DependentSetCriteria) criteria;
VariableContext vc = getContext(criteria).getVariableContext();
ValueIteratorSource vis = (ValueIteratorSource) vc.getGlobalValue(ref.getContextSymbol());
if (leftValue == null) {
return null;
}
Set<Object> values;
try {
values = vis.getCachedSet(ref.getValueExpression());
} catch (TeiidProcessingException e) {
throw new ExpressionEvaluationException(e);
}
if (values != null) {
return values.contains(leftValue);
}
vis.setUnused(true);
// them in memory
return true;
} else if (criteria instanceof SubquerySetCriteria) {
try {
valueIter = evaluateSubquery((SubquerySetCriteria) criteria, tuple);
} catch (TeiidProcessingException e) {
throw new ExpressionEvaluationException(e);
}
} else {
// $NON-NLS-1$
throw new AssertionError("unknown set criteria type");
}
while (valueIter.hasNext()) {
if (leftValue == null) {
return null;
}
Object possibleValue = valueIter.next();
Object value = null;
if (possibleValue instanceof Expression) {
try {
value = evaluate((Expression) possibleValue, tuple);
} catch (ExpressionEvaluationException e) {
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30323, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30323, possibleValue));
}
} else {
value = possibleValue;
}
if (value != null) {
if (Constant.COMPARATOR.compare(leftValue, value) == 0) {
return Boolean.valueOf(!criteria.isNegated());
}
// else try next value
} else {
result = null;
}
}
if (result == null) {
return null;
}
return Boolean.valueOf(criteria.isNegated());
}
Aggregations