Search in sources :

Example 6 with PreparedPlan

use of org.teiid.dqp.internal.process.PreparedPlan in project teiid by teiid.

the class CommandContext method getPlan.

public PreparedPlan getPlan(String key) {
    if (this.globalState.planCache == null) {
        return null;
    }
    CacheID id = new CacheID(new ParseInfo(), key, getVdbName(), getVdbVersion(), getConnectionId(), getUserName());
    PreparedPlan pp = this.globalState.planCache.get(id);
    if (pp != null) {
        if (id.getSessionId() != null) {
            setDeterminismLevel(Determinism.USER_DETERMINISTIC);
        } else if (id.getUserName() != null) {
            setDeterminismLevel(Determinism.SESSION_DETERMINISTIC);
        }
        return pp;
    }
    return null;
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan) ParseInfo(org.teiid.query.parser.ParseInfo)

Example 7 with PreparedPlan

use of org.teiid.dqp.internal.process.PreparedPlan in project teiid by teiid.

the class EmbeddedServer method start.

public synchronized void start(@SuppressWarnings("hiding") EmbeddedConfiguration config) {
    if (running != null) {
        throw new IllegalStateException();
    }
    this.dqp.setLocalProfile(this.embeddedProfile);
    this.shutdownListener.setBootInProgress(true);
    this.config = config;
    System.setProperty("jboss.node.name", config.getNodeName() == null ? "localhost" : config.getNodeName());
    this.cmr.setProvider(this);
    this.eventDistributorFactoryService = new EmbeddedEventDistributorFactoryService();
    this.eventDistributorFactoryService.start();
    this.dqp.setEventDistributor(this.eventDistributorFactoryService.getReplicatedEventDistributor());
    // $NON-NLS-1$
    this.scheduler = Executors.newScheduledThreadPool(config.getMaxAsyncThreads(), new NamedThreadFactory("Asynch Worker"));
    this.replicator = config.getObjectReplicator();
    if (this.replicator == null && config.getJgroupsConfigFile() != null) {
        channelFactory = new SimpleChannelFactory(config);
        this.replicator = new JGroupsObjectReplicator(channelFactory, this.scheduler);
        try {
            this.nodeTracker = new NodeTracker(channelFactory.createChannel("teiid-node-tracker"), config.getNodeName()) {

                @Override
                public ScheduledExecutorService getScheduledExecutorService() {
                    return scheduler;
                }
            };
        } catch (Exception e) {
            LogManager.logError(LogConstants.CTX_RUNTIME, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40089));
        }
    }
    this.eventDistributorFactoryService = new EmbeddedEventDistributorFactoryService();
    // must be called after the replicator is set
    this.eventDistributorFactoryService.start();
    this.dqp.setEventDistributor(this.eventDistributorFactoryService.getReplicatedEventDistributor());
    if (config.getTransactionManager() == null) {
        LogManager.logInfo(LogConstants.CTX_RUNTIME, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40089));
        this.transactionService.setTransactionManager((TransactionManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { TransactionManager.class }, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                throw new UnsupportedOperationException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40089));
            }
        }));
    } else {
        this.transactionService.setDetectTransactions(true);
        this.transactionService.setTransactionManager(config.getTransactionManager());
    }
    if (config.getSecurityHelper() != null) {
        this.sessionService.setSecurityHelper(config.getSecurityHelper());
    } else {
        this.sessionService.setSecurityHelper(new DoNothingSecurityHelper());
    }
    if (config.getSecurityDomain() != null) {
        this.sessionService.setSecurityDomain(config.getSecurityDomain());
    } else {
        // $NON-NLS-1$
        this.sessionService.setSecurityDomain("teiid-security");
    }
    this.sessionService.setVDBRepository(repo);
    setBufferManagerProperties(config);
    BufferService bs = getBufferService();
    this.dqp.setBufferManager(bs.getBufferManager());
    startVDBRepository();
    // $NON-NLS-1$
    rs = new SessionAwareCache<CachedResults>("resultset", config.getCacheFactory(), SessionAwareCache.Type.RESULTSET, config.getMaxResultSetCacheStaleness());
    // $NON-NLS-1$
    ppc = new SessionAwareCache<PreparedPlan>("preparedplan", config.getCacheFactory(), SessionAwareCache.Type.PREPAREDPLAN, 0);
    rs.setTupleBufferCache(bs.getTupleBufferCache());
    this.dqp.setResultsetCache(rs);
    ppc.setTupleBufferCache(bs.getTupleBufferCache());
    this.dqp.setPreparedPlanCache(ppc);
    this.dqp.setTransactionService((TransactionService) LogManager.createLoggingProxy(LogConstants.CTX_TXN_LOG, this.transactionService, new Class[] { TransactionService.class }, MessageLevel.DETAIL, Thread.currentThread().getContextClassLoader()));
    this.dqp.start(config);
    this.sessionService.setDqp(this.dqp);
    this.services.setSecurityHelper(this.sessionService.getSecurityHelper());
    if (this.config.getAuthenticationType() != null) {
        this.services.setAuthenticationType(this.config.getAuthenticationType());
        this.sessionService.setAuthenticationType(this.config.getAuthenticationType());
    }
    this.sessionService.start();
    this.services.setVDBRepository(this.repo);
    this.materializationMgr = getMaterializationManager();
    this.repo.addListener(this.materializationMgr);
    this.repo.setAllowEnvFunction(this.config.isAllowEnvFunction());
    if (this.nodeTracker != null) {
        this.nodeTracker.addNodeListener(this.materializationMgr);
    }
    this.logon = new LogonImpl(sessionService, null);
    services.registerClientService(ILogon.class, logon, LogConstants.CTX_SECURITY);
    DQP dqpProxy = DQP.class.cast(Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { DQP.class }, new SessionCheckingProxy(dqp, LogConstants.CTX_DQP, MessageLevel.TRACE)));
    services.registerClientService(DQP.class, dqpProxy, LogConstants.CTX_DQP);
    initDriver();
    List<SocketConfiguration> transports = config.getTransports();
    if (transports != null && !transports.isEmpty()) {
        for (SocketConfiguration socketConfig : transports) {
            SocketListener socketConnection = startTransport(socketConfig, bs.getBufferManager(), config.getMaxODBCLobSizeAllowed());
            if (socketConfig.getSSLConfiguration() != null) {
                try {
                    socketConfig.getSSLConfiguration().getServerSSLEngine();
                } catch (Exception e) {
                    throw new TeiidRuntimeException(e);
                }
            }
            this.transports.add(socketConnection);
        }
    }
    this.shutdownListener.setBootInProgress(false);
    this.shutdownListener.started();
    running = true;
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) JGroupsObjectReplicator(org.teiid.replication.jgroups.JGroupsObjectReplicator) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DQP(org.teiid.client.DQP) NamedThreadFactory(org.teiid.core.util.NamedThreadFactory) BufferService(org.teiid.dqp.service.BufferService) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) ConnectorManagerException(org.teiid.dqp.internal.datamgr.ConnectorManagerRepository.ConnectorManagerException) InvalidSessionException(org.teiid.client.security.InvalidSessionException) XMLStreamException(javax.xml.stream.XMLStreamException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) ConnectionException(org.teiid.net.ConnectionException) SQLException(java.sql.SQLException) TranslatorException(org.teiid.translator.TranslatorException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) URISyntaxException(java.net.URISyntaxException) TeiidException(org.teiid.core.TeiidException) SAXException(org.xml.sax.SAXException) VirtualDatabaseException(org.teiid.deployers.VirtualDatabaseException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) CachedResults(org.teiid.dqp.internal.process.CachedResults) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan)

