Search in sources :

Example 1 with SessionMetadata

use of org.teiid.adminapi.impl.SessionMetadata in project teiid by teiid.

the class SystemFunctionMethods method teiid_session_set.

@TeiidFunction(category = FunctionCategoryConstants.SYSTEM, determinism = Determinism.COMMAND_DETERMINISTIC, pushdown = PushDown.CANNOT_PUSHDOWN)
public static Object teiid_session_set(CommandContext context, String key, Object value) throws FunctionExecutionException {
    SessionMetadata session = context.getSession();
    Map<String, Object> variables = session.getSessionVariables();
    if (variables.size() > MAX_VARIABLES && !variables.containsKey(key)) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID31136, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31136, MAX_VARIABLES));
    }
    return context.setSessionVariable(key, value);
}
Also used : FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata)

Example 2 with SessionMetadata

use of org.teiid.adminapi.impl.SessionMetadata in project teiid by teiid.

the class TestFunctionLibrary method testSessionVariables.

@Test
public void testSessionVariables() throws Exception {
    CommandContext c = new CommandContext();
    c.setSession(new SessionMetadata());
    Object result = helpInvokeMethod("teiid_session_set", new Class<?>[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.OBJECT }, new Object[] { "key", "value" }, c);
    assertNull(result);
    result = helpInvokeMethod("teiid_session_get", new Class<?>[] { DataTypeManager.DefaultDataClasses.STRING }, new Object[] { "key" }, c);
    assertEquals("value", result);
    result = helpInvokeMethod("teiid_session_set", new Class<?>[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.OBJECT }, new Object[] { "key", "value1" }, c);
    assertEquals("value", result);
    result = helpInvokeMethod("teiid_session_get", new Class<?>[] { DataTypeManager.DefaultDataClasses.STRING }, new Object[] { "key" }, c);
    assertEquals("value1", result);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata) Test(org.junit.Test)

Example 3 with SessionMetadata

use of org.teiid.adminapi.impl.SessionMetadata in project teiid by teiid.

the class DQPCore method executeQuery.

/**
 * Execute the given query asynchly. Has a hard limit of only returning max rows fetch size rows.
 * @param command
 * @param vdb
 * @param user
 * @param app
 * @param timeoutInMilli
 * @param engine
 * @param listener
 * @return
 * @throws Throwable
 */
public static ResultsFuture<?> executeQuery(final Object command, final VDBMetaData vdb, final String user, final String app, final long timeoutInMilli, final DQPCore engine, final ResultsListener listener) throws Throwable {
    final SessionMetadata session = TempTableDataManager.createTemporarySession(user, app, vdb);
    final long requestID = 0L;
    DQPWorkContext workContext = new DQPWorkContext();
    if (engine.localProfile != null) {
        workContext.setConnectionProfile(engine.localProfile);
    }
    workContext.setUseCallingThread(true);
    workContext.setSession(session);
    workContext.setAdmin(true);
    final ResultsFuture<Void> resultFuture = new ResultsFuture<Void>();
    resultFuture.addCompletionListener(new ResultsFuture.CompletionListener<Void>() {

        @SuppressWarnings("unchecked")
        @Override
        public void onCompletion(ResultsFuture<Void> future) {
            ResultsFuture<?> response;
            try {
                response = engine.closeRequest(requestID);
                response.addCompletionListener(new ResultsFuture.CompletionListener() {

                    @Override
                    public void onCompletion(ResultsFuture future) {
                        engine.terminateSession(session.getSessionId());
                    }
                });
            } catch (Exception e) {
                engine.terminateSession(session.getSessionId());
            }
        }
    });
    workContext.runInContext(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            RequestMessage request = new RequestMessage();
            if (command instanceof String) {
                request.setCommands((String) command);
            } else {
                request.setCommands(command.toString());
                request.setCommand(command);
            }
            request.setExecutionId(requestID);
            // this would limit the number of rows that are returned.
            request.setRowLimit(engine.getMaxRowsFetchSize());
            ResultsFuture<ResultsMessage> message = engine.executeRequest(requestID, request, timeoutInMilli);
            message.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {

                @Override
                public void onCompletion(ResultsFuture<ResultsMessage> future) {
                    try {
                        ResultsMessage rm = future.get();
                        if (rm.getException() != null) {
                            throw rm.getException();
                        }
                        if (rm.isUpdateResult()) {
                            // $NON-NLS-1$
                            listener.onResults(Arrays.asList("update-count"), rm.getResultsList());
                            resultFuture.getResultsReceiver().receiveResults(null);
                        } else {
                            processResult(rm);
                        }
                    } catch (Exception e) {
                        resultFuture.getResultsReceiver().exceptionOccurred(e);
                    }
                }

                private void processResult(ResultsMessage rm) throws Exception {
                    if (rm.getException() != null) {
                        throw rm.getException();
                    }
                    listener.onResults(Arrays.asList(rm.getColumnNames()), rm.getResultsList());
                    if (rm.getFinalRow() == -1 || rm.getLastRow() < rm.getFinalRow()) {
                        ResultsFuture<ResultsMessage> next = engine.processCursorRequest(requestID, rm.getLastRow() + 1, 1024);
                        next.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {

                            @Override
                            public void onCompletion(ResultsFuture<ResultsMessage> future) {
                                try {
                                    processResult(future.get());
                                } catch (Exception e) {
                                    resultFuture.getResultsReceiver().exceptionOccurred(e);
                                }
                            }
                        });
                    } else {
                        resultFuture.getResultsReceiver().receiveResults(null);
                    }
                }
            });
            return null;
        }
    });
    return resultFuture;
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata) TeiidComponentException(org.teiid.core.TeiidComponentException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) XATransactionException(org.teiid.client.xa.XATransactionException) AdminException(org.teiid.adminapi.AdminException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ResultsFuture(org.teiid.client.util.ResultsFuture) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) RequestMessage(org.teiid.client.RequestMessage)

