-
Notifications
You must be signed in to change notification settings - Fork 2.9k
API: Add view interfaces #4925
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
API: Add view interfaces #4925
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * 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.catalog; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.exceptions.AlreadyExistsException; | ||
| import org.apache.iceberg.exceptions.NoSuchViewException; | ||
| import org.apache.iceberg.exceptions.NotFoundException; | ||
| import org.apache.iceberg.view.View; | ||
| import org.apache.iceberg.view.ViewBuilder; | ||
|
|
||
| /** A Catalog API for view create, drop, and load operations. */ | ||
| public interface ViewCatalog { | ||
|
|
||
| /** | ||
| * Return the name for this catalog. | ||
| * | ||
| * @return this catalog's name | ||
| */ | ||
| String name(); | ||
|
|
||
| /** | ||
| * Return all the identifiers under this namespace. | ||
| * | ||
| * @param namespace a namespace | ||
| * @return a list of identifiers for views | ||
| * @throws NotFoundException if the namespace is not found | ||
| */ | ||
| List<TableIdentifier> listViews(Namespace namespace); | ||
|
|
||
| /** | ||
| * Load a view. | ||
| * | ||
| * @param identifier a view identifier | ||
| * @return instance of {@link View} implementation referred by the identifier | ||
| * @throws NoSuchViewException if the view does not exist | ||
| */ | ||
| View loadView(TableIdentifier identifier); | ||
|
|
||
| /** | ||
| * Check whether view exists. | ||
| * | ||
| * @param identifier a view identifier | ||
| * @return true if the view exists, false otherwise | ||
| */ | ||
| default boolean viewExists(TableIdentifier identifier) { | ||
| try { | ||
| loadView(identifier); | ||
| return true; | ||
| } catch (NoSuchViewException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Instantiate a builder to create or replace a SQL view. | ||
| * | ||
| * @param identifier a view identifier | ||
| * @return a view builder | ||
| */ | ||
| ViewBuilder buildView(TableIdentifier identifier); | ||
|
|
||
| /** | ||
| * Drop a view. | ||
| * | ||
| * @param identifier a view identifier | ||
| * @return true if the view was dropped, false if the view did not exist | ||
| */ | ||
| boolean dropView(TableIdentifier identifier); | ||
|
|
||
| /** | ||
| * Rename a view. | ||
| * | ||
| * @param from identifier of the view to rename | ||
| * @param to new view identifier | ||
| * @throws NoSuchViewException if the "from" view does not exist | ||
| * @throws AlreadyExistsException if the "to" view already exists | ||
| */ | ||
| void renameView(TableIdentifier from, TableIdentifier to); | ||
|
|
||
| /** | ||
| * Invalidate cached view metadata from current catalog. | ||
| * | ||
| * <p>If the view is already loaded or cached, drop cached data. If the view does not exist or is | ||
| * not cached, do nothing. | ||
| * | ||
| * @param identifier a view identifier | ||
| */ | ||
| default void invalidateView(TableIdentifier identifier) {} | ||
|
|
||
| /** | ||
| * Initialize a view catalog given a custom name and a map of catalog properties. | ||
| * | ||
| * <p>A custom view catalog implementation must have a no-arg constructor. A compute engine like | ||
| * Spark or Flink will first initialize the catalog without any arguments, and then call this | ||
| * method to complete catalog initialization with properties passed into the engine. | ||
| * | ||
| * @param name a custom name for the catalog | ||
| * @param properties catalog properties | ||
| */ | ||
| default void initialize(String name, Map<String, String> properties) {} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * 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.exceptions; | ||
|
|
||
| import com.google.errorprone.annotations.FormatMethod; | ||
|
|
||
| /** Exception raised when attempting to load a view that does not exist. */ | ||
| public class NoSuchViewException extends RuntimeException { | ||
| @FormatMethod | ||
jzhuge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public NoSuchViewException(String message, Object... args) { | ||
| super(String.format(message, args)); | ||
| } | ||
|
|
||
| @FormatMethod | ||
| public NoSuchViewException(Throwable cause, String message, Object... args) { | ||
| super(String.format(message, args), cause); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * 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 org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.catalog.Namespace; | ||
|
|
||
| public interface SQLViewRepresentation extends ViewRepresentation { | ||
|
|
||
| @Override | ||
| default Type type() { | ||
| return Type.SQL; | ||
| } | ||
|
|
||
| /** The view query SQL text. */ | ||
| String query(); | ||
|
Comment on lines
+32
to
+33
Contributor
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. Why would not this method be also common with other representations?
Contributor
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. This is the SQL text of the view. Other view representations don't necessarily have a SQL query. I think this is reasonable. |
||
|
|
||
| /** The view query SQL dialect. */ | ||
| String dialect(); | ||
|
Comment on lines
+35
to
+36
Contributor
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. I feel this needs to be an
Contributor
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. I disagree that we want to define supported dialects. We don't want engine writers to need to come to the Iceberg community and get a new symbol in an enum before storing a view. I don't see much benefit to doing that.
Contributor
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. I think it is fine to do either: not take a position at all or take a well defined position. There could be a few scenarios where the intended meaning is not achieved. For example:
Contributor
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. Maybe what we need is an API to add supported dialects to the metadata? It is up to each catalog implementation to add their dialects, and view definitions can be associated with a dialect ID.
Member
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.
Already added /**
* Add a SQL {@link View} for a different dialect.
*
* @param sqlViewRepresentation a SQL view representation
* @return this for method chaining
* @throws IllegalArgumentException if the dialect is the same as the SQL view being built
*/
ViewBuilder withOtherSQLRepresentation(SQLViewRepresentation sqlViewRepresentation);
Contributor
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. I was referring to making dialects (not just view representations) a construct in the metadata where people can define and add them, e.g., some catalogs define
Contributor
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.
I don't think this is allowed. If you create a SQL view then it should contain SQL. In any case, being strict about dialects doesn't help prevent this.
This is a risk, but I think it isn't very likely. We should document the known dialect strings, but we shouldn't switch it to be an enum. |
||
|
|
||
| /** The default catalog when the view is created. */ | ||
| String defaultCatalog(); | ||
|
Contributor
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. this is a getter. wondering why
Member
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. In a Spark/Presto SQL session, you can run |
||
|
|
||
| /** The default namespace when the view is created. */ | ||
| Namespace defaultNamespace(); | ||
jzhuge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** The query output schema at version create time, without aliases. */ | ||
| Schema schema(); | ||
|
|
||
| /** The view field aliases. */ | ||
| List<String> fieldComments(); | ||
|
|
||
| /** The view field comments. */ | ||
| List<String> fieldAliases(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * 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.Map; | ||
| import org.apache.iceberg.PendingUpdate; | ||
|
|
||
| /** | ||
| * API for updating view properties. | ||
| * | ||
| * <p>Apply returns the updated view properties as a map for validation. | ||
| * | ||
| * <p>When committing, these changes will be applied to the current view metadata. Commit conflicts | ||
| * will be resolved by applying the pending changes to the new view metadata. | ||
| */ | ||
| public interface UpdateViewProperties extends PendingUpdate<Map<String, String>> { | ||
|
|
||
| /** | ||
| * Add a key/value property to the view. | ||
| * | ||
| * @param key a String key | ||
| * @param value a String value | ||
| * @return this for method chaining | ||
| * @throws NullPointerException If either the key or value is null | ||
| */ | ||
| UpdateViewProperties set(String key, String value); | ||
|
|
||
| /** | ||
| * Remove the given property key from the view. | ||
| * | ||
| * @param key a String key | ||
| * @return this for method chaining | ||
| * @throws NullPointerException If the key is null | ||
| */ | ||
| UpdateViewProperties remove(String key); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.Schema; | ||
|
|
||
| /** Interface for view definition. */ | ||
| public interface View { | ||
jzhuge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| String name(); | ||
|
|
||
| /** | ||
| * Return the {@link Schema schema} for this view. | ||
| * | ||
| * @return this table's schema | ||
| */ | ||
| Schema schema(); | ||
|
|
||
| /** | ||
| * Return a map of {@link Schema schema} for this view. | ||
| * | ||
| * @return this table's schema map | ||
| */ | ||
| Map<Integer, Schema> schemas(); | ||
|
|
||
| /** | ||
| * Get the current version for this view, or null if there are no versions. | ||
| * | ||
| * @return the current view version. | ||
| */ | ||
| ViewVersion currentVersion(); | ||
|
|
||
| /** | ||
| * Get the versions of this view. | ||
| * | ||
| * @return an Iterable of versions of this view. | ||
| */ | ||
| Iterable<ViewVersion> versions(); | ||
|
|
||
| /** | ||
| * Get a version in this view by ID. | ||
| * | ||
| * @param versionId version ID | ||
| * @return a version, or null if the ID cannot be found | ||
| */ | ||
| ViewVersion version(int versionId); | ||
|
|
||
| /** | ||
| * Get the version history of this table. | ||
| * | ||
| * @return a list of {@link ViewHistoryEntry} | ||
| */ | ||
| List<ViewHistoryEntry> history(); | ||
|
|
||
| /** | ||
| * Return a map of string properties for this view. | ||
| * | ||
| * @return this view's properties map | ||
| */ | ||
| Map<String, String> properties(); | ||
|
|
||
| /** | ||
| * Create a new {@link UpdateViewProperties} to update view properties. | ||
| * | ||
| * @return a new {@link UpdateViewProperties} | ||
| */ | ||
| UpdateViewProperties updateProperties(); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.