Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
119 changes: 119 additions & 0 deletions api/src/main/java/org/apache/iceberg/catalog/ViewCatalog.java
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would not this method be also common with other representations?

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this needs to be an Enum and somehow be merged with Type. @rdblue, @danielcweeks, @amogh-jahagirdar, thoughts? Further, it is better for the spec to take a position on supported dialects, or not take a position at all, but in this case SQL should not be a special type. Right now the position sounds a bit fuzzy.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  • One defines the view SQL using a non-SQL string (e.g., some other DSL), and still leverages SQLViewReperesentation for that. Even if this is permitted, it is not clear from the spec if it is okay.
  • The dialect could be the same but not standardized, e.g., dialects such as spark, Spark, SparkSQL, Spark SQL.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

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);

Copy link
Contributor

Choose a reason for hiding this comment

The 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 SparkSQL, id = 1 and TrinoSQL, id = 2, and each view representation references a standardized dialect ID. They are not part of the Iceberg spec, but defining and adding them dynamically is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One defines the view SQL using a non-SQL string

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.

The dialect could be the same but not standardized

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a getter. wondering why default...? should it just be catalog()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a Spark/Presto SQL session, you can run USE to set default catalog and namespace, thus the "default".


/** The default namespace when the view is created. */
Namespace defaultNamespace();

/** 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);
}
86 changes: 86 additions & 0 deletions api/src/main/java/org/apache/iceberg/view/View.java
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 {

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();
}
Loading