alopex-sql 0.8.8

SQL parser components for the Alopex DB dialect
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Planner error types for the Alopex SQL dialect.
//!
//! This module defines error types for the planning phase, including:
//! - Catalog errors (ALOPEX-C*): Table/column/index lookup failures
//! - Type errors (ALOPEX-T*): Type mismatches, constraint violations
//! - Feature errors (ALOPEX-F*): Unsupported features

use crate::ast::Span;
use thiserror::Error;

/// Planner errors for the Alopex SQL dialect.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum PlannerError {
    /// Invalid PRAGMA name or value.
    #[error("invalid PRAGMA '{name}': {reason}")]
    InvalidPragma { name: String, reason: String },
    // === Catalog Errors (ALOPEX-C*) ===
    /// ALOPEX-C001: Table not found.
    #[error("error[ALOPEX-C001]: table '{name}' not found at line {line}, column {column}")]
    TableNotFound {
        name: String,
        line: u64,
        column: u64,
    },

    /// ALOPEX-C002: Table already exists.
    #[error("error[ALOPEX-C002]: table '{name}' already exists")]
    TableAlreadyExists { name: String },

    /// ALOPEX-C003: Column not found.
    #[error(
        "error[ALOPEX-C003]: column '{column}' not found in table '{table}' at line {line}, column {col}"
    )]
    ColumnNotFound {
        column: String,
        table: String,
        line: u64,
        col: u64,
    },

    /// ALOPEX-C004: Ambiguous column reference.
    #[error(
        "error[ALOPEX-C004]: ambiguous column '{column}' found in tables: {tables:?} at line {line}, column {col}"
    )]
    AmbiguousColumn {
        column: String,
        tables: Vec<String>,
        line: u64,
        col: u64,
    },

    /// ALOPEX-C005: Index already exists.
    #[error("error[ALOPEX-C005]: index '{name}' already exists")]
    IndexAlreadyExists { name: String },

    /// ALOPEX-C006: Index not found.
    #[error("error[ALOPEX-C006]: index '{name}' not found")]
    IndexNotFound { name: String },

    /// ALOPEX-C007: FROM-clause table function not in the closed registry.
    #[error(
        "error[ALOPEX-C007]: table function '{name}' does not exist at line {line}, column {column}"
    )]
    UnknownTableFunction {
        name: String,
        line: u64,
        column: u64,
    },

    // === Type Errors (ALOPEX-T*) ===
    /// ALOPEX-T001: Type mismatch.
    #[error(
        "error[ALOPEX-T001]: type mismatch at line {line}, column {column}: expected {expected}, found {found}"
    )]
    TypeMismatch {
        expected: String,
        found: String,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T002: Invalid operator for type.
    #[error(
        "error[ALOPEX-T002]: invalid operator '{op}' for type '{type_name}' at line {line}, column {column}"
    )]
    InvalidOperator {
        op: String,
        type_name: String,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T003: NULL constraint violation.
    #[error(
        "error[ALOPEX-T003]: null constraint violation for column '{column}' at line {line}, column {col}"
    )]
    NullConstraintViolation { column: String, line: u64, col: u64 },

    /// ALOPEX-T004: Vector dimension mismatch.
    #[error(
        "error[ALOPEX-T004]: vector dimension mismatch at line {line}, column {column}: expected {expected}, found {found}"
    )]
    VectorDimensionMismatch {
        expected: u32,
        found: u32,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T005: Invalid metric.
    #[error(
        "error[ALOPEX-T005]: invalid metric '{value}' at line {line}, column {column}. Valid options: cosine, l2, inner"
    )]
    InvalidMetric {
        value: String,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T006: Column count does not match value count.
    #[error(
        "error[ALOPEX-T006]: column count ({columns}) does not match value count ({values}) at line {line}, column {column}"
    )]
    ColumnValueCountMismatch {
        columns: usize,
        values: usize,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T007: Invalid expression for the current context.
    #[error("error[ALOPEX-T007]: invalid expression: {message}")]
    InvalidExpression { message: String },

    /// ALOPEX-T008: Set-operation inputs expose different numbers of columns.
    #[error(
        "error[ALOPEX-T008]: set operation column count mismatch: left {left}, right {right} at line {line}, column {column}"
    )]
    SetOperationColumnCountMismatch {
        left: usize,
        right: usize,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T009: A CTE column-name list does not match its query width.
    #[error(
        "error[ALOPEX-T009]: common table expression '{cte}' declares {declared} column names but its query returns {actual} columns at line {line}, column {column}"
    )]
    CteColumnCountMismatch {
        cte: String,
        declared: usize,
        actual: usize,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T010: A CTE column-name list contains the same name twice.
    #[error(
        "error[ALOPEX-T010]: common table expression '{cte}' declares column '{name}' more than once at line {line}, column {column}"
    )]
    DuplicateCteColumn {
        cte: String,
        name: String,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T011: VALUES rows expose different numbers of columns.
    #[error(
        "error[ALOPEX-T011]: VALUES row {row} has {actual} columns but row 1 has {expected} at line {line}, column {column}"
    )]
    ValuesColumnCountMismatch {
        row: usize,
        expected: usize,
        actual: usize,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T012: A relation alias column list does not match the relation
    /// width. Covers derived tables, base tables, and table functions
    /// (issue #151).
    #[error(
        "error[ALOPEX-T012]: relation alias '{alias}' declares {declared} column names but the relation has {actual} columns at line {line}, column {column}"
    )]
    TableAliasColumnCountMismatch {
        alias: String,
        declared: usize,
        actual: usize,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T013: Row operands expose different numbers of fields.
    #[error(
        "error[ALOPEX-T013]: row value has {actual} fields but its comparison operand has {expected} at line {line}, column {column}"
    )]
    RowArityMismatch {
        expected: usize,
        actual: usize,
        line: u64,
        column: u64,
    },

    /// ALOPEX-T014: DISTINCT ON keys do not form the initial ORDER BY prefix.
    /// Message and semantics follow PostgreSQL error 42P10 (D2 in
    /// docs/sql-distinct-on.md).
    #[error(
        "error[ALOPEX-T014]: SELECT DISTINCT ON expressions must match initial ORDER BY expressions at line {line}, column {column}"
    )]
    DistinctOnOrderByMismatch { line: u64, column: u64 },

    /// ALOPEX-T015: RIGHT or FULL JOIN with a LATERAL right side (issue #151).
    /// PostgreSQL rejects the same shape: the correlated side cannot be the
    /// null-supplying side of the join.
    #[error(
        "error[ALOPEX-T015]: {join_type} JOIN cannot have a LATERAL right side at line {line}, column {column}"
    )]
    LateralJoinTypeUnsupported {
        join_type: String,
        line: u64,
        column: u64,
    },

    // === Feature Errors (ALOPEX-F*) ===
    /// ALOPEX-F001: Unsupported feature.
    #[error(
        "error[ALOPEX-F001]: feature '{feature}' is not supported in this version. Expected in {version}"
    )]
    UnsupportedFeature {
        feature: String,
        version: String,
        line: u64,
        column: u64,
    },
}

