Skip to content

Commit ba0463d

Browse files
authored
Merge pull request #507 from scylladb/pull_mvs_in_schema_pull
Pull materialized view information when fetching schema
2 parents 672204a + 70ef5ad commit ba0463d

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

.github/workflows/cassandra.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ jobs:
2727
run: cargo build --verbose --tests
2828
- name: Run tests on cassandra
2929
# test threads must be one because else database tests will run in parallel and will result in flaky tests
30-
run: cargo test --verbose -- --test-threads=1
30+
run: cargo test --verbose -- --test-threads=1 --skip test_views_in_schema_info

scylla/src/transport/load_balancing/token_aware.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ mod tests {
312312
replication_factor: 2,
313313
},
314314
tables: HashMap::new(),
315+
views: HashMap::new(),
315316
user_defined_types: HashMap::new(),
316317
},
317318
),
@@ -322,6 +323,7 @@ mod tests {
322323
replication_factor: 3,
323324
},
324325
tables: HashMap::new(),
326+
views: HashMap::new(),
325327
user_defined_types: HashMap::new(),
326328
},
327329
),
@@ -413,6 +415,7 @@ mod tests {
413415
.collect::<HashMap<_, _>>(),
414416
},
415417
tables: HashMap::new(),
418+
views: HashMap::new(),
416419
user_defined_types: HashMap::new(),
417420
},
418421
)]

scylla/src/transport/session_test.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,3 +1978,49 @@ async fn test_unprepared_reprepare_in_caching_session_execute() {
19781978
all_rows.sort();
19791979
assert_eq!(all_rows, vec![(1, 2, 3), (1, 3, 2)]);
19801980
}
1981+
1982+
#[tokio::test]
1983+
async fn test_views_in_schema_info() {
1984+
let _ = tracing_subscriber::fmt::try_init();
1985+
1986+
let uri = std::env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
1987+
let session = SessionBuilder::new().known_node(uri).build().await.unwrap();
1988+
let ks = unique_name();
1989+
1990+
session.query(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'SimpleStrategy', 'replication_factor' : 1}}", ks), &[]).await.unwrap();
1991+
session.use_keyspace(ks.clone(), false).await.unwrap();
1992+
1993+
session
1994+
.query("CREATE TABLE t(id int PRIMARY KEY, v int)", &[])
1995+
.await
1996+
.unwrap();
1997+
1998+
session.query("CREATE MATERIALIZED VIEW mv1 AS SELECT * FROM t WHERE v IS NOT NULL PRIMARY KEY (v, id)", &[]).await.unwrap();
1999+
session.query("CREATE MATERIALIZED VIEW mv2 AS SELECT id, v FROM t WHERE v IS NOT NULL PRIMARY KEY (v, id)", &[]).await.unwrap();
2000+
2001+
session.await_schema_agreement().await.unwrap();
2002+
session.refresh_metadata().await.unwrap();
2003+
2004+
let keyspace_meta = session
2005+
.get_cluster_data()
2006+
.get_keyspace_info()
2007+
.get(&ks)
2008+
.unwrap()
2009+
.clone();
2010+
2011+
let tables = keyspace_meta
2012+
.tables
2013+
.keys()
2014+
.collect::<std::collections::HashSet<&String>>();
2015+
2016+
let views = keyspace_meta
2017+
.views
2018+
.keys()
2019+
.collect::<std::collections::HashSet<&String>>();
2020+
2021+
assert_eq!(tables, std::collections::HashSet::from([&"t".to_string()]));
2022+
assert_eq!(
2023+
views,
2024+
std::collections::HashSet::from([&"mv1".to_string(), &"mv2".to_string()])
2025+
);
2026+
}

scylla/src/transport/topology.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub struct Keyspace {
5353
/// Empty HashMap may as well mean that the client disabled schema fetching in SessionConfig
5454
pub tables: HashMap<String, Table>,
5555
/// Empty HashMap may as well mean that the client disabled schema fetching in SessionConfig
56+
pub views: HashMap<String, Table>,
57+
/// Empty HashMap may as well mean that the client disabled schema fetching in SessionConfig
5658
pub user_defined_types: HashMap<String, Vec<(String, CqlType)>>,
5759
}
5860

@@ -460,13 +462,22 @@ async fn query_keyspaces(
460462
))?;
461463

462464
let mut result = HashMap::with_capacity(rows.len());
463-
let (mut all_tables, mut all_user_defined_types) = if fetch_schema {
465+
let (mut all_tables, mut all_views, mut all_user_defined_types) = if fetch_schema {
464466
(
465-
query_tables(conn).await?,
467+
query_tables(
468+
conn,
469+
"SELECT keyspace_name, table_name FROM system_schema.tables",
470+
)
471+
.await?,
472+
query_tables(
473+
conn,
474+
"SELECT keyspace_name, view_name FROM system_schema.views",
475+
)
476+
.await?,
466477
query_user_defined_types(conn).await?,
467478
)
468479
} else {
469-
(HashMap::new(), HashMap::new())
480+
(HashMap::new(), HashMap::new(), HashMap::new())
470481
};
471482

472483
for row in rows.into_typed::<(String, HashMap<String, String>)>() {
@@ -476,6 +487,7 @@ async fn query_keyspaces(
476487

477488
let strategy: Strategy = strategy_from_string_map(strategy_map)?;
478489
let tables = all_tables.remove(&keyspace_name).unwrap_or_default();
490+
let views = all_views.remove(&keyspace_name).unwrap_or_default();
479491
let user_defined_types = all_user_defined_types
480492
.remove(&keyspace_name)
481493
.unwrap_or_default();
@@ -485,6 +497,7 @@ async fn query_keyspaces(
485497
Keyspace {
486498
strategy,
487499
tables,
500+
views,
488501
user_defined_types,
489502
},
490503
);
@@ -533,8 +546,9 @@ async fn query_user_defined_types(
533546

534547
async fn query_tables(
535548
conn: &Connection,
549+
query_str: impl Into<String>,
536550
) -> Result<HashMap<String, HashMap<String, Table>>, QueryError> {
537-
let mut tables_query = Query::new("select keyspace_name, table_name from system_schema.tables");
551+
let mut tables_query = Query::new(query_str.into());
538552
tables_query.set_page_size(1024);
539553

540554
let rows = conn

0 commit comments

Comments
 (0)