-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
PostgreSqlQueryRunner.java
101 lines (85 loc) · 3.68 KB
/
PostgreSqlQueryRunner.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
/*
* 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.trino.plugin.postgresql;
import com.google.common.collect.ImmutableMap;
import io.airlift.log.Logger;
import io.airlift.log.Logging;
import io.trino.Session;
import io.trino.plugin.jmx.JmxPlugin;
import io.trino.plugin.tpch.TpchPlugin;
import io.trino.testing.DistributedQueryRunner;
import io.trino.tpch.TpchTable;
import java.util.HashMap;
import java.util.Map;
import static io.airlift.testing.Closeables.closeAllSuppress;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
import static io.trino.testing.QueryAssertions.copyTpchTables;
import static io.trino.testing.TestingSession.testSessionBuilder;
public final class PostgreSqlQueryRunner
{
private PostgreSqlQueryRunner() {}
private static final String TPCH_SCHEMA = "tpch";
public static DistributedQueryRunner createPostgreSqlQueryRunner(
TestingPostgreSqlServer server,
Map<String, String> extraProperties,
Map<String, String> connectorProperties,
Iterable<TpchTable<?>> tables)
throws Exception
{
DistributedQueryRunner queryRunner = null;
try {
queryRunner = DistributedQueryRunner.builder(createSession())
.setExtraProperties(extraProperties)
.build();
queryRunner.installPlugin(new TpchPlugin());
queryRunner.createCatalog("tpch", "tpch");
connectorProperties = new HashMap<>(ImmutableMap.copyOf(connectorProperties));
connectorProperties.putIfAbsent("connection-url", server.getJdbcUrl());
connectorProperties.putIfAbsent("connection-user", server.getUser());
connectorProperties.putIfAbsent("connection-password", server.getPassword());
connectorProperties.putIfAbsent("allow-drop-table", "true");
connectorProperties.putIfAbsent("postgresql.include-system-tables", "true");
queryRunner.installPlugin(new PostgreSqlPlugin());
queryRunner.createCatalog("postgresql", "postgresql", connectorProperties);
copyTpchTables(queryRunner, "tpch", TINY_SCHEMA_NAME, createSession(), tables);
return queryRunner;
}
catch (Throwable e) {
closeAllSuppress(e, queryRunner, server);
throw e;
}
}
public static Session createSession()
{
return testSessionBuilder()
.setCatalog("postgresql")
.setSchema(TPCH_SCHEMA)
.build();
}
public static void main(String[] args)
throws Exception
{
Logging.initialize();
DistributedQueryRunner queryRunner = createPostgreSqlQueryRunner(
new TestingPostgreSqlServer(),
ImmutableMap.of("http-server.http.port", "8080"),
ImmutableMap.of(),
TpchTable.getTables());
queryRunner.installPlugin(new JmxPlugin());
queryRunner.createCatalog("jmx", "jmx");
Logger log = Logger.get(PostgreSqlQueryRunner.class);
log.info("======== SERVER STARTED ========");
log.info("\n====\n%s\n====", queryRunner.getCoordinator().getBaseUrl());
}
}