diff --git a/core/src/test/java/org/apache/calcite/test/LatticeTest.java b/core/src/test/java/org/apache/calcite/test/LatticeTest.java
index 492b3775d7f9..0e79d12026ab 100644
--- a/core/src/test/java/org/apache/calcite/test/LatticeTest.java
+++ b/core/src/test/java/org/apache/calcite/test/LatticeTest.java
@@ -529,6 +529,62 @@ private void check(int n) throws IOException {
// TODO
}
+ /** Test case for
+ * [CALCITE-787]
+ * Wrong assignment of star table to Materialized views */
+ @Test public void testOneLatticeOneMV() {
+ final AtomicInteger counter = new AtomicInteger();
+ final Class clazz =
+ JdbcTest.EmpDeptTableFactory.class;
+
+ final String mv = " materializations: [\n"
+ + " {\n"
+ + " table: \"m0\",\n"
+ + " view: \"m0v\",\n"
+ + " sql: \"select * from \\\"foodmart\\\".\\\"sales_fact_1997\\\" where \\\"product_id\\\" = 10\" "
+ + " }\n"
+ + " ]\n";
+
+ final String model =
+ ""
+ + "{\n"
+ + " version: '1.0',\n"
+ + " schemas: [\n"
+ + JdbcTest.FOODMART_SCHEMA
+ + ",\n"
+ + " {\n"
+ + " name: 'adhoc',\n"
+ + " tables: [\n"
+ + " {\n"
+ + " name: 'EMPLOYEES',\n"
+ + " type: 'custom',\n"
+ + " factory: '"
+ + clazz.getName()
+ + "',\n"
+ + " operand: {'foo': true, 'bar': 345}\n"
+ + " }\n"
+ + " ],\n"
+ + " lattices: " + "[" + INVENTORY_LATTICE
+ + " ]\n"
+ + " },\n"
+ + " {\n"
+ + " name: 'mat',\n"
+ + mv
+ + " }\n"
+ + " ]\n"
+ + "}";
+
+ CalciteAssert.model(model)
+ .withDefaultSchema("foodmart")
+ .query("select * from \"foodmart\".\"sales_fact_1997\" where \"product_id\" = 10")
+ .enableMaterializations(true)
+ .substitutionMatches(
+ CalciteAssert.checkRel(
+ "EnumerableTableScan(table=[[mat, m0]])\n",
+ counter));
+ assertThat(counter.intValue(), equalTo(1));
+ }
+
private CalciteAssert.AssertThat foodmartModel(String... extras) {
return modelWithLattice("star",
"select 1 from \"foodmart\".\"sales_fact_1997\" as \"s\"\n"