Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5ad32b0
Initial read-only Snowflake Catalog implementation by @sfc-gh-mparmar…
sfc-gh-dhuo Dec 13, 2022
930a3f0
Add JdbcSnowflakeClientTest using mocks (#2)
dennishuo Dec 14, 2022
86b3d11
Merge branch 'main' into snowflake-catalog-initial
dennishuo Dec 14, 2022
076a14a
Add test { useJUnitPlatform() } tuple to iceberg-snowflake for
dennishuo Dec 16, 2022
a7b5aa7
Extract versions into versions.props per PR review
dennishuo Dec 17, 2022
dd5255c
Misc test-related refactors per review suggestions
dennishuo Dec 17, 2022
500b36b
Fix unsupported behaviors of loadNamedpaceMetadata and defaultWarehou…
dennishuo Dec 17, 2022
ad2c55f
Move TableIdentifier checks out of newTableOps into the
dennishuo Dec 17, 2022
7f13674
Refactor out any Namespace-related business logic from the lower
dennishuo Dec 17, 2022
58d258e
Finish migrating JdbcSnowflakeClientTest off any usage of org.junit.A…
dennishuo Dec 17, 2022
0183129
Style refactorings from review comments, expanded and moved InMemoryF…
dennishuo Dec 20, 2022
ca6deab
Fix behavior of getNamespaceMetadata to throw when the namespace doesn't
dennishuo Dec 21, 2022
b3a2842
Move private constructor to top, add assertion to test case.
dennishuo Dec 21, 2022
676d024
Define minimal ResultSetParser/QueryHarness classes to fully replace
dennishuo Dec 21, 2022
cc493d0
Update snowflake/src/main/java/org/apache/iceberg/snowflake/Snowflake…
dennishuo Dec 23, 2022
ce7e28c
Refactor style suggestions; remove debug-level logging, arguments in …
dennishuo Dec 23, 2022
2729e64
Fix precondition messages, remove getConf()
dennishuo Jan 6, 2023
4c1e79f
Clean up varargs.
dennishuo Jan 7, 2023
bc0c6ee
Make data members final, include rawJsonVal in toString for debuggabi…
dennishuo Jan 7, 2023
8240d9e
Merge branch 'main' into snowflake-catalog-initial
dennishuo Jan 9, 2023
ebe5dd6
Combine some small test cases into roundtrip test cases, misc cleanup
dennishuo Jan 10, 2023
9e9b9e6
Add comment for why a factory class is exposed for testing purposes.
dennishuo Jan 13, 2023
e8fee31
Merge remote-tracking branch 'origin/main' into snowflake-catalog-ini…
dennishuo Jan 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ ALIYUN:
GCP:
- gcp/**/*
DELL:
- dell/**/*
- dell/**/*
SNOWFLAKE:
- snowflake/**/*
20 changes: 20 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,26 @@ project(':iceberg-dell') {
}
}

project(':iceberg-snowflake') {
test {
useJUnitPlatform()
}

dependencies {
implementation project(':iceberg-core')
implementation project(':iceberg-common')
implementation project(path: ':iceberg-bundled-guava', configuration: 'shadow')
implementation project(':iceberg-aws')
Comment thread
danielcweeks marked this conversation as resolved.
Outdated
implementation "com.fasterxml.jackson.core:jackson-databind"
implementation "com.fasterxml.jackson.core:jackson-core"
implementation "commons-dbutils:commons-dbutils:1.7"

runtimeOnly("net.snowflake:snowflake-jdbc:3.13.22")
Comment thread
danielcweeks marked this conversation as resolved.
Outdated

testImplementation project(path: ':iceberg-core', configuration: 'testArtifacts')
}
}

