use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class ArrayTableNode method nextBatchDirect.
@Override
protected TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
Object array = getEvaluator(Collections.emptyMap()).evaluate(table.getArrayValue(), null);
if (array != null) {
ArrayList<Object> tuple = new ArrayList<Object>(projectionIndexes.length);
for (int output : projectionIndexes) {
ProjectedColumn col = table.getColumns().get(output);
try {
Object val = FunctionMethods.array_get(array, output + 1);
tuple.add(DataTypeManager.transformValue(val, table.getColumns().get(output).getSymbol().getType()));
} catch (TransformationException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30190, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30190, col.getName()));
} catch (SQLException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30188, e);
}
}
addBatchRow(tuple);
}
terminateBatches();
return pullBatch();
}
use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class BatchedUpdatePlan method nextBatch.
/**
* @see org.teiid.query.processor.ProcessorPlan#nextBatch()
* @since 4.2
*/
public TupleBatch nextBatch() throws BlockedException, TeiidComponentException, TeiidProcessingException {
for (; planIndex < updatePlans.length && (getContext() == null || getContext().getBatchUpdateException() == null); ) {
try {
if (!planOpened[planIndex]) {
// Open the plan only once
/* Defect 16166
* Some commands in a batch may depend on updates by previous commands in the same batch. A call
* to open() usually submits an atomic command, so calling open() on all the child plans at the same time
* will mean that the datasource may not be in the state expected by a later command within the batch. So,
* for a batch of commands, we only open() a later plan when we are finished with the previous plan to
* guarantee that the commands in the previous plan are completed before the commands in any subsequent
* plans are executed.
*/
openPlan();
} else if (this.planContexts[planIndex] != null) {
this.getContext().getTransactionServer().resume(this.planContexts[planIndex]);
}
// Execute nextBatch() on each plan in sequence
TupleBatch nextBatch = null;
do {
// Can throw BlockedException
nextBatch = updatePlans[planIndex].nextBatch();
List<List<?>> currentBatch = nextBatch.getTuples();
for (int i = 0; i < currentBatch.size(); i++, commandIndex++) {
updateCounts[commandIndex] = currentBatch.get(i);
}
} while (!nextBatch.getTerminationFlag());
// since we are done with the plan explicitly close it.
updatePlans[planIndex].close();
if (this.planContexts[planIndex] != null) {
TransactionService ts = this.getContext().getTransactionServer();
ts.commit(this.planContexts[planIndex]);
this.planContexts[planIndex] = null;
}
planIndex++;
} catch (BlockedException e) {
throw e;
} catch (TeiidComponentException | TeiidProcessingException e) {
if (singleResult) {
throw e;
}
Throwable cause = e;
if (e.getCause() instanceof TranslatorBatchException) {
TranslatorBatchException tbe = (TranslatorBatchException) e.getCause();
for (int i = 0; i < tbe.getUpdateCounts().length; i++) {
updateCounts[commandIndex++] = Arrays.asList(tbe.getUpdateCounts()[i]);
}
}
updateCounts = Arrays.copyOf(updateCounts, commandIndex);
getContext().setBatchUpdateException(cause);
} finally {
if (planIndex < updatePlans.length && this.planContexts[planIndex] != null) {
this.getContext().getTransactionServer().suspend(this.planContexts[planIndex]);
}
}
}
if (singleResult) {
long result = 0;
for (int i = 0; i < updateCounts.length; i++) {
int value = (Integer) updateCounts[i].get(0);
if (value == Statement.EXECUTE_FAILED) {
// the batch results rather than throwing an exception
throw new TeiidProcessingException(QueryPlugin.Event.TEIID31199, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31198));
}
if (value > 0) {
result += value;
}
}
TupleBatch batch = new TupleBatch(1, new List<?>[] { Arrays.asList((int) Math.min(Integer.MAX_VALUE, result)) });
batch.setTerminationFlag(true);
return batch;
}
// Add tuples to current batch
TupleBatch batch = new TupleBatch(1, updateCounts);
batch.setTerminationFlag(true);
return batch;
}
use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class TempTableDataManager method loadGlobalTable.
private TupleSource loadGlobalTable(final CommandContext context, final GroupSymbol group, final String tableName, final GlobalTableStore globalStore) throws TeiidComponentException, TeiidProcessingException {
LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30013, tableName));
final QueryMetadataInterface metadata = context.getMetadata();
final List<ElementSymbol> allColumns = ResolverUtil.resolveElementsInGroup(group, metadata);
final TempTable table = globalStore.createMatTable(tableName, group);
table.setUpdatable(false);
return new ProxyTupleSource() {
TupleSource insertTupleSource;
boolean success;
QueryProcessor qp;
boolean closed;
boolean errored;
@Override
protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
long rowCount = -1;
try {
if (insertTupleSource == null) {
String fullName = metadata.getFullName(group.getMetadataID());
String transformation = metadata.getVirtualPlan(group.getMetadataID()).getQuery();
qp = context.getQueryProcessorFactory().createQueryProcessor(transformation, fullName, context);
insertTupleSource = new BatchCollector.BatchProducerTupleSource(qp);
}
table.insert(insertTupleSource, allColumns, false, false, null);
table.getTree().compact();
rowCount = table.getRowCount();
Determinism determinism = qp.getContext().getDeterminismLevel();
context.setDeterminismLevel(determinism);
// TODO: could pre-process indexes to remove overlap
for (Object index : metadata.getIndexesInGroup(group.getMetadataID())) {
List<ElementSymbol> columns = GlobalTableStoreImpl.resolveIndex(metadata, allColumns, index);
table.addIndex(columns, false);
}
for (Object key : metadata.getUniqueKeysInGroup(group.getMetadataID())) {
List<ElementSymbol> columns = GlobalTableStoreImpl.resolveIndex(metadata, allColumns, key);
table.addIndex(columns, true);
}
CacheHint hint = table.getCacheHint();
if (hint != null && table.getPkLength() > 0) {
table.setUpdatable(hint.isUpdatable(false));
}
if (determinism.compareTo(Determinism.VDB_DETERMINISTIC) < 0 && (hint == null || hint.getScope() == null || Scope.VDB.compareTo(hint.getScope()) <= 0)) {
// $NON-NLS-1$
LogManager.logInfo(LogConstants.CTX_DQP, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31143, determinism, tableName));
}
globalStore.loaded(tableName, table);
success = true;
LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30014, tableName, rowCount));
return CollectionTupleSource.createUpdateCountTupleSource((int) Math.min(Integer.MAX_VALUE, rowCount));
} catch (BlockedException e) {
throw e;
} catch (Exception e) {
errored = true;
if (executor == null || !executor.isShutdown()) {
// if we're shutting down, no need to log
LogManager.logError(LogConstants.CTX_MATVIEWS, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30015, tableName));
}
closeSource();
rethrow(e);
throw new AssertionError();
}
}
@Override
public void closeSource() {
if (closed) {
return;
}
if (!errored && !success) {
LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31153, tableName));
}
closed = true;
if (!success) {
globalStore.failedLoad(tableName);
table.remove();
}
if (qp != null) {
qp.closeProcessing();
}
super.closeSource();
}
};
}
use of org.teiid.core.TeiidProcessingException 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();
}
}
}
use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class XQueryEvaluator method evaluateXQuery.
public static SaxonXQueryExpression.Result evaluateXQuery(final SaxonXQueryExpression xquery, Object context, Map<String, Object> parameterValues, final RowProcessor processor, CommandContext commandContext) throws TeiidProcessingException, TeiidComponentException {
DynamicQueryContext dynamicContext = new DynamicQueryContext(xquery.config);
SaxonXQueryExpression.Result result = new SaxonXQueryExpression.Result();
try {
for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
try {
Object value = entry.getValue();
Sequence s = null;
if (value instanceof SQLXML) {
value = XMLSystemFunctions.convertToSource(value);
result.sources.add((Source) value);
Source source = wrapStax((Source) value);
s = xquery.config.buildDocumentTree(source).getRootNode();
} else if (value instanceof java.util.Date) {
s = XQueryEvaluator.convertToAtomicValue(value);
} else if (value instanceof BinaryType) {
s = new HexBinaryValue(((BinaryType) value).getBytesDirect());
}
dynamicContext.setParameter(StructuredQName.fromClarkName(entry.getKey()), s);
} catch (TransformerException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30148, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30148, entry.getKey()));
}
}
if (context != null) {
Source source = XMLSystemFunctions.convertToSource(context);
result.sources.add(source);
source = wrapStax(source);
if (xquery.contextRoot != null) {
// create our own filter as this logic is not provided in the free saxon
AugmentedSource sourceInput = AugmentedSource.makeAugmentedSource(source);
sourceInput.addFilter(new FilterFactory() {
@Override
public ProxyReceiver makeFilter(Receiver arg0) {
return new PathMapFilter(xquery.contextRoot, arg0);
}
});
source = sourceInput;
// use streamable processing instead
if (xquery.streamingPath != null && processor != null) {
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_DQP, "Using stream processing for evaluation of", xquery.xQueryString);
}
// set to non-blocking in case default expression evaluation blocks
boolean isNonBlocking = commandContext.isNonBlocking();
commandContext.setNonBlocking(true);
final StreamingTransform myTransform = new StreamingTransform() {
public Nodes transform(Element elem) {
processor.processRow(XQueryEvaluator.wrap(elem, xquery.config));
return NONE;
}
};
Builder builder = new Builder(new SaxonReader(xquery.config, sourceInput), false, new StreamingPathFilter(xquery.streamingPath, xquery.namespaceMap).createNodeFactory(null, myTransform));
try {
// the builder is hard wired to parse the source, but the api will throw an exception if the stream is null
builder.build(FAKE_IS);
return result;
} catch (ParsingException e) {
if (e.getCause() instanceof TeiidRuntimeException) {
RelationalNode.unwrapException((TeiidRuntimeException) e.getCause());
}
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
} catch (IOException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
} finally {
if (!isNonBlocking) {
commandContext.setNonBlocking(false);
}
}
}
}
TreeInfo doc;
try {
doc = xquery.config.buildDocumentTree(source);
} catch (XPathException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
}
dynamicContext.setContextItem(doc.getRootNode());
}
try {
result.iter = xquery.xQuery.iterator(dynamicContext);
return result;
} catch (TransformerException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30152, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30152));
}
} finally {
if (result.iter == null) {
result.close();
}
}
}
Aggregations