Repository navigation
API,Core: Support registerView for view catalog - #14868
Conversation
| ViewMetadata metadata = ViewMetadataParser.read(metadataFile); | ||
| ops.commit(null, metadata); | ||
|
|
||
| return new BaseView(ops, fullTableName(name(), identifier)); |
There was a problem hiding this comment.
BaseMetastoreViewCatalog.create() creates the BaseView like this:
return new BaseView(ops, ViewUtil.fullViewName(name(), identifier));
Note, it constructs the view name using ViewUtil. Shouldn't we follow that here?
There was a problem hiding this comment.
Good observation.
I saw that CatalogUtil.fullTableName was more better as it handles slash and uri names. I also saw that same thing was used in HiveViewOperations.
I think probably we should unify ViewUtil.fullViewName and CatalogUtil.fullTableName use it everywhere. In a separate PR.
For this PR now, I updated back the changes to use ViewUtil.fullViewName.
47dc9eb to
74e20f8
Compare
| assertThat(registeredView.currentVersion()) | ||
| .as("Current version must match") | ||
| .isEqualTo(originalView.currentVersion()); | ||
| assertThat(registeredView.versions()) |
There was a problem hiding this comment.
Just checked for my own education, what is the content of ViewVersion. I see that there is a defaultCatalog member. I don't know much about this code so asking for my benefit: is this default catalog persisted into view metadata? When registering the view in catalog1 into catalog2, would the default catalog be updated too?
I guess for this to work the engine has to refer to the new catalog with the same name as it does the old catalog. Nor sure, we can expect that, but let me know what you think.
There was a problem hiding this comment.
Great question.
I did ask about this optional defaultCatalog field long ago and got an answer that it was for Spark.
Updating the metadata (with new catalog name) will leads to new metadata file which is not exactly a register case. So, If the engines requires it they can update the view with the new catalog name (only for Spark I guess) by adding the new version.
@nastra can add more on this as he was handling this during view spec design.
There was a problem hiding this comment.
In general the purpose of default catalog seems more general that Spark (even if potentially Spark uses it only for now). If the engine uses catalog.namespace.table naming and the view contains SELECT * FROM tbl; then the engine should know which catalog and namespace to use. Such an engine will know that defaultCatalog.defaultNamespace.tbl is the one used in the query.
However, when we move the table to a different catalog, won't it be an issue that the view metadata contains the name of the source catalog and not the new one?
Let's say you configure your engine like this:
- One catalog has the name
catalog1and it has namespacensand a tablens.tbl - Other catalog has the name
catalog2and it also has a namespacensand a tablens.tbl - The content of
catalog1.ns.tblandcatalog2.ns.tblis NOT the same
Now let's say you create a view in catalog1 as SELECT * FROM tbl. The default catalog here would be catalog1.
When you register this into catalog2, the default catalog will remain catalog1, so when querying catalog2.ns.registered_view it would still query catalog1.ns.tbl. However, I'd think the intention would be different: the view in catalog2 should be querying catalog2.ns.tbl.
I'm not sure how much this is intended by design, but would be nice to hear some thoughts before moving on. I think this defaultCatalog design makes sense for resolving the full table name (when the engine has a catalog layer in naming), however, could cause unintended side-effects when registering views across-catalogs.
There was a problem hiding this comment.
I know. Thats why I didn't oppose that we shouldn't modify the default catalog name. But modifying table metadata in the register operation is not good idea as the operation is expected to register the existing metadata without modifying it or without writing new metadata.
So, If engines needed, they can update the view to reflect the new catalog name. But I am not sure if Spark can update that view if the existing catalog name is different (unable to resolve it?). @nastra: Need your input on this.
I will test spark with newly registered table with different catalog name and see what happens.
There was a problem hiding this comment.
I have added a test on TestViews to use views which has invalid catalog name.
@TestTemplate
public void readFromViewWithInvalidCatalogName() throws NoSuchTableException {
insertRows(10);
String viewName = viewName("simpleView");
String sql = String.format("SELECT id FROM %s", tableName);
ViewCatalog viewCatalog = viewCatalog();
viewCatalog
.buildView(TableIdentifier.of(NAMESPACE, viewName))
.withQuery("spark", sql)
// use non-existing column name to make sure only the SQL definition for spark is loaded
.withQuery("trino", String.format("SELECT non_existing FROM %s", tableName))
.withDefaultNamespace(NAMESPACE)
.withDefaultCatalog("invalid_catalog")
.withSchema(schema(sql))
.create();
sql("SELECT * FROM %s", viewName);
}
querying the view in sql("SELECT * FROM %s", viewName) fails with below error
org.apache.spark.sql.AnalysisException: [TABLE_OR_VIEW_NOT_FOUND] The table or view `invalid_catalog`.`default`.`table` cannot be found. Verify the spelling and correctness of the schema and catalog.
If you did not qualify the name with a schema, verify the current_schema() output, or qualify the name with the correct schema and catalog.
To tolerate the error on drop use DROP VIEW IF EXISTS or DROP TABLE IF EXISTS. SQLSTATE: 42P01; line 1 pos 15;
'Project [*]
+- 'SubqueryAlias simpleView969944
+- 'Project [upcast(getcolumnbyordinal(0, IntegerType), IntegerType) AS id#16]
+- 'Project ['id]
+- 'UnresolvedRelation [invalid_catalog, default, table], [], false
at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.tableNotFound(package.scala:91)
at org.apache.spark.sql.catalyst.analysis.CheckAnalysis.$anonfun$checkAnalysis0$2(CheckAnalysis.scala:306)
at org.apache.spark.sql.catalyst.analysis.CheckAnalysis.$anonfun$checkAnalysis0$2$adapted(CheckAnalysis.scala:284)
So, spark cannot query the view registered from other catalog and it is a major interop concern (I vaguely remember brining this up while standardizing the view spec).
I don't think the register function should alter the catalog name as it will leads to new versionUpdate with new version id and creates a new metadata file which is not the purpose of the register function. So, spark users may not easily use that view (without using some APIs to alter it).
I think we need broader discussion for this and @nastra is on holidays. We should get back once he is back.
There was a problem hiding this comment.
Looks like we have consensus on this from the mailing list. There are many reasons to not to update the view during register view. more details: https://lists.apache.org/thread/mxqz0fp6vz3nt7nl70wgrz6d2gr41rzj
We can go ahead and review the PR.
| metadataFileLocation != null && !metadataFileLocation.isEmpty(), | ||
| "Cannot register an empty metadata file location as a view"); | ||
|
|
||
| if (viewExists(identifier)) { |
There was a problem hiding this comment.
what happens if a table with that name already exists?
There was a problem hiding this comment.
good catch. I will fix it.
I had this in back of my mind and forgot while implementing because I followed registerTable and it is missing viewExist check. I will fix registerTable in a follow up PR.
| .withDefaultNamespace(identifier.namespace()) | ||
| .withDefaultCatalog(catalog().name()) | ||
| .withQuery("spark", "select * from ns.tbl") | ||
| .withProperty(GC_ENABLED, "false") |
There was a problem hiding this comment.
why do we need this flag? It doesn't seem to be required when testing table registration
There was a problem hiding this comment.
The drop view will delete the view metadata. So, we will not have any file to register. Hence, needed to just delete entry from catalog and use file to register again.
| C catalog = catalog(); | ||
|
|
||
| // Register view is not yet supported for REST catalog | ||
| assumeThat(catalog instanceof RESTCatalog).isFalse(); |
There was a problem hiding this comment.
| assumeThat(catalog instanceof RESTCatalog).isFalse(); | |
| assumeThat(catalog).isNotInstanceOf(RESTCatalog.class); |
| .as("Current version must match") | ||
| .isEqualTo(originalView.currentVersion()); | ||
| assertThat(registeredView.versions()) | ||
| .as("versions must match") |
There was a problem hiding this comment.
nit: I don't think we need any of these .as() calls here
| assertThat(catalog.viewExists(identifier)).as("View must exist").isTrue(); | ||
| assertThat(registeredView.schema().asStruct()) | ||
| .as("Schema must match") | ||
| .isEqualTo(originalView.schema().asStruct()); |
There was a problem hiding this comment.
we should be explicit in all of these checks, so I'd add another .isEqualTo(SCHEMA.asStruct() here
| .isEqualTo(originalView.schema().asStruct()); | ||
| assertThat(registeredView.currentVersion()) | ||
| .as("Current version must match") | ||
| .isEqualTo(originalView.currentVersion()); |
There was a problem hiding this comment.
this should also be explicit and check similarly to how we check in basicCreateView() by additionally adding an .isEqualTo(ImmutableViewVersion.builder()...)
| assertThat(registeredView.versions()) | ||
| .as("versions must match") | ||
| .isEqualTo(originalView.versions()); | ||
| assertThat(registeredView.history()).as("History must match").isEqualTo(originalView.history()); |
There was a problem hiding this comment.
make sure to check the history similarly to basicCreateView in addition to the assertion you already have here
| C catalog = catalog(); | ||
|
|
||
| // Register view is not yet supported for REST catalog | ||
| assumeThat(catalog instanceof RESTCatalog).isFalse(); |
| } | ||
|
|
||
| @Test | ||
| public void registerExistingView() { |
There was a problem hiding this comment.
we probably should also add a test where we're trying to register a view with an identifier that is already being used for an existing table.
Also we probably should do the same for registering a table
There was a problem hiding this comment.
@ajantha-bhat can you please open a separate PR/issue to address this for the existing table registration?
There was a problem hiding this comment.
I tested already and it has handled for table because TableOperation.commit throws error if view exist with that name. But error message is not common as each operation has its own style of throwing error.
Couldn't do same for the view operations as I cannot call table exists there.
There was a problem hiding this comment.
do we have an existing test for this?
There was a problem hiding this comment.
Nope. I added this for test in CatalogTest but each catalog had its own error message format. So, we couldn't have common clean test
@Test
public void testRegisterTableWithExistingView() {
C catalog = catalog();
assumeThat(catalog).isInstanceOf(ViewCatalog.class);
TableIdentifier identifier = TableIdentifier.of("a", "t1");
if (requiresNamespaceCreate()) {
catalog.createNamespace(identifier.namespace());
}
((ViewCatalog) catalog)
.buildView(identifier)
.withSchema(SCHEMA)
.withDefaultNamespace(identifier.namespace())
.withDefaultCatalog(catalog().name())
.withQuery("spark", "select * from ns.tbl")
.create();
assertThat(((ViewCatalog)catalog).viewExists(identifier)).as("View should exist").isTrue();
assertThatThrownBy(() -> catalog.createTable(identifier, SCHEMA))
.isInstanceOf(AlreadyExistsException.class)
.hasMessageStartingWith("View with same name already exists: a.t1");
Table table = catalog.createTable(TableIdentifier.of("a", "temp"), SCHEMA);
String metadataLocation = ((BaseTable) table).operations().current().metadataFileLocation();
assertThatThrownBy(() -> catalog.registerTable(identifier, metadataLocation))
.isInstanceOf(AlreadyExistsException.class)
.hasMessageStartingWith("Table already exists: a.t1");
assertThat(catalog.dropTable(identifier)).isTrue();
}
74e20f8 to
67f7d83
Compare
67f7d83 to
56b988f
Compare
|
Looks like we have consensus on this from the mailing list. There are many reasons to not to update the view during register view. More details: https://lists.apache.org/thread/mxqz0fp6vz3nt7nl70wgrz6d2gr41rzj We can go ahead and review/merge the PR. cc: @nastra, @gaborkaszab, @ stevenzwu |
|
Also cc: @amogh-jahagirdar , @singhpk234 : please take a look. |
| * @param identifier a view identifier | ||
| * @param metadataFileLocation the location of a metadata file | ||
| * @return a View instance | ||
| * @throws AlreadyExistsException if the view already exists in the catalog. |
There was a problem hiding this comment.
| * @throws AlreadyExistsException if the view already exists in the catalog. | |
| * @throws AlreadyExistsException if a table/view with the same identifier already exists in the catalog. |
| } | ||
|
|
||
| @Test | ||
| public void registerViewWithExistingTable() { |
There was a problem hiding this comment.
| public void registerViewWithExistingTable() { | |
| public void registerViewThatAlreadyExistsAsTable() { |
this is a little bit more precise as it's not entirely clear what "registering a view with an existing table" means
| .withDefaultCatalog(catalog().name()) | ||
| .withQuery("spark", "select * from ns.tbl"); | ||
|
|
||
| assertThatThrownBy(viewBuilder::create) |
There was a problem hiding this comment.
I think what we really want to test here is to:
- first create the view and drop it but keep its metadata
- create a table with the same identifier (this should succeed)
- now try to register the original view metadata under the table's identifier (this should fail, saying that a table with the same name already exists)
gaborkaszab
left a comment
There was a problem hiding this comment.
Thanks you giving this a wider visibility on the dev@ list, @ajantha-bhat ! Since there were no objections, I think this is fine.
|
@singhpk234 and @amogh-jahagirdar : Any comments for this PR? @nastra suggested your review too on this. |
338ebb1 to
58760ca
Compare
|
Just rebased to resolve the conflicts in |
58760ca to
898eaa1
Compare
amogh-jahagirdar
left a comment
There was a problem hiding this comment.
Thanks @ajantha-bhat this looks reasonable to me!
|
|
||
| ViewOperations ops = newViewOps(identifier); | ||
| InputFile metadataFile = ((BaseViewOperations) ops).io().newInputFile(metadataFileLocation); | ||
| ViewMetadata metadata = ViewMetadataParser.read(metadataFile); |
There was a problem hiding this comment.
Minor: We may want to just add a ViewMetadataParser.read(FileIO, String location) API
There was a problem hiding this comment.
added and used it in all existing places.
|
thanks @ajantha-bhat |
Description:
Closes #[issue] (file the companion issue first and link it here)
Adds RegisterView(ctx, identifier, metadataLocation) to rest.Catalog,
the view equivalent of the existing RegisterTable. The method POSTs to POST
/v1/{prefix}/namespaces/{namespace}/register-view as defined in the
Iceberg REST catalog OpenAPI specification.
Motivation
The only way to migrate a view between REST catalogs without this method is
LoadView + CreateView. That approach is lossy: CreateView resets version-id to 1,
reassigns default-catalog to the target catalog name, and creates a new UUID
— producing a new view rather than a registration of the existing one.
RegisterView avoids all of this by pointing the target catalog at the existing
metadata file, exactly as RegisterTable does for tables.
Changes
- catalog/rest/rest.go: RegisterView on *Catalog — POSTs {name, metadata-location}
to /namespaces/{ns}/register-view, parses the response through the existing loadViewResponse
path, maps 404 → ErrNoSuchNamespace and 409 → ErrViewAlreadyExists.
- catalog/rest/rest_test.go: three test cases covering the 200, 404, and 409 responses,
following the same structure as the existing TestRegisterTable{200,404,409} tests.
Related
- Java implementation: apache/iceberg#14868
- REST OpenAPI spec: open-api/rest-catalog-open-api.yaml — operationId registerView
- Mailing list discussion: https://lists.apache.org/thread/mxqz0fp6vz3nt7nl70wgrz6d2gr41rzj
Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
## Description:
Adds RegisterView(ctx, identifier, metadataLocation) to rest.Catalog,
the view equivalent of the existing RegisterTable. The method POSTs to
POST /v1/{prefix}/namespaces/{namespace}/register-view as defined in the
Iceberg REST catalog OpenAPI specification.
## Motivation
Fixes: #752
The only way to migrate a view between REST catalogs without this method
is LoadView + CreateView. That approach is lossy: CreateView resets
version-id to 1, reassigns default-catalog to the target catalog name,
and creates a new UUID — producing a new view rather than a registration
of the existing one. RegisterView avoids all of this by pointing the
target catalog at the existing metadata file, exactly as RegisterTable
does for tables.
## Changes
- catalog/rest/rest.go: RegisterView on *Catalog — POSTs {name,
metadata-location} to /namespaces/{ns}/register-view, parses the
response through the existing loadViewResponse path, maps 404 →
ErrNoSuchNamespace and 409 → ErrViewAlreadyExists.
- catalog/rest/rest_test.go: three test cases covering the 200, 404, and
409 responses, following the same structure as the existing
TestRegisterTable{200,404,409} tests.
## Related
- Java implementation: apache/iceberg#14868
- REST OpenAPI spec: open-api/rest-catalog-open-api.yaml — operationId
registerView
- Mailing list discussion:
https://lists.apache.org/thread/mxqz0fp6vz3nt7nl70wgrz6d2gr41rzj
Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
## Description:
Adds RegisterView(ctx, identifier, metadataLocation) to rest.Catalog,
the view equivalent of the existing RegisterTable. The method POSTs to
POST /v1/{prefix}/namespaces/{namespace}/register-view as defined in the
Iceberg REST catalog OpenAPI specification.
## Motivation
Fixes: apache#752
The only way to migrate a view between REST catalogs without this method
is LoadView + CreateView. That approach is lossy: CreateView resets
version-id to 1, reassigns default-catalog to the target catalog name,
and creates a new UUID — producing a new view rather than a registration
of the existing one. RegisterView avoids all of this by pointing the
target catalog at the existing metadata file, exactly as RegisterTable
does for tables.
## Changes
- catalog/rest/rest.go: RegisterView on *Catalog — POSTs {name,
metadata-location} to /namespaces/{ns}/register-view, parses the
response through the existing loadViewResponse path, maps 404 →
ErrNoSuchNamespace and 409 → ErrViewAlreadyExists.
- catalog/rest/rest_test.go: three test cases covering the 200, 404, and
409 responses, following the same structure as the existing
TestRegisterTable{200,404,409} tests.
## Related
- Java implementation: apache/iceberg#14868
- REST OpenAPI spec: open-api/rest-catalog-open-api.yaml — operationId
registerView
- Mailing list discussion:
https://lists.apache.org/thread/mxqz0fp6vz3nt7nl70wgrz6d2gr41rzj
Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
ViewCatalog.mailing list discussion: https://lists.apache.org/thread/mxqz0fp6vz3nt7nl70wgrz6d2gr41rzj