Search in sources :

Example 1 with RelationalPlanner

use of org.teiid.query.optimizer.relational.RelationalPlanner in project teiid by teiid.

the class TestRuleAccessPatternValidation method helpPlan.

/**
 * Parses and resolves the command, creates a canonical relational plan,
 * and runs some of the optimizer rules, ending with the
 * RuleChooseAccessPattern.
 * @param command String command to parse, resolve and use for planning
 * @param rules empty RuleStack
 * @param groups Collection to add parsed and resolved GroupSymbols to
 * @return the root PlanNode of the query plan
 */
private PlanNode helpPlan(String command) throws Exception {
    Command query = QueryParser.getQueryParser().parseCommand(command);
    QueryResolver.resolveCommand(query, METADATA);
    // Generate canonical plan
    RelationalPlanner p = new RelationalPlanner();
    p.initialize(query, null, METADATA, FINDER, null, new CommandContext());
    PlanNode planNode = p.generatePlan(query);
    RelationalPlanner planner = new RelationalPlanner();
    final RuleStack rules = planner.buildRules();
    PlanNode testPlan = helpExecuteRules(rules, planNode, METADATA, DEBUG);
    return testPlan;
}
Also used : RelationalPlanner(org.teiid.query.optimizer.relational.RelationalPlanner) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) CommandContext(org.teiid.query.util.CommandContext) Command(org.teiid.query.sql.lang.Command) RuleStack(org.teiid.query.optimizer.relational.RuleStack)

Example 2 with RelationalPlanner

use of org.teiid.query.optimizer.relational.RelationalPlanner in project teiid by teiid.

the class TestRulePushSelectCriteria method testPushAcrossFrameWithAccessNode.

@Test
public void testPushAcrossFrameWithAccessNode() throws Exception {
    QueryMetadataInterface metadata = new TempMetadataAdapter(RealMetadataFactory.example1Cached(), new TempMetadataStore());
    // $NON-NLS-1$
    Command command = TestOptimizer.helpGetCommand("select * from (select * from pm1.g1 union select * from pm1.g2) x where e1 = 1", metadata, null);
    // $NON-NLS-1$
    Command subCommand = TestOptimizer.helpGetCommand("select * from pm1.g1 union select * from pm1.g2", metadata, null);
    RelationalPlanner p = new RelationalPlanner();
    CommandContext cc = new CommandContext();
    p.initialize(command, null, metadata, null, null, cc);
    PlanNode root = p.generatePlan(command);
    PlanNode child = p.generatePlan(subCommand);
    PlanNode sourceNode = NodeEditor.findNodePreOrder(root, NodeConstants.Types.SOURCE);
    sourceNode.addFirstChild(child);
    sourceNode.setProperty(NodeConstants.Info.SYMBOL_MAP, SymbolMap.createSymbolMap(sourceNode.getGroups().iterator().next(), (List<Expression>) child.getFirstChild().getProperty(Info.PROJECT_COLS), metadata));
    // add a dummy access node
    PlanNode accessNode = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    accessNode.addGroups(child.getFirstChild().getGroups());
    child.getFirstChild().addAsParent(accessNode);
    new RulePushSelectCriteria().execute(root, metadata, new DefaultCapabilitiesFinder(), new RuleStack(), AnalysisRecord.createNonRecordingRecord(), cc);
    // the select node should still be above the access node
    accessNode = NodeEditor.findNodePreOrder(root, NodeConstants.Types.ACCESS);
    assertEquals(NodeConstants.Types.SELECT, accessNode.getParent().getType());
    assertNull(NodeEditor.findNodePreOrder(accessNode, NodeConstants.Types.SELECT));
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) RelationalPlanner(org.teiid.query.optimizer.relational.RelationalPlanner) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) CommandContext(org.teiid.query.util.CommandContext) Command(org.teiid.query.sql.lang.Command) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) RuleStack(org.teiid.query.optimizer.relational.RuleStack) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) Test(org.junit.Test)

Example 3 with RelationalPlanner

use of org.teiid.query.optimizer.relational.RelationalPlanner 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)

Aggregations

RelationalPlanner (org.teiid.query.optimizer.relational.RelationalPlanner)3 CommandContext (org.teiid.query.util.CommandContext)3 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)2 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)2 RuleStack (org.teiid.query.optimizer.relational.RuleStack)2 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)2 Command (org.teiid.query.sql.lang.Command)2 List (java.util.List)1 Test (org.junit.Test)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 IDGenerator (org.teiid.core.id.IDGenerator)1 PreparedPlan (org.teiid.dqp.internal.process.PreparedPlan)1 Determinism (org.teiid.metadata.FunctionMethod.Determinism)1 Procedure (org.teiid.metadata.Procedure)1 AnalysisRecord (org.teiid.query.analysis.AnalysisRecord)1 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)1 TempCapabilitiesFinder (org.teiid.query.metadata.TempCapabilitiesFinder)1