-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add Snowflake JDBC Connector #2551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| =================== | ||
| Snowflake Connector | ||
| =================== | ||
|
|
||
| The Snowflake connector allows querying and creating tables in an | ||
| external Snowflake account. This can be used to join data between | ||
| different systems like Snowflake and Hive, or between two different | ||
| Snowflake accounts. | ||
|
|
||
| Configuration | ||
| ------------- | ||
|
|
||
| To configure the Snowflake connector, create a catalog properties file | ||
| in ``etc/catalog`` named, for example, ``snowflake.properties``, to | ||
| mount the Snowflake connector as the ``snowflake`` catalog. | ||
| Create the file with the following contents, replacing the | ||
| connection properties as appropriate for your setup: | ||
|
|
||
| .. code-block:: none | ||
|
|
||
| connector.name=snowflake | ||
| connection-url=jdbc:snowflake://<account>.snowflakecomputing.com | ||
| connection-user=root | ||
| connection-password=secret | ||
| snowflake.account=account | ||
| snowflake.database=database | ||
| snowflake.role=role | ||
| snowflake.warehouse=warehouse | ||
|
|
||
|
|
||
| Multiple Snowflake Databases or Accounts | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| The Snowflake connector can only access a single database within | ||
| a Snowflake account. Thus, if you have multiple Snowflake databases, | ||
| or want to connect to multiple Snowflake accounts, you must configure | ||
| multiple instances of the Snowflake connector. | ||
|
|
||
| To add another catalog, simply add another properties file to ``etc/catalog`` | ||
| with a different name, making sure it ends in ``.properties``. For example, | ||
| if you name the property file ``sales.properties``, Presto creates a | ||
| catalog named ``sales`` using the configured connector. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <?xml version="1.0"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>io.prestosql</groupId> | ||
| <artifactId>presto-root</artifactId> | ||
| <version>345-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>presto-snowflake</artifactId> | ||
| <description>Presto - Snowflake Connector</description> | ||
| <packaging>presto-plugin</packaging> | ||
|
|
||
| <properties> | ||
| <air.main.basedir>${project.parent.basedir}</air.main.basedir> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.prestosql</groupId> | ||
| <artifactId>presto-base-jdbc</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>io.airlift</groupId> | ||
| <artifactId>configuration</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.google.guava</groupId> | ||
| <artifactId>guava</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.google.inject</groupId> | ||
| <artifactId>guice</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>javax.inject</groupId> | ||
| <artifactId>javax.inject</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>net.snowflake</groupId> | ||
| <artifactId>snowflake-jdbc</artifactId> | ||
| <version>3.10.0</version> | ||
| </dependency> | ||
|
|
||
| <!-- Presto SPI --> | ||
| <dependency> | ||
| <groupId>io.prestosql</groupId> | ||
| <artifactId>presto-spi</artifactId> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>io.airlift</groupId> | ||
| <artifactId>slice</artifactId> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-annotations</artifactId> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.openjdk.jol</groupId> | ||
| <artifactId>jol-core</artifactId> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <!-- for testing --> | ||
|
|
||
| <dependency> | ||
| <groupId>io.prestosql</groupId> | ||
| <artifactId>presto-main</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.testng</groupId> | ||
| <artifactId>testng</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
72 changes: 72 additions & 0 deletions
72
presto-snowflake/src/main/java/io/prestosql/plugin/snowflake/SnowflakeClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * 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.plugin.snowflake; | ||
|
|
||
| import io.prestosql.plugin.jdbc.BaseJdbcClient; | ||
| import io.prestosql.plugin.jdbc.BaseJdbcConfig; | ||
| import io.prestosql.plugin.jdbc.ConnectionFactory; | ||
| import io.prestosql.plugin.jdbc.JdbcIdentity; | ||
| import io.prestosql.plugin.jdbc.JdbcSplit; | ||
| import io.prestosql.plugin.jdbc.WriteMapping; | ||
| import io.prestosql.spi.connector.ConnectorSession; | ||
| import io.prestosql.spi.type.Type; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.util.Optional; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| import static io.prestosql.plugin.jdbc.StandardColumnMappings.timestampWriteFunctionUsingSqlTimestamp; | ||
| import static io.prestosql.spi.type.TimestampType.TIMESTAMP_NANOS; | ||
|
|
||
| public class SnowflakeClient | ||
| extends BaseJdbcClient | ||
| { | ||
| @Inject | ||
| public SnowflakeClient(BaseJdbcConfig config, ConnectionFactory connectionFactory) | ||
| { | ||
| super(config, "\"", connectionFactory); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isLimitGuaranteed(ConnectorSession session) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected Optional<BiFunction<String, Long, String>> limitFunction() | ||
| { | ||
| return Optional.of((sql, limit) -> sql + " LIMIT " + limit); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteMapping toWriteMapping(ConnectorSession session, Type type) | ||
| { | ||
| if (TIMESTAMP_NANOS.equals(type)) { | ||
| return WriteMapping.longMapping("timestamp", timestampWriteFunctionUsingSqlTimestamp(TIMESTAMP_NANOS)); | ||
| } | ||
|
|
||
| return super.toWriteMapping(session, type); | ||
| } | ||
|
|
||
| // Required since SnowflakeConnectionV1 does not support setReadOnly | ||
| @Override | ||
| public Connection getConnection(JdbcIdentity identity, JdbcSplit split) throws SQLException | ||
| { | ||
| return connectionFactory.openConnection(identity); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
presto-snowflake/src/main/java/io/prestosql/plugin/snowflake/SnowflakeClientModule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * 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.plugin.snowflake; | ||
|
|
||
| import com.google.inject.Binder; | ||
| import com.google.inject.Key; | ||
| import com.google.inject.Module; | ||
| import com.google.inject.Provides; | ||
| import com.google.inject.Scopes; | ||
| import com.google.inject.Singleton; | ||
| import io.prestosql.plugin.jdbc.BaseJdbcConfig; | ||
| import io.prestosql.plugin.jdbc.ConnectionFactory; | ||
| import io.prestosql.plugin.jdbc.DriverConnectionFactory; | ||
| import io.prestosql.plugin.jdbc.ForBaseJdbc; | ||
| import io.prestosql.plugin.jdbc.JdbcClient; | ||
| import io.prestosql.plugin.jdbc.credential.CredentialProvider; | ||
| import net.snowflake.client.jdbc.SnowflakeDriver; | ||
|
|
||
| import java.util.Properties; | ||
|
|
||
| import static io.airlift.configuration.ConfigBinder.configBinder; | ||
|
|
||
| public class SnowflakeClientModule | ||
| implements Module | ||
| { | ||
| @Override | ||
| public void configure(Binder binder) | ||
| { | ||
| binder.bind(Key.get(JdbcClient.class, ForBaseJdbc.class)) | ||
pgagnon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .to(SnowflakeClient.class).in(Scopes.SINGLETON); | ||
|
|
||
| configBinder(binder).bindConfig(SnowflakeConfig.class); | ||
| } | ||
|
|
||
| @Singleton | ||
| @Provides | ||
| @ForBaseJdbc | ||
| public ConnectionFactory getConnectionFactory(BaseJdbcConfig baseJdbcConfig, SnowflakeConfig snowflakeConfig, CredentialProvider credentialProvider) | ||
| { | ||
| Properties properties = new Properties(); | ||
| snowflakeConfig.getAccount().ifPresent(account -> properties.setProperty("account", account)); | ||
| snowflakeConfig.getDatabase().ifPresent(database -> properties.setProperty("db", database)); | ||
| snowflakeConfig.getRole().ifPresent(role -> properties.setProperty("role", role)); | ||
| snowflakeConfig.getWarehouse().ifPresent(warehouse -> properties.setProperty("warehouse", warehouse)); | ||
|
|
||
| return new DriverConnectionFactory(new SnowflakeDriver(), baseJdbcConfig.getConnectionUrl(), properties, credentialProvider); | ||
| } | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
presto-snowflake/src/main/java/io/prestosql/plugin/snowflake/SnowflakeConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * 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.plugin.snowflake; | ||
|
|
||
| import io.airlift.configuration.Config; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public class SnowflakeConfig | ||
| { | ||
| private String account; | ||
pgagnon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private String database; | ||
| private String role; | ||
| private String warehouse; | ||
|
|
||
| public Optional<String> getAccount() | ||
| { | ||
| return Optional.ofNullable(account); | ||
| } | ||
|
|
||
| @Config("snowflake.account") | ||
| public SnowflakeConfig setAccount(String account) | ||
| { | ||
| this.account = account; | ||
| return this; | ||
| } | ||
|
|
||
| public Optional<String> getDatabase() | ||
| { | ||
| return Optional.ofNullable(database); | ||
| } | ||
|
|
||
| @Config("snowflake.database") | ||
| public SnowflakeConfig setDatabase(String database) | ||
| { | ||
| this.database = database; | ||
| return this; | ||
| } | ||
|
|
||
| public Optional<String> getRole() | ||
| { | ||
| return Optional.ofNullable(role); | ||
| } | ||
|
|
||
| @Config("snowflake.role") | ||
| public SnowflakeConfig setRole(String role) | ||
| { | ||
| this.role = role; | ||
| return this; | ||
| } | ||
|
|
||
| public Optional<String> getWarehouse() | ||
| { | ||
| return Optional.ofNullable(warehouse); | ||
| } | ||
|
|
||
| @Config("snowflake.warehouse") | ||
| public SnowflakeConfig setWarehouse(String warehouse) | ||
| { | ||
| this.warehouse = warehouse; | ||
| return this; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.