Search in sources :

Example 1 with PrestoConnection

use of com.facebook.presto.jdbc.PrestoConnection in project presto by prestodb.

the class JdbcTests method shouldSetLocale.

@Test(groups = JDBC)
public void shouldSetLocale() throws SQLException {
    if (usingPrestoJdbcDriver(connection())) {
        ((PrestoConnection) connection()).setLocale(CHINESE);
        try (Statement statement = connection().createStatement()) {
            QueryResult result = queryResult(statement, "SELECT date_format(TIMESTAMP '2001-01-09 09:04', '%M')");
            assertThat(result).contains(row("一月"));
        }
    } else {
        LOGGER.warn("shouldSetLocale() only applies to PrestoJdbcDriver");
    }
}
Also used : QueryResult(io.prestodb.tempto.query.QueryResult) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) Test(org.testng.annotations.Test) ProductTest(io.prestodb.tempto.ProductTest)

Example 2 with PrestoConnection

use of com.facebook.presto.jdbc.PrestoConnection in project presto by prestodb.

the class JdbcPrestoAction method executeOnce.

private <T> T executeOnce(Statement statement, QueryStage queryStage, StatementExecutor<T> statementExecutor) {
    String query = formatSql(statement, Optional.empty());
    String clientInfo = new ClientInfo(testId, testName, verificationContext.getSourceQueryName(), verificationContext.getSuite()).serialize();
    try (PrestoConnection connection = getConnection(queryStage, clientInfo)) {
        try (java.sql.Statement jdbcStatement = connection.createStatement()) {
            PrestoStatement prestoStatement = jdbcStatement.unwrap(PrestoStatement.class);
            prestoStatement.setProgressMonitor(statementExecutor.getProgressMonitor());
            return statementExecutor.execute(prestoStatement, query);
        }
    } catch (SQLException e) {
        throw exceptionClassifier.createException(queryStage, statementExecutor.getProgressMonitor().getLastQueryStats(), e);
    }
}
Also used : PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) SQLException(java.sql.SQLException) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection)

Example 3 with PrestoConnection

use of com.facebook.presto.jdbc.PrestoConnection in project presto by prestodb.

the class JdbcPrestoAction method getConnection.

private PrestoConnection getConnection() throws SQLException {
    PrestoConnection connection = DriverManager.getConnection(jdbcUrl, "user", null).unwrap(PrestoConnection.class);
    try {
        connection.setClientInfo("ApplicationName", "benchmark-test");
        connection.setCatalog(benchmarkQuery.getCatalog());
        connection.setSchema(benchmarkQuery.getSchema());
    } catch (SQLClientInfoException ignored) {
    // Do nothing
    }
    Map<String, String> sessionProperties = ImmutableMap.<String, String>builder().putAll(benchmarkQuery.getSessionProperties()).put(QUERY_MAX_EXECUTION_TIME, queryTimeout.toString()).build();
    for (Map.Entry<String, String> entry : sessionProperties.entrySet()) {
        connection.setSessionProperty(entry.getKey(), entry.getValue());
    }
    return connection;
}
Also used : SQLClientInfoException(java.sql.SQLClientInfoException) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 4 with PrestoConnection

use of com.facebook.presto.jdbc.PrestoConnection in project presto by prestodb.

the class JdbcPrestoAction method executeOnce.

private <R> QueryResult<R> executeOnce(Statement statement, Optional<ResultSetConverter<R>> converter) {
    String query = formatSql(statement, Optional.empty());
    ProgressMonitor progressMonitor = new ProgressMonitor();
    try (PrestoConnection connection = getConnection()) {
        try (java.sql.Statement jdbcStatement = connection.createStatement()) {
            PrestoStatement prestoStatement = jdbcStatement.unwrap(PrestoStatement.class);
            prestoStatement.setProgressMonitor(progressMonitor);
            ImmutableList.Builder<R> rows = ImmutableList.builder();
            if (converter.isPresent()) {
                try (ResultSet resultSet = jdbcStatement.executeQuery(query)) {
                    while (resultSet.next()) {
                        rows.add(converter.get().apply(resultSet));
                    }
                }
            } else {
                boolean moreResults = jdbcStatement.execute(query);
                if (moreResults) {
                    consumeResultSet(jdbcStatement.getResultSet());
                }
                do {
                    moreResults = jdbcStatement.getMoreResults();
                    if (moreResults) {
                        consumeResultSet(jdbcStatement.getResultSet());
                    }
                } while (moreResults || jdbcStatement.getUpdateCount() != -1);
            }
            checkState(progressMonitor.getLastQueryStats().isPresent(), "lastQueryStats is missing");
            return new QueryResult<>(rows.build(), progressMonitor.getLastQueryStats().get());
        }
    } catch (SQLException e) {
        throw exceptionClassifier.createException(progressMonitor.getLastQueryStats(), e);
    }
}
Also used : SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) QueryResult(com.facebook.presto.benchmark.framework.QueryResult) ResultSet(java.sql.ResultSet)

Example 5 with PrestoConnection

use of com.facebook.presto.jdbc.PrestoConnection in project presto by prestodb.

the class JdbcTests method shouldSetTimezone.

@Test(groups = JDBC)
public void shouldSetTimezone() throws SQLException {
    String timeZoneId = "Indian/Kerguelen";
    if (usingPrestoJdbcDriver(connection())) {
        ((PrestoConnection) connection()).setTimeZoneId(timeZoneId);
        assertConnectionTimezone(connection(), timeZoneId);
    } else {
        String prestoJdbcURLTestTimeZone;
        String testTimeZone = "TimeZoneID=" + timeZoneId + ";";
        if (prestoJdbcURL.contains("TimeZoneID=")) {
            prestoJdbcURLTestTimeZone = prestoJdbcURL.replaceFirst("TimeZoneID=[\\w/]*;", testTimeZone);
        } else {
            prestoJdbcURLTestTimeZone = prestoJdbcURL + ";" + testTimeZone;
        }
        Connection testConnection = DriverManager.getConnection(prestoJdbcURLTestTimeZone, prestoJdbcUser, prestoJdbcPassword);
        assertConnectionTimezone(testConnection, timeZoneId);
    }
}
Also used : Connection(java.sql.Connection) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) Test(org.testng.annotations.Test) ProductTest(io.prestodb.tempto.ProductTest)

Aggregations

PrestoConnection (com.facebook.presto.jdbc.PrestoConnection)7 PrestoStatement (com.facebook.presto.jdbc.PrestoStatement)2 ProductTest (io.prestodb.tempto.ProductTest)2 SQLClientInfoException (java.sql.SQLClientInfoException)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Test (org.testng.annotations.Test)2 QueryResult (com.facebook.presto.benchmark.framework.QueryResult)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 QueryResult (io.prestodb.tempto.query.QueryResult)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Map (java.util.Map)1