/
TestJdbcConnection.java
386 lines (337 loc) · 14.1 KB
/
TestJdbcConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.jdbc;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import com.google.inject.Scopes;
import io.airlift.log.Logging;
import io.prestosql.plugin.hive.HiveHadoop2Plugin;
import io.prestosql.server.testing.TestingPrestoServer;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.connector.ConnectorTableMetadata;
import io.prestosql.spi.connector.ConnectorTransactionHandle;
import io.prestosql.spi.connector.InMemoryRecordSet;
import io.prestosql.spi.connector.RecordCursor;
import io.prestosql.spi.connector.SchemaTableName;
import io.prestosql.spi.connector.SystemTable;
import io.prestosql.spi.predicate.TupleDomain;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Set;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static io.prestosql.jdbc.TestPrestoDriver.closeQuietly;
import static io.prestosql.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder;
import static io.prestosql.spi.connector.SystemTable.Distribution.ALL_NODES;
import static io.prestosql.spi.type.VarcharType.createUnboundedVarcharType;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
public class TestJdbcConnection
{
private TestingPrestoServer server;
@BeforeClass
public void setupServer()
throws Exception
{
Logging.initialize();
Module systemTables = binder -> newSetBinder(binder, SystemTable.class)
.addBinding().to(ExtraCredentialsSystemTable.class).in(Scopes.SINGLETON);
server = new TestingPrestoServer(ImmutableList.of(systemTables));
server.installPlugin(new HiveHadoop2Plugin());
server.createCatalog("hive", "hive-hadoop2", ImmutableMap.<String, String>builder()
.put("hive.metastore", "file")
.put("hive.metastore.catalog.dir", server.getBaseDataDir().resolve("hive").toAbsolutePath().toString())
.put("hive.security", "sql-standard")
.build());
try (Connection connection = createConnection();
Statement statement = connection.createStatement()) {
statement.execute("SET ROLE admin");
statement.execute("CREATE SCHEMA default");
statement.execute("CREATE SCHEMA fruit");
}
}
@AfterClass(alwaysRun = true)
public void tearDownServer()
{
closeQuietly(server);
}
@Test
public void testAutocommit()
throws SQLException
{
try (Connection connection = createConnection()) {
assertTrue(connection.getAutoCommit());
connection.setAutoCommit(false);
assertFalse(connection.getAutoCommit());
connection.setAutoCommit(true);
assertTrue(connection.getAutoCommit());
}
}
@Test
public void testCommit()
throws SQLException
{
try (Connection connection = createConnection()) {
connection.setAutoCommit(false);
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_commit (x bigint)");
}
try (Connection otherConnection = createConnection()) {
assertThat(listTables(otherConnection)).doesNotContain("test_commit");
}
connection.commit();
}
try (Connection connection = createConnection()) {
assertThat(listTables(connection)).contains("test_commit");
}
}
@Test
public void testImmediateCommit()
throws SQLException
{
try (Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.commit();
}
}
@Test
public void testRollback()
throws SQLException
{
try (Connection connection = createConnection()) {
connection.setAutoCommit(false);
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_rollback (x bigint)");
}
try (Connection otherConnection = createConnection()) {
assertThat(listTables(otherConnection)).doesNotContain("test_rollback");
}
connection.rollback();
}
try (Connection connection = createConnection()) {
assertThat(listTables(connection)).doesNotContain("test_rollback");
}
}
@Test
public void testImmediateRollback()
throws SQLException
{
try (Connection connection = createConnection()) {
connection.setAutoCommit(false);
connection.rollback();
}
}
@Test
public void testUse()
throws SQLException
{
try (Connection connection = createConnection()) {
assertThat(connection.getCatalog()).isEqualTo("hive");
assertThat(connection.getSchema()).isEqualTo("default");
// change schema
try (Statement statement = connection.createStatement()) {
statement.execute("USE fruit");
}
assertThat(connection.getCatalog()).isEqualTo("hive");
assertThat(connection.getSchema()).isEqualTo("fruit");
// change catalog and schema
try (Statement statement = connection.createStatement()) {
statement.execute("USE system.runtime");
}
assertThat(connection.getCatalog()).isEqualTo("system");
assertThat(connection.getSchema()).isEqualTo("runtime");
// run multiple queries
assertThat(listTables(connection)).contains("nodes");
assertThat(listTables(connection)).contains("queries");
assertThat(listTables(connection)).contains("tasks");
}
}
@Test
public void testSession()
throws SQLException
{
try (Connection connection = createConnection()) {
assertThat(listSession(connection))
.contains("join_distribution_type|AUTOMATIC|AUTOMATIC")
.contains("exchange_compression|false|false");
try (Statement statement = connection.createStatement()) {
statement.execute("SET SESSION join_distribution_type = 'BROADCAST'");
}
assertThat(listSession(connection))
.contains("join_distribution_type|BROADCAST|AUTOMATIC")
.contains("exchange_compression|false|false");
try (Statement statement = connection.createStatement()) {
statement.execute("SET SESSION exchange_compression = true");
}
assertThat(listSession(connection))
.contains("join_distribution_type|BROADCAST|AUTOMATIC")
.contains("exchange_compression|true|false");
try (Statement statement = connection.createStatement()) {
// setting Hive session properties requires the admin role
statement.execute("SET ROLE admin");
}
for (String part : ImmutableList.of(",", "=", ":", "|", "/", "\\", "'", "\\'", "''", "\"", "\\\"", "[", "]")) {
String value = format("/tmp/presto-%s-${USER}", part);
try {
try (Statement statement = connection.createStatement()) {
statement.execute(format("SET SESSION hive.temporary_staging_directory_path = '%s'", value.replace("'", "''")));
}
assertThat(listSession(connection))
.contains("join_distribution_type|BROADCAST|AUTOMATIC")
.contains("exchange_compression|true|false")
.contains(format("hive.temporary_staging_directory_path|%s|/tmp/presto-${USER}", value));
}
catch (Exception e) {
fail(format("Failed to set session property value to [%s]", value), e);
}
}
}
}
@Test
public void testApplicationName()
throws SQLException
{
try (Connection connection = createConnection()) {
assertConnectionSource(connection, "presto-jdbc");
}
try (Connection connection = createConnection()) {
connection.setClientInfo("ApplicationName", "testing");
assertConnectionSource(connection, "testing");
}
try (Connection connection = createConnection("applicationNamePrefix=fruit:")) {
assertConnectionSource(connection, "fruit:");
}
try (Connection connection = createConnection("applicationNamePrefix=fruit:")) {
connection.setClientInfo("ApplicationName", "testing");
assertConnectionSource(connection, "fruit:testing");
}
}
@Test
public void testExtraCredentials()
throws SQLException
{
try (Connection connection = createConnection("extraCredentials=test.token.foo:bar;test.token.abc:xyz")) {
Map<String, String> expectedCredentials = ImmutableMap.of("test.token.foo", "bar", "test.token.abc", "xyz");
PrestoConnection prestoConnection = connection.unwrap(PrestoConnection.class);
assertEquals(prestoConnection.getExtraCredentials(), expectedCredentials);
assertEquals(listExtraCredentials(connection), expectedCredentials);
}
}
private Connection createConnection()
throws SQLException
{
return createConnection("");
}
private Connection createConnection(String extra)
throws SQLException
{
String url = format("jdbc:presto://%s/hive/default?%s", server.getAddress(), extra);
return DriverManager.getConnection(url, "admin", null);
}
private static Set<String> listTables(Connection connection)
throws SQLException
{
ImmutableSet.Builder<String> set = ImmutableSet.builder();
try (Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SHOW TABLES")) {
while (rs.next()) {
set.add(rs.getString(1));
}
}
return set.build();
}
private static Set<String> listSession(Connection connection)
throws SQLException
{
ImmutableSet.Builder<String> set = ImmutableSet.builder();
try (Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SHOW SESSION")) {
while (rs.next()) {
set.add(Joiner.on('|').join(
rs.getString(1),
rs.getString(2),
rs.getString(3)));
}
}
return set.build();
}
private static Map<String, String> listExtraCredentials(Connection connection)
throws SQLException
{
ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM system.test.extra_credentials");
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
while (rs.next()) {
builder.put(rs.getString("name"), rs.getString("value"));
}
return builder.build();
}
private static void assertConnectionSource(Connection connection, String expectedSource)
throws SQLException
{
String queryId;
try (Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT 123")) {
queryId = rs.unwrap(PrestoResultSet.class).getQueryId();
}
try (PreparedStatement statement = connection.prepareStatement(
"SELECT source FROM system.runtime.queries WHERE query_id = ?")) {
statement.setString(1, queryId);
try (ResultSet rs = statement.executeQuery()) {
assertTrue(rs.next());
assertThat(rs.getString("source")).isEqualTo(expectedSource);
assertFalse(rs.next());
}
}
}
private static class ExtraCredentialsSystemTable
implements SystemTable
{
private static final SchemaTableName NAME = new SchemaTableName("test", "extra_credentials");
public static final ConnectorTableMetadata METADATA = tableMetadataBuilder(NAME)
.column("name", createUnboundedVarcharType())
.column("value", createUnboundedVarcharType())
.build();
@Override
public Distribution getDistribution()
{
return ALL_NODES;
}
@Override
public ConnectorTableMetadata getTableMetadata()
{
return METADATA;
}
@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint)
{
InMemoryRecordSet.Builder table = InMemoryRecordSet.builder(METADATA);
session.getIdentity().getExtraCredentials().forEach(table::addRow);
return table.build().cursor();
}
}
}