sochdb-query 2.0.4

SochDB query engine (sync-first execution and vector query planning)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! # SQL Compatibility Matrix
//!
//! This module defines SochDB's SQL dialect support and compatibility layer.
//!
//! ## Design Goals
//!
//! 1. **Portable Core**: SQL-92 compatible baseline that works across ecosystems
//! 2. **Dialect Sugar**: Support common dialect variants (MySQL, PostgreSQL, SQLite)
//! 3. **Single AST**: All dialects normalize to one canonical AST representation
//! 4. **Extensible**: Add new dialects without forking parsers/executors
//!
//! ## SQL Feature Matrix
//!
//! ### Guaranteed (Core SQL)
//!
//! | Category | Statement | Status | Notes |
//! |----------|-----------|--------|-------|
//! | DML | SELECT | ✅ | With WHERE, ORDER BY, LIMIT, OFFSET |
//! | DML | INSERT | ✅ | Single and multi-row |
//! | DML | UPDATE | ✅ | With WHERE clause |
//! | DML | DELETE | ✅ | With WHERE clause |
//! | DDL | CREATE TABLE | ✅ | With column types and constraints |
//! | DDL | DROP TABLE | ✅ | Basic form |
//! | DDL | ALTER TABLE | 🔄 | ADD/DROP COLUMN |
//! | DDL | CREATE INDEX | ✅ | Single and multi-column |
//! | DDL | DROP INDEX | ✅ | Basic form |
//! | Tx | BEGIN | ✅ | Start transaction |
//! | Tx | COMMIT | ✅ | Commit transaction |
//! | Tx | ROLLBACK | ✅ | Rollback transaction |
//!
//! ### Idempotent DDL
//!
//! | Statement | Status | Notes |
//! |-----------|--------|-------|
//! | CREATE TABLE IF NOT EXISTS | ✅ | No-op if exists |
//! | DROP TABLE IF EXISTS | ✅ | No-op if not exists |
//! | CREATE INDEX IF NOT EXISTS | ✅ | No-op if exists |
//! | DROP INDEX IF EXISTS | ✅ | No-op if not exists |
//!
//! ### Conflict/Upsert Family
//!
//! All of these normalize to `InsertStmt { on_conflict: Some(OnConflict { .. }) }`
//!
//! | Dialect | Syntax | Canonical AST |
//! |---------|--------|---------------|
//! | PostgreSQL | `ON CONFLICT DO NOTHING` | `OnConflict { action: DoNothing }` |
//! | PostgreSQL | `ON CONFLICT DO UPDATE SET ...` | `OnConflict { action: DoUpdate(...) }` |
//! | MySQL | `INSERT IGNORE` | `OnConflict { action: DoNothing }` |
//! | MySQL | `ON DUPLICATE KEY UPDATE` | `OnConflict { action: DoUpdate(...) }` |
//! | SQLite | `INSERT OR IGNORE` | `OnConflict { action: DoNothing }` |
//! | SQLite | `INSERT OR REPLACE` | `OnConflict { action: DoReplace }` |
//!
//! ### Out of Scope (Explicit Limitations)
//!
//! | Feature | Status | Reason |
//! |---------|--------|--------|
//! | Multi-table JOINs | ❌ | Complexity; single-table focus for v1 |
//! | Subqueries in WHERE | ❌ | Planning complexity |
//! | Window functions | ❌ | Future enhancement |
//! | CTEs (WITH clause) | ❌ | Future enhancement |
//! | Stored procedures | ❌ | Out of scope |
//!
//! ## Dialect Detection
//!
//! SochDB auto-detects dialect from syntax:
//! - `INSERT IGNORE` → MySQL mode
//! - `INSERT OR IGNORE` → SQLite mode
//! - `ON CONFLICT` → PostgreSQL mode
//!
//! All normalize to the same internal representation.

use std::fmt;

/// SQL Dialect for parsing/normalization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SqlDialect {
    /// Standard SQL-92 compatible (default)
    #[default]
    Standard,
    /// PostgreSQL dialect
    PostgreSQL,
    /// MySQL dialect
    MySQL,
    /// SQLite dialect
    SQLite,
}

