hawdb-sql 0.3.0

An embedded Rust graph database for local data-plane workloads
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub use hawdb_expression::sql::{
    Expr, ExprKind, SqlColumnRef, SqlComparisonOp, SqlExpression, SqlFunctionArgument,
    SqlIndexColumn, SqlLikeEscape, SqlNullOrder, SqlOrderDirection, SqlOrderItem, SqlPredicate,
    SqlSourceLocation, SqlSourceSpan, SqlValue,
};

use caseless::Caseless;
use hawdb_core::{HawDBError, LogicalType, Result};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SqlStatement {
    Explain(SqlExplainStatement),
    Select(SelectStatement),
    Insert(InsertStatement),
    Update(UpdateStatement),
    Delete(DeleteStatement),
    CreateTable(CreateTableStatement),
    CreateIndex(CreateIndexStatement),
    AlterTableAddColumn(AlterTableAddColumnStatement),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlExplainStatement {
    pub analyze: bool,
    pub statement: Box<SqlStatement>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SelectStatement {
    pub projection: Vec<SelectProjection>,
    pub distinct: bool,
    pub from: SqlTableName,
    pub from_alias: Option<String>,
    pub joins: Vec<SqlJoin>,
    pub selection: Option<SqlPredicate>,
    pub group_by: Vec<SqlColumnRef>,
    pub having: Option<SqlPredicate>,
    pub order_by: Vec<SqlOrderItem>,
    pub limit: Option<SqlBound>,
    pub offset: Option<SqlBound>,
    pub lock_strength: Option<SqlLockStrength>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqlLockStrength {
    Share,
    Update,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InsertStatement {
    pub table: SqlTableName,
    pub columns: Vec<String>,
    pub rows: Vec<Vec<SqlValue>>,
    pub on_conflict: Option<SqlOnConflict>,
    pub returning: Vec<SqlColumnRef>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateStatement {
    pub table: SqlTableName,
    pub alias: Option<String>,
    pub assignments: Vec<SqlAssignment>,
    pub selection: Option<SqlPredicate>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeleteStatement {
    pub table: SqlTableName,
    pub alias: Option<String>,
    pub selection: Option<SqlPredicate>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlAssignment {
    pub column: String,
    pub value: SqlAssignmentValue,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SqlAssignmentValue {
    Value(SqlValue),
    Column(SqlColumnRef),
    Arithmetic {
        left: SqlArithmeticOperand,
        operator: SqlArithmeticOperator,
        right: SqlArithmeticOperand,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SqlArithmeticOperand {
    Value(SqlValue),
    Column(SqlColumnRef),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqlArithmeticOperator {
    Add,
    Subtract,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlOnConflict {
    pub columns: Vec<String>,
    pub action: SqlConflictAction,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SqlConflictAction {
    DoNothing,
    DoUpdate(Vec<SqlAssignment>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateTableStatement {
    pub table: SqlTableName,
    pub if_not_exists: bool,
    pub columns: Vec<SqlColumnDefinition>,
    pub constraints: Vec<SqlTableConstraint>,
    pub storage: SqlTableStorage,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum SqlTableStorage {
    #[default]
    RowPage,
    StrictAppend {
        partition_key: Vec<String>,
        order_key: Vec<String>,
        generated_order: SqlGeneratedOrder,
    },
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SqlGeneratedOrder {
    #[default]
    CallerProvided,
    CommitSequence,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlColumnDefinition {
    pub name: String,
    pub data_type: SqlDataType,
    pub nullable: bool,
    pub default: Option<SqlColumnDefault>,
    pub primary_key: bool,
    pub unique: bool,
    pub references: Option<SqlForeignKeyReference>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SqlColumnDefault {
    Literal(SqlValue),
    UuidV7,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqlDataType {
    Boolean,
    BigInt,
    DoublePrecision,
    Text,
    Bytea,
    Uuid,
}

impl SqlDataType {
    pub const fn logical_type(self) -> LogicalType {
        match self {
            Self::Boolean => LogicalType::Boolean,
            Self::BigInt => LogicalType::Int64,
            Self::DoublePrecision => LogicalType::Float64,
            Self::Text => LogicalType::Text,
            Self::Bytea => LogicalType::Binary,
            Self::Uuid => LogicalType::Uuid,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SqlTableConstraint {
    PrimaryKey(Vec<String>),
    Unique(Vec<String>),
    ForeignKey {
        columns: Vec<String>,
        reference: SqlForeignKeyReference,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlForeignKeyReference {
    pub table: SqlTableName,
    pub columns: Vec<String>,
    pub on_delete: SqlReferentialAction,
    pub on_update: SqlReferentialAction,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqlReferentialAction {
    NoAction,
    Restrict,
    Cascade,
    SetNull,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateIndexStatement {
    pub name: String,
    pub table: SqlTableName,
    pub columns: Vec<SqlIndexColumn>,
    pub unique: bool,
    pub if_not_exists: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AlterTableAddColumnStatement {
    pub table: SqlTableName,
    pub if_not_exists: bool,
    pub column: SqlColumnDefinition,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqlBound {
    Literal(u64),
    Parameter(usize),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SqlTableName {
    pub schema: Option<String>,
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SelectProjection {
    Wildcard,
    Expression {
        expression: SqlExpression,
        alias: Option<String>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlJoin {
    pub kind: SqlJoinKind,
    pub table: SqlTableName,
    pub alias: Option<String>,
    pub on: SqlPredicate,
    /// First input visible to ON, with the SELECT base relation at ordinal zero.
    /// The current join's right input is the inclusive end of this scope.
    pub on_scope_start: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqlJoinKind {
    Inner,
    Left,
}

/// Matches SQL LIKE patterns with locale-independent Unicode default case folding for ILIKE.
pub fn sql_like_matches(
    value: &str,
    pattern: &str,
    escape: SqlLikeEscape,
    case_insensitive: bool,
) -> Result<bool> {
    let tokens = tokenize_like_pattern(pattern, escape, case_insensitive)?;
    let value = value.chars().collect::<Vec<_>>();

    let mut value_index = 0;
    let mut pattern_index = 0;
    let mut wildcard_index = None;
    let mut wildcard_value_index = 0;
    while value_index < value.len() {
        let matched = match tokens.get(pattern_index) {
            Some(LikeToken::Literal(expected)) => {
                if let Some(consumed) =
                    match_like_literal(&value[value_index..], expected, case_insensitive)
                {
                    value_index += consumed;
                    pattern_index += 1;
                    true
                } else {
                    false
                }
            }
            Some(LikeToken::One) => {
                value_index += 1;
                pattern_index += 1;
                true
            }
            Some(LikeToken::Many) => {
                wildcard_index = Some(pattern_index);
                wildcard_value_index = value_index;
                pattern_index += 1;
                true
            }
            None => false,
        };
        if matched {
            continue;
        }
        let Some(wildcard_index) = wildcard_index else {
            return Ok(false);
        };
        wildcard_value_index += 1;
        value_index = wildcard_value_index;
        pattern_index = wildcard_index + 1;
    }
    while matches!(tokens.get(pattern_index), Some(LikeToken::Many)) {
        pattern_index += 1;
    }
    Ok(pattern_index == tokens.len())
}

#[derive(Debug, Clone)]
enum LikeToken {
    Literal(String),
    One,
    Many,
}

fn tokenize_like_pattern(
    pattern: &str,
    escape: SqlLikeEscape,
    case_insensitive: bool,
) -> Result<Vec<LikeToken>> {
    let escape = match escape {
        SqlLikeEscape::Character(character) => Some(character),
        SqlLikeEscape::Disabled => None,
    };
    let mut tokens = Vec::with_capacity(pattern.chars().count());
    let mut characters = pattern.chars();
    while let Some(character) = characters.next() {
        if Some(character) == escape {
            let escaped = characters.next().ok_or_else(|| {
                HawDBError::Semantic("LIKE pattern ends with its escape character".to_string())
            })?;
            push_like_literal(&mut tokens, escaped, case_insensitive);
            continue;
        }
        match character {
            '%' if !matches!(tokens.last(), Some(LikeToken::Many)) => tokens.push(LikeToken::Many),
            '%' => {}
            '_' => tokens.push(LikeToken::One),
            literal => push_like_literal(&mut tokens, literal, case_insensitive),
        }
    }
    Ok(tokens)
}

fn push_like_literal(tokens: &mut Vec<LikeToken>, literal: char, case_insensitive: bool) {
    let literal = if case_insensitive {
        std::iter::once(literal).default_case_fold().collect()
    } else {
        literal.to_string()
    };
    if let Some(LikeToken::Literal(previous)) = tokens.last_mut() {
        previous.push_str(&literal);
    } else {
        tokens.push(LikeToken::Literal(literal));
    }
}

fn match_like_literal(value: &[char], expected: &str, case_insensitive: bool) -> Option<usize> {
    let mut actual = String::new();
    for (index, character) in value.iter().copied().enumerate() {
        if case_insensitive {
            actual.extend(std::iter::once(character).default_case_fold());
        } else {
            actual.push(character);
        }
        if !expected.starts_with(&actual) {
            return None;
        }
        if actual == expected {
            return Some(index + 1);
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn postgres_data_types_use_shared_logical_types() {
        assert_eq!(SqlDataType::Boolean.logical_type(), LogicalType::Boolean);
        assert_eq!(SqlDataType::BigInt.logical_type(), LogicalType::Int64);
        assert_eq!(
            SqlDataType::DoublePrecision.logical_type(),
            LogicalType::Float64
        );
        assert_eq!(SqlDataType::Text.logical_type(), LogicalType::Text);
        assert_eq!(SqlDataType::Bytea.logical_type(), LogicalType::Binary);
    }
}