manuzhang commented on code in PR #14984:
URL: https://github.com/apache/iceberg/pull/14984#discussion_r3642676320
##########
spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java:
##########
@@ -568,135 +567,106 @@ public View loadView(Identifier ident) throws
NoSuchViewException {
}
@Override
- public View createView(ViewInfo viewInfo)
+ public View createView(Identifier ident, View view)
throws ViewAlreadyExistsException, NoSuchNamespaceException {
- if (null != asViewCatalog && viewInfo != null) {
- Identifier ident = viewInfo.ident();
- String sql = viewInfo.sql();
- String currentCatalog = viewInfo.currentCatalog();
- String[] currentNamespace = viewInfo.currentNamespace();
- StructType schema = viewInfo.schema();
- String[] queryColumnNames = viewInfo.queryColumnNames();
- Map<String, String> properties = viewInfo.properties();
- Schema icebergSchema = SparkSchemaUtil.convert(schema);
-
- try {
- Map<String, String> props =
- ImmutableMap.<String, String>builder()
- .putAll(Spark3Util.rebuildCreateProperties(properties))
- .put(SparkView.QUERY_COLUMN_NAMES,
COMMA_JOINER.join(queryColumnNames))
- .buildKeepingLast();
-
- org.apache.iceberg.view.View view =
- asViewCatalog
- .buildView(buildIdentifier(ident))
- .withDefaultCatalog(currentCatalog)
- .withDefaultNamespace(Namespace.of(currentNamespace))
- .withQuery("spark", sql)
- .withSchema(icebergSchema)
- .withLocation(properties.get("location"))
- .withProperties(props)
- .create();
- return new SparkView(catalogName, view);
- } catch (org.apache.iceberg.exceptions.NoSuchNamespaceException e) {
- throw new NoSuchNamespaceException(currentNamespace);
- } catch (AlreadyExistsException e) {
- throw new ViewAlreadyExistsException(ident);
- }
+ try {
+ return commitView(ident, view, ViewCommit.CREATE);
+ } catch (NoSuchViewException e) {
+ throw unexpectedViewCommitException("create", ident, "reported that the
view is missing", e);
}
-
- throw new UnsupportedOperationException(
- "Creating a view is not supported by catalog: " + catalogName);
}
@Override
- public View replaceView(
- Identifier ident,
- String sql,
- String currentCatalog,
- String[] currentNamespace,
- StructType schema,
- String[] queryColumnNames,
- String[] columnAliases,
- String[] columnComments,
- Map<String, String> properties)
- throws NoSuchNamespaceException, NoSuchViewException {
- if (null != asViewCatalog) {
- Schema icebergSchema = SparkSchemaUtil.convert(schema);
-
- try {
- Map<String, String> props =
- ImmutableMap.<String, String>builder()
- .putAll(Spark3Util.rebuildCreateProperties(properties))
- .put(SparkView.QUERY_COLUMN_NAMES,
COMMA_JOINER.join(queryColumnNames))
- .buildKeepingLast();
-
- org.apache.iceberg.view.View view =
- asViewCatalog
- .buildView(buildIdentifier(ident))
- .withDefaultCatalog(currentCatalog)
- .withDefaultNamespace(Namespace.of(currentNamespace))
- .withQuery("spark", sql)
- .withSchema(icebergSchema)
- .withLocation(properties.get("location"))
- .withProperties(props)
- .createOrReplace();
- return new SparkView(catalogName, view);
- } catch (org.apache.iceberg.exceptions.NoSuchNamespaceException e) {
- throw new NoSuchNamespaceException(currentNamespace);
- } catch (org.apache.iceberg.exceptions.NoSuchViewException e) {
- throw new NoSuchViewException(ident);
- }
+ public View replaceView(Identifier ident, View view) throws
NoSuchViewException {
+ try {
+ return commitView(ident, view, ViewCommit.REPLACE);
+ } catch (NoSuchNamespaceException e) {
+ throw unexpectedViewCommitException(
+ "replace", ident, "reported that the namespace is missing", e);
+ } catch (ViewAlreadyExistsException e) {
+ throw unexpectedViewCommitException(
+ "replace", ident, "reported that the view already exists", e);
}
-
- throw new UnsupportedOperationException(
- "Replacing a view is not supported by catalog: " + catalogName);
}
@Override
- public View alterView(Identifier ident, ViewChange... changes)
- throws NoSuchViewException, IllegalArgumentException {
- if (null != asViewCatalog) {
- try {
- org.apache.iceberg.view.View view =
asViewCatalog.loadView(buildIdentifier(ident));
- UpdateViewProperties updateViewProperties = view.updateProperties();
-
- for (ViewChange change : changes) {
- if (change instanceof ViewChange.SetProperty) {
- ViewChange.SetProperty property = (ViewChange.SetProperty) change;
- verifyNonReservedPropertyIsSet(property.property());
- updateViewProperties.set(property.property(), property.value());
- } else if (change instanceof ViewChange.RemoveProperty) {
- ViewChange.RemoveProperty remove = (ViewChange.RemoveProperty)
change;
- verifyNonReservedPropertyIsUnset(remove.property());
- updateViewProperties.remove(remove.property());
- }
- }
+ public View createOrReplaceView(Identifier ident, View view)
+ throws ViewAlreadyExistsException, NoSuchNamespaceException {
+ try {
+ return commitView(ident, view, ViewCommit.CREATE_OR_REPLACE);
+ } catch (NoSuchViewException e) {
+ throw unexpectedViewCommitException(
+ "create or replace", ident, "reported that the view is missing", e);
+ }
+ }
- updateViewProperties.commit();
+ private static RuntimeException unexpectedViewCommitException(
+ String operation, Identifier ident, String failure, Exception cause) {
+ return new IllegalStateException(
+ String.format(
+ "Cannot %s view %s because the underlying catalog %s", operation,
ident, failure),
+ cause);
+ }
- return new SparkView(catalogName, view);
- } catch (org.apache.iceberg.exceptions.NoSuchViewException e) {
+ private View commitView(Identifier ident, View view, ViewCommit viewCommit)
+ throws ViewAlreadyExistsException, NoSuchNamespaceException,
NoSuchViewException {
+ Preconditions.checkArgument(view != null, "Invalid view metadata: null");
+
+ if (null == asViewCatalog) {
+ if (viewCommit == ViewCommit.REPLACE && tableExists(ident)) {
throw new NoSuchViewException(ident);
}
+
+ throw new UnsupportedOperationException(
+ "View operations are not supported by catalog: " + catalogName);
}
- throw new UnsupportedOperationException(
- "Altering a view is not supported by catalog: " + catalogName);
- }
+ View normalizedView = normalizeViewCurrentCatalog(catalogName, view);
+ String[] currentNamespace = normalizedView.currentNamespace();
+ Map<String, String> properties = normalizedView.properties();
+ Schema icebergSchema = SparkSchemaUtil.convert(normalizedView.schema());
+ Map<String, String> props = ViewUtil.createProperties(normalizedView);
- private static void verifyNonReservedProperty(String property, String
errorMsg) {
- if (SparkView.RESERVED_PROPERTIES.contains(property)) {
- throw new UnsupportedOperationException(String.format(errorMsg,
property));
- }
- }
+ try {
+ ViewBuilder builder =
+ asViewCatalog
+ .buildView(buildIdentifier(ident))
+ .withDefaultCatalog(normalizedView.currentCatalog())
+ .withDefaultNamespace(Namespace.of(currentNamespace))
+ .withQuery("spark", normalizedView.queryText())
+ .withSchema(icebergSchema)
+ .withLocation(properties.get(TableCatalog.PROP_LOCATION))
+ .withProperties(props);
+
+ org.apache.iceberg.view.View icebergView;
+ switch (viewCommit) {
+ case CREATE:
+ icebergView = builder.create();
+ break;
+ case REPLACE:
+ icebergView = builder.replace();
+ break;
+ case CREATE_OR_REPLACE:
+ icebergView = builder.createOrReplace();
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported view commit
operation: " + viewCommit);
+ }
- private static void verifyNonReservedPropertyIsUnset(String property) {
- verifyNonReservedProperty(property, "Cannot unset reserved property:
'%s'");
- }
+ return SparkView.toView(catalogName, icebergView);
+ } catch (org.apache.iceberg.exceptions.NoSuchNamespaceException e) {
+ throw new NoSuchNamespaceException(ident.namespace());
+ } catch (AlreadyExistsException e) {
+ if (viewCommit == ViewCommit.REPLACE && tableExists(ident)) {
+ throw new NoSuchViewException(ident);
+ } else if (viewCommit == ViewCommit.REPLACE) {
+ throw e;
Review Comment:
added comments to explain
--
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]