Search in sources :

Example 1 with IDGenerator

use of org.teiid.core.id.IDGenerator in project teiid by teiid.

the class QueryOptimizer method optimizePlan.

public static ProcessorPlan optimizePlan(Command command, QueryMetadataInterface metadata, IDGenerator idGenerator, CapabilitiesFinder capFinder, AnalysisRecord analysisRecord, CommandContext context) throws QueryMetadataException, TeiidComponentException, QueryPlannerException {
    if (analysisRecord == null) {
        analysisRecord = new AnalysisRecord(false, false);
    }
    if (context == null) {
        context = new CommandContext();
    }
    if (!(capFinder instanceof TempCapabilitiesFinder)) {
        capFinder = new TempCapabilitiesFinder(capFinder);
    }
    boolean debug = analysisRecord.recordDebug();
    if (!(metadata instanceof TempMetadataAdapter)) {
        metadata = new TempMetadataAdapter(metadata, new TempMetadataStore());
    }
    if (context.getMetadata() == null) {
        context.setMetadata(metadata);
    }
    // Create an ID generator that can be used for all plans to generate unique data node IDs
    if (idGenerator == null) {
        idGenerator = new IDGenerator();
    }
    if (debug) {
        // $NON-NLS-1$
        analysisRecord.println("\n----------------------------------------------------------------------------");
        // $NON-NLS-1$
        analysisRecord.println("OPTIMIZE: \n" + command);
    }
    if (command instanceof Insert) {
        Insert insert = (Insert) command;
        if (insert.isUpsert()) {
            // if not supported or there are trigger actions, then rewrite as a procedure
            // we do this here since we're changing the command type.
            // TODO: we could push this back into the rewrite, but it will need to be capabilities aware
            GroupSymbol group = insert.getGroup();
            Object modelId = metadata.getModelID(group.getMetadataID());
            boolean supportsUpsert = CapabilitiesUtil.supports(Capability.UPSERT, modelId, metadata, capFinder);
            if (!supportsUpsert) {
                try {
                    command = QueryRewriter.rewriteAsUpsertProcedure(insert, metadata, context);
                } catch (TeiidProcessingException e) {
                    throw new QueryPlannerException(e);
                }
                if (debug) {
                    // $NON-NLS-1$
                    analysisRecord.println("\n----------------------------------------------------------------------------");
                    // $NON-NLS-1$
                    analysisRecord.println("OPTIMIZE UPSERT PROCEDURE: \n" + command);
                }
            }
        }
    }
    ProcessorPlan result = null;
    switch(command.getType()) {
        case Command.TYPE_UPDATE_PROCEDURE:
            CreateProcedureCommand cupc = (CreateProcedureCommand) command;
            if (cupc.getUpdateType() != Command.TYPE_UNKNOWN || cupc.getVirtualGroup() == null) {
                // row update procedure or anon block
                result = planProcedure(command, metadata, idGenerator, capFinder, analysisRecord, context);
            } else {
                Object pid = cupc.getVirtualGroup().getMetadataID();
                if (pid instanceof TempMetadataID) {
                    TempMetadataID tid = (TempMetadataID) pid;
                    if (tid.getOriginalMetadataID() != null) {
                        pid = tid.getOriginalMetadataID();
                    }
                }
                String fullName = metadata.getFullName(pid);
                // $NON-NLS-1$
                fullName = "procedure cache:" + fullName;
                PreparedPlan pp = context.getPlan(fullName);
                if (pp == null) {
                    Determinism determinismLevel = context.resetDeterminismLevel();
                    try {
                        CommandContext clone = context.clone();
                        ProcessorPlan plan = planProcedure(command, metadata, idGenerator, capFinder, analysisRecord, clone);
                        // note that this is not a full prepared plan.  It is not usable by user queries.
                        if (pid instanceof Procedure) {
                            clone.accessedPlanningObject(pid);
                        }
                        pp = new PreparedPlan();
                        pp.setPlan(plan, clone);
                        context.putPlan(fullName, pp, context.getDeterminismLevel());
                    } finally {
                        context.setDeterminismLevel(determinismLevel);
                    }
                }
                result = pp.getPlan().clone();
                for (Object id : pp.getAccessInfo().getObjectsAccessed()) {
                    context.accessedPlanningObject(id);
                }
            }
            break;
        case Command.TYPE_BATCHED_UPDATE:
            result = BATCHED_UPDATE_PLANNER.optimize(command, idGenerator, metadata, capFinder, analysisRecord, context);
            break;
        case Command.TYPE_ALTER_PROC:
        case Command.TYPE_ALTER_TRIGGER:
        case Command.TYPE_ALTER_VIEW:
            result = DDL_PLANNER.optimize(command, idGenerator, metadata, capFinder, analysisRecord, context);
            break;
        case Command.TYPE_SOURCE_EVENT:
            result = SOURCE_EVENT_PLANNER.optimize(command, idGenerator, metadata, capFinder, analysisRecord, context);
            break;
        default:
            try {
                RelationalPlanner planner = new RelationalPlanner();
                planner.initialize(command, idGenerator, metadata, capFinder, analysisRecord, context);
                result = planner.optimize(command);
            } catch (QueryResolverException e) {
                throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30245, e);
            }
    }
    if (debug) {
        // $NON-NLS-1$
        analysisRecord.println("\n----------------------------------------------------------------------------");
        // $NON-NLS-1$
        analysisRecord.println("OPTIMIZATION COMPLETE:");
        // $NON-NLS-1$
        analysisRecord.println("PROCESSOR PLAN:\n" + result);
        // $NON-NLS-1$
        analysisRecord.println("============================================================================");
    }
    return result;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) Determinism(org.teiid.metadata.FunctionMethod.Determinism) CommandContext(org.teiid.query.util.CommandContext) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) TempMetadataID(org.teiid.query.metadata.TempMetadataID) TempCapabilitiesFinder(org.teiid.query.metadata.TempCapabilitiesFinder) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) Insert(org.teiid.query.sql.lang.Insert) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) RelationalPlanner(org.teiid.query.optimizer.relational.RelationalPlanner) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan) Procedure(org.teiid.metadata.Procedure) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) IDGenerator(org.teiid.core.id.IDGenerator) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Example 2 with IDGenerator