impl SqlDialect {
    /// Detect dialect from SQL text
    pub fn detect(sql: &str) -> Self {
        let upper = sql.to_uppercase();

        // MySQL: INSERT IGNORE
        if upper.contains("INSERT IGNORE") || upper.contains("ON DUPLICATE KEY") {
            return SqlDialect::MySQL;
        }

        // SQLite: INSERT OR IGNORE/REPLACE/ABORT
        if upper.contains("INSERT OR IGNORE")
            || upper.contains("INSERT OR REPLACE")
            || upper.contains("INSERT OR ABORT")
        {
            return SqlDialect::SQLite;
        }

        // PostgreSQL: ON CONFLICT
        if upper.contains("ON CONFLICT") {
            return SqlDialect::PostgreSQL;
        }

        SqlDialect::Standard
    }
}

impl fmt::Display for SqlDialect {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SqlDialect::Standard => write!(f, "Standard SQL"),
            SqlDialect::PostgreSQL => write!(f, "PostgreSQL"),
            SqlDialect::MySQL => write!(f, "MySQL"),
            SqlDialect::SQLite => write!(f, "SQLite"),
        }
    }
}

/// SQL Feature support level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FeatureSupport {
    /// Fully supported
    Full,
    /// Partially supported with limitations
    Partial,
    /// Planned for future release
    Planned,
    /// Not supported and not planned
    NotSupported,
}

impl fmt::Display for FeatureSupport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FeatureSupport::Full => write!(f, "✅ Full"),
            FeatureSupport::Partial => write!(f, "🔄 Partial"),
            FeatureSupport::Planned => write!(f, "📋 Planned"),
            FeatureSupport::NotSupported => write!(f, "❌ Not Supported"),
        }
    }
}

/// SQL Feature categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SqlFeature {
    // DML
    Select,
    Insert,
    Update,
    Delete,

    // DDL
    CreateTable,
    DropTable,
    AlterTable,
    CreateIndex,
    DropIndex,

    // Idempotent DDL
    CreateTableIfNotExists,
    DropTableIfExists,
    CreateIndexIfNotExists,
    DropIndexIfExists,

    // Conflict/Upsert
    OnConflictDoNothing,
    OnConflictDoUpdate,
    InsertIgnore,
    InsertOrIgnore,
    InsertOrReplace,
    OnDuplicateKeyUpdate,

    // Transactions
    Begin,
    Commit,
    Rollback,
    Savepoint,

    // Query features
    Where,
    OrderBy,
    Limit,
    Offset,
    GroupBy,
    Having,
    Distinct,

    // Joins (limited)
    InnerJoin,
    LeftJoin,
    RightJoin,
    CrossJoin,

    // Subqueries
    SubqueryInFrom,
    SubqueryInWhere,
    SubqueryInSelect,

    // Set operations
    Union,
    Intersect,
    Except,

    // Expressions
    ParameterizedQueries,
    CaseWhen,
    Cast,
    NullHandling,
    InList,
    Between,
    Like,

    // SochDB extensions
    VectorSearch,
    EmbeddingType,
    ContextWindow,
}

/// Get feature support level
pub fn get_feature_support(feature: SqlFeature) -> FeatureSupport {
    use SqlFeature::*;

    match feature {
        // Fully supported
        Select | Insert | Update | Delete => FeatureSupport::Full,
        CreateTable | DropTable | CreateIndex | DropIndex => FeatureSupport::Full,
        CreateTableIfNotExists | DropTableIfExists => FeatureSupport::Full,
        CreateIndexIfNotExists | DropIndexIfExists => FeatureSupport::Full,
        Begin | Commit | Rollback => FeatureSupport::Full,
        Where | OrderBy | Limit | Offset | Distinct => FeatureSupport::Full,
        ParameterizedQueries | NullHandling | InList | Like => FeatureSupport::Full,
        OnConflictDoNothing | InsertIgnore | InsertOrIgnore => FeatureSupport::Full,
        VectorSearch | EmbeddingType => FeatureSupport::Full,

        // Partially supported
        AlterTable => FeatureSupport::Partial, // ADD/DROP COLUMN only
        GroupBy | Having => FeatureSupport::Partial, // Basic support
        InnerJoin => FeatureSupport::Partial,  // Two-table only
        OnConflictDoUpdate | InsertOrReplace | OnDuplicateKeyUpdate => FeatureSupport::Partial,
        CaseWhen | Cast | Between => FeatureSupport::Partial,
        Union => FeatureSupport::Partial,
        SubqueryInFrom => FeatureSupport::Partial,
        Savepoint => FeatureSupport::Partial,
        ContextWindow => FeatureSupport::Partial,

        // Planned
        LeftJoin | RightJoin | CrossJoin => FeatureSupport::Planned,
        SubqueryInWhere | SubqueryInSelect => FeatureSupport::Planned,
        Intersect | Except => FeatureSupport::Planned,
    }
}

