use of org.teiid.core.TeiidException in project teiid by teiid.
the class RequestWorkItem method addCancelCode.
private Throwable addCancelCode(Throwable exception) {
String reason = null;
synchronized (this) {
reason = this.cancelReason;
}
if (exception instanceof TeiidException) {
TeiidException te = (TeiidException) exception;
if (SQLStates.QUERY_CANCELED.equals(te.getCode()) && EquivalenceUtil.areEqual(reason, te.getMessage())) {
return exception;
}
}
TeiidProcessingException tpe = new TeiidProcessingException(reason);
tpe.initCause(exception);
tpe.setCode(SQLStates.QUERY_CANCELED);
return tpe;
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class RequestWorkItem method addToCache.
private void addToCache() {
if (!doneProducingBatches || cid == null) {
return;
}
Determinism determinismLevel = processor.getContext().getDeterminismLevel();
CachedResults cr = new CachedResults();
cr.setCommand(originalCommand);
cr.setResults(resultsBuffer, processor.getProcessorPlan());
if (requestMsg.getRowLimit() > 0 && resultsBuffer.getRowCount() == requestMsg.getRowLimit() + (collector.isSaveLastRow() ? 1 : 0)) {
cr.setRowLimit(requestMsg.getRowLimit());
}
if (originalCommand.getCacheHint() != null) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Using cache hint", originalCommand.getCacheHint());
if (originalCommand.getCacheHint().getMinRows() != null && resultsBuffer.getRowCount() <= originalCommand.getCacheHint().getMinRows()) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Not caching result as there are fewer rows than needed", resultsBuffer.getRowCount());
return;
}
resultsBuffer.setPrefersMemory(originalCommand.getCacheHint().isPrefersMemory());
if (originalCommand.getCacheHint().getDeterminism() != null) {
determinismLevel = originalCommand.getCacheHint().getDeterminism();
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", processor.getContext().getDeterminismLevel(), " to ", determinismLevel });
}
// if not updatable, then remove the access info
if (!originalCommand.getCacheHint().isUpdatable(true)) {
cr.getAccessInfo().setSensitiveToMetadataChanges(false);
cr.getAccessInfo().getObjectsAccessed().clear();
}
}
if (determinismLevel.compareTo(Determinism.SESSION_DETERMINISTIC) <= 0) {
LogManager.logInfo(LogConstants.CTX_DQP, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30008, originalCommand));
}
try {
this.resultsBuffer.persistLobs();
} catch (TeiidException e) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_DQP, e, QueryPlugin.Util.getString("failed_to_cache"));
}
dqpCore.getRsCache().put(cid, determinismLevel, cr, originalCommand.getCacheHint() != null ? originalCommand.getCacheHint().getTtl() : null);
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class ConnectorWorkItem method handleBatch.
protected AtomicResultsMessage handleBatch() throws TranslatorException {
Assertion.assertTrue(!this.lastBatch);
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, new Object[] { this.id, "Getting results from connector" });
int batchSize = 0;
List<List<?>> rows = new ResizingArrayList<List<?>>(batchSize / 4);
try {
while (batchSize < this.requestMsg.getFetchSize()) {
List<?> row = this.execution.next();
if (row == null) {
this.lastBatch = true;
break;
}
if (row.size() != this.expectedColumns) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new AssertionError("Inproper results returned. Expected " + this.expectedColumns + " columns, but was " + row.size());
}
try {
try {
if (unmodifiableList) {
row = new ArrayList<Object>(row);
}
row = correctTypes(row);
} catch (UnsupportedOperationException | ArrayStoreException e) {
// the translator should be modifiable, but we should be lax
if (unmodifiableList) {
throw e;
}
unmodifiableList = true;
row = new ArrayList<Object>(row);
row = correctTypes(row);
}
} catch (TeiidException e) {
conversionError = e;
break;
}
if (this.procedureBatchHandler != null) {
row = this.procedureBatchHandler.padRow(row);
}
this.rowCount += 1;
batchSize++;
rows.add(row);
// Check for max result rows exceeded
if (this.requestMsg.getMaxResultRows() > -1 && this.rowCount >= this.requestMsg.getMaxResultRows()) {
if (this.rowCount == this.requestMsg.getMaxResultRows() && !this.requestMsg.isExceptionOnMaxRows()) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, new Object[] { this.id, "Exceeded max, returning", this.requestMsg.getMaxResultRows() });
this.lastBatch = true;
break;
} else if (this.rowCount > this.requestMsg.getMaxResultRows() && this.requestMsg.isExceptionOnMaxRows()) {
// $NON-NLS-1$
String msg = QueryPlugin.Util.getString("ConnectorWorker.MaxResultRowsExceed", this.requestMsg.getMaxResultRows());
throw new TranslatorException(QueryPlugin.Event.TEIID30478, msg);
}
}
}
} catch (DataNotAvailableException e) {
if (rows.size() == 0) {
throw e;
}
if (e.getWaitUntil() != null) {
// we have an await until that we need to enforce
this.dnae = e;
}
// else we can just ignore the delay
}
if (lastBatch) {
if (this.procedureBatchHandler != null) {
List<?> row = this.procedureBatchHandler.getParameterRow();
if (row != null) {
try {
row = correctTypes(row);
rows.add(row);
this.rowCount += 1;
} catch (TeiidException e) {
lastBatch = false;
conversionError = e;
}
}
}
// $NON-NLS-1$\
LogManager.logDetail(LogConstants.CTX_CONNECTOR, new Object[] { this.id, "Obtained last batch, total row count:", rowCount });
} else {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, new Object[] { this.id, "Obtained results from connector, current row count:", rowCount });
}
int currentRowCount = rows.size();
if (!lastBatch && currentRowCount == 0) {
// Defect 13366 - Should send all batches, even if they're zero size.
// Log warning if received a zero-size non-last batch from the connector.
LogManager.logWarning(LogConstants.CTX_CONNECTOR, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30004, requestMsg.getConnectorName()));
}
AtomicResultsMessage response = createResultsMessage(rows.toArray(new List[currentRowCount]));
// if we need to keep the execution alive, then we can not support implicit close.
response.setSupportsImplicitClose(!this.securityContext.keepExecutionAlive() && !explicitClose);
response.setWarnings(this.securityContext.getWarnings());
if (this.securityContext.getCacheDirective() != null) {
response.setScope(this.securityContext.getCacheDirective().getScope());
}
if (this.securityContext.getScope() != null && (response.getScope() == null || response.getScope().compareTo(this.securityContext.getScope()) > 0)) {
response.setScope(this.securityContext.getScope());
}
if (lastBatch) {
response.setFinalRow(rowCount);
}
return response;
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class ConnectorWorkItem method correctTypes.
private List<?> correctTypes(List row) throws TeiidException {
// TODO: add a proper intermediate schema
for (int i = 0; i < row.size(); i++) {
try {
Object value = row.get(i);
if (value == null) {
continue;
}
if (convertToRuntimeType[i]) {
Object result = convertToRuntimeType(requestMsg.getBufferManager(), value, this.schema[i], this.requestMsg.getCommandContext());
if (value == result && !DataTypeManager.DefaultDataClasses.OBJECT.equals(this.schema[i])) {
convertToRuntimeType[i] = false;
} else {
if (!explicitClose && isLob[i] && !copyLobs && !areLobsUsableAfterClose && DataTypeManager.isLOB(result.getClass()) && DataTypeManager.isLOB(DataTypeManager.convertToRuntimeType(value, false).getClass())) {
explicitClose = true;
}
row.set(i, result);
value = result;
}
}
if (convertToDesiredRuntimeType[i]) {
if (value != null) {
Object result = DataTypeManager.transformValue(value, value.getClass(), this.schema[i]);
if (isLob[i] && copyLobs) {
if (lobStore == null) {
// $NON-NLS-1$
lobStore = requestMsg.getBufferManager().createFileStore("lobs");
lobBuffer = new byte[1 << 14];
}
if (copyStreamingLobs) {
// if we are free, then we're either streaming or invalid
if (InputStreamFactory.getStorageMode((Streamable<?>) result) == StorageMode.FREE) {
try {
requestMsg.getBufferManager().persistLob((Streamable<?>) result, lobStore, lobBuffer);
explicitClose = true;
} catch (TeiidComponentException e) {
}
}
} else {
requestMsg.getBufferManager().persistLob((Streamable<?>) result, lobStore, lobBuffer);
}
} else if (value == result) {
convertToDesiredRuntimeType[i] = false;
continue;
}
row.set(i, result);
}
} else if (DataTypeManager.isValueCacheEnabled()) {
row.set(i, DataTypeManager.getCanonicalValue(value));
}
} catch (TeiidComponentException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID31176, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31176, this.requestMsg.getCommand().getProjectedSymbols().get(i), DataTypeManager.getDataTypeName(this.schema[i])));
} catch (TransformationException e) {
throw new TeiidException(QueryPlugin.Event.TEIID31176, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31176, this.requestMsg.getCommand().getProjectedSymbols().get(i), DataTypeManager.getDataTypeName(this.schema[i])));
}
}
return row;
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class RuleCollapseSource method createQuery.
private QueryCommand createQuery(CommandContext context, CapabilitiesFinder capFinder, PlanNode accessRoot, PlanNode node) throws QueryMetadataException, TeiidComponentException, QueryPlannerException {
QueryMetadataInterface metadata = context.getMetadata();
PlanNode setOpNode = NodeEditor.findNodePreOrder(node, NodeConstants.Types.SET_OP, NodeConstants.Types.SOURCE);
Object modelID = RuleRaiseAccess.getModelIDFromAccess(accessRoot, metadata);
if (setOpNode != null) {
Operation setOp = (Operation) setOpNode.getProperty(NodeConstants.Info.SET_OPERATION);
SetQuery unionCommand = new SetQuery(setOp);
boolean unionAll = ((Boolean) setOpNode.getProperty(NodeConstants.Info.USE_ALL)).booleanValue();
unionCommand.setAll(unionAll);
int count = 0;
OrderBy orderBy = null;
PlanNode sort = NodeEditor.findNodePreOrder(node, NodeConstants.Types.SORT, NodeConstants.Types.SET_OP);
if (sort != null) {
processOrderBy(sort, unionCommand, modelID, context, capFinder);
orderBy = unionCommand.getOrderBy();
unionCommand.setOrderBy(null);
// we have to remap if the primary projection is from a grouping
PlanNode groupNode = NodeEditor.findNodePreOrder(setOpNode.getFirstChild(), NodeConstants.Types.GROUP, NodeConstants.Types.SOURCE);
if (groupNode != null) {
SymbolMap symbolMap = (SymbolMap) groupNode.getProperty(NodeConstants.Info.SYMBOL_MAP);
ExpressionMappingVisitor.mapExpressions(orderBy, symbolMap.asMap(), true);
}
}
for (PlanNode child : setOpNode.getChildren()) {
QueryCommand command = createQuery(context, capFinder, accessRoot, child);
if (count == 0) {
unionCommand.setLeftQuery(command);
} else if (count == 1) {
unionCommand.setRightQuery(command);
} else {
unionCommand = new SetQuery(setOp, unionAll, unionCommand, command);
}
count++;
}
PlanNode limit = NodeEditor.findNodePreOrder(node, NodeConstants.Types.TUPLE_LIMIT, NodeConstants.Types.SET_OP);
if (limit != null) {
processLimit(limit, unionCommand, metadata);
}
unionCommand.setOrderBy(orderBy);
return unionCommand;
}
Query query = new Query();
Select select = new Select();
List<Expression> columns = (List<Expression>) node.getProperty(NodeConstants.Info.OUTPUT_COLS);
prepareSubqueries(ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(columns));
select.addSymbols(columns);
query.setSelect(select);
query.setFrom(new From());
buildQuery(accessRoot, node, query, context, capFinder);
if (!CapabilitiesUtil.useAnsiJoin(modelID, metadata, capFinder)) {
simplifyFromClause(query);
}
if (query.getCriteria() instanceof CompoundCriteria) {
query.setCriteria(QueryRewriter.optimizeCriteria((CompoundCriteria) query.getCriteria(), metadata));
}
if (columns.isEmpty()) {
if (CapabilitiesUtil.supports(Capability.QUERY_SELECT_EXPRESSION, modelID, metadata, capFinder)) {
// $NON-NLS-1$
select.addSymbol(new ExpressionSymbol("dummy", new Constant(1)));
} else {
// TODO: need to ensure the type is consistent
// - should be rare as the source would typically support select expression if it supports union
select.addSymbol(selectOutputElement(query.getFrom().getGroups(), metadata));
}
}
PlanNode groupNode = NodeEditor.findNodePreOrder(node, NodeConstants.Types.GROUP, NodeConstants.Types.SOURCE);
if (groupNode != null) {
if (query.getOrderBy() != null) {
query.setOrderBy(query.getOrderBy().clone());
}
if (query.getHaving() != null) {
query.setHaving((Criteria) query.getHaving().clone());
}
query.setSelect(query.getSelect().clone());
SymbolMap symbolMap = (SymbolMap) groupNode.getProperty(NodeConstants.Info.SYMBOL_MAP);
// map back to expression form
ExpressionMappingVisitor.mapExpressions(query.getOrderBy(), symbolMap.asMap(), true);
ExpressionMappingVisitor.mapExpressions(query.getSelect(), symbolMap.asMap(), true);
ExpressionMappingVisitor.mapExpressions(query.getHaving(), symbolMap.asMap(), true);
if (query.getHaving() != null && !CapabilitiesUtil.supports(Capability.QUERY_HAVING, modelID, metadata, capFinder)) {
Select sel = query.getSelect();
GroupBy groupBy = query.getGroupBy();
Criteria having = query.getHaving();
query.setHaving(null);
OrderBy orderBy = query.getOrderBy();
query.setOrderBy(null);
Limit limit = query.getLimit();
query.setLimit(null);
Set<AggregateSymbol> aggs = new HashSet<AggregateSymbol>();
aggs.addAll(AggregateSymbolCollectorVisitor.getAggregates(having, true));
Set<Expression> expr = new HashSet<Expression>();
for (Expression ex : sel.getProjectedSymbols()) {
Expression selectExpression = SymbolMap.getExpression(ex);
aggs.remove(selectExpression);
expr.add(selectExpression);
}
int originalSelect = sel.getSymbols().size();
sel.addSymbols(aggs);
if (groupBy != null) {
for (Expression ex : groupBy.getSymbols()) {
ex = SymbolMap.getExpression(ex);
if (expr.add(ex)) {
sel.addSymbol(ex);
}
}
}
Query outerQuery = null;
try {
// $NON-NLS-1$
outerQuery = QueryRewriter.createInlineViewQuery(new GroupSymbol("X"), query, metadata, query.getSelect().getProjectedSymbols());
} catch (TeiidException err) {
throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30257, err);
}
Iterator<Expression> iter = outerQuery.getSelect().getProjectedSymbols().iterator();
HashMap<Expression, Expression> expressionMap = new HashMap<Expression, Expression>();
for (Expression symbol : query.getSelect().getProjectedSymbols()) {
// need to unwrap on both sides as the select expression could be aliased
// TODO: could add an option to createInlineViewQuery to disable alias creation
expressionMap.put(SymbolMap.getExpression(symbol), SymbolMap.getExpression(iter.next()));
}
ExpressionMappingVisitor.mapExpressions(having, expressionMap, true);
outerQuery.setCriteria(having);
ExpressionMappingVisitor.mapExpressions(orderBy, expressionMap, true);
outerQuery.setOrderBy(orderBy);
outerQuery.setLimit(limit);
ExpressionMappingVisitor.mapExpressions(select, expressionMap, true);
outerQuery.getSelect().setSymbols(outerQuery.getSelect().getProjectedSymbols().subList(0, originalSelect));
outerQuery.setOption(query.getOption());
query = outerQuery;
}
if (query.getGroupBy() != null) {
// we check for group by expressions here to create an ANSI SQL plan
boolean hasExpression = false;
boolean hasLiteral = false;
for (final Iterator<Expression> iterator = query.getGroupBy().getSymbols().iterator(); iterator.hasNext(); ) {
Expression ex = iterator.next();
hasExpression |= !(ex instanceof ElementSymbol);
hasLiteral |= EvaluatableVisitor.willBecomeConstant(ex, true);
}
if ((hasExpression && !CapabilitiesUtil.supports(Capability.QUERY_FUNCTIONS_IN_GROUP_BY, modelID, metadata, capFinder)) || hasLiteral) {
// if group by expressions are not support, add an inline view to compensate
query = RuleCollapseSource.rewriteGroupByAsView(query, metadata, false);
if (query.getHaving() != null) {
// dependent sets will have been added a having
List<Criteria> crits = Criteria.separateCriteriaByAnd(query.getHaving());
for (Iterator<Criteria> iter = crits.iterator(); iter.hasNext(); ) {
Criteria crit = iter.next();
if (crit instanceof DependentSetCriteria) {
query.setCriteria(Criteria.combineCriteria(query.getCriteria(), crit));
iter.remove();
}
}
query.setHaving(Criteria.combineCriteria(crits));
}
}
if (query.getOrderBy() != null && groupNode.hasBooleanProperty(Info.ROLLUP) && !CapabilitiesUtil.supports(Capability.QUERY_ORDERBY_EXTENDED_GROUPING, modelID, metadata, capFinder)) {
// if ordering is not directly supported over extended grouping, add an inline view to compensate
query = RuleCollapseSource.rewriteGroupByAsView(query, metadata, true);
}
}
}
return query;
}
Aggregations