Example 8 with PreparedPlan

use of org.teiid.dqp.internal.process.PreparedPlan 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 9 with PreparedPlan

use of org.teiid.dqp.internal.process.PreparedPlan in project teiid by teiid.

the class TestProcessor method createCommandContext.

public static CommandContext createCommandContext() {
    Properties props = new Properties();
    // $NON-NLS-1$ //$NON-NLS-2$
    props.setProperty("soap_host", "my.host.com");
    // $NON-NLS-1$ //$NON-NLS-2$
    props.setProperty("soap_port", "12345");
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    CommandContext context = new CommandContext("test", "user", null, "myvdb", 1, DEBUG);
    context.setProcessorBatchSize(BufferManager.DEFAULT_PROCESSOR_BATCH_SIZE);
    context.setBufferManager(BufferManagerFactory.getStandaloneBufferManager());
    context.setPreparedPlanCache(new SessionAwareCache<PreparedPlan>("preparedplan", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.PREPAREDPLAN, 0));
    return context;
}
Also used : CommandContext(org.teiid.query.util.CommandContext) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan)

Example 10 with PreparedPlan

use of org.teiid.dqp.internal.process.PreparedPlan in project teiid by teiid.

the class TestFunctionPushdown method testMustPushdownOverGrouping.

@Test
public void testMustPushdownOverGrouping() throws Exception {
    TransformationMetadata tm = RealMetadataFactory.fromDDL("create foreign function func (param integer) returns integer; create foreign table g1 (e1 integer)", "x", "y");
    BasicSourceCapabilities bsc = new BasicSourceCapabilities();
    bsc.setCapabilitySupport(Capability.SELECT_WITHOUT_FROM, true);
    bsc.setCapabilitySupport(Capability.QUERY_SELECT_EXPRESSION, true);
    final DefaultCapabilitiesFinder capFinder = new DefaultCapabilitiesFinder(bsc);
    CommandContext cc = TestProcessor.createCommandContext();
    cc.setQueryProcessorFactory(new QueryProcessor.ProcessorFactory() {

        @Override
        public PreparedPlan getPreparedPlan(String query, String recursionGroup, CommandContext commandContext, QueryMetadataInterface metadata) throws TeiidProcessingException, TeiidComponentException {
            return null;
        }

        @Override
        public CapabilitiesFinder getCapabiltiesFinder() {
            return capFinder;
        }

        @Override
        public QueryProcessor createQueryProcessor(String query, String recursionGroup, CommandContext commandContext, Object... params) throws TeiidProcessingException, TeiidComponentException {
            // TODO Auto-generated method stub
            return null;
        }
    });
    cc.setMetadata(tm);
    // $NON-NLS-1$
    String sql = "select func(e1) from g1 group by e1";
    ProcessorPlan plan = helpPlan(sql, tm, null, capFinder, new String[] { "SELECT y.g1.e1 FROM y.g1" }, // $NON-NLS-1$
    ComparisonMode.EXACT_COMMAND_STRING);
    HardcodedDataManager dataManager = new HardcodedDataManager();
    dataManager.addData("SELECT y.g1.e1 FROM y.g1", new List[] { Arrays.asList(1), Arrays.asList(2) });
    dataManager.addData("SELECT func(1)", new List[] { Arrays.asList(2) });
    dataManager.addData("SELECT func(2)", new List[] { Arrays.asList(3) });
    TestProcessor.helpProcess(plan, cc, dataManager, new List[] { Arrays.asList(2), Arrays.asList(3) });
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) TeiidProcessingException(org.teiid.core.TeiidProcessingException) FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Test(org.junit.Test)

Aggregations

PreparedPlan (org.teiid.dqp.internal.process.PreparedPlan)11 CommandContext (org.teiid.query.util.CommandContext)8 TeiidProcessingException (org.teiid.core.TeiidProcessingException)6 Test (org.junit.Test)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)5 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)5 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)5 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)4 CapabilitiesFinder (org.teiid.query.optimizer.capabilities.CapabilitiesFinder)4 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 NamedThreadFactory (org.teiid.core.util.NamedThreadFactory)2 CachedResults (org.teiid.dqp.internal.process.CachedResults)2 SessionAwareCache (org.teiid.dqp.internal.process.SessionAwareCache)2 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1