2222import org .apache .fluss .exception .IllegalConfigurationException ;
2323import org .apache .fluss .exception .InvalidPartitionException ;
2424import org .apache .fluss .exception .InvalidTableException ;
25+ import org .apache .fluss .flink .lake .LakeFlinkCatalog ;
2526import org .apache .fluss .flink .utils .FlinkConversionsTest ;
2627import org .apache .fluss .server .testutils .FlussClusterExtension ;
2728import org .apache .fluss .utils .ExceptionUtils ;
3637import org .apache .flink .table .catalog .CatalogPartitionSpec ;
3738import org .apache .flink .table .catalog .CatalogTable ;
3839import org .apache .flink .table .catalog .Column ;
40+ import org .apache .flink .table .catalog .GenericInMemoryCatalog ;
3941import org .apache .flink .table .catalog .IntervalFreshness ;
4042import org .apache .flink .table .catalog .ObjectPath ;
4143import org .apache .flink .table .catalog .ResolvedCatalogMaterializedTable ;
5860import org .apache .flink .table .expressions .utils .ResolvedExpressionMock ;
5961import org .apache .flink .table .factories .Factory ;
6062import org .apache .flink .table .factories .FactoryUtil ;
61- import org .junit .jupiter .api .AfterAll ;
62- import org .junit .jupiter .api .BeforeAll ;
63+ import org .junit .jupiter .api .AfterEach ;
6364import org .junit .jupiter .api .BeforeEach ;
6465import org .junit .jupiter .api .Test ;
6566import org .junit .jupiter .api .extension .RegisterExtension ;
7879import static org .apache .fluss .flink .FlinkConnectorOptions .BUCKET_KEY ;
7980import static org .apache .fluss .flink .FlinkConnectorOptions .BUCKET_NUMBER ;
8081import static org .apache .fluss .flink .FlinkConnectorOptions .SCAN_STARTUP_MODE ;
82+ import static org .apache .fluss .flink .adapter .CatalogTableAdapter .toCatalogTable ;
8183import static org .apache .fluss .flink .utils .CatalogTableTestUtils .addOptions ;
8284import static org .apache .fluss .flink .utils .CatalogTableTestUtils .checkEqualsIgnoreSchema ;
8385import static org .apache .fluss .flink .utils .CatalogTableTestUtils .checkEqualsRespectSchema ;
@@ -101,7 +103,8 @@ class FlinkCatalogTest {
101103 private static final FlinkConversionsTest .TestRefreshHandler REFRESH_HANDLER =
102104 new FlinkConversionsTest .TestRefreshHandler ("jobID: xxx, clusterId: yyy" );
103105
104- static Catalog catalog ;
106+ private Catalog catalog ;
107+ private MockLakeFlinkCatalog mockLakeCatalog ;
105108 private final ObjectPath tableInDefaultDb = new ObjectPath (DEFAULT_DB , "t1" );
106109
107110 private static Configuration initConfig () {
@@ -110,7 +113,7 @@ private static Configuration initConfig() {
110113 return configuration ;
111114 }
112115
113- private ResolvedSchema createSchema () {
116+ protected ResolvedSchema createSchema () {
114117 return new ResolvedSchema (
115118 Arrays .asList (
116119 Column .physical ("first" , DataTypes .STRING ().notNull ()),
@@ -128,7 +131,7 @@ private CatalogTable newCatalogTable(Map<String, String> options) {
128131 private CatalogTable newCatalogTable (
129132 ResolvedSchema resolvedSchema , Map <String , String > options ) {
130133 CatalogTable origin =
131- CatalogTable . of (
134+ toCatalogTable (
132135 Schema .newBuilder ().fromResolvedSchema (resolvedSchema ).build (),
133136 "test comment" ,
134137 Collections .emptyList (),
@@ -158,29 +161,36 @@ private CatalogMaterializedTable newCatalogMaterializedTable(
158161 return new ResolvedCatalogMaterializedTable (origin , resolvedSchema );
159162 }
160163
161- @ BeforeAll
162- static void beforeAll () {
164+ protected FlinkCatalog initCatalog (
165+ String catalogName ,
166+ String databaseName ,
167+ String bootstrapServers ,
168+ LakeFlinkCatalog lakeFlinkCatalog ) {
169+ return new FlinkCatalog (
170+ catalogName ,
171+ databaseName ,
172+ bootstrapServers ,
173+ Thread .currentThread ().getContextClassLoader (),
174+ Collections .emptyMap (),
175+ lakeFlinkCatalog );
176+ }
177+
178+ @ BeforeEach
179+ void beforeEach () throws Exception {
163180 // set fluss conf
164181 Configuration flussConf = FLUSS_CLUSTER_EXTENSION .getClientConfig ();
182+
183+ mockLakeCatalog =
184+ new MockLakeFlinkCatalog (
185+ CATALOG_NAME , Thread .currentThread ().getContextClassLoader ());
165186 catalog =
166- new FlinkCatalog (
187+ initCatalog (
167188 CATALOG_NAME ,
168189 DEFAULT_DB ,
169190 String .join ("," , flussConf .get (BOOTSTRAP_SERVERS )),
170- Thread .currentThread ().getContextClassLoader (),
171- Collections .emptyMap ());
191+ mockLakeCatalog );
172192 catalog .open ();
173- }
174193
175- @ AfterAll
176- static void afterAll () {
177- if (catalog != null ) {
178- catalog .close ();
179- }
180- }
181-
182- @ BeforeEach
183- void beforeEach () throws Exception {
184194 // First check if database exists, and drop it if it does
185195 if (catalog .databaseExists (DEFAULT_DB )) {
186196 catalog .dropDatabase (DEFAULT_DB , true , true );
@@ -198,6 +208,13 @@ void beforeEach() throws Exception {
198208 }
199209 }
200210
211+ @ AfterEach
212+ void afterEach () {
213+ if (catalog != null ) {
214+ catalog .close ();
215+ }
216+ }
217+
201218 @ Test
202219 void testCreateTable () throws Exception {
203220 Map <String , String > options = new HashMap <>();
@@ -270,7 +287,7 @@ void testCreateTable() throws Exception {
270287 ResolvedSchema resolvedSchema = this .createSchema ();
271288 CatalogTable table2 =
272289 new ResolvedCatalogTable (
273- CatalogTable . of (
290+ toCatalogTable (
274291 Schema .newBuilder ().fromResolvedSchema (resolvedSchema ).build (),
275292 "test comment" ,
276293 Collections .singletonList ("first" ),
@@ -306,6 +323,11 @@ void testCreateAlreadyExistsLakeTable() throws Exception {
306323 CatalogTable table = this .newCatalogTable (options );
307324 catalog .createTable (lakeTablePath , table , false );
308325 assertThat (catalog .tableExists (lakeTablePath )).isTrue ();
326+ // get the lake table from lake catalog.
327+ mockLakeCatalog .registerLakeTable (lakeTablePath , table );
328+ assertThat ((CatalogTable ) catalog .getTable (new ObjectPath (DEFAULT_DB , "lake_table$lake" )))
329+ .isEqualTo (table );
330+
309331 // drop fluss table
310332 catalog .dropTable (lakeTablePath , false );
311333 assertThat (catalog .tableExists (lakeTablePath )).isFalse ();
@@ -363,7 +385,7 @@ void testCreateTableWithWatermarkAndComputedCol() throws Exception {
363385 UniqueConstraint .primaryKey (
364386 "PK_first" , Collections .singletonList ("first" )));
365387 CatalogTable origin =
366- CatalogTable . of (
388+ toCatalogTable (
367389 Schema .newBuilder ().fromResolvedSchema (resolvedSchema ).build (),
368390 "test comment" ,
369391 Collections .emptyList (),
@@ -648,7 +670,7 @@ void testOperatePartitions() throws Exception {
648670 ResolvedSchema resolvedSchema = this .createSchema ();
649671 CatalogTable table2 =
650672 new ResolvedCatalogTable (
651- CatalogTable . of (
673+ toCatalogTable (
652674 Schema .newBuilder ().fromResolvedSchema (resolvedSchema ).build (),
653675 "test comment" ,
654676 Collections .singletonList ("first" ),
@@ -758,7 +780,7 @@ void testCreatePartitions() throws Exception {
758780 ObjectPath partitionedPath = new ObjectPath (DEFAULT_DB , "partitioned_table1" );
759781 CatalogTable partitionedTable =
760782 new ResolvedCatalogTable (
761- CatalogTable . of (
783+ toCatalogTable (
762784 Schema .newBuilder ().fromResolvedSchema (resolvedSchema ).build (),
763785 "test comment" ,
764786 Collections .singletonList ("first" ),
@@ -845,7 +867,7 @@ void testStatisticsOperations() throws Exception {
845867 ResolvedSchema schema = createSchema ();
846868 CatalogTable partTable =
847869 new ResolvedCatalogTable (
848- CatalogTable . of (
870+ toCatalogTable (
849871 Schema .newBuilder ().fromResolvedSchema (schema ).build (),
850872 "partitioned table for stats" ,
851873 Collections .singletonList ("first" ),
@@ -974,4 +996,23 @@ private void createAndCheckAndDropTable(
974996 checkEqualsRespectSchema ((CatalogTable ) tableCreated , table );
975997 catalog .dropTable (tablePath , false );
976998 }
999+
1000+ private static class MockLakeFlinkCatalog extends LakeFlinkCatalog {
1001+ private final GenericInMemoryCatalog catalog ;
1002+
1003+ public MockLakeFlinkCatalog (String catalogName , ClassLoader classLoader ) {
1004+ super (catalogName , classLoader );
1005+ catalog = new GenericInMemoryCatalog (catalogName , DEFAULT_DB );
1006+ }
1007+
1008+ @ Override
1009+ public Catalog getLakeCatalog (Configuration tableOptions ) {
1010+ return catalog ;
1011+ }
1012+
1013+ void registerLakeTable (ObjectPath tablePath , CatalogTable table )
1014+ throws TableAlreadyExistException , DatabaseNotExistException {
1015+ catalog .createTable (tablePath , table , false );
1016+ }
1017+ }
9771018}
0 commit comments