use of org.teiid.core.id.IDGenerator in project teiid by teiid.

the class TestMultiSourcePlanToProcessConverter method helpTestMultiSourcePlan.

public ProcessorPlan helpTestMultiSourcePlan(QueryMetadataInterface metadata, String userSql, String multiModel, int sourceCount, ProcessorDataManager dataMgr, List<?>[] expectedResults, VDBMetaData vdb, List<?> params, Options options, SourceCapabilities bsc) throws Exception {
    Map<String, String> multiSourceModels = MultiSourceMetadataWrapper.getMultiSourceModels(vdb);
    for (String model : multiSourceModels.keySet()) {
        char sourceID = 'a';
        // by default every model has one binding associated, but for multi-source there were none assigned.
        ModelMetaData m = vdb.getModel(model);
        int x = m.getSourceNames().size();
        for (int i = x; i < sourceCount; i++, sourceID++) {
            // $NON-NLS-1$ //$NON-NLS-2$
            m.addSourceMapping("" + sourceID, "translator", null);
        }
    }
    QueryMetadataInterface wrapper = new MultiSourceMetadataWrapper(metadata, multiSourceModels);
    wrapper = new TempMetadataAdapter(wrapper, new TempMetadataStore());
    DQPWorkContext workContext = RealMetadataFactory.buildWorkContext(wrapper, vdb);
    AnalysisRecord analysis = new AnalysisRecord(DEBUG, DEBUG);
    Command command = TestResolver.helpResolve(userSql, wrapper);
    ValidatorReport report = Validator.validate(command, metadata);
    if (report.hasItems()) {
        fail(report.toString());
    }
    // Plan
    command = QueryRewriter.rewrite(command, wrapper, null);
    DefaultCapabilitiesFinder fakeFinder = new DefaultCapabilitiesFinder(bsc);
    CapabilitiesFinder finder = new TempCapabilitiesFinder(fakeFinder);
    IDGenerator idGenerator = new IDGenerator();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    CommandContext context = new CommandContext("test", "user", null, vdb.getName(), vdb.getVersion(), false);
    context.setDQPWorkContext(workContext);
    context.setOptions(options);
    ProcessorPlan plan = QueryOptimizer.optimizePlan(command, wrapper, idGenerator, finder, analysis, context);
    if (DEBUG) {
        System.out.println(analysis.getDebugLog());
        // $NON-NLS-1$
        System.out.println("\nMultiSource Plan:");
        System.out.println(plan);
    }
    if (params != null) {
        TestProcessor.setParameterValues(params, command, context);
    }
    TestProcessor.helpProcess(plan, context, dataMgr, expectedResults);
    return plan;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) DQPWorkContext(org.teiid.dqp.internal.process.DQPWorkContext) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) CommandContext(org.teiid.query.util.CommandContext) TempCapabilitiesFinder(org.teiid.query.metadata.TempCapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) ValidatorReport(org.teiid.query.validator.ValidatorReport) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) TempCapabilitiesFinder(org.teiid.query.metadata.TempCapabilitiesFinder) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Command(org.teiid.query.sql.lang.Command) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) IDGenerator(org.teiid.core.id.IDGenerator) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Aggregations

IDGenerator (org.teiid.core.id.IDGenerator)2 AnalysisRecord (org.teiid.query.analysis.AnalysisRecord)2 TempCapabilitiesFinder (org.teiid.query.metadata.TempCapabilitiesFinder)2 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)2 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)2 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)2 CommandContext (org.teiid.query.util.CommandContext)2 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)1 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 DQPWorkContext (org.teiid.dqp.internal.process.DQPWorkContext)1 PreparedPlan (org.teiid.dqp.internal.process.PreparedPlan)1 Determinism (org.teiid.metadata.FunctionMethod.Determinism)1 Procedure (org.teiid.metadata.Procedure)1 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)1 TempMetadataID (org.teiid.query.metadata.TempMetadataID)1 CapabilitiesFinder (org.teiid.query.optimizer.capabilities.CapabilitiesFinder)1 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)1