Search in sources :

Example 21 with QueryNode

use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.

the class TestValidator method exampleMetadata4.

public static TransformationMetadata exampleMetadata4() {
    MetadataStore metadataStore = new MetadataStore();
    // Create metadata objects
    // $NON-NLS-1$
    Schema modelObj = RealMetadataFactory.createPhysicalModel("test", metadataStore);
    // $NON-NLS-1$
    Table groupObj = RealMetadataFactory.createPhysicalGroup("group", modelObj);
    // $NON-NLS-1$
    RealMetadataFactory.createElement("e0", groupObj, DataTypeManager.DefaultDataTypes.INTEGER);
    // $NON-NLS-1$
    RealMetadataFactory.createElement("e1", groupObj, DataTypeManager.DefaultDataTypes.STRING);
    // $NON-NLS-1$
    RealMetadataFactory.createElement("e2", groupObj, DataTypeManager.DefaultDataTypes.STRING);
    // $NON-NLS-1$
    Schema vModelObj = RealMetadataFactory.createVirtualModel("vTest", metadataStore);
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode vNode = new QueryNode("SELECT * FROM test.group");
    // $NON-NLS-1$
    Table vGroupObj = RealMetadataFactory.createVirtualGroup("vGroup", vModelObj, vNode);
    // $NON-NLS-1$
    Column vElemObj0 = RealMetadataFactory.createElement("e0", vGroupObj, DataTypeManager.DefaultDataTypes.INTEGER);
    // $NON-NLS-1$
    Column vElemObj1 = RealMetadataFactory.createElement("e1", vGroupObj, DataTypeManager.DefaultDataTypes.STRING);
    // $NON-NLS-1$
    RealMetadataFactory.createElement("e2", vGroupObj, DataTypeManager.DefaultDataTypes.STRING);
    List<Column> elements = new ArrayList<Column>(2);
    elements.add(vElemObj0);
    elements.add(vElemObj1);
    // e1 //$NON-NLS-1$
    RealMetadataFactory.createAccessPattern("ap1", vGroupObj, elements);
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode vNode2 = new QueryNode("SELECT * FROM vTest.vGroup");
    // $NON-NLS-1$
    Table vGroupObj2 = RealMetadataFactory.createVirtualGroup("vGroup2", vModelObj, vNode2);
    // $NON-NLS-1$
    Column vElemObj20 = RealMetadataFactory.createElement("e0", vGroupObj2, DataTypeManager.DefaultDataTypes.INTEGER);
    // $NON-NLS-1$
    Column vElemObj21 = RealMetadataFactory.createElement("e1", vGroupObj2, DataTypeManager.DefaultDataTypes.STRING);
    // $NON-NLS-1$
    RealMetadataFactory.createElement("e2", vGroupObj2, DataTypeManager.DefaultDataTypes.STRING);
    elements = new ArrayList<Column>(2);
    elements.add(vElemObj20);
    elements.add(vElemObj21);
    // e1 //$NON-NLS-1$
    RealMetadataFactory.createAccessPattern("vTest.vGroup2.ap1", vGroupObj2, elements);
    return RealMetadataFactory.createTransformationMetadata(metadataStore, "example4");
}
Also used : MetadataStore(org.teiid.metadata.MetadataStore) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) QueryNode(org.teiid.query.mapping.relational.QueryNode) Schema(org.teiid.metadata.Schema) ArrayList(java.util.ArrayList)

Example 22 with QueryNode

use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.

the class RelationalPlanner method resolveVirtualGroup.