/// Compatibility matrix for different SQL dialects
pub struct CompatibilityMatrix;

impl CompatibilityMatrix {
    /// Check if a feature is supported
    pub fn is_supported(feature: SqlFeature) -> bool {
        matches!(
            get_feature_support(feature),
            FeatureSupport::Full | FeatureSupport::Partial
        )
    }

    /// Get all fully supported features
    pub fn fully_supported() -> Vec<SqlFeature> {
        use SqlFeature::*;
        vec![
            Select,
            Insert,
            Update,
            Delete,
            CreateTable,
            DropTable,
            CreateIndex,
            DropIndex,
            CreateTableIfNotExists,
            DropTableIfExists,
            CreateIndexIfNotExists,
            DropIndexIfExists,
            Begin,
            Commit,
            Rollback,
            Where,
            OrderBy,
            Limit,
            Offset,
            Distinct,
            ParameterizedQueries,
            NullHandling,
            InList,
            Like,
            OnConflictDoNothing,
            InsertIgnore,
            InsertOrIgnore,
            VectorSearch,
            EmbeddingType,
        ]
    }

    /// Print the compatibility matrix as a formatted table
    pub fn print_matrix() -> String {
        let mut output = String::new();
        output.push_str("# SochDB SQL Compatibility Matrix\n\n");

        output.push_str("## Core DML\n\n");
        output.push_str("| Feature | Support |\n");
        output.push_str("|---------|--------|\n");
        for feature in &[
            SqlFeature::Select,
            SqlFeature::Insert,
            SqlFeature::Update,
            SqlFeature::Delete,
        ] {
            output.push_str(&format!(
                "| {:?} | {} |\n",
                feature,
                get_feature_support(*feature)
            ));
        }

        output.push_str("\n## DDL\n\n");
        output.push_str("| Feature | Support |\n");
        output.push_str("|---------|--------|\n");
        for feature in &[
            SqlFeature::CreateTable,
            SqlFeature::DropTable,
            SqlFeature::AlterTable,
            SqlFeature::CreateIndex,
            SqlFeature::DropIndex,
            SqlFeature::CreateTableIfNotExists,
            SqlFeature::DropTableIfExists,
        ] {
            output.push_str(&format!(
                "| {:?} | {} |\n",
                feature,
                get_feature_support(*feature)
            ));
        }

        output.push_str("\n## Conflict/Upsert\n\n");
        output.push_str("| Feature | Support |\n");
        output.push_str("|---------|--------|\n");
        for feature in &[
            SqlFeature::OnConflictDoNothing,
            SqlFeature::OnConflictDoUpdate,
            SqlFeature::InsertIgnore,
            SqlFeature::InsertOrIgnore,
            SqlFeature::InsertOrReplace,
            SqlFeature::OnDuplicateKeyUpdate,
        ] {
            output.push_str(&format!(
                "| {:?} | {} |\n",
                feature,
                get_feature_support(*feature)
            ));
        }

        output
    }
}

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

    #[test]
    fn test_dialect_detection() {
        assert_eq!(
            SqlDialect::detect("SELECT * FROM users"),
            SqlDialect::Standard
        );
        assert_eq!(
            SqlDialect::detect("INSERT IGNORE INTO users VALUES (1)"),
            SqlDialect::MySQL
        );
        assert_eq!(
            SqlDialect::detect("INSERT OR IGNORE INTO users VALUES (1)"),
            SqlDialect::SQLite
        );
        assert_eq!(
            SqlDialect::detect("INSERT INTO users VALUES (1) ON CONFLICT DO NOTHING"),
            SqlDialect::PostgreSQL
        );
    }

    #[test]
    fn test_feature_support() {
        assert_eq!(
            get_feature_support(SqlFeature::Select),
            FeatureSupport::Full
        );
        assert_eq!(
            get_feature_support(SqlFeature::AlterTable),
            FeatureSupport::Partial
        );
        assert_eq!(
            get_feature_support(SqlFeature::LeftJoin),
            FeatureSupport::Planned
        );
    }

    #[test]
    fn test_compatibility_matrix() {
        assert!(CompatibilityMatrix::is_supported(SqlFeature::Select));
        assert!(CompatibilityMatrix::is_supported(SqlFeature::AlterTable));
        assert!(!CompatibilityMatrix::is_supported(SqlFeature::LeftJoin));
    }
}