rdblue commented on code in PR #9423:
URL: https://github.com/apache/iceberg/pull/9423#discussion_r1466949343
##########
spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java:
##########
@@ -886,6 +886,202 @@ private String viewName(String viewName) {
return viewName + new Random().nextInt(1000000);
}
+ @Test
+ public void createViewIfNotExists() {
+ String viewName = "viewThatAlreadyExists";
+ sql("CREATE VIEW %s AS SELECT id FROM %s", viewName, tableName);
+
+ assertThatThrownBy(() -> sql("CREATE VIEW %s AS SELECT id FROM %s",
viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ String.format(
+ "Cannot create view %s.%s because it already exists",
NAMESPACE, viewName));
+
+ // using IF NOT EXISTS should work
+ assertThatNoException()
+ .isThrownBy(
+ () -> sql("CREATE VIEW IF NOT EXISTS %s AS SELECT id FROM %s",
viewName, tableName));
+ }
+
+ @Test
+ public void createViewWithInvalidSQL() {
+ assertThatThrownBy(() -> sql("CREATE VIEW simpleViewWithInvalidSQL AS
invalid SQL"))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Syntax error");
+ }
+
+ @Test
+ public void createViewReferencingTempView() throws NoSuchTableException {
+ insertRows(10);
+ String tempView = "temporaryViewBeingReferencedInAnotherView";
+ String viewReferencingTempView = "viewReferencingTemporaryView";
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 5",
tempView, tableName);
+
+ // creating a view that references a TEMP VIEW shouldn't be possible
+ assertThatThrownBy(
+ () -> sql("CREATE VIEW %s AS SELECT id FROM %s",
viewReferencingTempView, tempView))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
+ .hasMessageContaining(viewReferencingTempView)
+ .hasMessageContaining("of the type VIEW because it references to the
temporary object")
+ .hasMessageContaining(tempView);
+ }
+
+ @Test
+ public void createViewReferencingGlobalTempView() throws
NoSuchTableException {
+ insertRows(10);
+ String globalTempView = "globalTemporaryViewBeingReferenced";
+ String viewReferencingTempView = "viewReferencingGlobalTemporaryView";
+
+ sql(
+ "CREATE GLOBAL TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 5",
+ globalTempView, tableName);
+
+ // creating a view that references a GLOBAL TEMP VIEW shouldn't be possible
+ assertThatThrownBy(
+ () ->
+ sql(
+ "CREATE VIEW %s AS SELECT id FROM global_temp.%s",
+ viewReferencingTempView, globalTempView))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
+ .hasMessageContaining(viewReferencingTempView)
+ .hasMessageContaining("of the type VIEW because it references to the
temporary object")
+ .hasMessageContaining(globalTempView);
+ }
+
+ @Test
+ public void createViewUsingNonExistingTable() {
+ assertThatThrownBy(
+ () -> sql("CREATE VIEW viewWithNonExistingTable AS SELECT id FROM
non_existing"))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("The table or view `non_existing` cannot be
found");
+ }
+
+ @Test
+ public void createViewWithMismatchedColumnCounts() {
+ String viewName = "viewWithMismatchedColumnCounts";
+
+ assertThatThrownBy(
+ () -> sql("CREATE VIEW %s (id, data) AS SELECT id FROM %s",
viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(String.format("Cannot create view %s.%s",
NAMESPACE, viewName))
+ .hasMessageContaining("not enough data columns")
+ .hasMessageContaining("View columns: id, data")
+ .hasMessageContaining("Data columns: id");
+
+ assertThatThrownBy(
+ () -> sql("CREATE VIEW %s (id) AS SELECT id, data FROM %s",
viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(String.format("Cannot create view %s.%s",
NAMESPACE, viewName))
+ .hasMessageContaining("too many data columns")
+ .hasMessageContaining("View columns: id")
+ .hasMessageContaining("Data columns: id, data");
+ }
+
+ @Test
+ public void createViewWithColumnAliases() throws NoSuchTableException {
Review Comment:
Yeah, I don't see other tests that check the schema so this one should
definitely do it.
--
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]