Correctly handle materialized views