private Command resolveVirtualGroup(GroupSymbol virtualGroup) throws QueryMetadataException, TeiidComponentException, TeiidProcessingException {
    QueryNode qnode = null;
    Object metadataID = virtualGroup.getMetadataID();
    boolean noCache = isNoCacheGroup(metadata, metadataID, option);
    boolean isMaterializedGroup = metadata.hasMaterialization(metadataID);
    String cacheString = SQLConstants.Reserved.SELECT;
    if (isMaterializedGroup) {
        Object matMetadataId = metadata.getMaterialization(metadataID);
        String matTableName = null;
        CacheHint hint = null;
        boolean isImplicitGlobal = matMetadataId == null;
        if (isImplicitGlobal) {
            TempMetadataID tid = context.getGlobalTableStore().getGlobalTempTableMetadataId(metadataID);
            matTableName = tid.getID();
            hint = tid.getCacheHint();
            matMetadataId = tid;
        } else {
            matTableName = metadata.getFullName(matMetadataId);
        }
        if (noCache) {
            // not use cache
            qnode = metadata.getVirtualPlan(metadataID);
            // TODO: update the table for defaultMat
            // $NON-NLS-1$
            recordAnnotation(analysisRecord, Annotation.MATERIALIZED_VIEW, Priority.LOW, "SimpleQueryResolver.materialized_table_not_used", virtualGroup, matTableName);
        } else {
            this.context.accessedPlanningObject(matMetadataId);
            qnode = new QueryNode(null);
            List<ElementSymbol> symbols = new ArrayList<ElementSymbol>();
            for (ElementSymbol el : ResolverUtil.resolveElementsInGroup(virtualGroup, metadata)) {
                symbols.add(new ElementSymbol(el.getShortName()));
            }
            Query query = createMatViewQuery(metadataID, matMetadataId, matTableName, symbols, isImplicitGlobal);
            query.setCacheHint(hint);
            qnode.setCommand(query);
            // $NON-NLS-1$
            cacheString = "matview";
            // $NON-NLS-1$
            recordAnnotation(analysisRecord, Annotation.MATERIALIZED_VIEW, Priority.LOW, "SimpleQueryResolver.Query_was_redirected_to_Mat_table", virtualGroup, matTableName);
        }
    } else {
        // Not a materialized view - query the primary transformation
        qnode = metadata.getVirtualPlan(metadataID);
    }
    Command result = (Command) QueryResolver.resolveView(virtualGroup, qnode, cacheString, metadata, false).getCommand().clone();
    return QueryRewriter.rewrite(result, metadata, context);
}
Also used : CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) QueryNode(org.teiid.query.mapping.relational.QueryNode) TempMetadataID(org.teiid.query.metadata.TempMetadataID) LanguageObject(org.teiid.query.sql.LanguageObject)

Example 23 with QueryNode

use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.

the class MetadataValidator method validate.

