提交 78bf5f04 authored 作者: Owner's avatar Owner

Clean up code style, add tests for DROP ALL OBJECTS with CTEs

上级 f0efc5d3
......@@ -45,6 +45,7 @@ public class TestGeneralCommonTableQueries extends AbstractBaseForCommonTableExp
testSimple4RowRecursiveQuery();
testSimple2By4RowRecursiveQuery();
testSimple3RowRecursiveQueryWithLazyEval();
testSimple3RowRecursiveQueryDropAllObjects();
}
private void testSimpleSelect() throws Exception {
......@@ -552,6 +553,29 @@ public class TestGeneralCommonTableQueries extends AbstractBaseForCommonTableExp
} finally {
config = backupConfig;
}
}
private void testSimple3RowRecursiveQueryDropAllObjects() throws Exception {
String[] expectedRowData = new String[]{"|6"};
String[] expectedColumnTypes = new String[]{"BIGINT"};
String[] expectedColumnNames = new String[]{"SUM(N)"};
String setupSQL = "DROP ALL OBJECTS;";
String withQuery = "select sum(n) from ("
+" with recursive r(n) as ("
+" (select 1) union all (select n+1 from r where n < 3)"
+" ),"
+" dummyUnsedCte(n) as ("
+" select 1 "
+" )"
+" select n from r"
+")";
int maxRetries = 10;
int expectedNumberOfRows = expectedRowData.length;
testRepeatedQueryWithSetup(maxRetries, expectedRowData, expectedColumnNames, expectedNumberOfRows, setupSQL,
withQuery, maxRetries - 1, expectedColumnTypes);
}
}
......@@ -19,7 +19,9 @@ import org.h2.test.TestBase;
public class TestMergeUsing extends TestBase implements Trigger {
private static final String GATHER_ORDERED_RESULTS_SQL = "SELECT ID, NAME FROM PARENT ORDER BY ID ASC";
private static int triggerTestingUpdateCount = 0;
private static int triggerTestingUpdateCount;
private String triggerName;
/**
* Run just this test.
......@@ -30,8 +32,6 @@ public class TestMergeUsing extends TestBase implements Trigger {
TestBase.createCaller().init().test();
}
private String triggerName;
@Override
public void test() throws Exception {
......
......@@ -27,6 +27,8 @@ public class TestPersistentCommonTableExpressions extends AbstractBaseForCommonT
testRecursiveTable();
testPersistentNonRecursiveTableInCreateView();
testPersistentRecursiveTableInCreateView();
testPersistentNonRecursiveTableInCreateViewDropAllObjects();
testPersistentRecursiveTableInCreateViewDropAllObjects();
}
private void testRecursiveTable() throws Exception {
......@@ -179,4 +181,89 @@ public class TestPersistentCommonTableExpressions extends AbstractBaseForCommonT
testRepeatedQueryWithSetup(maxRetries, expectedRowData, expectedColumnNames, expectedNumberOfRows, setupSQL,
withQuery, maxRetries - 1, expectedColumnTypes);
}
private void testPersistentNonRecursiveTableInCreateViewDropAllObjects() throws Exception {
String setupSQL = ""
+"DROP ALL OBJECTS; \n"
+"CREATE TABLE my_table ( \n"
+" id INTEGER, \n"
+" parent_fk INTEGER \n"
+"); \n"
+" \n"
+"INSERT INTO my_table ( id, parent_fk) VALUES ( 1, NULL ); \n"
+"INSERT INTO my_table ( id, parent_fk) VALUES ( 11, 1 ); \n"
+"INSERT INTO my_table ( id, parent_fk) VALUES ( 111, 11 ); \n"
+"INSERT INTO my_table ( id, parent_fk) VALUES ( 12, 1 ); \n"
+"INSERT INTO my_table ( id, parent_fk) VALUES ( 121, 12 ); \n"
+" \n"
+"CREATE OR REPLACE VIEW v_my_nr_tree AS \n"
+"WITH tree_cte_nr (sub_tree_root_id, tree_level, parent_fk, child_fk) AS ( \n"
+" SELECT mt.ID AS sub_tree_root_id, CAST(0 AS INT) AS tree_level, mt.parent_fk, mt.id \n"
+" FROM my_table mt \n"
+"), \n"
+"unused_cte AS ( SELECT 1 AS unUsedColumn ) \n"
+"SELECT sub_tree_root_id, tree_level, parent_fk, child_fk FROM tree_cte_nr; \n";
String withQuery = "SELECT * FROM v_my_nr_tree";
int maxRetries = 6;
String[] expectedRowData = new String[]{
"|1|0|null|1",
"|11|0|1|11",
"|111|0|11|111",
"|12|0|1|12",
"|121|0|12|121",
};
String[] expectedColumnNames = new String[]{"SUB_TREE_ROOT_ID", "TREE_LEVEL", "PARENT_FK", "CHILD_FK"};
String[] expectedColumnTypes = new String[]{"INTEGER", "INTEGER", "INTEGER", "INTEGER"};
int expectedNumberOfRows = 5;
testRepeatedQueryWithSetup(maxRetries, expectedRowData, expectedColumnNames, expectedNumberOfRows, setupSQL,
withQuery, maxRetries - 1, expectedColumnTypes);
}
private void testPersistentRecursiveTableInCreateViewDropAllObjects() throws Exception {
String setuoSQL = "--SET TRACE_LEVEL_SYSTEM_OUT 3;\n"
+"DROP ALL OBJECTS; \n"
+"CREATE TABLE my_tree ( \n"
+" id INTEGER, \n"
+" parent_fk INTEGER \n"
+"); \n"
+" \n"
+"INSERT INTO my_tree ( id, parent_fk) VALUES ( 1, NULL ); \n"
+"INSERT INTO my_tree ( id, parent_fk) VALUES ( 11, 1 ); \n"
+"INSERT INTO my_tree ( id, parent_fk) VALUES ( 111, 11 ); \n"
+"INSERT INTO my_tree ( id, parent_fk) VALUES ( 12, 1 ); \n"
+"INSERT INTO my_tree ( id, parent_fk) VALUES ( 121, 12 ); \n"
+" \n"
+"CREATE OR REPLACE VIEW v_my_tree AS \n"
+"WITH RECURSIVE tree_cte (sub_tree_root_id, tree_level, parent_fk, child_fk) AS ( \n"
+" SELECT mt.ID AS sub_tree_root_id, CAST(0 AS INT) AS tree_level, mt.parent_fk, mt.id \n"
+" FROM my_tree mt \n"
+" UNION ALL \n"
+" SELECT sub_tree_root_id, mtc.tree_level + 1 AS tree_level, mtc.parent_fk, mt.id \n"
+" FROM my_tree mt \n"
+"INNER JOIN tree_cte mtc ON mtc.child_fk = mt.parent_fk \n"
+"), \n"
+"unused_cte AS ( SELECT 1 AS unUsedColumn ) \n"
+"SELECT sub_tree_root_id, tree_level, parent_fk, child_fk FROM tree_cte; \n";
String withQuery = "SELECT * FROM v_my_tree";
int maxRetries = 4;
String[] expectedRowData = new String[]{"|1|0|null|1",
"|11|0|1|11",
"|111|0|11|111",
"|12|0|1|12",
"|121|0|12|121",
"|1|1|null|11",
"|11|1|1|111",
"|1|1|null|12",
"|12|1|1|121",
"|1|2|null|111",
"|1|2|null|121"
};
String[] expectedColumnNames = new String[]{"SUB_TREE_ROOT_ID", "TREE_LEVEL", "PARENT_FK", "CHILD_FK"};
String[] expectedColumnTypes = new String[]{"INTEGER", "INTEGER", "INTEGER", "INTEGER"};
int expectedNumberOfRows = 11;
testRepeatedQueryWithSetup(maxRetries, expectedRowData, expectedColumnNames, expectedNumberOfRows, setuoSQL,
withQuery, maxRetries - 1, expectedColumnTypes);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论