Search in sources :

Example 16 with TempMetadataID

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;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) TempMetadataID(org.teiid.query.metadata.TempMetadataID) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Example 17 with TempMetadataID

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());
}
Also used : TempMetadataID(org.teiid.query.metadata.TempMetadataID) Test(org.junit.Test)

Example 18 with TempMetadataID

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;
}
Also used : TempMetadataID(org.teiid.query.metadata.TempMetadataID)

Example 19 with TempMetadataID

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;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Table(org.teiid.metadata.Table) TempMetadataID(org.teiid.query.metadata.TempMetadataID) SymbolMap(org.teiid.query.sql.util.SymbolMap) CacheHint(org.teiid.query.sql.lang.CacheHint) LinkedHashMap(java.util.LinkedHashMap) KeyRecord(org.teiid.metadata.KeyRecord) Expression(org.teiid.query.sql.symbol.Expression) Column(org.teiid.metadata.Column) QueryNode(org.teiid.query.mapping.relational.QueryNode) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Collection(java.util.Collection) ReplicatedObject(org.teiid.query.ReplicatedObject) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SymbolMap(org.teiid.query.sql.util.SymbolMap)

Example 20 with TempMetadataID

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;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CacheHint(org.teiid.query.sql.lang.CacheHint) QueryNode(org.teiid.query.mapping.relational.QueryNode) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol)

Aggregations

TempMetadataID (org.teiid.query.metadata.TempMetadataID)48 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)21 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)16 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)12 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Test (org.junit.Test)7 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)6 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)6 HashSet (java.util.HashSet)5 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)5 LanguageObject (org.teiid.query.sql.LanguageObject)4 CacheHint (org.teiid.query.sql.lang.CacheHint)4 Expression (org.teiid.query.sql.symbol.Expression)4 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)3 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)3