private void validate(VDBMetaData vdb, ModelMetaData model, AbstractMetadataRecord record, ValidatorReport report, QueryMetadataInterface metadata, MetadataFactory mf) {
    ValidatorReport resolverReport = null;
    try {
        if (record instanceof Procedure) {
            Procedure p = (Procedure) record;
            Command command = parser.parseProcedure(p.getQueryPlan(), false);
            validateNoReferences(command, report, model);
            QueryResolver.resolveCommand(command, new GroupSymbol(p.getFullName()), Command.TYPE_STORED_PROCEDURE, metadata, false);
            resolverReport = Validator.validate(command, metadata);
            determineDependencies(p, command);
        } else if (record instanceof Table) {
            Table t = (Table) record;
            GroupSymbol symbol = new GroupSymbol(t.getFullName());
            ResolverUtil.resolveGroup(symbol, metadata);
            String selectTransformation = t.getSelectTransformation();
            QueryNode node = null;
            if (t.isVirtual()) {
                QueryCommand command = (QueryCommand) parser.parseCommand(selectTransformation);
                validateNoReferences(command, report, model);
                QueryResolver.resolveCommand(command, metadata);
                resolverReport = Validator.validate(command, metadata);
                if (!resolverReport.hasItems() && (t.getColumns() == null || t.getColumns().isEmpty())) {
                    List<Expression> symbols = command.getProjectedSymbols();
                    for (Expression column : symbols) {
                        try {
                            addColumn(Symbol.getShortName(column), column.getType(), t, mf);
                        } catch (TranslatorException e) {
                            log(report, model, e.getMessage());
                        }
                    }
                }
                node = QueryResolver.resolveView(symbol, new QueryNode(selectTransformation), SQLConstants.Reserved.SELECT, metadata, true);
                if (t.getColumns() != null && !t.getColumns().isEmpty()) {
                    determineDependencies(t, command);
                    if (t.getInsertPlan() != null && t.isInsertPlanEnabled()) {
                        validateUpdatePlan(model, report, metadata, t, t.getInsertPlan(), Command.TYPE_INSERT);
                    }
                    if (t.getUpdatePlan() != null && t.isUpdatePlanEnabled()) {
                        validateUpdatePlan(model, report, metadata, t, t.getUpdatePlan(), Command.TYPE_UPDATE);
                    }
                    if (t.getDeletePlan() != null && t.isDeletePlanEnabled()) {
                        validateUpdatePlan(model, report, metadata, t, t.getDeletePlan(), Command.TYPE_DELETE);
                    }
                }
            }
            boolean addCacheHint = false;
            if (t.isVirtual() && t.isMaterialized() && t.getMaterializedTable() == null) {
                List<KeyRecord> fbis = t.getFunctionBasedIndexes();
                List<GroupSymbol> groups = Arrays.asList(symbol);
                if (fbis != null && !fbis.isEmpty()) {
                    for (KeyRecord fbi : fbis) {
                        for (int j = 0; j < fbi.getColumns().size(); j++) {
                            Column c = fbi.getColumns().get(j);
                            if (c.getParent() != fbi) {
                                continue;
                            }
                            String exprString = c.getNameInSource();
                            try {
                                Expression ex = parser.parseExpression(exprString);
                                validateNoReferences(ex, report, model);
                                ResolverVisitor.resolveLanguageObject(ex, groups, metadata);
                                if (!ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(ex).isEmpty()) {
                                    log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31114, exprString, fbi.getFullName()));
                                }
                                EvaluatableVisitor ev = new EvaluatableVisitor();
                                PreOrPostOrderNavigator.doVisit(ex, ev, PreOrPostOrderNavigator.PRE_ORDER);
                                if (ev.getDeterminismLevel().compareTo(Determinism.VDB_DETERMINISTIC) < 0) {
                                    log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31115, exprString, fbi.getFullName()));
                                }
                            } catch (QueryResolverException e) {
                                log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31116, exprString, fbi.getFullName(), e.getMessage()));
                            }
                        }
                    }
                }
            } else {
                addCacheHint = true;
            }
            if (node != null && addCacheHint && t.isMaterialized()) {
                CacheHint cacheHint = node.getCommand().getCacheHint();
                Long ttl = -1L;
                if (cacheHint != null) {
                    if (cacheHint.getTtl() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_TTL, false) == null) {
                        ttl = cacheHint.getTtl();
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_TTL, String.valueOf(ttl));
                    }
                    if (cacheHint.getUpdatable() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_UPDATABLE, false) == null) {
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_UPDATABLE, String.valueOf(cacheHint.getUpdatable()));
                    }
                    if (cacheHint.getPrefersMemory() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_PREFER_MEMORY, false) == null) {
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_PREFER_MEMORY, String.valueOf(cacheHint.getPrefersMemory()));
                    }
                    if (cacheHint.getScope() != null && t.getProperty(MaterializationMetadataRepository.MATVIEW_SHARE_SCOPE, false) == null) {
                        log(report, model, Severity.WARNING, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31252, t.getName(), cacheHint.getScope().name()));
                        t.setProperty(MaterializationMetadataRepository.MATVIEW_SHARE_SCOPE, MaterializationMetadataRepository.Scope.IMPORTED.name());
                    }
                }
            }
        }
        processReport(model, record, report, resolverReport);
    } catch (TeiidException e) {
        log(report, model, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31080, record.getFullName(), e.getMessage()));
    }
}
Also used : CacheHint(org.teiid.query.sql.lang.CacheHint) ValidatorReport(org.teiid.query.validator.ValidatorReport) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidException(org.teiid.core.TeiidException) EvaluatableVisitor(org.teiid.query.sql.visitor.EvaluatableVisitor) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Command(org.teiid.query.sql.lang.Command) QueryCommand(org.teiid.query.sql.lang.QueryCommand) Expression(org.teiid.query.sql.symbol.Expression) QueryNode(org.teiid.query.mapping.relational.QueryNode) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryCommand(org.teiid.query.sql.lang.QueryCommand) TranslatorException(org.teiid.translator.TranslatorException)

Example 24 with QueryNode

use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.

the class TestCoherenceTranslator method getTradeTranslationUtility.

/**
 * this is used as an alternative to loading the vdb
 * @return
 */
