Skip to content

Commit

Permalink
Merge 'Invalidate prepared statements for views when their schema cha…
Browse files Browse the repository at this point in the history
…nges.' from Eliran Sinvani

When a base table changes and altered, so does the views that might
refer to the added column (which includes "SELECT *" views and also
views that might need to use this column for rows lifetime (virtual
columns).
However the query processor implementation for views change notification
was an empty function.
Since views are tables, the query processor needs to at least treat them
as such (and maybe in the future, do also some MV specific stuff).
This commit adds a call to `on_update_column_family` from within
`on_update_view`.
The side effect true to this date is that prepared statements for views
which changed due to a base table change will be invalidated.

Fixes #16392

This series also adds a test which fails without this fix and passes when the fix is applied.

Closes #16897

* github.com:scylladb/scylladb:
  Add test for mv prepared statements invalidation on base alter
  query processor: treat view changes at least as table changes

(cherry picked from commit 5810396)
  • Loading branch information
avikivity authored and nyh committed Jan 23, 2024
1 parent 5a05ccc commit 351d6d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cql3/query_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,9 @@ void query_processor::migration_subscriber::on_update_aggregate(const sstring& k
void query_processor::migration_subscriber::on_update_view(
const sstring& ks_name,
const sstring& view_name, bool columns_changed) {
// scylladb/scylladb#16392 - Materialized views are also tables so we need at least handle
// them as such when changed.
on_update_column_family(ks_name, view_name, columns_changed);
}

void query_processor::migration_subscriber::on_drop_keyspace(const sstring& ks_name) {
Expand Down
14 changes: 14 additions & 0 deletions test/cql-pytest/test_materialized_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,17 @@ def test_mv_override_clustering_order_error(cql, test_keyspace):
with pytest.raises(InvalidRequest, match="CLUSTERING ORDER BY"):
with new_materialized_view(cql, table, '*', 'p, c, x', 'p is not null and c is not null and x is not null', 'with clustering order by (c ASC, x ASC, z DESC)') as mv:
pass

# This test is regression testing added after fixing:
# https://github.com/scylladb/scylladb/issues/16392 - the gist of the issue is that
# prepared statements on views are not invalidated when the base table changes.
def test_mv_prepared_statement_with_altered_base(cql, test_keyspace):
with new_test_table(cql, test_keyspace, 'id int PRIMARY KEY, v1 int') as base:
with new_materialized_view(cql, table=base, select='*', pk='id', where='id IS NOT NULL') as view:
base_query = cql.prepare(f"SELECT * FROM {base} WHERE id=?")
view_query = cql.prepare(f"SELECT * FROM {view} WHERE id=?")
cql.execute(f"INSERT INTO {base} (id,v1) VALUES (0,0)")
assert cql.execute(base_query,[0]) == cql.execute(view_query,[0])
cql.execute(f"ALTER TABLE {base} ADD (v2 int)")
cql.execute(f"INSERT INTO {base} (id,v1,v2) VALUES (1,1,1)")
assert list(cql.execute(base_query,[1])) == list(cql.execute(view_query,[1]))

0 comments on commit 351d6d6

Please sign in to comment.