use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class DataTierManagerImpl method processSystemQuery.
/**
* @param command
* @param workItem
* @return
* @throws TeiidComponentException
* @throws TeiidProcessingException
*/
private TupleSource processSystemQuery(CommandContext context, Command command, DQPWorkContext workContext) throws TeiidComponentException, TeiidProcessingException {
String vdbName = workContext.getVdbName();
String vdbVersion = workContext.getVdbVersion();
final VDBMetaData vdb = workContext.getVDB();
TransformationMetadata indexMetadata = vdb.getAttachment(TransformationMetadata.class);
CompositeMetadataStore metadata = indexMetadata.getMetadataStore();
if (command instanceof Query) {
Query query = (Query) command;
UnaryFromClause ufc = (UnaryFromClause) query.getFrom().getClauses().get(0);
GroupSymbol group = ufc.getGroup();
if (StringUtil.startsWithIgnoreCase(group.getNonCorrelationName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
final SystemAdminTables sysTable = SystemAdminTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
BaseExtractionTable<?> et = systemAdminTables.get(sysTable);
return et.processQuery(query, vdb, indexMetadata, context);
}
final SystemTables sysTable = SystemTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
BaseExtractionTable<?> et = systemTables.get(sysTable);
return et.processQuery(query, vdb, indexMetadata, context);
}
Collection<List<?>> rows = new ArrayList<List<?>>();
StoredProcedure proc = (StoredProcedure) command;
if (StringUtil.startsWithIgnoreCase(proc.getProcedureCallableName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
final SystemAdminProcs sysProc = SystemAdminProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
switch(sysProc) {
case LOGMSG:
case ISLOGGABLE:
String level = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
String logContext = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
Object message = null;
if (sysProc == SystemAdminProcs.LOGMSG) {
message = ((Constant) proc.getParameter(4).getExpression()).getValue();
}
int msgLevel = getLevel(level);
boolean logged = false;
if (LogManager.isMessageToBeRecorded(logContext, msgLevel)) {
if (message != null) {
LogManager.log(msgLevel, logContext, message);
}
logged = true;
}
if (proc.returnParameters()) {
rows.add(Arrays.asList(logged));
}
return new CollectionTupleSource(rows.iterator());
case SETPROPERTY:
try {
String uuid = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
String key = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
Clob value = (Clob) ((Constant) proc.getParameter(4).getExpression()).getValue();
key = MetadataFactory.resolvePropertyKey(null, key);
String strVal = null;
String result = null;
if (value != null) {
if (value.length() > MAX_VALUE_LENGTH) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30548, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30548, MAX_VALUE_LENGTH));
}
strVal = ObjectConverterUtil.convertToString(value.getCharacterStream());
}
final AbstractMetadataRecord target = getByUuid(metadata, uuid);
if (target == null) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30549, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30549, uuid));
}
AbstractMetadataRecord schema = target;
while (!(schema instanceof Schema) && schema.getParent() != null) {
schema = schema.getParent();
}
if (schema instanceof Schema && vdb.getImportedModels().contains(((Schema) schema).getName())) {
// $NON-NLS-1$
throw new TeiidProcessingException(QueryPlugin.Event.TEIID31098, QueryPlugin.Util.getString("ValidationVisitor.invalid_alter", uuid));
}
if (getMetadataRepository(target, vdb) != null) {
getMetadataRepository(target, vdb).setProperty(vdbName, vdbVersion, target, key, strVal);
}
result = DdlPlan.setProperty(vdb, target, key, strVal);
if (eventDistributor != null) {
eventDistributor.setProperty(vdbName, vdbVersion, uuid, key, strVal);
}
// materialization depends upon the property values
// $NON-NLS-1$
indexMetadata.addToMetadataCache(target, "transformation/matview", null);
if (proc.returnParameters()) {
if (result == null) {
rows.add(Arrays.asList((Clob) null));
} else {
rows.add(Arrays.asList(new ClobType(new ClobImpl(result))));
}
}
return new CollectionTupleSource(rows.iterator());
} catch (SQLException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30550, e);
} catch (IOException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30551, e);
}
}
final Table table = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
switch(sysProc) {
case SETCOLUMNSTATS:
final String columnName = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
Column c = null;
for (Column col : table.getColumns()) {
if (col.getName().equalsIgnoreCase(columnName)) {
c = col;
break;
}
}
if (c == null) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30552, columnName + TransformationMetadata.NOT_EXISTS_MESSAGE);
}
Number distinctVals = (Number) ((Constant) proc.getParameter(3).getExpression()).getValue();
Number nullVals = (Number) ((Constant) proc.getParameter(4).getExpression()).getValue();
String max = (String) ((Constant) proc.getParameter(5).getExpression()).getValue();
String min = (String) ((Constant) proc.getParameter(6).getExpression()).getValue();
final ColumnStats columnStats = new ColumnStats();
columnStats.setDistinctValues(distinctVals);
columnStats.setNullValues(nullVals);
columnStats.setMaximumValue(max);
columnStats.setMinimumValue(min);
if (getMetadataRepository(table, vdb) != null) {
getMetadataRepository(table, vdb).setColumnStats(vdbName, vdbVersion, c, columnStats);
}
DdlPlan.setColumnStats(vdb, c, columnStats);
if (eventDistributor != null) {
eventDistributor.setColumnStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), columnName, columnStats);
}
break;
case SETTABLESTATS:
Constant val = (Constant) proc.getParameter(2).getExpression();
final Number cardinality = (Number) val.getValue();
TableStats tableStats = new TableStats();
tableStats.setCardinality(cardinality);
if (getMetadataRepository(table, vdb) != null) {
getMetadataRepository(table, vdb).setTableStats(vdbName, vdbVersion, table, tableStats);
}
DdlPlan.setTableStats(vdb, table, tableStats);
if (eventDistributor != null) {
eventDistributor.setTableStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), tableStats);
}
break;
}
return new CollectionTupleSource(rows.iterator());
}
final SystemProcs sysTable = SystemProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
switch(sysTable) {
case GETXMLSCHEMAS:
try {
Object groupID = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
List<SQLXMLImpl> schemas = indexMetadata.getXMLSchemas(groupID);
for (SQLXMLImpl schema : schemas) {
rows.add(Arrays.asList(new XMLType(schema)));
}
} catch (QueryMetadataException e) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30553, e);
}
break;
case ARRAYITERATE:
Object array = ((Constant) proc.getParameter(1).getExpression()).getValue();
if (array != null) {
final Object[] vals;
if (array instanceof Object[]) {
vals = (Object[]) array;
} else {
ArrayImpl arrayImpl = (ArrayImpl) array;
vals = arrayImpl.getValues();
}
return new CollectionTupleSource(new Iterator<List<?>>() {
int index = 0;
@Override
public boolean hasNext() {
return index < vals.length;
}
@Override
public List<?> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return Arrays.asList(vals[index++]);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
});
}
}
return new CollectionTupleSource(rows.iterator());
}
use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class LobWorkItem method run.
public void run() {
LobChunk chunk = null;
Exception ex = null;
boolean shouldClose = false;
try {
// save for future
if (stream == null) {
stream = createLobStream(streamId);
}
// now get the chunk from stream
chunk = stream.getNextChunk();
parent.dataBytes.addAndGet(chunk.getBytes().length);
shouldClose = chunk.isLast();
} catch (TeiidComponentException e) {
LogManager.logWarning(org.teiid.logging.LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30027));
ex = e;
} catch (IOException | SQLException e) {
// treat this as a processing exception
ex = new TeiidProcessingException(e);
}
synchronized (this) {
if (ex != null) {
resultsReceiver.exceptionOccurred(ex);
shouldClose = true;
} else {
resultsReceiver.receiveResults(chunk);
}
resultsReceiver = null;
}
if (shouldClose) {
close();
}
}
use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class ConnectorWorkItem method convertToRuntimeType.
static Object convertToRuntimeType(BufferManager bm, Object value, Class<?> desiredType, CommandContext context) throws TransformationException {
if (desiredType != DataTypeManager.DefaultDataClasses.XML || !(value instanceof Source)) {
if (value instanceof DataSource) {
final DataSource ds = (DataSource) value;
try {
// Teiid uses the datasource interface in a degenerate way that
// reuses the stream, so we test for that here
InputStream initial = ds.getInputStream();
InputStream other = null;
try {
other = ds.getInputStream();
} catch (IOException e) {
// likely streaming
}
if (other != null && initial != other) {
initial.close();
other.close();
if (value instanceof InputStreamFactory) {
return asLob((InputStreamFactory) value, desiredType);
}
return asLob(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ds.getInputStream();
}
}, desiredType);
}
// $NON-NLS-1$
FileStore fs = bm.createFileStore("bytes");
// TODO: guess at the encoding from the content type
FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
SaveOnReadInputStream is = new SaveOnReadInputStream(initial, fsisf);
if (context != null) {
context.addCreatedLob(fsisf);
}
return asLob(is.getInputStreamFactory(), desiredType);
} catch (IOException e) {
throw new TransformationException(QueryPlugin.Event.TEIID30500, e, e.getMessage());
}
}
if (value instanceof InputStreamFactory) {
return asLob((InputStreamFactory) value, desiredType);
}
if (value instanceof GeometryInputSource) {
GeometryInputSource gis = (GeometryInputSource) value;
try {
InputStream is = gis.getEwkb();
if (is != null) {
return GeometryUtils.geometryFromEwkb(is, gis.getSrid());
}
} catch (Exception e) {
throw new TransformationException(e);
}
try {
Reader r = gis.getGml();
if (r != null) {
return GeometryUtils.geometryFromGml(r, gis.getSrid());
}
} catch (Exception e) {
throw new TransformationException(e);
}
}
}
if (value instanceof Source) {
if (!(value instanceof InputStreamFactory)) {
if (value instanceof StreamSource) {
StreamSource ss = (StreamSource) value;
InputStream is = ss.getInputStream();
Reader r = ss.getReader();
if (is == null && r != null) {
is = new ReaderInputStream(r, Streamable.CHARSET);
}
// $NON-NLS-1$
final FileStore fs = bm.createFileStore("xml");
final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
value = new SaveOnReadInputStream(is, fsisf).getInputStreamFactory();
if (context != null) {
context.addCreatedLob(fsisf);
}
} else if (value instanceof StAXSource) {
// TODO: do this lazily. if the first access to get the STaXSource, then
// it's more efficient to let the processing happen against STaX
StAXSource ss = (StAXSource) value;
try {
// $NON-NLS-1$
final FileStore fs = bm.createFileStore("xml");
final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
value = new SaveOnReadInputStream(new XMLInputStream(ss, XMLSystemFunctions.getOutputFactory(true)), fsisf).getInputStreamFactory();
if (context != null) {
context.addCreatedLob(fsisf);
}
} catch (XMLStreamException e) {
throw new TransformationException(e);
}
} else {
// maybe dom or some other source we want to get out of memory
StandardXMLTranslator sxt = new StandardXMLTranslator((Source) value);
SQLXMLImpl sqlxml;
try {
sqlxml = XMLSystemFunctions.saveToBufferManager(bm, sxt, context);
} catch (TeiidComponentException e) {
throw new TransformationException(e);
} catch (TeiidProcessingException e) {
throw new TransformationException(e);
}
return new XMLType(sqlxml);
}
}
return new XMLType(new SQLXMLImpl((InputStreamFactory) value));
}
return DataTypeManager.convertToRuntimeType(value, desiredType != DataTypeManager.DefaultDataClasses.OBJECT);
}
use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class DQPCore method start.
public void start(DQPConfiguration theConfig) {
this.config = theConfig;
this.authorizationValidator = config.getAuthorizationValidator();
this.chunkSize = config.getLobChunkSizeInKB() * 1024;
this.processWorkerPool = config.getTeiidExecutor();
// we don't want cancellations waiting on normal processing, so they get a small dedicated pool
// TODO: overflow to the worker pool
// $NON-NLS-1$
timeoutExecutor = ExecutorUtils.newFixedThreadPool(3, "Server Side Timeout");
this.cancellationTimer = new EnhancedTimer(timeoutExecutor, timeoutExecutor);
this.maxActivePlans = config.getMaxActivePlans();
if (this.maxActivePlans > config.getMaxThreads()) {
LogManager.logWarning(LogConstants.CTX_DQP, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30006, this.maxActivePlans, config.getMaxThreads()));
this.maxActivePlans = config.getMaxThreads();
}
// for now options are scoped to the engine - vdb scoping is a todo
options = new Options();
options.setAssumeMatchingCollation(false);
options.setProperties(config.getProperties());
// $NON-NLS-1$
PropertiesUtils.setBeanProperties(options, options.getProperties(), "org.teiid", true);
this.bufferManager.setOptions(options);
// hack to set the max active plans
this.bufferManager.setMaxActivePlans(this.maxActivePlans);
try {
this.bufferManager.initialize();
} catch (TeiidComponentException e) {
throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30496, e);
}
this.userRequestSourceConcurrency = config.getUserRequestSourceConcurrency();
if (this.userRequestSourceConcurrency < 1) {
this.userRequestSourceConcurrency = Math.min(config.getMaxThreads(), 2 * config.getMaxThreads() / this.maxActivePlans);
}
DataTierManagerImpl processorDataManager = new DataTierManagerImpl(this, this.bufferManager, this.config.isDetectingChangeEvents());
processorDataManager.setEventDistributor(eventDistributor);
dataTierMgr = new TempTableDataManager(processorDataManager, this.bufferManager, this.rsCache);
dataTierMgr.setExecutor(new TempTableDataManager.RequestExecutor() {
@Override
public void execute(String command, List<?> parameters) {
final String sessionId = DQPWorkContext.getWorkContext().getSessionId();
RequestMessage request = new RequestMessage(command);
request.setParameterValues(parameters);
request.setStatementType(StatementType.PREPARED);
ResultsFuture<ResultsMessage> result;
try {
result = executeRequest(0, request);
} catch (TeiidProcessingException e) {
throw new TeiidRuntimeException(e);
}
result.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {
@Override
public void onCompletion(ResultsFuture<ResultsMessage> future) {
terminateSession(sessionId);
}
});
}
@Override
public boolean isShutdown() {
return shutdown;
}
});
dataTierMgr.setEventDistributor(eventDistributor);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
LogManager.logDetail(LogConstants.CTX_DQP, "DQPCore started maxThreads", this.config.getMaxThreads(), "maxActivePlans", this.maxActivePlans, "source concurrency", this.userRequestSourceConcurrency);
}
use of org.teiid.core.TeiidProcessingException in project teiid by teiid.
the class TestJoinNode method helpCreateJoin.
protected void helpCreateJoin() {
// $NON-NLS-1$
ElementSymbol es1 = new ElementSymbol("e1");
es1.setType(DataTypeManager.DefaultDataClasses.INTEGER);
// $NON-NLS-1$
ElementSymbol es2 = new ElementSymbol("e2");
es2.setType(DataTypeManager.DefaultDataClasses.INTEGER);
List leftElements = new ArrayList();
leftElements.add(es1);
leftNode = new BlockingFakeRelationalNode(1, leftTuples);
leftNode.setElements(leftElements);
List rightElements = new ArrayList();
rightElements.add(es2);
rightNode = new BlockingFakeRelationalNode(2, rightTuples) {
@Override
public boolean hasBuffer() {
return false;
}
@Override
public TupleBuffer getBufferDirect(int maxRows) throws BlockedException, TeiidComponentException, TeiidProcessingException {
fail();
throw new AssertionError();
}
};
rightNode.setElements(rightElements);
List joinElements = new ArrayList();
joinElements.add(es1);
joinElements.add(es2);
join = new JoinNode(3);
joinStrategy = new NestedLoopJoinStrategy();
join.setJoinStrategy(joinStrategy);
join.setElements(joinElements);
join.setJoinType(joinType);
switch(criteriaType) {
case NO_CRITERIA:
break;
case EQUAL_CRITERIA:
join.setJoinExpressions(Arrays.asList(es1), Arrays.asList(es2));
joinStrategy = new MergeJoinStrategy(SortOption.SORT, SortOption.SORT, false);
join.setJoinStrategy(joinStrategy);
break;
case FUNCTION_CRITERIA:
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
Function func = new Function("lookup", new Expression[] { new Constant("pm1.g1"), new Constant("e2"), new Constant("e1"), es1 });
// $NON-NLS-1$
FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("lookup", new Class[] { String.class, String.class, String.class, Integer.class });
func.setFunctionDescriptor(desc);
func.setType(DataTypeManager.DefaultDataClasses.INTEGER);
CompareCriteria joinCriteria = new CompareCriteria(es2, CompareCriteria.EQ, func);
join.setJoinCriteria(joinCriteria);
break;
}
}
Aggregations