impl PlannerError {
    /// Create a TableNotFound error from a span.
    pub fn table_not_found(name: impl Into<String>, span: Span) -> Self {
        Self::TableNotFound {
            name: name.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a TableAlreadyExists error.
    pub fn table_already_exists(name: impl Into<String>) -> Self {
        Self::TableAlreadyExists { name: name.into() }
    }

    /// Create an UnknownTableFunction error from a span.
    pub fn unknown_table_function(name: impl Into<String>, span: Span) -> Self {
        Self::UnknownTableFunction {
            name: name.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a LateralJoinTypeUnsupported error from a span.
    pub fn lateral_join_type_unsupported(join_type: impl Into<String>, span: Span) -> Self {
        Self::LateralJoinTypeUnsupported {
            join_type: join_type.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a ColumnNotFound error from a span.
    pub fn column_not_found(
        column: impl Into<String>,
        table: impl Into<String>,
        span: Span,
    ) -> Self {
        Self::ColumnNotFound {
            column: column.into(),
            table: table.into(),
            line: span.start.line,
            col: span.start.column,
        }
    }

    /// Create an AmbiguousColumn error from a span.
    pub fn ambiguous_column(column: impl Into<String>, tables: Vec<String>, span: Span) -> Self {
        Self::AmbiguousColumn {
            column: column.into(),
            tables,
            line: span.start.line,
            col: span.start.column,
        }
    }

    /// Create an IndexAlreadyExists error.
    pub fn index_already_exists(name: impl Into<String>) -> Self {
        Self::IndexAlreadyExists { name: name.into() }
    }

    /// Create an IndexNotFound error.
    pub fn index_not_found(name: impl Into<String>) -> Self {
        Self::IndexNotFound { name: name.into() }
    }

    /// Create a TypeMismatch error from a span.
    pub fn type_mismatch(
        expected: impl Into<String>,
        found: impl Into<String>,
        span: Span,
    ) -> Self {
        Self::TypeMismatch {
            expected: expected.into(),
            found: found.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create an InvalidExpression error.
    pub fn invalid_expression(message: impl Into<String>) -> Self {
        Self::InvalidExpression {
            message: message.into(),
        }
    }

    /// Create a set-operation column-count error from a span.
    pub fn set_operation_column_count_mismatch(left: usize, right: usize, span: Span) -> Self {
        Self::SetOperationColumnCountMismatch {
            left,
            right,
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a CTE column-count error from a span.
    pub fn cte_column_count_mismatch(
        cte: impl Into<String>,
        declared: usize,
        actual: usize,
        span: Span,
    ) -> Self {
        Self::CteColumnCountMismatch {
            cte: cte.into(),
            declared,
            actual,
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a duplicate CTE column-name error from a span.
    pub fn duplicate_cte_column(
        cte: impl Into<String>,
        column_name: impl Into<String>,
        span: Span,
    ) -> Self {
        Self::DuplicateCteColumn {
            cte: cte.into(),
            name: column_name.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a VALUES row-width error from a span.
    pub fn values_column_count_mismatch(
        row: usize,
        expected: usize,
        actual: usize,
        span: Span,
    ) -> Self {
        Self::ValuesColumnCountMismatch {
            row,
            expected,
            actual,
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a derived-table alias-width error from a span.
    pub fn table_alias_column_count_mismatch(
        alias: impl Into<String>,
        declared: usize,
        actual: usize,
        span: Span,
    ) -> Self {
        Self::TableAliasColumnCountMismatch {
            alias: alias.into(),
            declared,
            actual,
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create an InvalidOperator error from a span.
    pub fn invalid_operator(
        op: impl Into<String>,
        type_name: impl Into<String>,
        span: Span,
    ) -> Self {
        Self::InvalidOperator {
            op: op.into(),
            type_name: type_name.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a NullConstraintViolation error from a span.
    pub fn null_constraint_violation(column: impl Into<String>, span: Span) -> Self {
        Self::NullConstraintViolation {
            column: column.into(),
            line: span.start.line,
            col: span.start.column,
        }
    }

    /// Create a VectorDimensionMismatch error from a span.
    pub fn vector_dimension_mismatch(expected: u32, found: u32, span: Span) -> Self {
        Self::VectorDimensionMismatch {
            expected,
            found,
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create an InvalidMetric error from a span.
    pub fn invalid_metric(value: impl Into<String>, span: Span) -> Self {
        Self::InvalidMetric {
            value: value.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a ColumnValueCountMismatch error from a span.
    pub fn column_value_count_mismatch(columns: usize, values: usize, span: Span) -> Self {
        Self::ColumnValueCountMismatch {
            columns,
            values,
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create a DistinctOnOrderByMismatch error from a span.
    pub fn distinct_on_order_by_mismatch(span: Span) -> Self {
        Self::DistinctOnOrderByMismatch {
            line: span.start.line,
            column: span.start.column,
        }
    }

    /// Create an UnsupportedFeature error from a span.
    pub fn unsupported_feature(
        feature: impl Into<String>,
        version: impl Into<String>,
        span: Span,
    ) -> Self {
        Self::UnsupportedFeature {
            feature: feature.into(),
            version: version.into(),
            line: span.start.line,
            column: span.start.column,
        }
    }
}