-
Notifications
You must be signed in to change notification settings - Fork 295
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
Add snowflake JDBC Connector #454
Closed
rs-alexis
wants to merge
34
commits into
awslabs:master
from
retentionscience:rl/ise-1728/add_snowflake_drivers
Closed
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
5f2ac29
Add a Protected Getter on the JDBCFactory in JDBCRecordHandler, so ch…
rs-alexis 5f85f9f
Add an intermediate constructor for more flexibility
rs-alexis 9fd5019
Make MySqlQueryStringBuilder constructor public. so that piece of cod…
rs-alexis 645a2cd
Add an intermediate constructor for more flexibility
rs-alexis 347ac93
add snowflake drivers
rs-richardlau bf65adb
add missing case
rs-richardlau ed92b0b
Use MySQL Metadatahandler + Recordhandler for Snowflake
rs-alexis bfb54cf
add logger to ResultSet
rs-richardlau 8577242
remove extra comma
rs-richardlau c521cb9
forgot semicolon
rs-richardlau 0de25a0
use double quotes, not single
rs-richardlau 28e31d7
constant LOGGER instead of class"
rs-richardlau 0631334
try adding a snowflake metadata handler
rs-richardlau f6fd9f5
import snowflake handler
rs-richardlau 116f929
try overriding ResultSet in child Snowflake
rs-richardlau 8ddd852
add imports
rs-richardlau 92fc4ae
try a different table name
rs-richardlau 6f2b3a9
try to upcase strings
rs-richardlau 68af094
Merge branch 'rl/ise-1728/add_snowflake_drivers' of https://github.co…
rs-alexis 7cc03d2
Fix
rs-alexis 2046bb6
No partition, no splits for now
rs-alexis f5141df
Fix
rs-alexis 3756f11
Fix
rs-alexis 2f5c3d1
Update lib
rs-alexis 9c1ac87
Add logger
rs-alexis 71a47be
Add logger
rs-alexis c8596b9
Change partition
rs-alexis 3ad61e1
Fix Snowflake handlers
rs-alexis f789aea
Uppercase everything
rs-alexis d9d9a92
Remove comments
rs-alexis f04497b
Fix lib
rs-alexis 14ad8d0
Fix + Finished Specs
rs-alexis adca384
Merge remote-tracking branch 'origin/master' into ad/ise-1728/add_sno…
rs-alexis e2ae729
Merge remote-tracking branch 'upstream/master' into ad/ise-1728/add_s…
rs-alexis 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 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
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
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
125 changes: 125 additions & 0 deletions
125
...rc/main/java/com/amazonaws/connectors/athena/jdbc/snowflake/SnowflakeMetadataHandler.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,125 @@ | ||
/*- | ||
* #%L | ||
* athena-jdbc | ||
* %% | ||
* Copyright (C) 2019 Amazon Web Services | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package com.amazonaws.connectors.athena.jdbc.snowflake; | ||
|
||
import com.amazonaws.athena.connector.lambda.QueryStatusChecker; | ||
import com.amazonaws.athena.connector.lambda.data.BlockAllocator; | ||
import com.amazonaws.athena.connector.lambda.data.BlockWriter; | ||
import com.amazonaws.athena.connector.lambda.data.SchemaBuilder; | ||
import com.amazonaws.athena.connector.lambda.domain.Split; | ||
import com.amazonaws.athena.connector.lambda.domain.TableName; | ||
import com.amazonaws.athena.connector.lambda.metadata.GetSplitsRequest; | ||
import com.amazonaws.athena.connector.lambda.metadata.GetSplitsResponse; | ||
import com.amazonaws.athena.connector.lambda.metadata.GetTableLayoutRequest; | ||
import com.amazonaws.connectors.athena.jdbc.connection.DatabaseConnectionConfig; | ||
import com.amazonaws.connectors.athena.jdbc.connection.GenericJdbcConnectionFactory; | ||
import com.amazonaws.connectors.athena.jdbc.connection.JdbcConnectionFactory; | ||
import com.amazonaws.connectors.athena.jdbc.manager.JDBCUtil; | ||
import com.amazonaws.connectors.athena.jdbc.manager.JdbcMetadataHandler; | ||
import com.amazonaws.services.athena.AmazonAthena; | ||
import com.amazonaws.services.secretsmanager.AWSSecretsManager; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.DatabaseMetaData; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Handles metadata for Snowflake. | ||
*/ | ||
public class SnowflakeMetadataHandler | ||
extends JdbcMetadataHandler | ||
{ | ||
static final Map<String, String> JDBC_PROPERTIES = ImmutableMap.of("databaseTerm", "SCHEMA"); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(SnowflakeMetadataHandler.class); | ||
|
||
/** | ||
* Instantiates handler to be used by Lambda function directly. | ||
* | ||
* Recommend using {@link com.amazonaws.connectors.athena.jdbc.MultiplexingJdbcCompositeHandler} instead. | ||
*/ | ||
public SnowflakeMetadataHandler() | ||
{ | ||
this(JDBCUtil.getSingleDatabaseConfigFromEnv(JdbcConnectionFactory.DatabaseEngine.SNOWFLAKE)); | ||
} | ||
|
||
public SnowflakeMetadataHandler(final DatabaseConnectionConfig databaseConnectionConfig) | ||
{ | ||
super(databaseConnectionConfig, new GenericJdbcConnectionFactory(databaseConnectionConfig, JDBC_PROPERTIES)); | ||
} | ||
|
||
@VisibleForTesting | ||
protected SnowflakeMetadataHandler(final DatabaseConnectionConfig databaseConnectionConfig, final AWSSecretsManager secretsManager, | ||
final AmazonAthena athena, final JdbcConnectionFactory jdbcConnectionFactory) | ||
{ | ||
super(databaseConnectionConfig, secretsManager, athena, jdbcConnectionFactory); | ||
} | ||
|
||
@Override | ||
protected String escapeNamePattern(final String name, final String escape) | ||
{ | ||
return super.escapeNamePattern(name, escape).toUpperCase(); | ||
} | ||
|
||
@Override | ||
protected ResultSet getColumns(final String catalogName, final TableName tableHandle, final DatabaseMetaData metadata) | ||
throws SQLException | ||
{ | ||
String escape = metadata.getSearchStringEscape(); | ||
return metadata.getColumns( | ||
catalogName, | ||
escapeNamePattern(tableHandle.getSchemaName(), escape), | ||
escapeNamePattern(tableHandle.getTableName(), escape), | ||
null); | ||
} | ||
|
||
@Override | ||
public Schema getPartitionSchema(final String catalogName) | ||
{ | ||
SchemaBuilder schemaBuilder = SchemaBuilder.newBuilder(); | ||
return schemaBuilder.build(); | ||
} | ||
|
||
@Override | ||
public void getPartitions(final BlockWriter blockWriter, final GetTableLayoutRequest getTableLayoutRequest, QueryStatusChecker queryStatusChecker) | ||
{ } | ||
|
||
@Override | ||
public GetSplitsResponse doGetSplits(BlockAllocator blockAllocator, GetSplitsRequest getSplitsRequest) | ||
{ | ||
LOGGER.info("doGetSplits: enter - " + getSplitsRequest); | ||
|
||
String catalogName = getSplitsRequest.getCatalogName(); | ||
Set<Split> splits = new HashSet<>(); | ||
|
||
Split split = Split.newBuilder(makeSpillLocation(getSplitsRequest), makeEncryptionKey()).build(); | ||
splits.add(split); | ||
|
||
LOGGER.info("doGetSplits: exit - " + splits.size()); | ||
return new GetSplitsResponse(catalogName, splits); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...main/java/com/amazonaws/connectors/athena/jdbc/snowflake/SnowflakeQueryStringBuilder.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,62 @@ | ||
/*- | ||
* #%L | ||
* athena-jdbc | ||
* %% | ||
* Copyright (C) 2019 Amazon Web Services | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package com.amazonaws.connectors.athena.jdbc.snowflake; | ||
|
||
import com.amazonaws.athena.connector.lambda.domain.Split; | ||
import com.amazonaws.connectors.athena.jdbc.manager.JdbcSplitQueryBuilder; | ||
import com.google.common.base.Strings; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Extends {@link JdbcSplitQueryBuilder} and implements PostGreSql specific SQL clauses for split. | ||
* | ||
* PostGreSql partitions through child tables that can be used in a FROM clause. | ||
*/ | ||
public class SnowflakeQueryStringBuilder | ||
extends JdbcSplitQueryBuilder | ||
{ | ||
SnowflakeQueryStringBuilder(final String quoteCharacters) | ||
{ | ||
super(quoteCharacters); | ||
} | ||
|
||
@Override | ||
protected String getFromClauseWithSplit(String catalog, String schema, String table, Split split) | ||
{ | ||
StringBuilder tableName = new StringBuilder(); | ||
if (!Strings.isNullOrEmpty(catalog)) { | ||
tableName.append(quote(catalog.toUpperCase())).append('.'); | ||
} | ||
if (!Strings.isNullOrEmpty(schema)) { | ||
tableName.append(quote(schema.toUpperCase())).append('.'); | ||
} | ||
tableName.append(quote(table.toUpperCase())); | ||
|
||
return String.format(" FROM %s ", tableName); | ||
} | ||
|
||
@Override | ||
protected List<String> getPartitionWhereClauses(final Split split) | ||
{ | ||
return Collections.emptyList(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
.../src/main/java/com/amazonaws/connectors/athena/jdbc/snowflake/SnowflakeRecordHandler.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,93 @@ | ||
/*- | ||
* #%L | ||
* athena-jdbc | ||
* %% | ||
* Copyright (C) 2019 Amazon Web Services | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package com.amazonaws.connectors.athena.jdbc.snowflake; | ||
|
||
import com.amazonaws.athena.connector.lambda.domain.Split; | ||
import com.amazonaws.athena.connector.lambda.domain.TableName; | ||
import com.amazonaws.athena.connector.lambda.domain.predicate.Constraints; | ||
import com.amazonaws.connectors.athena.jdbc.connection.DatabaseConnectionConfig; | ||
import com.amazonaws.connectors.athena.jdbc.connection.GenericJdbcConnectionFactory; | ||
import com.amazonaws.connectors.athena.jdbc.connection.JdbcConnectionFactory; | ||
import com.amazonaws.connectors.athena.jdbc.manager.JDBCUtil; | ||
import com.amazonaws.connectors.athena.jdbc.manager.JdbcRecordHandler; | ||
import com.amazonaws.connectors.athena.jdbc.manager.JdbcSplitQueryBuilder; | ||
import com.amazonaws.services.athena.AmazonAthena; | ||
import com.amazonaws.services.athena.AmazonAthenaClientBuilder; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import com.amazonaws.services.secretsmanager.AWSSecretsManager; | ||
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.apache.commons.lang3.Validate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public class SnowflakeRecordHandler | ||
extends JdbcRecordHandler | ||
{ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(SnowflakeRecordHandler.class); | ||
|
||
private static final int FETCH_SIZE = 1000; | ||
|
||
private final JdbcSplitQueryBuilder jdbcSplitQueryBuilder; | ||
|
||
private static final String SNOWFLAKE_QUOTE_CHARACTER = "\""; | ||
|
||
/** | ||
* Instantiates handler to be used by Lambda function directly. | ||
* | ||
* Recommend using {@link com.amazonaws.connectors.athena.jdbc.MultiplexingJdbcCompositeHandler} instead. | ||
*/ | ||
public SnowflakeRecordHandler() | ||
{ | ||
this(JDBCUtil.getSingleDatabaseConfigFromEnv(JdbcConnectionFactory.DatabaseEngine.SNOWFLAKE)); | ||
} | ||
|
||
public SnowflakeRecordHandler(final DatabaseConnectionConfig databaseConnectionConfig) | ||
{ | ||
this(databaseConnectionConfig, AmazonS3ClientBuilder.defaultClient(), AWSSecretsManagerClientBuilder.defaultClient(), AmazonAthenaClientBuilder.defaultClient(), | ||
new GenericJdbcConnectionFactory(databaseConnectionConfig, SnowflakeMetadataHandler.JDBC_PROPERTIES), new SnowflakeQueryStringBuilder(SNOWFLAKE_QUOTE_CHARACTER)); | ||
} | ||
|
||
@VisibleForTesting | ||
SnowflakeRecordHandler(final DatabaseConnectionConfig databaseConnectionConfig, final AmazonS3 amazonS3, final AWSSecretsManager secretsManager, | ||
final AmazonAthena athena, final JdbcConnectionFactory jdbcConnectionFactory, final JdbcSplitQueryBuilder jdbcSplitQueryBuilder) | ||
{ | ||
super(amazonS3, secretsManager, athena, databaseConnectionConfig, jdbcConnectionFactory); | ||
this.jdbcSplitQueryBuilder = Validate.notNull(jdbcSplitQueryBuilder, "query builder must not be null"); | ||
} | ||
|
||
@Override | ||
public PreparedStatement buildSplitSql(Connection jdbcConnection, String catalogName, TableName tableName, Schema schema, Constraints constraints, Split split) | ||
throws SQLException | ||
{ | ||
PreparedStatement preparedStatement = jdbcSplitQueryBuilder.buildSql(jdbcConnection, null, tableName.getSchemaName(), tableName.getTableName(), schema, constraints, split); | ||
|
||
// Disable fetching all rows. | ||
preparedStatement.setFetchSize(FETCH_SIZE); | ||
|
||
return preparedStatement; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does snowflake support any partitioning or parallelism that we can exploit to create multiple splits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the big question, Snowflake supports micro-partitioning but not sure yet how that would fit in the structure of the code yet (https://docs.snowflake.com/en/user-guide/tables-clustering-keys.html)
I'm open to feedback on that