@Memoized
boolean versionFileExists() {
return file('version.txt').exists()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.ClientPoolImpl;

class JdbcClientPool extends ClientPoolImpl<Connection, SQLException> {
public class JdbcClientPool extends ClientPoolImpl<Connection, SQLException> {

private final String dbUrl;
private final Map<String, String> properties;

JdbcClientPool(String dbUrl, Map<String, String> props) {
public JdbcClientPool(String dbUrl, Map<String, String> props) {
this(
Integer.parseInt(
props.getOrDefault(
Expand All @@ -42,7 +42,7 @@ class JdbcClientPool extends ClientPoolImpl<Connection, SQLException> {
props);
}

JdbcClientPool(int poolSize, String dbUrl, Map<String, String> props) {
public JdbcClientPool(int poolSize, String dbUrl, Map<String, String> props) {
super(poolSize, SQLNonTransientConnectionException.class, true);
properties = props;
this.dbUrl = dbUrl;
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include 'hive-metastore'
include 'nessie'
include 'gcp'
include 'dell'
include 'snowflake'

project(':api').name = 'iceberg-api'
project(':common').name = 'iceberg-common'
Expand All @@ -51,6 +52,7 @@ project(':hive-metastore').name = 'iceberg-hive-metastore'
project(':nessie').name = 'iceberg-nessie'
project(':gcp').name = 'iceberg-gcp'
project(':dell').name = 'iceberg-dell'
project(':snowflake').name = 'iceberg-snowflake'

if (null != System.getProperty("allVersions")) {
System.setProperty("flinkVersions", System.getProperty("knownFlinkVersions"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.iceberg.snowflake;

import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.jdbc.JdbcClientPool;
import org.apache.iceberg.jdbc.UncheckedInterruptedException;
import org.apache.iceberg.jdbc.UncheckedSQLException;
import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
import org.apache.iceberg.snowflake.entities.SnowflakeSchema;
import org.apache.iceberg.snowflake.entities.SnowflakeTable;
import org.apache.iceberg.snowflake.entities.SnowflakeTableMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This implementation of SnowflakeClient builds on top of Snowflake's JDBC driver to interact with
* Snowflake's Iceberg-aware resource model. Despite using JDBC libraries, the resource model is
* derived from Snowflake's own first-class support for Iceberg tables as opposed to using an opaque
* JDBC layer to store Iceberg metadata itself in an Iceberg-agnostic database.
*
* <p>This thus differs from the JdbcCatalog in that Snowflake's service provides the source of
Comment thread
danielcweeks marked this conversation as resolved.
Outdated
* truth of Iceberg metadata, rather than serving as a storage layer for a client-defined Iceberg
* resource model.
*/
public class JdbcSnowflakeClient implements SnowflakeClient {
Comment thread
danielcweeks marked this conversation as resolved.
Outdated
public static final String EXPECTED_JDBC_IMPL = "net.snowflake.client.jdbc.SnowflakeDriver";
Comment thread
danielcweeks marked this conversation as resolved.
Outdated

private static final Logger LOG = LoggerFactory.getLogger(JdbcSnowflakeClient.class);
Comment thread
danielcweeks marked this conversation as resolved.
Outdated
private final JdbcClientPool connectionPool;
private QueryRunner queryRunner = new QueryRunner(true);
Comment thread
danielcweeks marked this conversation as resolved.
Outdated

JdbcSnowflakeClient(JdbcClientPool conn) {
connectionPool = conn;
Comment thread
danielcweeks marked this conversation as resolved.
}

@VisibleForTesting
void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}

@Override
public List<SnowflakeSchema> listSchemas(Namespace namespace) {
StringBuilder baseQuery = new StringBuilder("SHOW SCHEMAS");
Object[] queryParams = null;
if (namespace == null || namespace.isEmpty()) {
// for empty or null namespace search for all schemas at account level where the user
// has access to list.
baseQuery.append(" IN ACCOUNT");
} else {
// otherwise restrict listing of schema within the database.
baseQuery.append(" IN DATABASE IDENTIFIER(?)");
queryParams = new Object[] {namespace.level(SnowflakeResources.NAMESPACE_DB_LEVEL - 1)};
}

final String finalQuery = baseQuery.toString();
final Object[] finalQueryParams = queryParams;
List<SnowflakeSchema> schemas;
try {
schemas =
connectionPool.run(
conn ->
queryRunner.query(
conn, finalQuery, SnowflakeSchema.createHandler(), finalQueryParams));
} catch (SQLException e) {
throw new UncheckedSQLException(
e,
"Failed to list schemas for namespace %s",
namespace != null ? namespace.toString() : "");
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e, "Interrupted while listing schemas");
}
return schemas;
}

@Override
public List<SnowflakeTable> listIcebergTables(Namespace namespace) {
StringBuilder baseQuery = new StringBuilder("SHOW ICEBERG TABLES");
Comment thread
danielcweeks marked this conversation as resolved.
Object[] queryParams = null;
if (namespace.length() == SnowflakeResources.MAX_NAMESPACE_DEPTH) {
// For two level namespace, search for iceberg tables within the given schema.
baseQuery.append(" IN SCHEMA IDENTIFIER(?)");
queryParams =
new Object[] {
String.format(
"%s.%s",
namespace.level(SnowflakeResources.NAMESPACE_DB_LEVEL - 1),
namespace.level(SnowflakeResources.NAMESPACE_SCHEMA_LEVEL - 1))
};
} else if (namespace.length() == SnowflakeResources.NAMESPACE_DB_LEVEL) {
// For one level namespace, search for iceberg tables within the given database.
baseQuery.append(" IN DATABASE IDENTIFIER(?)");
queryParams = new Object[] {namespace.level(SnowflakeResources.NAMESPACE_DB_LEVEL - 1)};
} else {
// For empty or db level namespace, search at account level.
baseQuery.append(" IN ACCOUNT");
}

final String finalQuery = baseQuery.toString();
final Object[] finalQueryParams = queryParams;
List<SnowflakeTable> tables;
try {
tables =
connectionPool.run(
conn ->
queryRunner.query(
conn, finalQuery, SnowflakeTable.createHandler(), finalQueryParams));
} catch (SQLException e) {
throw new UncheckedSQLException(
e, "Failed to list tables for namespace %s", namespace.toString());
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e, "Interrupted while listing tables");
}
return tables;
}

@Override
public SnowflakeTableMetadata getTableMetadata(TableIdentifier tableIdentifier) {
SnowflakeTableMetadata tableMeta;
try {
final String finalQuery = "SELECT SYSTEM$GET_ICEBERG_TABLE_INFORMATION(?) AS METADATA";
tableMeta =
connectionPool.run(
conn ->
queryRunner.query(
conn,
finalQuery,
SnowflakeTableMetadata.createHandler(),
tableIdentifier.toString()));
} catch (SQLException e) {
throw new UncheckedSQLException(
e, "Failed to get table metadata for %s", tableIdentifier.toString());
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e, "Interrupted while getting table metadata");
}
return tableMeta;
}

@Override
public void close() {
connectionPool.close();
Comment thread
danielcweeks marked this conversation as resolved.
}
}
Loading