Search in sources :

Example 11 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class GeometryUtils method geometryToGeoJson.

public static ClobType geometryToGeoJson(GeometryType geometry) throws FunctionExecutionException {
    Geometry jtsGeometry = getGeometry(geometry);
    GeoJSONWriter writer = new GeoJSONWriter();
    try {
        GeoJSON geoJson = writer.write(jtsGeometry);
        ClobType result = new ClobType(new ClobImpl(geoJson.toString()));
        result.setType(Type.JSON);
        return result;
    } catch (Exception e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) GeoJSON(org.wololo.geojson.GeoJSON) GeoJSONWriter(org.wololo.jts2geojson.GeoJSONWriter) ClobImpl(org.teiid.core.types.ClobImpl) SQLException(java.sql.SQLException) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException)

Example 12 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class GeometryUtils method geometryToGml.

public static ClobType geometryToGml(CommandContext ctx, GeometryType geometry, boolean withGmlPrefix) throws FunctionExecutionException {
    Geometry jtsGeometry = getGeometry(geometry);
    GMLWriter writer = new GMLWriter();
    if (!withGmlPrefix) {
        if (geometry.getSrid() != SRID_4326) {
            if (geometry.getSrid() == GeometryType.UNKNOWN_SRID) {
                throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31161));
            }
            jtsGeometry = GeometryTransformUtils.transform(ctx, jtsGeometry, SRID_4326);
        }
        writer.setPrefix(null);
    } else if (geometry.getSrid() != GeometryType.UNKNOWN_SRID) {
    // TODO: should include the srsName
    // writer.setSrsName(String.valueOf(geometry.getSrid()));
    }
    String gmlText = writer.write(jtsGeometry);
    return new ClobType(new ClobImpl(gmlText));
}
Also used : ClobType(org.teiid.core.types.ClobType) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) GMLWriter(com.vividsolutions.jts.io.gml2.GMLWriter) ClobImpl(org.teiid.core.types.ClobImpl)

Example 13 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class SystemFunctionMethods method teiid_session_set.

@TeiidFunction(category = FunctionCategoryConstants.SYSTEM, determinism = Determinism.COMMAND_DETERMINISTIC, pushdown = PushDown.CANNOT_PUSHDOWN)
public static Object teiid_session_set(CommandContext context, String key, Object value) throws FunctionExecutionException {
    SessionMetadata session = context.getSession();
    Map<String, Object> variables = session.getSessionVariables();
    if (variables.size() > MAX_VARIABLES && !variables.containsKey(key)) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID31136, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31136, MAX_VARIABLES));
    }
    return context.setSessionVariable(key, value);
}
Also used : FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata)

Example 14 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class SubqueryAwareEvaluator method evaluatePushdown.

/**
 * Implements must pushdown function handling if supported by the source.
 *
 * The basic strategy is to create a dummy subquery to represent the evaluation
 */
@Override
protected Object evaluatePushdown(Function function, List<?> tuple, Object[] values) throws TeiidComponentException, TeiidProcessingException {
    final FunctionDescriptor fd = function.getFunctionDescriptor();
    if (fd.getMethod() == null) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30341, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30341, fd.getFullName()));
    }
    String schema = null;
    if (fd.getMethod().getParent() == null || !fd.getMethod().getParent().isPhysical()) {
        // find a suitable target
        // TODO: do better than a linear search
        VDBMetaData vdb = this.context.getVdb();
        CapabilitiesFinder capabiltiesFinder = this.context.getQueryProcessorFactory().getCapabiltiesFinder();
        for (ModelMetaData mmd : vdb.getModelMetaDatas().values()) {
            if (!mmd.isSource()) {
                continue;
            }
            SourceCapabilities caps = capabiltiesFinder.findCapabilities(mmd.getName());
            if (caps.supportsCapability(Capability.SELECT_WITHOUT_FROM) && caps.supportsFunction(fd.getMethod().getFullName())) {
                schema = mmd.getName();
                break;
            }
        }
        if (schema == null) {
            throw new FunctionExecutionException(QueryPlugin.Event.TEIID30341, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30341, fd.getFullName()));
        }
    } else {
        if (!CapabilitiesUtil.supports(Capability.SELECT_WITHOUT_FROM, fd.getMethod().getParent(), context.getMetadata(), context.getQueryProcessorFactory().getCapabiltiesFinder())) {
            if (elements != null) {
                Integer index = (Integer) elements.get(function);
                if (index != null) {
                    return tuple.get(index.intValue());
                }
            }
            throw new FunctionExecutionException(QueryPlugin.Event.TEIID30341, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30341, fd.getFullName()));
        }
        schema = fd.getSchema();
    }
    ScalarSubquery ss = null;
    if (functionState != null) {
        ss = functionState.get(function);
    }
    Expression[] functionArgs = new Expression[values.length];
    for (int i = 0; i < values.length; i++) {
        functionArgs[i] = new Constant(values[i]);
    }
    if (ss == null) {
        final Query command = new Query();
        Select select = new Select();
        command.setSelect(select);
        Function f = new Function(function.getName(), functionArgs);
        f.setType(function.getType());
        f.setFunctionDescriptor(fd);
        select.addSymbol(f);
        ss = new ScalarSubquery(command);
        SymbolMap correlatedReferences = new SymbolMap();
        Collection<ElementSymbol> elements = ElementCollectorVisitor.getElements(function, true);
        if (!elements.isEmpty()) {
            for (ElementSymbol es : elements) {
                correlatedReferences.addMapping(es, es);
            }
            command.setCorrelatedReferences(correlatedReferences);
        }
        command.setProcessorPlan(new SimpleProcessorPlan(command, schema, fd, Arrays.asList(new Constant(null, fd.getReturnType()))));
    } else {
        ((Function) ((ExpressionSymbol) ss.getCommand().getProjectedSymbols().get(0)).getExpression()).setArgs(functionArgs);
    }
    if (functionState == null) {
        this.functionState = new HashMap<Function, ScalarSubquery>(2);
    }
    functionState.put(function, ss);
    return internalEvaluate(ss, tuple);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) Query(org.teiid.query.sql.lang.Query) Constant(org.teiid.query.sql.symbol.Constant) SymbolMap(org.teiid.query.sql.util.SymbolMap) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Function(org.teiid.query.sql.symbol.Function) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) Expression(org.teiid.query.sql.symbol.Expression) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) Select(org.teiid.query.sql.lang.Select) SourceCapabilities(org.teiid.query.optimizer.capabilities.SourceCapabilities)

