Skip to content

Commit 9296fc3

Browse files
Add connection properties for Druid connector
1 parent 5d545c0 commit 9296fc3

File tree

5 files changed

+154
-6
lines changed

5 files changed

+154
-6
lines changed

docs/src/main/sphinx/connector/druid.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ connection-user=root
4545
connection-password=secret
4646
```
4747

48+
Additionally, the following configuration properties can be set depending on the use-case:
49+
50+
:::{list-table} Druid configuration properties
51+
:widths: 30, 55, 15
52+
:header-rows: 1
53+
54+
* - Property name
55+
- Description
56+
- Default
57+
* - `druid.execution-timeout`
58+
- Query timeout for druid queries, beyond which unfinished queries will be cancelled.
59+
0 timeout means no timeout (up to the server-side maximum query timeout,
60+
`druid.server.http.maxQueryTimeout`)
61+
example: `10s`, `1m`, `1000ms`.
62+
- None.
63+
* - `druid.sql-timezone`
64+
- Time zone for a connection. For example, "America/Los_Angeles" or an offset
65+
like "-08:00". This parameter affects how time functions and timestamp literals behave.
66+
- `UTC`
67+
:::
68+
4869
Now you can access your Druid database in Trino with the `example` catalog
4970
name from the properties file.
5071

plugin/trino-druid/pom.xml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
<classifier>classes</classifier>
2727
</dependency>
2828

29+
<dependency>
30+
<groupId>io.airlift</groupId>
31+
<artifactId>configuration</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>io.airlift</groupId>
36+
<artifactId>units</artifactId>
37+
</dependency>
38+
2939
<dependency>
3040
<groupId>io.trino</groupId>
3141
<artifactId>trino-base-jdbc</artifactId>
@@ -116,14 +126,14 @@
116126
</dependency>
117127

118128
<dependency>
119-
<groupId>io.airlift</groupId>
120-
<artifactId>units</artifactId>
121-
<scope>runtime</scope>
129+
<groupId>com.squareup.okhttp3</groupId>
130+
<artifactId>okhttp-jvm</artifactId>
131+
<scope>test</scope>
122132
</dependency>
123133

124134
<dependency>
125-
<groupId>com.squareup.okhttp3</groupId>
126-
<artifactId>okhttp-jvm</artifactId>
135+
<groupId>io.airlift</groupId>
136+
<artifactId>configuration-testing</artifactId>
127137
<scope>test</scope>
128138
</dependency>
129139

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.plugin.druid;
15+
16+
import io.airlift.configuration.Config;
17+
import io.airlift.units.Duration;
18+
19+
public class DruidConfig
20+
{
21+
private Duration executionTimeout; // milliseconds
22+
23+
private String sqlTimeZone;
24+
25+
@Config("druid.execution-timeout")
26+
public DruidConfig setExecutionTimeout(Duration executionTimeout)
27+
{
28+
this.executionTimeout = executionTimeout;
29+
return this;
30+
}
31+
32+
public Duration getExecutionTimeout()
33+
{
34+
return executionTimeout;
35+
}
36+
37+
@Config("druid.sql-timezone")
38+
public DruidConfig setSqlTimeZone(String sqlTimeZone)
39+
{
40+
this.sqlTimeZone = sqlTimeZone;
41+
return this;
42+
}
43+
44+
public String getSqlTimeZone()
45+
{
46+
return sqlTimeZone;
47+
}
48+
}

plugin/trino-druid/src/main/java/io/trino/plugin/druid/DruidJdbcClientModule.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package io.trino.plugin.druid;
1515

16+
import com.google.common.base.Strings;
1617
import com.google.inject.Binder;
1718
import com.google.inject.Module;
1819
import com.google.inject.Provides;
@@ -29,7 +30,10 @@
2930
import io.trino.spi.function.table.ConnectorTableFunction;
3031
import org.apache.calcite.avatica.remote.Driver;
3132

33+
import java.util.Properties;
34+
3235
import static com.google.inject.multibindings.Multibinder.newSetBinder;
36+
import static io.airlift.configuration.ConfigBinder.configBinder;
3337

3438
public class DruidJdbcClientModule
3539
implements Module
@@ -38,16 +42,30 @@ public class DruidJdbcClientModule
3842
public void configure(Binder binder)
3943
{
4044
binder.bind(JdbcClient.class).annotatedWith(ForBaseJdbc.class).to(DruidJdbcClient.class).in(Scopes.SINGLETON);
45+
configBinder(binder).bindConfig(DruidConfig.class);
4146
newSetBinder(binder, ConnectorTableFunction.class).addBinding().toProvider(Query.class).in(Scopes.SINGLETON);
4247
}
4348

4449
@Provides
4550
@Singleton
4651
@ForBaseJdbc
47-
public static ConnectionFactory createConnectionFactory(BaseJdbcConfig config, CredentialProvider credentialProvider, OpenTelemetry openTelemetry)
52+
public static ConnectionFactory createConnectionFactory(BaseJdbcConfig config, CredentialProvider credentialProvider, DruidConfig druidConfig, OpenTelemetry openTelemetry)
4853
{
4954
return DriverConnectionFactory.builder(new Driver(), config.getConnectionUrl(), credentialProvider)
55+
.setConnectionProperties(getConnectionProperties(druidConfig))
5056
.setOpenTelemetry(openTelemetry)
5157
.build();
5258
}
59+
60+
private static Properties getConnectionProperties(DruidConfig druidConfig)
61+
{
62+
Properties connectionProperties = new Properties();
63+
if (druidConfig.getExecutionTimeout() != null) {
64+
connectionProperties.setProperty("timeout", String.valueOf(druidConfig.getExecutionTimeout().toMillis()));
65+
}
66+
if (!Strings.isNullOrEmpty(druidConfig.getSqlTimeZone())) {
67+
connectionProperties.setProperty("sqlTimeZone", druidConfig.getSqlTimeZone());
68+
}
69+
return connectionProperties;
70+
}
5371
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.plugin.druid;
15+
16+
import com.google.common.collect.ImmutableMap;
17+
import io.airlift.units.Duration;
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.util.Map;
21+
22+
import static io.airlift.configuration.testing.ConfigAssertions.assertFullMapping;
23+
import static io.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults;
24+
import static io.airlift.configuration.testing.ConfigAssertions.recordDefaults;
25+
import static java.util.concurrent.TimeUnit.SECONDS;
26+
27+
class TestDruidConfig
28+
{
29+
@Test
30+
void testDefaults()
31+
{
32+
assertRecordedDefaults(recordDefaults(DruidConfig.class)
33+
.setExecutionTimeout(null)
34+
.setSqlTimeZone(null));
35+
}
36+
37+
@Test
38+
void testExplicitPropertyMappings()
39+
{
40+
Map<String, String> properties = ImmutableMap.<String, String>builder()
41+
.put("druid.execution-timeout", "10s")
42+
.put("druid.sql-timezone", "UTC")
43+
.buildOrThrow();
44+
45+
DruidConfig expected = new DruidConfig()
46+
.setExecutionTimeout(new Duration(10, SECONDS))
47+
.setSqlTimeZone("UTC");
48+
49+
assertFullMapping(properties, expected);
50+
}
51+
}

0 commit comments

Comments
 (0)