use of org.teiid.query.sql.symbol.GroupSymbol 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.query.sql.symbol.GroupSymbol in project teiid by teiid.
the class DataTierTupleSource method checkForUpdates.
private void checkForUpdates(AtomicResultsMessage results, Command command, EventDistributor distributor, int commandIndex, long ts) {
if (!RelationalNodeUtil.isUpdate(command) || !(command instanceof ProcedureContainer)) {
return;
}
ProcedureContainer pc = (ProcedureContainer) command;
GroupSymbol gs = pc.getGroup();
Integer zero = Integer.valueOf(0);
if (results.getResults().length <= commandIndex || zero.equals(results.getResults()[commandIndex].get(0))) {
return;
}
Object metadataId = gs.getMetadataID();
if (metadataId == null) {
return;
}
if (!(metadataId instanceof Table)) {
if (metadataId instanceof TempMetadataID) {
TempMetadataID tid = (TempMetadataID) metadataId;
if (tid.getTableData().getModel() != null) {
tid.getTableData().dataModified((Integer) results.getResults()[commandIndex].get(0));
}
}
return;
}
Table t = (Table) metadataId;
t.setLastDataModification(ts);
if (distributor != null) {
distributor.dataModification(this.workItem.getDqpWorkContext().getVdbName(), this.workItem.getDqpWorkContext().getVdbVersion(), t.getParent().getName(), t.getName());
}
}
use of org.teiid.query.sql.symbol.GroupSymbol in project teiid by teiid.
the class MetaDataProcessor method createElementMetadata.
private Map<Integer, Object> createElementMetadata(String label, ElementSymbol symbol) throws QueryMetadataException, TeiidComponentException {
Object elementID = symbol.getMetadataID();
Map<Integer, Object> column = new HashMap<Integer, Object>();
column.put(ResultsMetadataConstants.AUTO_INCREMENTING, Boolean.valueOf(metadata.elementSupports(elementID, SupportConstants.Element.AUTO_INCREMENT)));
column.put(ResultsMetadataConstants.CASE_SENSITIVE, Boolean.valueOf(metadata.elementSupports(elementID, SupportConstants.Element.CASE_SENSITIVE)));
column.put(ResultsMetadataConstants.CURRENCY, Boolean.FALSE);
Class<?> type = symbol.getType();
column.put(ResultsMetadataConstants.DATA_TYPE, DataTypeManager.getDataTypeName(type));
column.put(ResultsMetadataConstants.ELEMENT_LABEL, label);
column.put(ResultsMetadataConstants.ELEMENT_NAME, labelAsName ? label : metadata.getName(elementID));
GroupSymbol group = symbol.getGroupSymbol();
if (group == null || group.getMetadataID() == null) {
column.put(ResultsMetadataConstants.GROUP_NAME, null);
} else {
column.put(ResultsMetadataConstants.GROUP_NAME, metadata.getFullName(group.getMetadataID()));
}
boolean allowsNull = metadata.elementSupports(elementID, SupportConstants.Element.NULL);
boolean unknown = metadata.elementSupports(elementID, SupportConstants.Element.NULL_UNKNOWN);
Integer nullable = null;
if (unknown) {
nullable = ResultsMetadataConstants.NULL_TYPES.UNKNOWN;
} else {
if (allowsNull) {
nullable = ResultsMetadataConstants.NULL_TYPES.NULLABLE;
} else {
nullable = ResultsMetadataConstants.NULL_TYPES.NOT_NULL;
}
}
column.put(ResultsMetadataConstants.NULLABLE, nullable);
column.put(ResultsMetadataConstants.RADIX, new Integer(metadata.getRadix(elementID)));
column.put(ResultsMetadataConstants.SCALE, new Integer(metadata.getScale(elementID)));
int precision = getColumnPrecision(type, elementID);
column.put(ResultsMetadataConstants.PRECISION, new Integer(precision));
column.put(ResultsMetadataConstants.DISPLAY_SIZE, getColumnDisplaySize(precision, type, elementID));
boolean comparable = metadata.elementSupports(elementID, SupportConstants.Element.SEARCHABLE_COMPARE);
boolean likable = metadata.elementSupports(elementID, SupportConstants.Element.SEARCHABLE_LIKE);
Integer searchable = null;
if (comparable) {
if (likable) {
searchable = ResultsMetadataConstants.SEARCH_TYPES.SEARCHABLE;
} else {
searchable = ResultsMetadataConstants.SEARCH_TYPES.ALLEXCEPTLIKE;
}
} else {
if (likable) {
searchable = ResultsMetadataConstants.SEARCH_TYPES.LIKE_ONLY;
} else {
searchable = ResultsMetadataConstants.SEARCH_TYPES.UNSEARCHABLE;
}
}
column.put(ResultsMetadataConstants.SEARCHABLE, searchable);
column.put(ResultsMetadataConstants.SIGNED, Boolean.valueOf(metadata.elementSupports(elementID, SupportConstants.Element.SIGNED)));
column.put(ResultsMetadataConstants.VIRTUAL_DATABASE_NAME, vdbName);
column.put(ResultsMetadataConstants.VIRTUAL_DATABASE_VERSION, vdbVersion);
column.put(ResultsMetadataConstants.WRITABLE, new Boolean(metadata.elementSupports(elementID, SupportConstants.Element.UPDATE)));
return column;
}
use of org.teiid.query.sql.symbol.GroupSymbol in project teiid by teiid.
the class MetaDataProcessor method createXMLColumnMetadata.
private Map<Integer, Object> createXMLColumnMetadata(Query xmlCommand) {
GroupSymbol doc = xmlCommand.getFrom().getGroups().get(0);
Map<Integer, Object> xmlMetadata = getDefaultColumn(doc.getName(), XML_COLUMN_NAME, XMLType.class);
// Override size as XML may be big
xmlMetadata.put(ResultsMetadataConstants.DISPLAY_SIZE, JDBCSQLTypeInfo.XML_COLUMN_LENGTH);
return xmlMetadata;
}
use of org.teiid.query.sql.symbol.GroupSymbol in project teiid by teiid.
the class LanguageBridgeFactory method translate.
public With translate(List<WithQueryCommand> with) {
if (with == null || with.isEmpty()) {
return null;
}
With result = new With();
ArrayList<WithItem> items = new ArrayList<WithItem>(with.size());
for (WithQueryCommand withQueryCommand : with) {
WithItem item = new WithItem();
GroupSymbol group = withQueryCommand.getGroupSymbol();
if (withQueryCommand.getCommand() != null && excludeWithName != null && excludeWithName.equalsIgnoreCase(group.getName())) {
group = RulePlaceAccess.recontextSymbol(withQueryCommand.getGroupSymbol(), commandContext.getGroups());
group.setDefinition(null);
if (remappedGroups == null) {
remappedGroups = new IdentityHashMap<Object, GroupSymbol>();
}
this.remappedGroups.put(group.getMetadataID(), group);
}
item.setTable(translate(group));
if (withQueryCommand.getColumns() != null) {
List<ColumnReference> translatedElements = new ArrayList<ColumnReference>(withQueryCommand.getColumns().size());
for (ElementSymbol es : withQueryCommand.getColumns()) {
ColumnReference cr = translate(es);
translatedElements.add(cr);
if (withQueryCommand.getCommand() == null) {
// we want to convey the metadata to the source layer if possible
Object mid = es.getMetadataID();
if (mid instanceof TempMetadataID) {
TempMetadataID tid = (TempMetadataID) mid;
mid = tid.getOriginalMetadataID();
}
if (mid instanceof Column) {
cr.setMetadataObject((Column) mid);
}
}
}
item.setColumns(translatedElements);
}
if (withQueryCommand.getCommand() != null) {
item.setSubquery(translate(withQueryCommand.getCommand()));
} else {
item.setDependentValues(new TupleBufferList(withQueryCommand.getTupleBuffer()));
}
item.setRecusive(withQueryCommand.isRecursive());
items.add(item);
}
result.setItems(items);
return result;
}
Aggregations