-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,36 @@ | ||
================== | ||
Snowflake Connector | ||
================== | ||
|
||
The Snowflake connector allows querying and creating tables in a 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 Redshift 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/?db=database | ||
connection-user=root | ||
connection-password=secret | ||
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 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 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,89 @@ | ||
<?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>331-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>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.prestosql</groupId> | ||
<artifactId>presto-main</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
85 changes: 85 additions & 0 deletions
85
presto-snowflake/src/main/java/io/prestosql/plugin/snowflake/SnowflakeClient.java
This file contains 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,85 @@ | ||
/* | ||
* 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.PreparedStatement; | ||
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; | ||
|
||
public class SnowflakeClient | ||
extends BaseJdbcClient | ||
{ | ||
private final SnowflakeConfig snowflakeConfig; | ||
|
||
@Inject | ||
public SnowflakeClient(BaseJdbcConfig config, SnowflakeConfig snowflakeConfig, ConnectionFactory connectionFactory) | ||
{ | ||
super(config, "\"", connectionFactory); | ||
this.snowflakeConfig = snowflakeConfig; | ||
} | ||
|
||
@Override | ||
public PreparedStatement getPreparedStatement(Connection connection, String sql) | ||
throws SQLException | ||
{ | ||
PreparedStatement statement = connection.prepareStatement(sql); | ||
statement.setFetchSize(snowflakeConfig.getFetchSize()); | ||
return statement; | ||
} | ||
|
||
@Override | ||
protected Optional<BiFunction<String, Long, String>> limitFunction() | ||
{ | ||
return Optional.of((sql, limit) -> sql + " LIMIT " + limit); | ||
} | ||
|
||
@Override | ||
public boolean isLimitGuaranteed() | ||
{ | ||
return true; | ||
} | ||
|
||
@Override | ||
public WriteMapping toWriteMapping(ConnectorSession session, Type type) | ||
{ | ||
if (TIMESTAMP.equals(type)) { | ||
return WriteMapping.longMapping("timestamp", timestampWriteFunctionUsingSqlTimestamp(session)); | ||
} | ||
|
||
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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
presto-snowflake/src/main/java/io/prestosql/plugin/snowflake/SnowflakeClientModule.java
This file contains 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,55 @@ | ||
/* | ||
* 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.TypeHandlingJdbcConfig; | ||
import io.prestosql.plugin.jdbc.credential.CredentialConfig; | ||
import io.prestosql.plugin.jdbc.credential.CredentialProvider; | ||
import net.snowflake.client.jdbc.SnowflakeDriver; | ||
|
||
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)) | ||
.to(SnowflakeClient.class).in(Scopes.SINGLETON); | ||
|
||
configBinder(binder).bindConfig(BaseJdbcConfig.class); | ||
configBinder(binder).bindConfig(TypeHandlingJdbcConfig.class); | ||
configBinder(binder).bindConfig(SnowflakeConfig.class); | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
@ForBaseJdbc | ||
public ConnectionFactory getConnectionFactory(BaseJdbcConfig config, CredentialProvider credentialProvider) | ||
{ | ||
return new DriverConnectionFactory(new SnowflakeDriver(), config, credentialProvider); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
presto-snowflake/src/main/java/io/prestosql/plugin/snowflake/SnowflakeConfig.java
This file contains 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,37 @@ | ||
/* | ||
* 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 io.airlift.configuration.LegacyConfig; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
public class SnowflakeConfig | ||
{ | ||
private int fetchSize = 1000; | ||
|
||
@NotNull | ||
public int getFetchSize() | ||
{ | ||
return fetchSize; | ||
} | ||
|
||
@Config("snowflake.fetch-size") | ||
public SnowflakeConfig setFetchSize(Integer fetchSize) | ||
{ | ||
this.fetchSize = fetchSize; | ||
return this; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
presto-snowflake/src/main/java/io/prestosql/plugin/snowflake/SnowflakePlugin.java
This file contains 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,25 @@ | ||
/* | ||
* 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.JdbcPlugin; | ||
|
||
public class SnowflakePlugin | ||
extends JdbcPlugin | ||
{ | ||
public SnowflakePlugin() | ||
{ | ||
super("snowflake", new SnowflakeClientModule()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
presto-snowflake/src/test/java/io/prestosql/plugin/snowflake/TestSnowflakePlugin.java
This file contains 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,33 @@ | ||
/* | ||
* 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.common.collect.ImmutableMap; | ||
import io.prestosql.spi.Plugin; | ||
import io.prestosql.spi.connector.ConnectorFactory; | ||
import io.prestosql.testing.TestingConnectorContext; | ||
import org.testng.annotations.Test; | ||
|
||
import static com.google.common.collect.Iterables.getOnlyElement; | ||
|
||
public class TestSnowflakePlugin | ||
{ | ||
@Test | ||
public void testCreateConnector() | ||
{ | ||
Plugin plugin = new SnowflakePlugin(); | ||
ConnectorFactory factory = getOnlyElement(plugin.getConnectorFactories()); | ||
factory.create("test", ImmutableMap.of("connection-url", "test"), new TestingConnectorContext()); | ||
} | ||
} |