Skip to content

Commit

Permalink
Add Snowflake JDBC Connector
Browse files Browse the repository at this point in the history
  • Loading branch information
pgagnon committed Oct 21, 2020
1 parent 5ad74ea commit 8357007
Show file tree
Hide file tree
Showing 11 changed files with 458 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<module>presto-server-main</module>
<module>presto-server-rpm</module>
<module>presto-session-property-managers</module>
<module>presto-snowflake</module>
<module>presto-spi</module>
<module>presto-sqlserver</module>
<module>presto-teradata-functions</module>
Expand Down
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/connector.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ from different data sources.
Prometheus <connector/prometheus>
Redis <connector/redis>
Redshift <connector/redshift>
Snowflake <connector/snowflake>
SQL Server <connector/sqlserver>
System <connector/system>
Thrift <connector/thrift>
Expand Down
42 changes: 42 additions & 0 deletions presto-docs/src/main/sphinx/connector/snowflake.rst
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.
6 changes: 6 additions & 0 deletions presto-server/src/main/provisio/presto.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,10 @@
<unpack />
</artifact>
</artifactSet>

<artifactSet to="plugin/snowflake">
<artifact id="${project.groupId}:presto-snowflake:zip:${project.version}">
<unpack />
</artifact>
</artifactSet>
</runtime>
90 changes: 90 additions & 0 deletions presto-snowflake/pom.xml
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>
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);
}
}
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))
.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);
}
}
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;
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;
}
}
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());
}
}

0 comments on commit 8357007

Please sign in to comment.