Search in sources :

Example 21 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class TextTableNode method process.

private void process() throws TeiidProcessingException {
    while (true) {
        synchronized (this) {
            if (isBatchFull()) {
                return;
            }
            StringBuilder line = readLine(lineWidth, table.isFixedWidth());
            if (line == null) {
                terminateBatches();
                break;
            }
            String parentSelector = null;
            if (table.getSelector() != null) {
                if (line.length() < table.getSelector().length()) {
                    continue;
                }
                if (!line.substring(0, table.getSelector().length()).equals(table.getSelector())) {
                    if (parentLines == null) {
                        // doesn't match any selector
                        continue;
                    }
                    parentSelector = line.substring(0, table.getSelector().length());
                    if (!parentLines.containsKey(parentSelector)) {
                        // doesn't match any selector
                        continue;
                    }
                }
            }
            List<String> vals = parseLine(line);
            if (parentSelector != null) {
                this.parentLines.put(parentSelector, vals);
                continue;
            } else if (table.getSelector() != null && !table.getSelector().equals(vals.get(0))) {
                continue;
            }
            rowNumber++;
            List<Object> tuple = new ArrayList<Object>(projectionIndexes.length);
            for (int output : projectionIndexes) {
                TextColumn col = table.getColumns().get(output);
                String val = null;
                int index = output;
                if (col.isOrdinal()) {
                    if (rowNumber > Integer.MAX_VALUE) {
                        throw new TeiidRuntimeException(new TeiidProcessingException(QueryPlugin.Event.TEIID31174, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31174)));
                    }
                    tuple.add((int) rowNumber);
                    continue;
                }
                if (col.getSelector() != null) {
                    vals = this.parentLines.get(col.getSelector());
                    index = col.getPosition() - 1;
                } else if (nameIndexes != null) {
                    index = nameIndexes.get(col.getName());
                }
                if (vals == null || index >= vals.size()) {
                    // throw new TeiidProcessingException(QueryPlugin.Util.getString("TextTableNode.no_value", col.getName(), textLine, systemId)); //$NON-NLS-1$
                    tuple.add(null);
                    continue;
                }
                val = vals.get(index);
                try {
                    tuple.add(DataTypeManager.transformValue(val, table.getColumns().get(output).getSymbol().getType()));
                } catch (TransformationException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30176, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30176, col.getName(), textLine, systemId));
                }
            }
            addBatchRow(tuple);
            if (rowNumber == limit) {
                terminateBatches();
                break;
            }
        }
    }
}
Also used : TransformationException(org.teiid.core.types.TransformationException) ArrayList(java.util.ArrayList) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 22 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class TextTableNode method nextBatchDirect.

@Override
protected synchronized TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    if (reader == null) {
        initReader();
    }
    if (reader == null) {
        terminateBatches();
        return pullBatch();
    }
    if (isLastBatch()) {
        return pullBatch();
    }
    if (isBatchFull()) {
        TupleBatch result = pullBatch();
        // read ahead
        processAsynch();
        return result;
    }
    unwrapException(asynchException);
    processAsynch();
    if (this.getContext().getWorkItem() == null) {
        // this is for compatibility with engine tests that are below the level of using the work item
        synchronized (this) {
            while (running) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new TeiidRuntimeException(e);
                }
            }
        }
    }
    // $NON-NLS-1$
    throw BlockedException.block("Blocking on results from file processing.");
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 23 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class XMLTableNode method evaluate.

private void evaluate(final boolean useFinalBuffer) throws TeiidComponentException, ExpressionEvaluationException, BlockedException, TeiidProcessingException {
    if (result != null || this.buffer != null) {
        return;
    }
    setReferenceValues(this.table);
    final HashMap<String, Object> parameters = new HashMap<String, Object>();
    Evaluator eval = getEvaluator(Collections.emptyMap());
    eval.evaluateParameters(this.table.getPassing(), null, parameters);
    final Object contextItem;
    if (parameters.containsKey(null)) {
        contextItem = parameters.remove(null);
        // null context item mean no rows
        if (contextItem == null) {
            result = new Result();
            result.iter = EmptyIterator.emptyIterator();
            streaming = false;
            return;
        }
    } else {
        contextItem = null;
    }
    if (this.table.getXQueryExpression().isStreaming()) {
        if (this.buffer == null) {
            this.buffer = this.getBufferManager().createTupleBuffer(getOutputElements(), getConnectionID(), TupleSourceType.PROCESSOR);
            if (!useFinalBuffer) {
                this.buffer.setForwardOnly(true);
            }
        }
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    XQueryEvaluator.evaluateXQuery(table.getXQueryExpression(), contextItem, parameters, XMLTableNode.this, getContext());
                } catch (TeiidRuntimeException e) {
                    if (e != EARLY_TERMINATION) {
                        asynchException = e;
                    }
                } catch (Throwable e) {
                    asynchException = new TeiidRuntimeException(e);
                } finally {
                    synchronized (XMLTableNode.this) {
                        if (buffer != null && asynchException == null) {
                            try {
                                buffer.close();
                            } catch (TeiidComponentException e) {
                                asynchException = new TeiidRuntimeException(e);
                            }
                        }
                        state = State.DONE;
                        XMLTableNode.this.notifyAll();
                    }
                }
            }
        };
        this.getContext().getExecutor().execute(r);
        return;
    }
    try {
        result = XQueryEvaluator.evaluateXQuery(this.table.getXQueryExpression(), contextItem, parameters, null, this.getContext());
    } catch (TeiidRuntimeException e) {
        unwrapException(e);
    }
}
Also used : HashMap(java.util.HashMap) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidComponentException(org.teiid.core.TeiidComponentException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) XQueryEvaluator(org.teiid.query.xquery.saxon.XQueryEvaluator) Evaluator(org.teiid.query.eval.Evaluator) ConversionResult(net.sf.saxon.type.ConversionResult) Result(org.teiid.query.xquery.saxon.SaxonXQueryExpression.Result)

Example 24 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException 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)

Example 25 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class IntegrationTestRestWebserviceGeneration method httpCall.

private String httpCall(String url, String method, String params) throws Exception {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestMethod(method);
    connection.setDoOutput(true);
    if (method.equalsIgnoreCase("post")) {
        OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
        wr.write(params);
        wr.flush();
    }
    int code = connection.getResponseCode();
    if (code >= 400) {
        throw new TeiidRuntimeException(String.valueOf(code));
    }
    return ObjectConverterUtil.convertToString(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) OutputStreamWriter(java.io.OutputStreamWriter) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) URL(java.net.URL)

Aggregations

TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)103 IOException (java.io.IOException)27 TeiidComponentException (org.teiid.core.TeiidComponentException)22 TeiidException (org.teiid.core.TeiidException)22 ArrayList (java.util.ArrayList)20 TeiidProcessingException (org.teiid.core.TeiidProcessingException)17 SQLException (java.sql.SQLException)11 ObjectInputStream (java.io.ObjectInputStream)9 HashMap (java.util.HashMap)9 InputStream (java.io.InputStream)7 Map (java.util.Map)7 Test (org.junit.Test)7 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)7 ObjectOutputStream (java.io.ObjectOutputStream)6 List (java.util.List)6 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)6 XMLStreamException (javax.xml.stream.XMLStreamException)5 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 JsonObject (com.couchbase.client.java.document.json.JsonObject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4