rdblue commented on code in PR #7880: URL: https://github.com/apache/iceberg/pull/7880#discussion_r1337878846
########## core/src/main/java/org/apache/iceberg/view/BaseMetastoreViewCatalog.java: ########## @@ -0,0 +1,217 @@ +/* + * 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.view; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.BaseMetastoreCatalog; +import org.apache.iceberg.Schema; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.catalog.ViewCatalog; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.NoSuchViewException; +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; + +public abstract class BaseMetastoreViewCatalog extends BaseMetastoreCatalog implements ViewCatalog { + protected abstract ViewOperations newViewOps(TableIdentifier identifier); + + @Override + public void initialize(String name, Map<String, String> properties) { + super.initialize(name, properties); + } + + @Override + public String name() { + return super.name(); + } + + @Override + public View loadView(TableIdentifier identifier) { + if (isValidIdentifier(identifier)) { + ViewOperations ops = newViewOps(identifier); + if (ops.current() == null) { + throw new NoSuchViewException("View does not exist: %s", identifier); + } else { + return new BaseView(newViewOps(identifier), ViewUtil.fullViewName(name(), identifier)); + } + } + + throw new NoSuchViewException("Invalid view identifier: %s", identifier); + } + + @Override + public ViewBuilder buildView(TableIdentifier identifier) { + return new BaseViewBuilder(identifier); + } + + protected class BaseViewBuilder implements ViewBuilder { + private final TableIdentifier identifier; + private final ImmutableViewVersion.Builder viewVersionBuilder = ImmutableViewVersion.builder(); + private final Map<String, String> properties = Maps.newHashMap(); + private final List<ViewRepresentation> representations = Lists.newArrayList(); + private Namespace defaultNamespace = null; + private Schema schema = null; + + protected BaseViewBuilder(TableIdentifier identifier) { + Preconditions.checkArgument( + isValidIdentifier(identifier), "Invalid view identifier: %s", identifier); + this.identifier = identifier; + } + + @Override + public ViewBuilder withSchema(Schema newSchema) { + this.schema = newSchema; + viewVersionBuilder.schemaId(newSchema.schemaId()); + return this; + } + + @Override + public ViewBuilder withQuery(String dialect, String sql) { + representations.add( + ImmutableSQLViewRepresentation.builder().dialect(dialect).sql(sql).build()); + return this; + } + + @Override + public ViewBuilder withDefaultCatalog(String defaultCatalog) { + viewVersionBuilder.defaultCatalog(defaultCatalog); + return this; + } + + @Override + public ViewBuilder withDefaultNamespace(Namespace namespace) { + this.defaultNamespace = namespace; + return this; + } + + @Override + public ViewBuilder withProperties(Map<String, String> newProperties) { + this.properties.putAll(newProperties); + return this; + } + + @Override + public ViewBuilder withProperty(String key, String value) { + this.properties.put(key, value); + return this; + } + + @Override + public View create() { + return create(newViewOps(identifier)); + } + + @Override + public View replace() { + return replace(newViewOps(identifier)); + } + + @Override + public View createOrReplace() { + ViewOperations ops = newViewOps(identifier); + if (null == ops.current()) { + return create(ops); + } else { + return replace(ops); + } + } + + private View create(ViewOperations ops) { + if (null != ops.current()) { + throw new AlreadyExistsException("View already exists: %s", identifier); + } + + Preconditions.checkState( + !representations.isEmpty(), "Cannot create view without specifying a query"); + Preconditions.checkState(null != schema, "Cannot create view without specifying schema"); + Preconditions.checkState( + null != defaultNamespace, "Cannot create view without specifying a default namespace"); + + ViewVersion viewVersion = + viewVersionBuilder + .versionId(1) + .addAllRepresentations(representations) + .defaultNamespace(defaultNamespace) Review Comment: Missing `withDefaultCatalog`? Looks like this must be a case that is untested as well. ########## core/src/main/java/org/apache/iceberg/view/BaseMetastoreViewCatalog.java: ########## @@ -0,0 +1,217 @@ +/* + * 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.view; + +import java.util.List; +import java.util.Map; +import org.apache.iceberg.BaseMetastoreCatalog; +import org.apache.iceberg.Schema; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.catalog.ViewCatalog; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.NoSuchViewException; +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; + +public abstract class BaseMetastoreViewCatalog extends BaseMetastoreCatalog implements ViewCatalog { + protected abstract ViewOperations newViewOps(TableIdentifier identifier); + + @Override + public void initialize(String name, Map<String, String> properties) { + super.initialize(name, properties); + } + + @Override + public String name() { + return super.name(); + } + + @Override + public View loadView(TableIdentifier identifier) { + if (isValidIdentifier(identifier)) { + ViewOperations ops = newViewOps(identifier); + if (ops.current() == null) { + throw new NoSuchViewException("View does not exist: %s", identifier); + } else { + return new BaseView(newViewOps(identifier), ViewUtil.fullViewName(name(), identifier)); + } + } + + throw new NoSuchViewException("Invalid view identifier: %s", identifier); + } + + @Override + public ViewBuilder buildView(TableIdentifier identifier) { + return new BaseViewBuilder(identifier); + } + + protected class BaseViewBuilder implements ViewBuilder { + private final TableIdentifier identifier; + private final ImmutableViewVersion.Builder viewVersionBuilder = ImmutableViewVersion.builder(); + private final Map<String, String> properties = Maps.newHashMap(); + private final List<ViewRepresentation> representations = Lists.newArrayList(); + private Namespace defaultNamespace = null; + private Schema schema = null; + + protected BaseViewBuilder(TableIdentifier identifier) { + Preconditions.checkArgument( + isValidIdentifier(identifier), "Invalid view identifier: %s", identifier); + this.identifier = identifier; + } + + @Override + public ViewBuilder withSchema(Schema newSchema) { + this.schema = newSchema; + viewVersionBuilder.schemaId(newSchema.schemaId()); + return this; + } + + @Override + public ViewBuilder withQuery(String dialect, String sql) { + representations.add( + ImmutableSQLViewRepresentation.builder().dialect(dialect).sql(sql).build()); + return this; + } + + @Override + public ViewBuilder withDefaultCatalog(String defaultCatalog) { + viewVersionBuilder.defaultCatalog(defaultCatalog); + return this; + } + + @Override + public ViewBuilder withDefaultNamespace(Namespace namespace) { + this.defaultNamespace = namespace; + return this; + } + + @Override + public ViewBuilder withProperties(Map<String, String> newProperties) { + this.properties.putAll(newProperties); + return this; + } + + @Override + public ViewBuilder withProperty(String key, String value) { + this.properties.put(key, value); + return this; + } + + @Override + public View create() { + return create(newViewOps(identifier)); + } + + @Override + public View replace() { + return replace(newViewOps(identifier)); + } + + @Override + public View createOrReplace() { + ViewOperations ops = newViewOps(identifier); + if (null == ops.current()) { + return create(ops); + } else { + return replace(ops); + } + } + + private View create(ViewOperations ops) { + if (null != ops.current()) { + throw new AlreadyExistsException("View already exists: %s", identifier); + } + + Preconditions.checkState( + !representations.isEmpty(), "Cannot create view without specifying a query"); + Preconditions.checkState(null != schema, "Cannot create view without specifying schema"); + Preconditions.checkState( + null != defaultNamespace, "Cannot create view without specifying a default namespace"); + + ViewVersion viewVersion = + viewVersionBuilder + .versionId(1) + .addAllRepresentations(representations) + .defaultNamespace(defaultNamespace) Review Comment: Missing `withDefaultCatalog`? Looks like this must be a case that is untested as well. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
