1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! Issue #702 (review fixup): CI-guarded unit tests for the `is_static` bitmap
//! filtering logic introduced in `row_decoder.rs`.
//!
//! ## What this tests
//!
//! The `columns_in_order` filtering at the core of the row-cell parsing path
//! (row_decoder.rs ~3310-3343) filters `ColumnInfo` entries from the
//! serialization header to include only the column group that matches the current
//! row kind (`is_static`). This is critical for tables that have BOTH static and
//! regular columns: including the wrong group shifts all bitmap indices and causes
//! cells to be misread or dropped.
//!
//! These tests reproduce that logic with synthetic `ColumnInfo` / `Column` data
//! so they run in CI **without** any binary SSTable data.
//!
//! ## Why this matters
//!
//! Before issue #702 the bitmap index was computed over ALL non-key columns,
//! mixing static and regular columns. For a table with 1 static column + 2
//! regular columns, the static row's bitmap was size-3 but only 1 column was
//! static, so bit-0 mapped to the static column correctly — but when Cassandra
//! encoded a regular row's bitmap it was relative to the 2 regular columns only.
//! Using a combined list caused bit-0 to hit the STATIC column instead of the
//! first REGULAR column, silently dropping the regular column's data.
use cqlite_core::parser::ColumnInfo;
use cqlite_core::schema::{ClusteringColumn, KeyColumn};
use cqlite_core::schema::{Column, TableSchema};
use std::collections::HashMap;
// ---------------------------------------------------------------------------
// Helpers — mirror the filtering logic from row_decoder.rs
// ---------------------------------------------------------------------------
/// Simulate the `columns_in_order` build-and-filter logic from the row parser:
/// 1. Build a schema-column lookup map.
/// 2. Iterate serialization-header columns, keep only those where
/// `!is_primary_key && !is_clustering && col.is_static == row_is_static`.
/// 3. Look up each surviving entry in the schema map.
///
/// Returns the ordered list of schema `Column` references that should be parsed.
fn columns_in_order_for_row<'schema>(
header_cols: &[ColumnInfo],
schema: &'schema TableSchema,
row_is_static: bool,
) -> Vec<&'schema Column> {
let schema_map: HashMap<&str, &Column> = schema
.columns
.iter()
.map(|c| (c.name.as_str(), c))
.collect();
header_cols
.iter()
.filter(|c| !c.is_primary_key && !c.is_clustering && c.is_static == row_is_static)
.filter_map(|c| schema_map.get(c.name.as_str()).copied())
.collect()
}
/// Apply a `missing_columns_bitmap` to a column list.
/// Bit `i` set → column `i` is ABSENT.
/// Columns at index >= 64 are always included.
fn apply_bitmap(columns: Vec<&Column>, bitmap: u64) -> Vec<&Column> {
columns
.into_iter()
.enumerate()
.filter(|(idx, _)| *idx >= 64 || (bitmap & (1u64 << idx)) == 0)
.map(|(_, col)| col)
.collect()
}
// ---------------------------------------------------------------------------
// Synthetic schema factory
// ---------------------------------------------------------------------------
/// Build a schema with 1 static column (`static_col`) and 2 regular columns
/// (`reg_a`, `reg_b`), plus pk and ck keys.
fn mixed_static_schema() -> TableSchema {
TableSchema {
keyspace: "test".to_string(),
table: "mixed".to_string(),
partition_keys: vec![KeyColumn {
name: "pk".to_string(),
data_type: "int".to_string(),
position: 0,
}],
clustering_keys: vec![ClusteringColumn {
name: "ck".to_string(),
data_type: "int".to_string(),
position: 0,
order: cqlite_core::schema::ClusteringOrder::Asc,
}],
columns: vec![
Column {
name: "static_col".to_string(),
data_type: "text".to_string(),
nullable: true,
default: None,
is_static: true,
},
Column {
name: "reg_a".to_string(),
data_type: "text".to_string(),
nullable: true,
default: None,
is_static: false,
},
Column {
name: "reg_b".to_string(),
data_type: "int".to_string(),
nullable: true,
default: None,
is_static: false,
},
],
comments: HashMap::new(),
dropped_columns: HashMap::new(),
}
}
/// Serialization-header columns for `mixed_static_schema`, in Cassandra's
/// serialization order (alphabetical by name within each kind, keys first).
fn mixed_static_header_cols() -> Vec<ColumnInfo> {
vec![
// pk (primary key)
ColumnInfo {
name: "pk".to_string(),
column_type: "int".to_string(),
is_primary_key: true,
key_position: Some(0),
is_static: false,
is_clustering: false,
clustering_reversed: false,
},
// ck (clustering)
ColumnInfo {
name: "ck".to_string(),
column_type: "int".to_string(),
is_primary_key: false,
key_position: None,
is_static: false,
is_clustering: true,
clustering_reversed: false,
},
// static_col (static)
ColumnInfo {
name: "static_col".to_string(),
column_type: "text".to_string(),
is_primary_key: false,
key_position: None,
is_static: true,
is_clustering: false,
clustering_reversed: false,
},
// reg_a (regular)
ColumnInfo {
name: "reg_a".to_string(),
column_type: "text".to_string(),
is_primary_key: false,
key_position: None,
is_static: false,
is_clustering: false,
clustering_reversed: false,
},
// reg_b (regular)
ColumnInfo {
name: "reg_b".to_string(),
column_type: "int".to_string(),
is_primary_key: false,
key_position: None,
is_static: false,
is_clustering: false,
clustering_reversed: false,
},
]
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
/// A regular row (is_static=false) must see only regular columns, NOT the
/// static column, so that its missing_columns_bitmap indices are correct.
#[test]
fn regular_row_sees_only_regular_columns() {
let schema = mixed_static_schema();
let header = mixed_static_header_cols();
let cols = columns_in_order_for_row(&header, &schema, false);
assert_eq!(
cols.len(),
2,
"regular row must see exactly 2 regular columns, not {:?}",
cols.iter().map(|c| &c.name).collect::<Vec<_>>()
);
assert_eq!(cols[0].name, "reg_a", "first regular column must be reg_a");
assert_eq!(cols[1].name, "reg_b", "second regular column must be reg_b");
// The static column must NOT appear.
assert!(
cols.iter().all(|c| !c.is_static),
"no static column should appear in a regular-row column list"
);
}
/// A static row (is_static=true) must see only static columns.
#[test]
fn static_row_sees_only_static_columns() {
let schema = mixed_static_schema();
let header = mixed_static_header_cols();
let cols = columns_in_order_for_row(&header, &schema, true);
assert_eq!(
cols.len(),
1,
"static row must see exactly 1 static column, not {:?}",
cols.iter().map(|c| &c.name).collect::<Vec<_>>()
);
assert_eq!(cols[0].name, "static_col");
assert!(cols[0].is_static, "static_col must be marked is_static");
}
/// When Cassandra sets bit-0 of the regular-row bitmap the FIRST regular column
/// is absent. With the old (pre-#702) logic that merged static+regular columns,
/// the static column occupied index-0 and setting bit-0 would wrongly drop the
/// static column from the mixed list — but since the static column is also
/// excluded from a regular row, the net effect was that bit-0 hit `reg_a` on
/// the old path only if static was listed first. The fix ensures bit-0 ALWAYS
/// maps to the first REGULAR column for a regular row.
#[test]
fn bitmap_bit0_drops_first_regular_column_not_static() {
let schema = mixed_static_schema();
let header = mixed_static_header_cols();
// Regular-row column list: [reg_a, reg_b]
let cols = columns_in_order_for_row(&header, &schema, false);
assert_eq!(cols.len(), 2);
// Bitmap: bit-0 set → reg_a is absent.
let present = apply_bitmap(cols, 0b01);
assert_eq!(
present.len(),
1,
"only 1 column should survive bitmap 0b01 on a 2-column regular list"
);
assert_eq!(
present[0].name, "reg_b",
"with bit-0 set, reg_a should be absent and reg_b should remain"
);
}
/// Bit-1 drops the second regular column (`reg_b`).
#[test]
fn bitmap_bit1_drops_second_regular_column() {
let schema = mixed_static_schema();
let header = mixed_static_header_cols();
let cols = columns_in_order_for_row(&header, &schema, false);
let present = apply_bitmap(cols, 0b10);
assert_eq!(present.len(), 1);
assert_eq!(present[0].name, "reg_a");
}
/// All-columns-absent bitmap (0b11 for 2 columns) yields an empty list.
#[test]
fn bitmap_all_absent_yields_empty() {
let schema = mixed_static_schema();
let header = mixed_static_header_cols();
let cols = columns_in_order_for_row(&header, &schema, false);
let present = apply_bitmap(cols, 0b11);
assert!(
present.is_empty(),
"all-bits-set bitmap should yield zero columns"
);
}
/// A schema with ONLY regular columns (no static) should behave identically to
/// pre-#702 behaviour: all non-key columns appear in a regular row.
#[test]
fn pure_regular_schema_unaffected_by_static_filter() {
let schema = TableSchema {
keyspace: "test".to_string(),
table: "no_static".to_string(),
partition_keys: vec![KeyColumn {
name: "pk".to_string(),
data_type: "int".to_string(),
position: 0,
}],
clustering_keys: vec![],
columns: vec![
Column {
name: "col_a".to_string(),
data_type: "text".to_string(),
nullable: true,
default: None,
is_static: false,
},
Column {
name: "col_b".to_string(),
data_type: "int".to_string(),
nullable: true,
default: None,
is_static: false,
},
],
comments: HashMap::new(),
dropped_columns: HashMap::new(),
};
let header = vec![
ColumnInfo {
name: "pk".to_string(),
column_type: "int".to_string(),
is_primary_key: true,
key_position: Some(0),
is_static: false,
is_clustering: false,
clustering_reversed: false,
},
ColumnInfo {
name: "col_a".to_string(),
column_type: "text".to_string(),
is_primary_key: false,
key_position: None,
is_static: false,
is_clustering: false,
clustering_reversed: false,
},
ColumnInfo {
name: "col_b".to_string(),
column_type: "int".to_string(),
is_primary_key: false,
key_position: None,
is_static: false,
is_clustering: false,
clustering_reversed: false,
},
];
let cols = columns_in_order_for_row(&header, &schema, false);
assert_eq!(cols.len(), 2, "all regular columns must appear");
assert_eq!(cols[0].name, "col_a");
assert_eq!(cols[1].name, "col_b");
}