@@ -1461,4 +1461,117 @@ public void testAsyncReloadSkipsWhenAlreadyReloaded() throws Exception {
14611461 Assertions .assertEquals (initialReloadState , mv .getReloadState (),
14621462 "Async onReload should skip when already reloaded" );
14631463 }
1464+
1465+ /**
1466+ * Test that dropping a partition updates the lastSchemaUpdateTime to invalidate cached plans.
1467+ * This ensures that when a partition is dropped, any cached plans referencing the MV are invalidated.
1468+ */
1469+ @ Test
1470+ public void testDropPartitionUpdatesSchemaUpdateTime () throws Exception {
1471+ starRocksAssert .withDatabase ("test" ).useDatabase ("test" )
1472+ .withTable ("CREATE TABLE base_table_drop_partition\n " +
1473+ "(\n " +
1474+ " k1 date,\n " +
1475+ " k2 int,\n " +
1476+ " v1 int sum\n " +
1477+ ")\n " +
1478+ "PARTITION BY RANGE(k1)\n " +
1479+ "(\n " +
1480+ " PARTITION p1 values [('2022-02-01'),('2022-02-16')),\n " +
1481+ " PARTITION p2 values [('2022-02-16'),('2022-03-01'))\n " +
1482+ ")\n " +
1483+ "DISTRIBUTED BY HASH(k2) BUCKETS 3\n " +
1484+ "PROPERTIES('replication_num' = '1');" )
1485+ .withMaterializedView ("CREATE MATERIALIZED VIEW test_mv_drop_partition\n " +
1486+ "PARTITION BY k1\n " +
1487+ "DISTRIBUTED BY HASH(k2) BUCKETS 3\n " +
1488+ "REFRESH manual\n " +
1489+ "as select k1,k2,v1 from base_table_drop_partition;" );
1490+
1491+ Database testDb = GlobalStateMgr .getCurrentState ().getLocalMetastore ().getDb ("test" );
1492+ MaterializedView mv = ((MaterializedView ) GlobalStateMgr .getCurrentState ().getLocalMetastore ()
1493+ .getTable (testDb .getFullName (), "test_mv_drop_partition" ));
1494+
1495+ // Wait for initial reload
1496+ mv .waitForReloaded ();
1497+ Assertions .assertTrue (mv .hasReloaded ());
1498+
1499+ // Get the initial schema update time
1500+ long initialSchemaUpdateTime = mv .lastSchemaUpdateTime .get ();
1501+
1502+ // Wait a small amount to ensure time progresses
1503+ Thread .sleep (10 );
1504+
1505+ // Record time before drop partition
1506+ long beforeDropTime = System .nanoTime ();
1507+
1508+ // Drop a partition
1509+ mv .dropPartition (testDb .getId (), "p1" , false );
1510+
1511+ // Verify the partition was dropped
1512+ Assertions .assertNull (mv .getPartition ("p1" ), "Partition p1 should be dropped" );
1513+
1514+ // Verify lastSchemaUpdateTime was updated
1515+ long afterDropSchemaUpdateTime = mv .lastSchemaUpdateTime .get ();
1516+ Assertions .assertTrue (afterDropSchemaUpdateTime > initialSchemaUpdateTime ,
1517+ "lastSchemaUpdateTime should be updated after dropping partition" );
1518+ Assertions .assertTrue (afterDropSchemaUpdateTime >= beforeDropTime ,
1519+ "lastSchemaUpdateTime should be >= time before drop" );
1520+ }
1521+
1522+ /**
1523+ * Test that force dropping a partition also updates the lastSchemaUpdateTime.
1524+ */
1525+ @ Test
1526+ public void testForceDropPartitionUpdatesSchemaUpdateTime () throws Exception {
1527+ starRocksAssert .withDatabase ("test" ).useDatabase ("test" )
1528+ .withTable ("CREATE TABLE base_table_force_drop\n " +
1529+ "(\n " +
1530+ " k1 date,\n " +
1531+ " k2 int,\n " +
1532+ " v1 int sum\n " +
1533+ ")\n " +
1534+ "PARTITION BY RANGE(k1)\n " +
1535+ "(\n " +
1536+ " PARTITION p1 values [('2022-02-01'),('2022-02-16')),\n " +
1537+ " PARTITION p2 values [('2022-02-16'),('2022-03-01'))\n " +
1538+ ")\n " +
1539+ "DISTRIBUTED BY HASH(k2) BUCKETS 3\n " +
1540+ "PROPERTIES('replication_num' = '1');" )
1541+ .withMaterializedView ("CREATE MATERIALIZED VIEW test_mv_force_drop\n " +
1542+ "PARTITION BY k1\n " +
1543+ "DISTRIBUTED BY HASH(k2) BUCKETS 3\n " +
1544+ "REFRESH manual\n " +
1545+ "as select k1,k2,v1 from base_table_force_drop;" );
1546+
1547+ Database testDb = GlobalStateMgr .getCurrentState ().getLocalMetastore ().getDb ("test" );
1548+ MaterializedView mv = ((MaterializedView ) GlobalStateMgr .getCurrentState ().getLocalMetastore ()
1549+ .getTable (testDb .getFullName (), "test_mv_force_drop" ));
1550+
1551+ // Wait for initial reload
1552+ mv .waitForReloaded ();
1553+ Assertions .assertTrue (mv .hasReloaded ());
1554+
1555+ // Get the initial schema update time
1556+ long initialSchemaUpdateTime = mv .lastSchemaUpdateTime .get ();
1557+
1558+ // Wait a small amount to ensure time progresses
1559+ Thread .sleep (10 );
1560+
1561+ // Record time before drop partition
1562+ long beforeDropTime = System .nanoTime ();
1563+
1564+ // Force drop a partition
1565+ mv .dropPartition (testDb .getId (), "p1" , true );
1566+
1567+ // Verify the partition was dropped
1568+ Assertions .assertNull (mv .getPartition ("p1" ), "Partition p1 should be force dropped" );
1569+
1570+ // Verify lastSchemaUpdateTime was updated
1571+ long afterDropSchemaUpdateTime = mv .lastSchemaUpdateTime .get ();
1572+ Assertions .assertTrue (afterDropSchemaUpdateTime > initialSchemaUpdateTime ,
1573+ "lastSchemaUpdateTime should be updated after force dropping partition" );
1574+ Assertions .assertTrue (afterDropSchemaUpdateTime >= beforeDropTime ,
1575+ "lastSchemaUpdateTime should be >= time before force drop" );
1576+ }
14641577}
0 commit comments