diff --git a/core/schema.rs b/core/schema.rs index 4b259e74f..6a6889732 100644 --- a/core/schema.rs +++ b/core/schema.rs @@ -810,6 +810,12 @@ pub struct Index { pub columns: Vec, pub unique: bool, pub ephemeral: bool, + /// Does the index have a rowid as the last column? + /// This is the case for btree indexes (persistent or ephemeral) that + /// have been created based on a table with a rowid. + /// For example, WITHOUT ROWID tables (not supported in Limbo yet), + /// and SELECT DISTINCT ephemeral indexes will not have a rowid. + pub has_rowid: bool, } #[allow(dead_code)] @@ -863,6 +869,7 @@ impl Index { columns: index_columns, unique, ephemeral: false, + has_rowid: table.has_rowid, }) } _ => todo!("Expected create index statement"), @@ -925,6 +932,7 @@ impl Index { columns: primary_keys, unique: true, ephemeral: false, + has_rowid: table.has_rowid, }); } @@ -956,6 +964,7 @@ impl Index { }], unique: true, ephemeral: false, + has_rowid: table.has_rowid, }) } else { None @@ -1027,6 +1036,7 @@ impl Index { columns: index_cols.collect(), unique: true, ephemeral: false, + has_rowid: table.has_rowid, } }); indices.extend(unique_set_indices); diff --git a/core/translate/index.rs b/core/translate/index.rs index 0fc1528cf..9a8aea937 100644 --- a/core/translate/index.rs +++ b/core/translate/index.rs @@ -63,6 +63,7 @@ pub fn translate_create_index( .collect(), unique: unique_if_not_exists.0, ephemeral: false, + has_rowid: tbl.has_rowid, }); // Allocate the necessary cursors: diff --git a/core/translate/main_loop.rs b/core/translate/main_loop.rs index 7d2e05ef2..0589c5fae 100644 --- a/core/translate/main_loop.rs +++ b/core/translate/main_loop.rs @@ -101,6 +101,7 @@ pub fn init_loop( pos_in_table: 0, collation: None, // FIXME: this should be inferred from the expression }], + has_rowid: false, unique: false, }); let cursor_id = program.alloc_cursor_id( diff --git a/core/translate/optimizer/join.rs b/core/translate/optimizer/join.rs index ec126d2a9..8e5174baa 100644 --- a/core/translate/optimizer/join.rs +++ b/core/translate/optimizer/join.rs @@ -633,6 +633,7 @@ mod tests { unique: true, ephemeral: false, root_page: 1, + has_rowid: true, }); available_indexes.insert("test_table".to_string(), vec![index]); @@ -697,6 +698,7 @@ mod tests { unique: true, ephemeral: false, root_page: 1, + has_rowid: true, }); available_indexes.insert("table1".to_string(), vec![index1]); @@ -807,6 +809,7 @@ mod tests { unique: true, ephemeral: false, root_page: 1, + has_rowid: true, }); available_indexes.insert(table_name.to_string(), vec![index]); }); @@ -822,6 +825,7 @@ mod tests { unique: false, ephemeral: false, root_page: 1, + has_rowid: true, }); let order_id_idx = Arc::new(Index { name: "order_items_order_id_idx".to_string(), @@ -835,6 +839,7 @@ mod tests { unique: false, ephemeral: false, root_page: 1, + has_rowid: true, }); available_indexes diff --git a/core/translate/optimizer/mod.rs b/core/translate/optimizer/mod.rs index af26ac9b8..2664953af 100644 --- a/core/translate/optimizer/mod.rs +++ b/core/translate/optimizer/mod.rs @@ -786,6 +786,10 @@ fn ephemeral_index_build( ephemeral: true, table_name: table_reference.table.get_name().to_string(), root_page: 0, + has_rowid: table_reference + .table + .btree() + .map_or(false, |btree| btree.has_rowid), }; ephemeral_index