private TranslationUtility getTradeTranslationUtility() {
    MetadataStore metadataStore = new MetadataStore();
    // Create TRADE
    // $NON-NLS-1$
    Schema trading = RealMetadataFactory.createPhysicalModel("Trade_Object", metadataStore);
    // Create physical groups
    // $NON-NLS-1$
    Table trade = RealMetadataFactory.createPhysicalGroup("TRADE", trading);
    trade.setNameInSource("org.teiid.translator.coherence.Trade");
    // Create physical elements
    String[] elemNames = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
    "NAME", // $NON-NLS-1$ //$NON-NLS-2$
    "TRADEID" };
    String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.LONG };
    List<Column> cols = RealMetadataFactory.createElements(trade, elemNames, elemTypes);
    // Set name in source on each column
    String[] nameInSource = new String[] { "Name", "TradeId" };
    for (int i = 0; i < nameInSource.length; i++) {
        cols.get(i).setNameInSource(nameInSource[i]);
    }
    List<Column> keys = new ArrayList(1);
    keys.add(cols.get(1));
    KeyRecord trade_pk = RealMetadataFactory.createKey(Type.Primary, "TradeID_PK", trade, keys);
    // LEG
    // $NON-NLS-1$
    Table leg = RealMetadataFactory.createPhysicalGroup("LEG", trading);
    leg.setNameInSource("org.teiid.translator.coherence.Leg");
    // Create physical elements
    String[] legNames = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
    "LEGID", // $NON-NLS-1$ //$NON-NLS-2$
    "NOTATIONAL", // $NON-NLS-1$ //$NON-NLS-2$
    "NAME", // $NON-NLS-1$ //$NON-NLS-2$
    "TRADEID" };
    String[] legTypes = new String[] { DataTypeManager.DefaultDataTypes.LONG, DataTypeManager.DefaultDataTypes.DOUBLE, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.LONG };
    List<Column> legcols = RealMetadataFactory.createElements(leg, legNames, legTypes);
    // Set name in source on each column
    String[] legnameInSource = new String[] { "LegId", "Notational", "Name", "TradeId" };
    for (int i = 0; i < legnameInSource.length; i++) {
        legcols.get(i).setNameInSource(legnameInSource[i]);
    }
    legcols.get(3).setSelectable(false);
    legcols.get(3).setUpdatable(false);
    List<Column> legkeys = new ArrayList(1);
    keys.add(legcols.get(0));
    RealMetadataFactory.createKey(Type.Primary, "Leg_ID_PK", leg, legkeys);
    List<Column> foreignkey = new ArrayList(1);
    foreignkey.add(legcols.get(3));
    ForeignKey fk = RealMetadataFactory.createForeignKey("TRADE_FK", leg, foreignkey, trade_pk);
    fk.setNameInSource("Legs");
    fk.setParent(trade);
    // Set column-specific properties
    // cols.get(0).setSelectable(false);
    // cols.get(0).setSearchType(SearchType.Unsearchable);
    Schema tradeview = RealMetadataFactory.createVirtualModel("Trade_View", metadataStore);
    QueryNode qn = new QueryNode("SELECT Trade_Object.Trade.TradeID, Trade_Object.Trade.Name, Trade_Object.Leg.LegID, Trade_Object.Leg.Notational, Trade_Object.Leg.Name AS LegName " + " FROM Trade_Object.Trade, Trade_Object.Leg " + "WHERE	Trade_Object.Trade.TradeID = Trade_Object.Leg.TradeID " + "SELECT * FROM Trade.Trad " + " WHERE Trade.Trade.TradeID = Trade.Trade.TradeID");
    Table trades = RealMetadataFactory.createVirtualGroup("Trades", tradeview, qn);
    return new TranslationUtility(RealMetadataFactory.createTransformationMetadata(metadataStore, "Trade"));
}
Also used : Table(org.teiid.metadata.Table) Schema(org.teiid.metadata.Schema) ArrayList(java.util.ArrayList) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ForeignKey(org.teiid.metadata.ForeignKey) MetadataStore(org.teiid.metadata.MetadataStore) KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) QueryNode(org.teiid.query.mapping.relational.QueryNode)

Example 25 with QueryNode

use of org.teiid.query.mapping.relational.QueryNode in project teiid by teiid.

the class TempMetadataAdapter method getVirtualPlan.

public QueryNode getVirtualPlan(Object groupID) throws TeiidComponentException, QueryMetadataException {
    if (this.queryNodes != null) {
        QueryNode node = this.queryNodes.get(groupID);
        if (node != null) {
            return node;
        }
    }
    if (groupID instanceof TempMetadataID && !(actualMetadata instanceof TempMetadataAdapter)) {
        TempMetadataID tid = (TempMetadataID) groupID;
        QueryNode queryNode = tid.getQueryNode();
        if (queryNode != null) {
            return queryNode;
        }
        throw new QueryMetadataException(QueryPlugin.Event.TEIID31265, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31265, tid.getName()));
    }
    return this.actualMetadata.getVirtualPlan(groupID);
}
Also used : QueryNode(org.teiid.query.mapping.relational.QueryNode) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException)

Aggregations

QueryNode (org.teiid.query.mapping.relational.QueryNode)39 Schema (org.teiid.metadata.Schema)20 MetadataStore (org.teiid.metadata.MetadataStore)19 Table (org.teiid.metadata.Table)15 Column (org.teiid.metadata.Column)9 CompositeMetadataStore (org.teiid.query.metadata.CompositeMetadataStore)9 Test (org.junit.Test)8 Procedure (org.teiid.metadata.Procedure)8 ArrayList (java.util.ArrayList)7 List (java.util.List)5 ProcedureParameter (org.teiid.metadata.ProcedureParameter)5 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)5 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)4 Command (org.teiid.query.sql.lang.Command)4 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)4 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)3 KeyRecord (org.teiid.metadata.KeyRecord)3 TempMetadataID (org.teiid.query.metadata.TempMetadataID)3 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)3 CacheHint (org.teiid.query.sql.lang.CacheHint)3