Example 15 with FunctionExecutionException

use of org.teiid.api.exception.query.FunctionExecutionException in project teiid by teiid.

the class GroupingNode method initAccumulator.

static AggregateFunction initAccumulator(AggregateSymbol aggSymbol, RelationalNode node, LinkedHashMap<Expression, Integer> expressionIndexes) {
    int[] argIndexes = new int[aggSymbol.getArgs().length];
    AggregateFunction result = null;
    Expression[] args = aggSymbol.getArgs();
    Class<?>[] inputTypes = new Class[args.length];
    for (int j = 0; j < args.length; j++) {
        inputTypes[j] = args[j].getType();
        argIndexes[j] = getIndex(args[j], expressionIndexes);
    }
    Type function = aggSymbol.getAggregateFunction();
    switch(function) {
        case RANK:
        case DENSE_RANK:
            result = new RankingFunction(function);
            break;
        // same as count(*)
        case ROW_NUMBER:
        case COUNT:
            result = new Count();
            break;
        case SUM:
            result = new Sum();
            break;
        case AVG:
            result = new Avg();
            break;
        case MIN:
            result = new Min();
            break;
        case MAX:
            result = new Max();
            break;
        case XMLAGG:
            result = new XMLAgg();
            break;
        case ARRAY_AGG:
            result = new ArrayAgg();
            break;
        case JSONARRAY_AGG:
            result = new JSONArrayAgg();
            break;
        case TEXTAGG:
            result = new TextAgg((TextLine) args[0]);
            break;
        case STRING_AGG:
            result = new StringAgg(aggSymbol.getType() == DataTypeManager.DefaultDataClasses.BLOB);
            break;
        case FIRST_VALUE:
            result = new FirstLastValue(aggSymbol.getType(), true);
            break;
        case LAST_VALUE:
            result = new FirstLastValue(aggSymbol.getType(), false);
            break;
        case LEAD:
        case LAG:
            result = new LeadLagValue();
            break;
        case USER_DEFINED:
            try {
                result = new UserDefined(aggSymbol.getFunctionDescriptor());
            } catch (FunctionExecutionException e) {
                throw new TeiidRuntimeException(e);
            }
            break;
        default:
            result = new StatsFunction(function);
    }
    if (aggSymbol.getOrderBy() != null) {
        int numOrderByItems = aggSymbol.getOrderBy().getOrderByItems().size();
        List<OrderByItem> orderByItems = new ArrayList<OrderByItem>(numOrderByItems);
        List<ElementSymbol> schema = createSortSchema(result, inputTypes);
        argIndexes = Arrays.copyOf(argIndexes, argIndexes.length + numOrderByItems);
        for (ListIterator<OrderByItem> iterator = aggSymbol.getOrderBy().getOrderByItems().listIterator(); iterator.hasNext(); ) {
            OrderByItem item = iterator.next();
            argIndexes[args.length + iterator.previousIndex()] = getIndex(item.getSymbol(), expressionIndexes);
            ElementSymbol element = new ElementSymbol(String.valueOf(iterator.previousIndex()));
            element.setType(item.getSymbol().getType());
            schema.add(element);
            OrderByItem newItem = item.clone();
            newItem.setSymbol(element);
            orderByItems.add(newItem);
        }
        SortingFilter filter = new SortingFilter(result, node.getBufferManager(), node.getConnectionID(), aggSymbol.isDistinct());
        filter.setElements(schema);
        filter.setSortItems(orderByItems);
        result = filter;
    } else if (aggSymbol.isDistinct()) {
        SortingFilter filter = new SortingFilter(result, node.getBufferManager(), node.getConnectionID(), true);
        List<ElementSymbol> elements = createSortSchema(result, inputTypes);
        filter.setElements(elements);
        result = filter;
    }
    result.setArgIndexes(argIndexes);
    if (aggSymbol.getCondition() != null) {
        result.setConditionIndex(getIndex(aggSymbol.getCondition(), expressionIndexes));
    }
    result.initialize(aggSymbol.getType(), inputTypes);
    return result;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) OrderByItem(org.teiid.query.sql.lang.OrderByItem) ArrayList(java.util.ArrayList) List(java.util.List) TextLine(org.teiid.query.sql.symbol.TextLine) Type(org.teiid.query.sql.symbol.AggregateSymbol.Type) Expression(org.teiid.query.sql.symbol.Expression)

Aggregations

FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)31 IOException (java.io.IOException)9 SQLException (java.sql.SQLException)9 ParseException (com.vividsolutions.jts.io.ParseException)5 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 LanguageObject (org.teiid.query.sql.LanguageObject)4 ClobType (org.teiid.core.types.ClobType)3 TransformationException (org.teiid.core.types.TransformationException)3 WKBReader (com.vividsolutions.jts.io.WKBReader)2 WKTReader (com.vividsolutions.jts.io.WKTReader)2 StringReader (java.io.StringReader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathException (net.sf.saxon.trans.XPathException)2 BlockedException (org.teiid.common.buffer.BlockedException)2