@@ -189,8 +189,9 @@ public void testMaterializedViewMetadata()
189189
190190 assertUpdate ("CREATE MATERIALIZED VIEW test_mv_metadata AS SELECT id, name FROM test_mv_metadata_base WHERE id > 0" );
191191
192- assertQueryReturnsEmptyResult ("SELECT table_name FROM information_schema.tables " +
193- "WHERE table_schema = 'test_schema' AND table_name = 'test_mv_metadata' AND table_type = 'MATERIALIZED VIEW'" );
192+ assertQuery ("SELECT table_name, table_type FROM information_schema.tables " +
193+ "WHERE table_schema = 'test_schema' AND table_name = 'test_mv_metadata'" ,
194+ "VALUES ('test_mv_metadata', 'MATERIALIZED VIEW')" );
194195
195196 assertUpdate ("DROP MATERIALIZED VIEW test_mv_metadata" );
196197 assertUpdate ("DROP TABLE test_mv_metadata_base" );
@@ -1451,4 +1452,147 @@ public void testCreateMaterializedViewWithSameNameAsExistingTable()
14511452 assertUpdate ("DROP TABLE existing_table_name" );
14521453 assertUpdate ("DROP TABLE test_mv_base" );
14531454 }
1455+
1456+ @ Test
1457+ public void testInformationSchemaMaterializedViews ()
1458+ {
1459+ assertUpdate ("CREATE TABLE test_is_mv_base1 (id BIGINT, name VARCHAR, value BIGINT)" );
1460+ assertUpdate ("CREATE TABLE test_is_mv_base2 (category VARCHAR, amount BIGINT)" );
1461+
1462+ assertUpdate ("INSERT INTO test_is_mv_base1 VALUES (1, 'Alice', 100), (2, 'Bob', 200)" , 2 );
1463+ assertUpdate ("INSERT INTO test_is_mv_base2 VALUES ('A', 50), ('B', 75)" , 2 );
1464+
1465+ assertUpdate ("CREATE MATERIALIZED VIEW test_is_mv1 AS SELECT id, name, value FROM test_is_mv_base1 WHERE id > 0" );
1466+ assertUpdate ("CREATE MATERIALIZED VIEW test_is_mv2 AS SELECT category, SUM(amount) as total FROM test_is_mv_base2 GROUP BY category" );
1467+
1468+ assertQuery (
1469+ "SELECT table_name FROM information_schema.materialized_views " +
1470+ "WHERE table_schema = 'test_schema' AND table_name IN ('test_is_mv1', 'test_is_mv2') " +
1471+ "ORDER BY table_name" ,
1472+ "VALUES ('test_is_mv1'), ('test_is_mv2')" );
1473+
1474+ assertQuery (
1475+ "SELECT table_catalog, table_schema, table_name, storage_schema, storage_table_name, base_tables " +
1476+ "FROM information_schema.materialized_views " +
1477+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv1'" ,
1478+ "SELECT 'iceberg', 'test_schema', 'test_is_mv1', 'test_schema', '__mv_storage__test_is_mv1', 'iceberg.test_schema.test_is_mv_base1'" );
1479+
1480+ assertQuery (
1481+ "SELECT COUNT(*) FROM information_schema.materialized_views " +
1482+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv1' " +
1483+ "AND view_definition IS NOT NULL AND length(view_definition) > 0" ,
1484+ "SELECT 1" );
1485+
1486+ assertQuery (
1487+ "SELECT table_name FROM information_schema.materialized_views " +
1488+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv2'" ,
1489+ "VALUES ('test_is_mv2')" );
1490+
1491+ assertQuery (
1492+ "SELECT COUNT(*) FROM information_schema.materialized_views " +
1493+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv1' " +
1494+ "AND view_owner IS NOT NULL" ,
1495+ "SELECT 1" );
1496+
1497+ assertQuery (
1498+ "SELECT COUNT(*) FROM information_schema.materialized_views " +
1499+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv1' " +
1500+ "AND view_security IS NOT NULL" ,
1501+ "SELECT 1" );
1502+
1503+ assertQuery (
1504+ "SELECT base_tables FROM information_schema.materialized_views " +
1505+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv2'" ,
1506+ "VALUES ('iceberg.test_schema.test_is_mv_base2')" );
1507+
1508+ assertUpdate ("DROP MATERIALIZED VIEW test_is_mv1" );
1509+ assertUpdate ("DROP MATERIALIZED VIEW test_is_mv2" );
1510+ assertUpdate ("DROP TABLE test_is_mv_base1" );
1511+ assertUpdate ("DROP TABLE test_is_mv_base2" );
1512+
1513+ assertQuery (
1514+ "SELECT COUNT(*) FROM information_schema.materialized_views " +
1515+ "WHERE table_schema = 'test_schema' AND table_name IN ('test_is_mv1', 'test_is_mv2')" ,
1516+ "VALUES 0" );
1517+ }
1518+
1519+ @ Test
1520+ public void testInformationSchemaTablesWithMaterializedViews ()
1521+ {
1522+ assertUpdate ("CREATE TABLE test_is_tables_base (id BIGINT, name VARCHAR)" );
1523+ assertUpdate ("CREATE VIEW test_is_tables_view AS SELECT id, name FROM test_is_tables_base" );
1524+ assertUpdate ("CREATE MATERIALIZED VIEW test_is_tables_mv AS SELECT id, name FROM test_is_tables_base" );
1525+
1526+ assertQuery (
1527+ "SELECT table_name, table_type FROM information_schema.tables " +
1528+ "WHERE table_schema = 'test_schema' AND table_name IN ('test_is_tables_base', 'test_is_tables_view', 'test_is_tables_mv') " +
1529+ "ORDER BY table_name" ,
1530+ "VALUES ('test_is_tables_base', 'BASE TABLE'), ('test_is_tables_mv', 'MATERIALIZED VIEW'), ('test_is_tables_view', 'VIEW')" );
1531+
1532+ assertQuery (
1533+ "SELECT table_name FROM information_schema.views " +
1534+ "WHERE table_schema = 'test_schema' AND table_name IN ('test_is_tables_view', 'test_is_tables_mv') " +
1535+ "ORDER BY table_name" ,
1536+ "VALUES ('test_is_tables_view')" );
1537+
1538+ assertUpdate ("DROP MATERIALIZED VIEW test_is_tables_mv" );
1539+ assertUpdate ("DROP VIEW test_is_tables_view" );
1540+ assertUpdate ("DROP TABLE test_is_tables_base" );
1541+ }
1542+
1543+ @ Test
1544+ public void testInformationSchemaMaterializedViewsAfterRefresh ()
1545+ {
1546+ assertUpdate ("CREATE TABLE test_is_mv_refresh_base (id BIGINT, value BIGINT)" );
1547+ assertUpdate ("INSERT INTO test_is_mv_refresh_base VALUES (1, 100), (2, 200)" , 2 );
1548+ assertUpdate ("CREATE MATERIALIZED VIEW test_is_mv_refresh AS SELECT id, value FROM test_is_mv_refresh_base" );
1549+
1550+ assertQuery (
1551+ "SELECT freshness_state FROM information_schema.materialized_views " +
1552+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv_refresh'" ,
1553+ "SELECT 'NOT_MATERIALIZED'" );
1554+
1555+ assertUpdate ("REFRESH MATERIALIZED VIEW test_is_mv_refresh" , 2 );
1556+
1557+ assertQuery (
1558+ "SELECT freshness_state FROM information_schema.materialized_views " +
1559+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv_refresh'" ,
1560+ "SELECT 'FULLY_MATERIALIZED'" );
1561+
1562+ assertUpdate ("INSERT INTO test_is_mv_refresh_base VALUES (3, 300)" , 1 );
1563+
1564+ assertQuery (
1565+ "SELECT freshness_state FROM information_schema.materialized_views " +
1566+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv_refresh'" ,
1567+ "SELECT 'PARTIALLY_MATERIALIZED'" );
1568+
1569+ assertUpdate ("UPDATE test_is_mv_refresh_base SET value = 250 WHERE id = 2" , 1 );
1570+
1571+ assertQuery (
1572+ "SELECT freshness_state FROM information_schema.materialized_views " +
1573+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv_refresh'" ,
1574+ "SELECT 'PARTIALLY_MATERIALIZED'" );
1575+
1576+ assertUpdate ("DELETE FROM test_is_mv_refresh_base WHERE id = 1" , 1 );
1577+
1578+ assertQuery (
1579+ "SELECT freshness_state FROM information_schema.materialized_views " +
1580+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv_refresh'" ,
1581+ "SELECT 'PARTIALLY_MATERIALIZED'" );
1582+
1583+ assertUpdate ("REFRESH MATERIALIZED VIEW test_is_mv_refresh" , 2 );
1584+
1585+ assertQuery (
1586+ "SELECT freshness_state FROM information_schema.materialized_views " +
1587+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv_refresh'" ,
1588+ "SELECT 'FULLY_MATERIALIZED'" );
1589+
1590+ assertUpdate ("DROP MATERIALIZED VIEW test_is_mv_refresh" );
1591+ assertUpdate ("DROP TABLE test_is_mv_refresh_base" );
1592+
1593+ assertQuery (
1594+ "SELECT COUNT(*) FROM information_schema.materialized_views " +
1595+ "WHERE table_schema = 'test_schema' AND table_name = 'test_is_mv_refresh'" ,
1596+ "VALUES 0" );
1597+ }
14541598}
0 commit comments