use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.
the class GeometryUtils method geometryFromClob.
public static GeometryType geometryFromClob(ClobType wkt, Integer srid, boolean allowEwkt) throws FunctionExecutionException {
Reader r = null;
try {
WKTReader reader = new WKTReader(GEOMETRY_FACTORY);
r = wkt.getCharacterStream();
if (allowEwkt) {
PushbackReader pbr = new PushbackReader(r, 1);
r = pbr;
char[] expected = new char[] { 's', 'r', 'i', 'd', '=' };
int expectedIndex = 0;
StringBuilder sridBuffer = null;
for (int i = 0; i < 100000; i++) {
int charRead = pbr.read();
if (charRead == -1) {
break;
}
if (expectedIndex == expected.length) {
// parse srid
if (sridBuffer == null) {
sridBuffer = new StringBuilder(4);
}
if (charRead == ';') {
if (sridBuffer.length() == 0) {
pbr.unread(charRead);
}
break;
}
sridBuffer.append((char) charRead);
continue;
}
if (expectedIndex == 0 && Character.isWhitespace(charRead)) {
continue;
}
if (expected[expectedIndex] != Character.toLowerCase(charRead)) {
pbr.unread(charRead);
break;
}
expectedIndex++;
}
if (sridBuffer != null) {
srid = Integer.parseInt(sridBuffer.toString());
}
}
Geometry jtsGeometry = reader.read(r);
if (jtsGeometry == null) {
// for some reason io and parse exceptions are caught in their logic if they occur on the first word, then null is returned
throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31203));
}
if (!allowEwkt && (jtsGeometry.getSRID() != GeometryType.UNKNOWN_SRID || (jtsGeometry.getCoordinate() != null && !Double.isNaN(jtsGeometry.getCoordinate().z)))) {
// $NON-NLS-1$
throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31160, "EWKT"));
}
if (srid == null) {
srid = jtsGeometry.getSRID();
}
return getGeometryType(jtsGeometry, srid);
} catch (ParseException e) {
throw new FunctionExecutionException(e);
} catch (SQLException e) {
throw new FunctionExecutionException(e);
} catch (IOException e) {
throw new FunctionExecutionException(e);
} finally {
if (r != null) {
try {
r.close();
} catch (IOException e) {
}
}
}
}
use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.
the class QueryRewriter method simplifyParseFormatFunction.
private Criteria simplifyParseFormatFunction(CompareCriteria crit) {
// TODO: this can be relaxed for order preserving operations
if (!(crit.getOperator() == CompareCriteria.EQ || crit.getOperator() == CompareCriteria.NE)) {
return crit;
}
boolean isFormat = false;
Function leftFunction = (Function) crit.getLeftExpression();
String funcName = leftFunction.getName();
String inverseFunction = null;
if (StringUtil.startsWithIgnoreCase(funcName, "parse")) {
// $NON-NLS-1$
String type = funcName.substring(5);
if (!PARSE_FORMAT_TYPES.contains(type)) {
return crit;
}
// $NON-NLS-1$
inverseFunction = "format" + type;
} else if (StringUtil.startsWithIgnoreCase(funcName, "format")) {
// $NON-NLS-1$
String type = funcName.substring(6);
if (!PARSE_FORMAT_TYPES.contains(type)) {
return crit;
}
// $NON-NLS-1$
inverseFunction = "parse" + type;
isFormat = true;
} else {
return crit;
}
Expression rightExpr = crit.getRightExpression();
if (!(rightExpr instanceof Constant)) {
return crit;
}
Expression leftExpr = leftFunction.getArgs()[0];
Expression formatExpr = leftFunction.getArgs()[1];
if (!(formatExpr instanceof Constant)) {
return crit;
}
String format = (String) ((Constant) formatExpr).getValue();
FunctionLibrary funcLib = this.metadata.getFunctionLibrary();
FunctionDescriptor descriptor = funcLib.findFunction(inverseFunction, new Class[] { rightExpr.getType(), formatExpr.getType() });
if (descriptor == null) {
return crit;
}
Object value = ((Constant) rightExpr).getValue();
try {
Object result = descriptor.invokeFunction(new Object[] { context, ((Constant) rightExpr).getValue(), format }, null, this.context);
result = leftFunction.getFunctionDescriptor().invokeFunction(new Object[] { context, result, format }, null, this.context);
if (Constant.COMPARATOR.compare(value, result) != 0) {
return getSimpliedCriteria(crit, leftExpr, crit.getOperator() != CompareCriteria.EQ, true);
}
} catch (FunctionExecutionException e) {
// Not all numeric formats are invertable, so just return the criteria as it may still be valid
return crit;
} catch (BlockedException e) {
return crit;
}
// parseFunctions are all potentially narrowing
if (!isFormat) {
return crit;
}
// TODO: if format is not lossy, then invert the function
return crit;
}
use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.
the class QueryRewriter method simplifyTimestampMerge.
/**
* This method also applies the same simplification for Case 1829. This is conceptually
* the same thing but done using the timestampCreate system function.
*
* formatDate(rpcolli_physical.RPCOLLI.Table_B.date_field, 'yyyy-MM-dd')
* || formatTime(rpcolli_physical.RPCOLLI.Table_B.time_field, ' HH:mm:ss') = '1969-09-20 18:30:45'
*
* -------------
*
* rpcolli_physical.RPCOLLI.Table_B.date_field = {d'1969-09-20'}
* AND
* rpcolli_physical.RPCOLLI.Table_B.time_field = {t'18:30:45'}
*
* @param criteria Compare criteria
* @return Simplified criteria, if possible
*/
private Criteria simplifyTimestampMerge(CompareCriteria criteria) {
if (criteria.getOperator() != CompareCriteria.EQ) {
return criteria;
}
Expression leftExpr = criteria.getLeftExpression();
Expression rightExpr = criteria.getRightExpression();
// Allow for concat and string literal to be on either side
Function concatFunction = null;
Constant timestampConstant = null;
if (leftExpr instanceof Function && rightExpr instanceof Constant) {
concatFunction = (Function) leftExpr;
timestampConstant = (Constant) rightExpr;
} else {
return criteria;
}
// Verify data type of string constant and that constant has a value
if (!timestampConstant.getType().equals(DataTypeManager.DefaultDataClasses.STRING)) {
return criteria;
}
// Verify function is concat function
if (!(concatFunction.getName().equalsIgnoreCase("concat") || concatFunction.getName().equals("||"))) {
// $NON-NLS-1$ //$NON-NLS-2$
return criteria;
}
// Verify concat has formatdate and formattime functions
Expression[] args = concatFunction.getArgs();
if (!(args[0] instanceof Function && args[1] instanceof Function)) {
return criteria;
}
Function formatDateFunction = (Function) args[0];
Function formatTimeFunction = (Function) args[1];
if (!(formatDateFunction.getName().equalsIgnoreCase("formatdate") && formatTimeFunction.getName().equalsIgnoreCase("formattime"))) {
// $NON-NLS-1$ //$NON-NLS-2$
return criteria;
}
// Verify format functions have constants
if (!(formatDateFunction.getArgs()[1] instanceof Constant && formatTimeFunction.getArgs()[1] instanceof Constant)) {
return criteria;
}
// Verify length of combined date/time constants == timestamp constant
String dateFormat = (String) ((Constant) formatDateFunction.getArgs()[1]).getValue();
String timeFormat = (String) ((Constant) formatTimeFunction.getArgs()[1]).getValue();
String timestampValue = (String) timestampConstant.getValue();
// Passed all the checks, so build the optimized version
try {
Timestamp ts = FunctionMethods.parseTimestamp(this.context, timestampValue, dateFormat + timeFormat);
Constant dateConstant = new Constant(TimestampWithTimezone.createDate(ts));
CompareCriteria dateCompare = new CompareCriteria(formatDateFunction.getArgs()[0], CompareCriteria.EQ, dateConstant);
Constant timeConstant = new Constant(TimestampWithTimezone.createTime(ts));
CompareCriteria timeCompare = new CompareCriteria(formatTimeFunction.getArgs()[0], CompareCriteria.EQ, timeConstant);
CompoundCriteria compCrit = new CompoundCriteria(CompoundCriteria.AND, dateCompare, timeCompare);
return compCrit;
} catch (FunctionExecutionException e) {
return criteria;
}
}
use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.
the class QueryRewriter method rewriteExpressionDirect.
private Expression rewriteExpressionDirect(Expression expression) throws TeiidComponentException, TeiidProcessingException {
if (expression instanceof Constant) {
return expression;
}
if (expression instanceof ElementSymbol) {
ElementSymbol es = (ElementSymbol) expression;
Class<?> type = es.getType();
if (!processing && es.isExternalReference()) {
if (variables == null) {
return new Reference(es);
}
Expression value = variables.get(es);
if (value == null) {
if (es.getGroupSymbol().getSchema() == null) {
String grpName = es.getGroupSymbol().getName();
if (grpName.equals(ProcedureReservedWords.CHANGING)) {
// $NON-NLS-1$
Assertion.failed("Changing value should not be null");
}
}
} else if (value instanceof Constant) {
if (value.getType() == type) {
return value;
}
try {
return new Constant(FunctionMethods.convert(context, ((Constant) value).getValue(), DataTypeManager.getDataTypeName(type)), es.getType());
} catch (FunctionExecutionException e) {
throw new QueryValidatorException(e);
}
}
return new Reference(es);
}
return expression;
}
boolean isBindEligible = true;
if (expression instanceof AggregateSymbol) {
expression = rewriteExpression((AggregateSymbol) expression);
} else if (expression instanceof Function) {
isBindEligible = !isConstantConvert(expression);
expression = rewriteFunction((Function) expression);
} else if (expression instanceof CaseExpression) {
expression = rewriteCaseExpression((CaseExpression) expression);
} else if (expression instanceof SearchedCaseExpression) {
expression = rewriteCaseExpression((SearchedCaseExpression) expression);
} else if (expression instanceof ScalarSubquery) {
ScalarSubquery subquery = (ScalarSubquery) expression;
if (subquery.shouldEvaluate() && processing) {
return new Constant(evaluator.evaluate(subquery, null), subquery.getType());
}
rewriteSubqueryContainer(subquery, true);
if (!RelationalNodeUtil.shouldExecute(subquery.getCommand(), false, true)) {
return new Constant(null, subquery.getType());
}
if (subquery.getCommand().getProcessorPlan() == null) {
addImplicitLimit(subquery, 2);
}
return expression;
} else if (expression instanceof ExpressionSymbol) {
expression = rewriteExpressionDirect(((ExpressionSymbol) expression).getExpression());
} else if (expression instanceof Criteria) {
expression = rewriteCriteria((Criteria) expression);
} else if (expression instanceof XMLSerialize) {
rewriteExpressions(expression);
XMLSerialize serialize = (XMLSerialize) expression;
if (isNull(serialize.getExpression())) {
return new Constant(null, serialize.getType());
}
if (serialize.getDeclaration() == null && serialize.isDocument()) {
if ((serialize.getVersion() != null && !serialize.getVersion().equals("1.0"))) {
// $NON-NLS-1$
serialize.setDeclaration(true);
} else if (serialize.getEncoding() != null) {
Charset encoding = Charset.forName(serialize.getEncoding());
if (!encoding.equals(Charset.forName("UTF-8")) && !encoding.equals(Charset.forName("UTF-16"))) {
// $NON-NLS-1$ //$NON-NLS-2$
serialize.setDeclaration(true);
}
}
}
} else if (expression instanceof XMLCast) {
XMLCast cast = (XMLCast) expression;
if (cast.getType() == DefaultDataClasses.XML) {
XMLQuery xmlQuery = new XMLQuery();
// $NON-NLS-1$
xmlQuery.setXquery("$i");
// $NON-NLS-1$
xmlQuery.setPassing(Arrays.asList(new DerivedColumn("i", cast.getExpression())));
xmlQuery.compileXqueryExpression();
return xmlQuery;
}
} else {
rewriteExpressions(expression);
}
if (!processing) {
if (!EvaluatableVisitor.isFullyEvaluatable(expression, true)) {
return expression;
}
} else if (!(expression instanceof Reference) && !EvaluatableVisitor.isEvaluatable(expression, EvaluationLevel.PROCESSING)) {
return expression;
}
return evaluate(expression, isBindEligible);
}
use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.
the class XQueryEvaluator method evaluateXMLQuery.
/**
* @param tuple
* @param xmlQuery
* @param exists - check only for the existence of a non-empty result
* @return Boolean if exists is true, otherwise an XMLType value
* @throws BlockedException
* @throws TeiidComponentException
* @throws FunctionExecutionException
*/
public static Object evaluateXMLQuery(List<?> tuple, XMLQuery xmlQuery, boolean exists, Map<String, Object> parameters, CommandContext context) throws BlockedException, TeiidComponentException, FunctionExecutionException {
boolean emptyOnEmpty = xmlQuery.getEmptyOnEmpty() == null || xmlQuery.getEmptyOnEmpty();
Result result = null;
try {
XMLQueryRowProcessor rp = null;
if (xmlQuery.getXQueryExpression().isStreaming()) {
rp = new XMLQueryRowProcessor(exists, context);
}
try {
Object contextItem = null;
if (parameters.containsKey(null)) {
contextItem = parameters.remove(null);
if (contextItem == null) {
return null;
}
}
result = evaluateXQuery(xmlQuery.getXQueryExpression(), contextItem, parameters, rp, context);
if (result == null) {
return null;
}
if (exists) {
if (result.iter.next() == null) {
return false;
}
return true;
}
} catch (TeiidRuntimeException e) {
if (e.getCause() instanceof XPathException) {
throw (XPathException) e.getCause();
}
throw e;
}
if (rp != null) {
if (exists) {
return rp.hasItem;
}
XMLType.Type type = rp.type;
if (type == null) {
if (!emptyOnEmpty) {
return null;
}
type = Type.CONTENT;
}
XMLType val = rp.concat.close(context);
val.setType(rp.type);
return val;
}
return xmlQuery.getXQueryExpression().createXMLType(result.iter, context.getBufferManager(), emptyOnEmpty, context);
} catch (TeiidProcessingException e) {
throw new FunctionExecutionException(QueryPlugin.Event.TEIID30333, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30333, e.getMessage()));
} catch (XPathException e) {
throw new FunctionExecutionException(QueryPlugin.Event.TEIID30333, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30333, e.getMessage()));
} finally {
if (result != null) {
result.close();
}
}
}
Aggregations