RisingLight 是一个教育用的 OLAP 数据库,它采用列式存储,利用 merge-tree 结构,将多个列组合在一起,可以实现关系模型。对 RisingLight 的相关描述可以在这里查看。
为什么要学习 RisingLight,有以下几个(个人)原因
- RisingLight 是一个教育用的 OLAP 数据库,因此不会太复杂。而且该有的功能(比如parse,bind,optimizer,execute)都有,而且与一些知名的系统有许多共同点,很适合学习
- 代码量不是很大,使用了很多第三方库,但是还是能从中了解到相关的过程,阅读时不会过度陷入细节中
- 使用 Rust 编写,在阅读过程中可以学习 Rust
- 文档齐全
Architecture Overview
SQL 是一种声明式语言,也就是说它只说明了它要执行什么操作,但是没有说明要怎么执行。所以,为了执行 SQL 语句,我们需要对其进行解析,才能知道该如何执行。RisingLight 的文档描述了它的 architecture
- SQL 经过 Parser 解析器解析,生成抽象语法树 AST。RisingLight 中使用 sqlparser-rs 解析 SQL
- Binder 对 AST 中的符号引用进行校验和解析(比如查找要查询的表、字段等信息是否能够在 catalog 中找到相对应的信息,并对其进行绑定),生成新的 AST,AST 主要是用 egg 实现的
- Optimizer 对生成的逻辑计划进行优化,生成物理计划。优化部分主要是使用的是启发式的规则,而由 egg 对语法树进行优化
- Executor 根据物理计划执行,得到结果返回
这各流程从 RisingLight 的代码中也可以看出来
| 12
 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
 
 | pub async fn run(&self, sql: &str) -> Result<Vec<Chunk>, Error> {
 if let Some(cmdline) = sql.trim().strip_prefix('\\') {
 return self.run_internal(cmdline).await;
 }
 
 let optimizer = crate::planner::Optimizer::new(
 self.catalog.clone(),
 self.get_storage_statistics().await?,
 crate::planner::Config {
 enable_range_filter_scan: self.storage.support_range_filter_scan(),
 table_is_sorted_by_primary_key: self.storage.table_is_sorted_by_primary_key(),
 },
 );
 
 let stmts = parse(sql)?;
 let mut outputs: Vec<Chunk> = vec![];
 for stmt in stmts {
 if self.handle_set(&stmt)? {
 continue;
 }
 let mut binder = crate::binder::Binder::new(self.catalog.clone());
 let bound = binder.bind(stmt)?;
 let optimized = optimizer.optimize(&bound);
 let executor = match self.storage.clone() {
 StorageImpl::InMemoryStorage(s) => {
 crate::executor::build(optimizer.clone(), s, &optimized)
 }
 StorageImpl::SecondaryStorage(s) => {
 crate::executor::build(optimizer.clone(), s, &optimized)
 }
 };
 let output = executor.try_collect().await?;
 let chunk = Chunk::new(output);
 
 outputs.push(chunk);
 }
 Ok(outputs)
 }
 
 | 
Storage Overview
RisingLight 实现了是一个列式存储引擎,并通过将列进行组合,产生与行等价的效果,RisingLight 将之称为 secondary。同样,官方提供了文档来描述它的存储结构
secondary 采用 <table_id>_<rowset_id> 的方式将同一行的文件存储在一起,下面是文件的目录结构
tree risinglight.secondary.db
| 12
 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
 
 | risinglight.secondary.db├── 0_1
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   └── 3.idx
 ├── 1_5
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 2.col
 │   └── 2.idx
 ├── 2_3
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   ├── 6.idx
 │   ├── 7.col
 │   ├── 7.idx
 │   ├── 8.col
 │   └── 8.idx
 ├── 3_6
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   └── 6.idx
 ├── 4_4
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   └── 4.idx
 ├── 5_0
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   ├── 6.idx
 │   ├── 7.col
 │   └── 7.idx
 ├── 6_2
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   ├── 6.idx
 │   ├── 7.col
 │   ├── 7.idx
 │   ├── 8.col
 │   └── 8.idx
 ├── 7_10
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 10.col
 │   ├── 10.idx
 │   ├── 11.col
 │   ├── 11.idx
 │   ├── 12.col
 │   ├── 12.idx
 │   ├── 13.col
 │   ├── 13.idx
 │   ├── 14.col
 │   ├── 14.idx
 │   ├── 15.col
 │   ├── 15.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   ├── 6.idx
 │   ├── 7.col
 │   ├── 7.idx
 │   ├── 8.col
 │   ├── 8.idx
 │   ├── 9.col
 │   └── 9.idx
 ├── 7_7
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 10.col
 │   ├── 10.idx
 │   ├── 11.col
 │   ├── 11.idx
 │   ├── 12.col
 │   ├── 12.idx
 │   ├── 13.col
 │   ├── 13.idx
 │   ├── 14.col
 │   ├── 14.idx
 │   ├── 15.col
 │   ├── 15.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   ├── 6.idx
 │   ├── 7.col
 │   ├── 7.idx
 │   ├── 8.col
 │   ├── 8.idx
 │   ├── 9.col
 │   └── 9.idx
 ├── 7_8
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 10.col
 │   ├── 10.idx
 │   ├── 11.col
 │   ├── 11.idx
 │   ├── 12.col
 │   ├── 12.idx
 │   ├── 13.col
 │   ├── 13.idx
 │   ├── 14.col
 │   ├── 14.idx
 │   ├── 15.col
 │   ├── 15.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   ├── 6.idx
 │   ├── 7.col
 │   ├── 7.idx
 │   ├── 8.col
 │   ├── 8.idx
 │   ├── 9.col
 │   └── 9.idx
 ├── 7_9
 │   ├── 0.col
 │   ├── 0.idx
 │   ├── 1.col
 │   ├── 1.idx
 │   ├── 10.col
 │   ├── 10.idx
 │   ├── 11.col
 │   ├── 11.idx
 │   ├── 12.col
 │   ├── 12.idx
 │   ├── 13.col
 │   ├── 13.idx
 │   ├── 14.col
 │   ├── 14.idx
 │   ├── 15.col
 │   ├── 15.idx
 │   ├── 2.col
 │   ├── 2.idx
 │   ├── 3.col
 │   ├── 3.idx
 │   ├── 4.col
 │   ├── 4.idx
 │   ├── 5.col
 │   ├── 5.idx
 │   ├── 6.col
 │   ├── 6.idx
 │   ├── 7.col
 │   ├── 7.idx
 │   ├── 8.col
 │   ├── 8.idx
 │   ├── 9.col
 │   └── 9.idx
 ├── dv
 └── manifest.json
 
 |