Skip to content

Commit b3468f9

Browse files
committed
topology: accept keyspace udts in into_cql_type
Previously this method accepted a map with UDTs for all keyspaces. This is not necessary: UDTs in one keyspace can not reference UDTs in another keyspace.
1 parent a216932 commit b3468f9

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

scylla/src/cluster/metadata.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,25 @@ impl PreCqlType {
221221
pub(crate) fn into_cql_type(
222222
self,
223223
keyspace_name: &String,
224-
udts: &HashMap<String, HashMap<String, Arc<UserDefinedType>>>,
224+
keyspace_udts: &HashMap<String, Arc<UserDefinedType>>,
225225
) -> CqlType {
226226
match self {
227227
PreCqlType::Native(n) => CqlType::Native(n),
228228
PreCqlType::Collection { frozen, type_ } => CqlType::Collection {
229229
frozen,
230-
type_: type_.into_collection_type(keyspace_name, udts),
230+
type_: type_.into_collection_type(keyspace_name, keyspace_udts),
231231
},
232232
PreCqlType::Tuple(t) => CqlType::Tuple(
233233
t.into_iter()
234-
.map(|t| t.into_cql_type(keyspace_name, udts))
234+
.map(|t| t.into_cql_type(keyspace_name, keyspace_udts))
235235
.collect(),
236236
),
237237
PreCqlType::Vector { type_, dimensions } => CqlType::Vector {
238-
type_: Box::new(type_.into_cql_type(keyspace_name, udts)),
238+
type_: Box::new(type_.into_cql_type(keyspace_name, keyspace_udts)),
239239
dimensions,
240240
},
241241
PreCqlType::UserDefinedType { frozen, name } => {
242-
let definition = match udts
243-
.get(keyspace_name)
244-
.and_then(|per_keyspace_udts| per_keyspace_udts.get(&name))
245-
{
242+
let definition = match keyspace_udts.get(&name) {
246243
Some(def) => Ok(def.clone()),
247244
None => Err(MissingUserDefinedType {
248245
name,
@@ -360,18 +357,18 @@ impl PreCollectionType {
360357
pub(crate) fn into_collection_type(
361358
self,
362359
keyspace_name: &String,
363-
udts: &HashMap<String, HashMap<String, Arc<UserDefinedType>>>,
360+
keyspace_udts: &HashMap<String, Arc<UserDefinedType>>,
364361
) -> CollectionType {
365362
match self {
366363
PreCollectionType::List(t) => {
367-
CollectionType::List(Box::new(t.into_cql_type(keyspace_name, udts)))
364+
CollectionType::List(Box::new(t.into_cql_type(keyspace_name, keyspace_udts)))
368365
}
369366
PreCollectionType::Map(tk, tv) => CollectionType::Map(
370-
Box::new(tk.into_cql_type(keyspace_name, udts)),
371-
Box::new(tv.into_cql_type(keyspace_name, udts)),
367+
Box::new(tk.into_cql_type(keyspace_name, keyspace_udts)),
368+
Box::new(tv.into_cql_type(keyspace_name, keyspace_udts)),
372369
),
373370
PreCollectionType::Set(t) => {
374-
CollectionType::Set(Box::new(t.into_cql_type(keyspace_name, udts)))
371+
CollectionType::Set(Box::new(t.into_cql_type(keyspace_name, keyspace_udts)))
375372
}
376373
}
377374
}
@@ -1129,22 +1126,23 @@ async fn query_user_defined_types(
11291126
field_types,
11301127
} = udt_row;
11311128

1129+
let keyspace_name_clone = keyspace_name.clone();
1130+
let keyspace_entry = udts.entry(keyspace_name).or_insert_with(HashMap::new);
1131+
11321132
let mut fields = Vec::with_capacity(field_names.len());
11331133

11341134
for (field_name, field_type) in field_names.into_iter().zip(field_types.into_iter()) {
1135-
let cql_type = field_type.into_cql_type(&keyspace_name, &udts);
1135+
let cql_type = field_type.into_cql_type(&keyspace_name_clone, keyspace_entry);
11361136
fields.push((field_name, cql_type));
11371137
}
11381138

11391139
let udt = Arc::new(UserDefinedType {
11401140
name: type_name.clone(),
1141-
keyspace: keyspace_name.clone(),
1141+
keyspace: keyspace_name_clone,
11421142
field_types: fields,
11431143
});
11441144

1145-
udts.entry(keyspace_name)
1146-
.or_insert_with(HashMap::new)
1147-
.insert(type_name, udt);
1145+
keyspace_entry.insert(type_name, udt);
11481146
}
11491147

11501148
Ok(udts)
@@ -1509,6 +1507,8 @@ async fn query_tables_schema(
15091507
}
15101508
);
15111509

1510+
let empty_map = HashMap::new();
1511+
15121512
let mut tables_schema = HashMap::new();
15131513

15141514
rows.map(|row_result| {
@@ -1518,8 +1518,9 @@ async fn query_tables_schema(
15181518
return Ok::<_, QueryError>(());
15191519
}
15201520

1521+
let keyspace_udts = udts.get(&keyspace_name).unwrap_or(&empty_map);
15211522
let pre_cql_type = map_string_to_cql_type(&type_)?;
1522-
let cql_type = pre_cql_type.into_cql_type(&keyspace_name, udts);
1523+
let cql_type = pre_cql_type.into_cql_type(&keyspace_name, keyspace_udts);
15231524

15241525
let kind = ColumnKind::from_str(&kind).map_err(|_| {
15251526
MetadataError::Tables(TablesMetadataError::UnknownColumnKind {

0 commit comments

Comments
 (0)