Skip to content

Commit

Permalink
[SPARK-45807][SQL] Add createOrReplaceView(..) / replaceView(..) to V…
Browse files Browse the repository at this point in the history
…iewCatalog

### What changes were proposed in this pull request?

ViewCatalog API improvements described in [SPIP](https://docs.google.com/document/d/1XOxFtloiMuW24iqJ-zJnDzHl2KMxipTjJoxleJFz66A/edit?usp=sharing) that didn't make it into the codebase as part of apache#37556

### Why are the changes needed?

Required for DataSourceV2 view support.

### Does this PR introduce _any_ user-facing change?

No
### How was this patch tested?

N/A

### Was this patch authored or co-authored using generative AI tooling?

N/A

Closes apache#43677 from nastra/SPARK-45807.

Authored-by: Eduard Tudenhoefner <etudenhoefner@gmail.com>
Signed-off-by: Holden Karau <holden@pigscanfly.ca>
  • Loading branch information
nastra authored and holdenk committed Dec 5, 2023
1 parent ef05fb6 commit 89673da
Showing 1 changed file with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,92 @@ View createView(
String[] columnComments,
Map<String, String> properties) throws ViewAlreadyExistsException, NoSuchNamespaceException;

/**
* Replace a view in the catalog.
* <p>
* The default implementation has a race condition.
* Catalogs are encouraged to implement this operation atomically.
*
* @param ident a view identifier
* @param sql the SQL text that defines the view
* @param currentCatalog the current catalog
* @param currentNamespace the current namespace
* @param schema the view query output schema
* @param queryColumnNames the query column names
* @param columnAliases the column aliases
* @param columnComments the column comments
* @param properties the view properties
* @throws NoSuchViewException If the view doesn't exist or is a table
* @throws NoSuchNamespaceException If the identifier namespace does not exist (optional)
*/
default void replaceView(
Identifier ident,
String sql,
String currentCatalog,
String[] currentNamespace,
StructType schema,
String[] queryColumnNames,
String[] columnAliases,
String[] columnComments,
Map<String, String> properties) throws NoSuchViewException, NoSuchNamespaceException {
if (viewExists(ident)) {
dropView(ident);
try {
createView(ident, sql, currentCatalog, currentNamespace, schema,
queryColumnNames, columnAliases, columnComments, properties);
}
catch (ViewAlreadyExistsException e) {
throw new RuntimeException("Race condition when dropping and creating view", e);
}
} else {
throw new NoSuchViewException(ident);
}
}

/**
* Create or replace a view in the catalog.
* <p>
* The default implementation has race conditions.
* Catalogs are encouraged to implement this operation atomically.
*
* @param ident a view identifier
* @param sql the SQL text that defines the view
* @param currentCatalog the current catalog
* @param currentNamespace the current namespace
* @param schema the view query output schema
* @param queryColumnNames the query column names
* @param columnAliases the column aliases
* @param columnComments the column comments
* @param properties the view properties
* @throws NoSuchNamespaceException If the identifier namespace does not exist (optional)
*/
default void createOrReplaceView(
Identifier ident,
String sql,
String currentCatalog,
String[] currentNamespace,
StructType schema,
String[] queryColumnNames,
String[] columnAliases,
String[] columnComments,
Map<String, String> properties) throws NoSuchNamespaceException {
if (viewExists(ident)) {
try {
replaceView(ident, sql, currentCatalog, currentNamespace, schema,
queryColumnNames, columnAliases, columnComments, properties);
} catch (NoSuchViewException e) {
throw new RuntimeException("Race condition when checking and replacing view", e);
}
} else {
try {
createView(ident, sql, currentCatalog, currentNamespace, schema,
queryColumnNames, columnAliases, columnComments, properties);
} catch (ViewAlreadyExistsException e) {
throw new RuntimeException("Race condition when checking and creating view", e);
}
}
}

/**
* Apply {@link ViewChange changes} to a view in the catalog.
* <p>
Expand Down

0 comments on commit 89673da

Please sign in to comment.