use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.
the class QueryRewriter method createInlineViewQuery.
public static Query createInlineViewQuery(GroupSymbol inlineGroup, Command nested, QueryMetadataInterface metadata, List<? extends Expression> actualSymbols) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
Query query = new Query();
Select select = new Select();
query.setSelect(select);
From from = new From();
from.addClause(new UnaryFromClause(inlineGroup));
TempMetadataStore store = new TempMetadataStore();
TempMetadataAdapter tma = new TempMetadataAdapter(metadata, store);
if (nested instanceof QueryCommand) {
Query firstProject = ((QueryCommand) nested).getProjectedQuery();
makeSelectUnique(firstProject.getSelect(), false);
}
TempMetadataID gid = store.addTempGroup(inlineGroup.getName(), nested.getProjectedSymbols());
inlineGroup.setMetadataID(gid);
List<Class<?>> actualTypes = new ArrayList<Class<?>>(nested.getProjectedSymbols().size());
for (Expression ses : actualSymbols) {
actualTypes.add(ses.getType());
}
List<Expression> selectSymbols = SetQuery.getTypedProjectedSymbols(ResolverUtil.resolveElementsInGroup(inlineGroup, tma), actualTypes, tma);
Iterator<? extends Expression> iter = actualSymbols.iterator();
for (Expression ses : selectSymbols) {
ses = (Expression) ses.clone();
Expression actual = iter.next();
if (!Symbol.getShortName(ses).equals(Symbol.getShortName(actual))) {
if (ses instanceof AliasSymbol) {
((AliasSymbol) ses).setShortName(Symbol.getShortName(actual));
} else {
ses = new AliasSymbol(Symbol.getShortName(actual), ses);
}
}
select.addSymbol(ses);
}
query.setFrom(from);
QueryResolver.resolveCommand(query, tma);
query.setOption(nested.getOption() != null ? (Option) nested.getOption().clone() : null);
from.getClauses().clear();
SubqueryFromClause sqfc = new SubqueryFromClause(inlineGroup.getName());
sqfc.setCommand(nested);
sqfc.getGroupSymbol().setMetadataID(inlineGroup.getMetadataID());
from.addClause(sqfc);
// copy the metadata onto the new query so that temp metadata adapters will be used in later calls
query.getTemporaryMetadata().getData().putAll(store.getData());
return query;
}
use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.
the class TestMaterialization method testFunctionBasedIndexQuery.
@Test
public void testFunctionBasedIndexQuery() throws Exception {
TempMetadataID id = this.globalStore.getGlobalTempTableMetadataId(metadata.getGroupID("MatView.vgroup2a"));
assertEquals("SELECT MatView.VGroup2a.*, ucase(x) FROM MatView.VGroup2a option nocache MatView.VGroup2a", id.getQueryNode().getQuery());
}
use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.
the class GlobalTableStoreImpl method updateMatViewRow.
@Override
public List<?> updateMatViewRow(String matTableName, List<?> tuple, boolean delete) throws TeiidComponentException {
TempTable tempTable = tableStore.getTempTable(matTableName);
if (tempTable != null) {
TempMetadataID id = tableStore.getMetadataStore().getTempGroupID(matTableName);
synchronized (id) {
boolean clone = tempTable.getActive().get() != 0;
if (clone) {
tempTable = tempTable.clone();
}
List<?> result = tempTable.updateTuple(tuple, delete);
if (clone) {
swapTempTable(matTableName, tempTable);
}
return result;
}
}
return null;
}
use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.
the class GlobalTableStoreImpl method getGlobalTempTableMetadataId.
@Override
public TempMetadataID getGlobalTempTableMetadataId(Object viewId) throws TeiidProcessingException, TeiidComponentException {
String matViewName = metadata.getFullName(viewId);
String matTableName = RelationalPlanner.MAT_PREFIX + matViewName.toUpperCase();
GroupSymbol group = new GroupSymbol(matViewName);
group.setMetadataID(viewId);
TempMetadataID id = tableStore.getMetadataStore().getTempGroupID(matTableName);
// define the table preserving the key/index information and ensure that only a single instance exists
if (id == null) {
synchronized (viewId) {
id = tableStore.getMetadataStore().getTempGroupID(matTableName);
LinkedHashMap<Expression, Integer> newExprs = null;
if (id == null) {
List<ElementSymbol> allCols = ResolverUtil.resolveElementsInGroup(group, metadata);
QueryNode qnode = metadata.getVirtualPlan(viewId);
if (viewId instanceof Table) {
Table t = (Table) viewId;
List<KeyRecord> fbis = t.getFunctionBasedIndexes();
if (!fbis.isEmpty()) {
List<GroupSymbol> groups = Arrays.asList(group);
int i = 0;
newExprs = new LinkedHashMap<Expression, Integer>();
for (KeyRecord keyRecord : fbis) {
for (int j = 0; j < keyRecord.getColumns().size(); j++) {
Column c = keyRecord.getColumns().get(j);
if (c.getParent() != keyRecord) {
continue;
}
String exprString = c.getNameInSource();
Expression ex = QueryParser.getQueryParser().parseExpression(exprString);
Integer index = newExprs.get(ex);
if (index == null) {
ResolverVisitor.resolveLanguageObject(ex, groups, metadata);
ex = QueryRewriter.rewriteExpression(ex, null, metadata);
String colName = TEIID_FBI + i;
while (t.getColumnByName(colName) != null) {
colName = TEIID_FBI + (++i);
}
ElementSymbol es = new ElementSymbol(colName);
es.setType(ex.getType());
allCols.add(es);
c.setPosition(allCols.size());
newExprs.put(ex, allCols.size());
ex = (Expression) ex.clone();
} else {
c.setPosition(index);
}
}
}
ResolverUtil.clearGroupInfo(group, metadata);
// $NON-NLS-1$
StringBuilder query = new StringBuilder("SELECT ");
// $NON-NLS-1$
query.append(group).append(".*, ");
for (Iterator<Expression> iter = newExprs.keySet().iterator(); iter.hasNext(); ) {
query.append(iter.next());
if (iter.hasNext()) {
// $NON-NLS-1$
query.append(", ");
}
}
// $NON-NLS-1$ //$NON-NLS-2$
query.append(" FROM ").append(group).append(" option nocache ").append(group);
qnode = new QueryNode(query.toString());
}
}
id = tableStore.getMetadataStore().addTempGroup(matTableName, allCols, false, true);
id.setQueryNode(qnode);
id.setCardinality((int) metadata.getCardinality(viewId));
id.setOriginalMetadataID(viewId);
Object pk = metadata.getPrimaryKey(viewId);
if (pk != null) {
ArrayList<TempMetadataID> primaryKey = resolveIndex(metadata, id, pk);
id.setPrimaryKey(primaryKey);
}
Collection keys = metadata.getUniqueKeysInGroup(viewId);
for (Object key : keys) {
id.addUniqueKey(resolveIndex(metadata, id, key));
}
Collection indexes = metadata.getIndexesInGroup(viewId);
for (Object index : indexes) {
id.addIndex(index, resolveIndex(metadata, id, index));
}
if (newExprs != null) {
Table table = (Table) viewId;
List<KeyRecord> fbis = table.getFunctionBasedIndexes();
for (KeyRecord keyRecord : fbis) {
id.addIndex(keyRecord, resolveIndex(metadata, id, keyRecord));
}
GroupSymbol gs = new GroupSymbol(matTableName);
gs.setMetadataID(id);
SymbolMap map = SymbolMap.createSymbolMap(group, ResolverUtil.resolveElementsInGroup(gs, metadata).subList(0, allCols.size() - newExprs.size()), metadata);
LinkedHashMap<Expression, Integer> mappedExprs = new LinkedHashMap<Expression, Integer>();
for (Map.Entry<Expression, Integer> entry : newExprs.entrySet()) {
Expression ex = (Expression) entry.getKey().clone();
ExpressionMappingVisitor.mapExpressions(ex, map.asMap());
mappedExprs.put(ex, entry.getValue());
}
id.getTableData().setFunctionBasedExpressions(mappedExprs);
}
}
}
}
updateCacheHint(viewId, group, id);
return id;
}
use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.
the class GlobalTableStoreImpl method getCodeTableMetadataId.
@Override
public TempMetadataID getCodeTableMetadataId(String codeTableName, String returnElementName, String keyElementName, String matTableName) throws TeiidComponentException, QueryMetadataException {
ElementSymbol keyElement = new ElementSymbol(matTableName + ElementSymbol.SEPARATOR + keyElementName);
ElementSymbol returnElement = new ElementSymbol(matTableName + ElementSymbol.SEPARATOR + returnElementName);
keyElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + keyElementName))));
returnElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + returnElementName))));
TempMetadataID id = this.tableStore.getMetadataStore().getTempGroupID(matTableName);
if (id == null) {
synchronized (this) {
id = this.tableStore.getMetadataStore().addTempGroup(matTableName, Arrays.asList(keyElement, returnElement), false, true);
// $NON-NLS-1$
String queryString = Reserved.SELECT + ' ' + new ElementSymbol(keyElementName) + " ," + new ElementSymbol(returnElementName) + ' ' + Reserved.FROM + ' ' + new GroupSymbol(codeTableName);
id.setQueryNode(new QueryNode(queryString));
id.setPrimaryKey(id.getElements().subList(0, 1));
CacheHint hint = new CacheHint(true, null);
id.setCacheHint(hint);
}
}
return id;
}
Aggregations