Search in sources :

Example 1 with ClientSession

use of com.facebook.presto.client.ClientSession in project presto by prestodb.

the class PrestoConnection method startQuery.

StatementClient startQuery(String sql) {
    String source = firstNonNull(clientInfo.get("ApplicationName"), "presto-jdbc");
    ClientSession session = new ClientSession(httpUri, user, source, clientInfo.get("ClientInfo"), catalog.get(), schema.get(), timeZoneId.get(), locale.get(), ImmutableMap.copyOf(sessionProperties), transactionId.get(), false, new Duration(2, MINUTES));
    return queryExecutor.startQuery(session, sql);
}
Also used : ClientSession(com.facebook.presto.client.ClientSession) Duration(io.airlift.units.Duration)

Example 2 with ClientSession

use of com.facebook.presto.client.ClientSession in project presto by prestodb.

the class Console method process.

private static void process(QueryRunner queryRunner, String sql, OutputFormat outputFormat, boolean interactive) {
    try (Query query = queryRunner.startQuery(sql)) {
        query.renderOutput(System.out, outputFormat, interactive);
        ClientSession session = queryRunner.getSession();
        // update session properties if present
        if (!query.getSetSessionProperties().isEmpty() || !query.getResetSessionProperties().isEmpty()) {
            Map<String, String> sessionProperties = new HashMap<>(session.getProperties());
            sessionProperties.putAll(query.getSetSessionProperties());
            sessionProperties.keySet().removeAll(query.getResetSessionProperties());
            session = withProperties(session, sessionProperties);
        }
        // update prepared statements if present
        if (!query.getAddedPreparedStatements().isEmpty() || !query.getDeallocatedPreparedStatements().isEmpty()) {
            Map<String, String> preparedStatements = new HashMap<>(session.getPreparedStatements());
            preparedStatements.putAll(query.getAddedPreparedStatements());
            preparedStatements.keySet().removeAll(query.getDeallocatedPreparedStatements());
            session = withPreparedStatements(session, preparedStatements);
        }
        // update transaction ID if necessary
        if (query.isClearTransactionId()) {
            session = stripTransactionId(session);
        }
        if (query.getStartedTransactionId() != null) {
            session = withTransactionId(session, query.getStartedTransactionId());
        }
        queryRunner.setSession(session);
    } catch (RuntimeException e) {
        System.err.println("Error running command: " + e.getMessage());
        if (queryRunner.getSession().isDebug()) {
            e.printStackTrace();
        }
    }
}
Also used : HashMap(java.util.HashMap) ClientSession(com.facebook.presto.client.ClientSession)

Example 3 with ClientSession

use of com.facebook.presto.client.ClientSession in project presto by prestodb.

the class TestClientOptions method testUpdateSessionParameters.

@Test
public void testUpdateSessionParameters() throws Exception {
    ClientOptions options = new ClientOptions();
    ClientSession session = options.toClientSession();
    SqlParser sqlParser = new SqlParser();
    ImmutableMap<String, String> existingProperties = ImmutableMap.of("query_max_memory", "10GB", "distributed_join", "true");
    ImmutableMap<String, String> preparedStatements = ImmutableMap.of("my_query", "select * from foo");
    session = Console.processSessionParameterChange(sqlParser.createStatement("USE test_catalog.test_schema"), session, existingProperties, preparedStatements);
    assertEquals(session.getCatalog(), "test_catalog");
    assertEquals(session.getSchema(), "test_schema");
    assertEquals(session.getProperties().get("query_max_memory"), "10GB");
    assertEquals(session.getProperties().get("distributed_join"), "true");
    assertEquals(session.getPreparedStatements().get("my_query"), "select * from foo");
    session = Console.processSessionParameterChange(sqlParser.createStatement("USE test_schema_b"), session, existingProperties, preparedStatements);
    assertEquals(session.getCatalog(), "test_catalog");
    assertEquals(session.getSchema(), "test_schema_b");
    assertEquals(session.getProperties().get("query_max_memory"), "10GB");
    assertEquals(session.getProperties().get("distributed_join"), "true");
    assertEquals(session.getPreparedStatements().get("my_query"), "select * from foo");
    session = Console.processSessionParameterChange(sqlParser.createStatement("USE test_catalog_2.test_schema"), session, existingProperties, preparedStatements);
    assertEquals(session.getCatalog(), "test_catalog_2");
    assertEquals(session.getSchema(), "test_schema");
    assertEquals(session.getProperties().get("query_max_memory"), "10GB");
    assertEquals(session.getProperties().get("distributed_join"), "true");
    assertEquals(session.getPreparedStatements().get("my_query"), "select * from foo");
}
Also used : ClientSession(com.facebook.presto.client.ClientSession) SqlParser(com.facebook.presto.sql.parser.SqlParser) Test(org.testng.annotations.Test)

Example 4 with ClientSession

use of com.facebook.presto.client.ClientSession in project presto by prestodb.

the class TestTableNameCompleter method testAutoCompleteWithoutSchema.

@Test
public void testAutoCompleteWithoutSchema() {
    ClientSession session = new ClientOptions().toClientSession();
    QueryRunner runner = QueryRunner.create(session, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), false, null);
    TableNameCompleter completer = new TableNameCompleter(runner);
    assertEquals(completer.complete("SELECT is_infi", 14, ImmutableList.of()), 7);
}
Also used : ClientSession(com.facebook.presto.client.ClientSession) Test(org.testng.annotations.Test)

Example 5 with ClientSession

use of com.facebook.presto.client.ClientSession in project presto by prestodb.

the class TestClientOptions method testDefault.

@Test
public void testDefault() {
    ClientSession session = new ClientOptions().toClientSession();
    assertEquals(session.getServer().toString(), "http://localhost:8080");
    assertEquals(session.getSource(), "presto-cli");
}
Also used : ClientSession(com.facebook.presto.client.ClientSession) Test(org.testng.annotations.Test)

Aggregations

ClientSession (com.facebook.presto.client.ClientSession)18 Test (org.testng.annotations.Test)8 Duration (io.airlift.units.Duration)4 HashMap (java.util.HashMap)4 StatementClient (com.facebook.presto.client.StatementClient)2 StatementClientFactory.newStatementClient (com.facebook.presto.client.StatementClientFactory.newStatementClient)2 QueryId (com.facebook.presto.spi.QueryId)2 File (java.io.File)2 IOException (java.io.IOException)2 Level (com.facebook.airlift.log.Level)1 Logging (com.facebook.airlift.log.Logging)1 LoggingConfiguration (com.facebook.airlift.log.LoggingConfiguration)1 QueryPreprocessor.preprocessQuery (com.facebook.presto.cli.QueryPreprocessor.preprocessQuery)1 QueryError (com.facebook.presto.client.QueryError)1 QueryStatusInfo (com.facebook.presto.client.QueryStatusInfo)1 SelectedRole (com.facebook.presto.spi.security.SelectedRole)1 ResourceEstimates (com.facebook.presto.spi.session.ResourceEstimates)1 SqlParser (com.facebook.presto.sql.parser.SqlParser)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1