Example 4 with SessionMetadata

use of org.teiid.adminapi.impl.SessionMetadata in project teiid by teiid.

the class TestWithClauseProcessing method testRecursiveUnion.

@Test
public void testRecursiveUnion() throws Exception {
    // $NON-NLS-1$
    String sql = "WITH t(n) AS ( (VALUES (1) union all values(2)) UNION (SELECT n+1 FROM t WHERE n < 64 union all SELECT e2 from pm1.g1) ) SELECT sum(n) FROM t;";
    List<?>[] expected = new List[] { Arrays.asList(2080l) };
    FakeDataManager dataManager = new FakeDataManager();
    dataManager.setBlockOnce();
    sampleData1(dataManager);
    ProcessorPlan plan = helpGetPlan(helpParse(sql), RealMetadataFactory.example1Cached());
    CommandContext cc = createCommandContext();
    cc.setSession(new SessionMetadata());
    helpProcess(plan, cc, dataManager, expected);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata) List(java.util.List) Test(org.junit.Test)

Example 5 with SessionMetadata

use of org.teiid.adminapi.impl.SessionMetadata in project teiid by teiid.

the class TestWithClauseProcessing method testRecursiveWithPushdownNotFullyPushed.

@Test
public void testRecursiveWithPushdownNotFullyPushed() throws Exception {
    FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    caps.setCapabilitySupport(Capability.COMMON_TABLE_EXPRESSIONS, true);
    // $NON-NLS-1$
    capFinder.addCapabilities("pm1", caps);
    // $NON-NLS-1$
    String sql = "WITH t(n, i) AS ( select 1,2 from pm1.g2 UNION ALL SELECT n+1, e2 FROM t, pm1.g1 WHERE n < 64 and pm1.g1.e2 = t.n ) SELECT * FROM t;";
    HardcodedDataManager dataManager = new HardcodedDataManager(RealMetadataFactory.example1Cached());
    dataManager.addData("SELECT g_0.e2 AS c_0 FROM g1 AS g_0 WHERE g_0.e2 < 64 AND g_0.e2 = 1 ORDER BY c_0", Arrays.asList(1));
    dataManager.addData("SELECT g_0.e2 AS c_0 FROM g1 AS g_0 WHERE g_0.e2 < 64 AND g_0.e2 = 2 ORDER BY c_0");
    dataManager.addData("SELECT 2 FROM g2 AS g_0", Arrays.asList(2));
    ProcessorPlan plan = TestOptimizer.helpPlan(sql, RealMetadataFactory.example1Cached(), null, capFinder, new String[] { "SELECT t.n, t.i FROM t" }, ComparisonMode.EXACT_COMMAND_STRING);
    CommandContext cc = createCommandContext();
    cc.setSession(new SessionMetadata());
    helpProcess(plan, cc, dataManager, new List[] { Arrays.asList(1, 2), Arrays.asList(2, 1) });
    caps.setCapabilitySupport(Capability.RECURSIVE_COMMON_TABLE_EXPRESSIONS, true);
    plan = TestOptimizer.helpPlan(sql, RealMetadataFactory.example1Cached(), null, capFinder, new String[] { "SELECT t.n, t.i FROM t" }, ComparisonMode.EXACT_COMMAND_STRING);
    cc = createCommandContext();
    cc.setSession(new SessionMetadata());
    helpProcess(plan, cc, dataManager, new List[] { Arrays.asList(1, 2), Arrays.asList(2, 1) });
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata) Test(org.junit.Test)

Aggregations

SessionMetadata (org.teiid.adminapi.impl.SessionMetadata)35 Test (org.junit.Test)14 CommandContext (org.teiid.query.util.CommandContext)9 Properties (java.util.Properties)6 InvalidSessionException (org.teiid.client.security.InvalidSessionException)6 DQPWorkContext (org.teiid.dqp.internal.process.DQPWorkContext)5 SessionServiceException (org.teiid.dqp.service.SessionServiceException)5 List (java.util.List)4 LoginException (javax.security.auth.login.LoginException)4 VDBMetaData (org.teiid.adminapi.impl.VDBMetaData)4 LogonException (org.teiid.client.security.LogonException)4 SessionToken (org.teiid.client.security.SessionToken)4 Credentials (org.teiid.security.Credentials)4 ArrayList (java.util.ArrayList)3 Subject (javax.security.auth.Subject)3 TeiidComponentException (org.teiid.core.TeiidComponentException)3 SQLException (java.sql.SQLException)2 LogonResult (org.teiid.client.security.LogonResult)2 CompositeVDB (org.teiid.deployers.CompositeVDB)2 VDBLifeCycleListener (org.teiid.deployers.VDBLifeCycleListener)2