-
Notifications
You must be signed in to change notification settings - Fork 4
Initial read-only Snowflake Catalog implementation by @sfc-gh-mparmar #1
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
Changes from 7 commits
4cc650f
618024c
bada3ce
e7ef4e5
f4fe21b
c37b924
c198747
4a7fd5f
f3074a1
e0048e4
8df8cdf
930c24f
7d9d303
ba737db
124bbd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,4 +82,6 @@ ALIYUN: | |
| GCP: | ||
| - gcp/**/* | ||
| DELL: | ||
| - dell/**/* | ||
| - dell/**/* | ||
| SNOWFLAKE: | ||
| - snowflake/**/* | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * 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.io.Closeable; | ||
| import java.util.List; | ||
| import org.apache.iceberg.catalog.Namespace; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
| import org.apache.iceberg.snowflake.entities.SnowflakeSchema; | ||
| import org.apache.iceberg.snowflake.entities.SnowflakeTable; | ||
| import org.apache.iceberg.snowflake.entities.SnowflakeTableMetadata; | ||
|
|
||
| public interface QueryFactory extends Closeable { | ||
|
||
| List<SnowflakeSchema> listSchemas(Namespace namespace); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're going to use this as an abstraction layer allowing us to easily swap out the bottom connectivity layer into Snowflake APIs, we should make the input argument also use Snowflake concepts, so that the business logic of translating an Iceberg Namespace into a Snowflake Database/Schema is common across implementations, while the way a list of tables is fetched given a db.schema can vary based on underlying implementations.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there's actually a fair amount of JDBC-specific logic in translating the namespace into the right predicate or identifier in the SQL statement, I'll wait until we add another lower-level client impl before extracting the namespace-conversion out, and for now just focus on refacting within JdbcSnowflakeClient to reduce duplication of boilerplate. |
||
|
|
||
| List<SnowflakeTable> listIcebergTables(Namespace namespace); | ||
|
|
||
| SnowflakeTableMetadata getTableMetadata(TableIdentifier tableIdentifier); | ||
|
|
||
| void close(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /* | ||
| * 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.io.Closeable; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.BaseMetastoreCatalog; | ||
| import org.apache.iceberg.CatalogProperties; | ||
| import org.apache.iceberg.CatalogUtil; | ||
| import org.apache.iceberg.TableOperations; | ||
| import org.apache.iceberg.catalog.Namespace; | ||
| import org.apache.iceberg.catalog.SupportsNamespaces; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
| import org.apache.iceberg.exceptions.NoSuchNamespaceException; | ||
| import org.apache.iceberg.hadoop.Configurable; | ||
| import org.apache.iceberg.io.FileIO; | ||
| 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.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.snowflake.entities.SnowflakeSchema; | ||
| import org.apache.iceberg.snowflake.entities.SnowflakeTable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class SnowflakeCatalog extends BaseMetastoreCatalog | ||
| implements Closeable, SupportsNamespaces, Configurable<Object> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(SnowflakeCatalog.class); | ||
|
|
||
| private Object conf; | ||
| private String catalogName = SnowflakeResources.DEFAULT_CATALOG_NAME; | ||
| private Map<String, String> catalogProperties = null; | ||
| private FileIO fileIO; | ||
| private QueryFactory queryFactory; | ||
|
|
||
| public SnowflakeCatalog() {} | ||
|
|
||
| public void setQueryFactory(QueryFactory factory) { | ||
| queryFactory = factory; | ||
| } | ||
|
|
||
| @Override | ||
| public List<TableIdentifier> listTables(Namespace namespace) { | ||
| Preconditions.checkArgument( | ||
| namespace.length() <= SnowflakeResources.MAX_NAMESPACE_DEPTH, | ||
| "Snowflake doesn't supports more than 2 levels of namespace"); | ||
|
||
|
|
||
| List<SnowflakeTable> sfTables = queryFactory.listIcebergTables(namespace); | ||
|
|
||
| return sfTables.stream() | ||
| .map( | ||
| table -> | ||
| TableIdentifier.of(table.getDatabase(), table.getSchemaName(), table.getName())) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean dropTable(TableIdentifier identifier, boolean purge) { | ||
sfc-gh-dhuo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void renameTable(TableIdentifier from, TableIdentifier to) {} | ||
|
|
||
| @Override | ||
| public void initialize(String name, Map<String, String> properties) { | ||
| catalogProperties = properties; | ||
|
|
||
| String uri = properties.get(CatalogProperties.URI); | ||
| Preconditions.checkNotNull(uri, "JDBC connection URI is required"); | ||
|
|
||
| if (name != null) { | ||
| this.catalogName = name; | ||
| } | ||
|
|
||
| LOG.debug("Connecting to JDBC database {}", properties.get(CatalogProperties.URI)); | ||
|
||
|
|
||
| JdbcClientPool connectionPool = new JdbcClientPool(uri, properties); | ||
|
|
||
| if (queryFactory == null) { | ||
| queryFactory = new SnowflakeQueryFactory(connectionPool); | ||
| } | ||
|
|
||
| String fileIOImpl = SnowflakeResources.DEFAULT_FILE_IO_IMPL; | ||
|
|
||
| if (null != catalogProperties.get(CatalogProperties.FILE_IO_IMPL)) { | ||
| fileIOImpl = catalogProperties.get(CatalogProperties.FILE_IO_IMPL); | ||
| } | ||
|
|
||
| fileIO = CatalogUtil.loadFileIO(fileIOImpl, catalogProperties, conf); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| queryFactory.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public void createNamespace(Namespace namespace, Map<String, String> metadata) {} | ||
|
||
|
|
||
| @Override | ||
| public List<Namespace> listNamespaces(Namespace namespace) { | ||
| Preconditions.checkArgument( | ||
| namespace.length() <= SnowflakeResources.MAX_NAMESPACE_DEPTH, | ||
| "Snowflake doesn't supports more than 2 levels of namespace"); | ||
|
||
| List<Namespace> namespaceList = Lists.newArrayList(); | ||
|
||
| try { | ||
| List<SnowflakeSchema> sfSchemas = queryFactory.listSchemas(namespace); | ||
| namespaceList = | ||
| sfSchemas.stream() | ||
| .map(schema -> Namespace.of(schema.getDatabase(), schema.getName())) | ||
| .collect(Collectors.toList()); | ||
| } catch (UncheckedSQLException | UncheckedInterruptedException ex) { | ||
| LOG.error("{}", ex.getMessage(), ex); | ||
|
||
| } | ||
|
|
||
| if (namespace.length() == SnowflakeResources.MAX_NAMESPACE_DEPTH) { | ||
|
||
| if (namespaceList.stream() | ||
| .anyMatch(n -> n.toString().equalsIgnoreCase(namespace.toString()))) { | ||
| return Arrays.asList(namespace); | ||
| } else { | ||
| return Lists.newArrayList(); | ||
| } | ||
| } | ||
|
|
||
| return namespaceList; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> loadNamespaceMetadata(Namespace namespace) | ||
| throws NoSuchNamespaceException { | ||
| List<Namespace> allNamespaces = listNamespaces(namespace); | ||
|
|
||
| if (allNamespaces.size() == 0) { | ||
| throw new NoSuchNamespaceException("Could not find namespace %s", namespace); | ||
| } | ||
|
|
||
| Map<String, String> nameSpaceMetadata = Maps.newHashMap(); | ||
| nameSpaceMetadata.put("name", namespace.toString()); | ||
| return nameSpaceMetadata; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean dropNamespace(Namespace namespace) { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean setProperties(Namespace namespace, Map<String, String> properties) { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean removeProperties(Namespace namespace, Set<String> properties) { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected TableOperations newTableOps(TableIdentifier tableIdentifier) { | ||
| Preconditions.checkArgument( | ||
| tableIdentifier.namespace().length() <= SnowflakeResources.MAX_NAMESPACE_DEPTH, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update per above.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| "Snowflake doesn't supports more than 2 levels of namespace"); | ||
|
|
||
| return new SnowflakeTableOperations( | ||
| queryFactory, fileIO, catalogProperties, catalogName, tableIdentifier); | ||
| } | ||
|
|
||
| @Override | ||
| protected String defaultWarehouseLocation(TableIdentifier tableIdentifier) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void setConf(Object conf) { | ||
| this.conf = conf; | ||
| } | ||
|
|
||
| public Object getConf() { | ||
| return conf; | ||
| } | ||
| } | ||
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.
Need javadoc comments on all the classes.
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.
Added for classes that can benefit from explanation