lix 0.12.3

Embeddable version control for apps and AI agents.
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
use lix::LixError;
use lix::Value;

simulation_test!(sql_missing_table_has_lix_error_code, |sim| async move {
    let engine = sim.boot_engine().await;
    let session = sim.wrap_session(
        engine
            .open_session()
            .await
            .expect("main session should open"),
        &engine,
    );

    let error = session
        .execute("SELECT * FROM missing_table", &[])
        .await
        .expect_err("missing table should fail");

    assert_eq!(error.code, LixError::CODE_TABLE_NOT_FOUND);
    assert!(error.hint().is_some(), "expected discovery hint: {error}");
});

simulation_test!(sql_missing_column_has_lix_error_code, |sim| async move {
    let engine = sim.boot_engine().await;
    let session = sim.wrap_session(
        engine
            .open_session()
            .await
            .expect("main session should open"),
        &engine,
    );

    let error = session
        .execute("SELECT missing_column FROM lix_file", &[])
        .await
        .expect_err("missing column should fail");

    assert_eq!(error.code, LixError::CODE_COLUMN_NOT_FOUND);
});

simulation_test!(
    sql_duplicate_projection_name_is_parse_error,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine
                .open_session()
                .await
                .expect("main session should open"),
            &engine,
        );

        let error = session
            .execute("SELECT 1 AS x, 2 AS x", &[])
            .await
            .expect_err("duplicate projection names should fail during planning");

        assert_eq!(error.code, LixError::CODE_PARSE_ERROR);
    }
);

simulation_test!(
    sql_postgresql_numbered_placeholders_bind_positionally,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = engine
            .open_session()
            .await
            .expect("main session should open");

        let result = session
            .execute(
                "SELECT * FROM lix_file WHERE id = $1",
                &[Value::Text(
                    "6d697373-696e-872d-8669-6c6500000000".to_string(),
                )],
            )
            .await
            .expect("numbered placeholder should bind when no rows match");
        assert_eq!(result.len(), 0);

        let result = session
            .execute(
                "SELECT '?' AS literal, $1 AS first, $2 AS second",
                &[Value::Integer(10), Value::Text("second".to_string())],
            )
            .await
            .expect("numbered placeholders should bind by index");
        let row = result.rows().first().expect("query should return one row");
        assert_eq!(
            row.values(),
            &[
                Value::Text("?".to_string()),
                Value::Integer(10),
                Value::Text("second".to_string()),
            ]
        );

        let result = session
            .execute(
                "SELECT 'it''s ?' AS escaped_literal, $1 AS bound_value -- ? in comment\n",
                &[Value::Integer(42)],
            )
            .await
            .expect("normalization should preserve escaped literals and comments");
        let row = result.rows().first().expect("query should return one row");
        assert_eq!(
            row.values(),
            &[Value::Text("it's ?".to_string()), Value::Integer(42)]
        );

        let error = session
            .execute("SELECT $1 AS missing_param", &[])
            .await
            .expect_err("numbered placeholder without a value should fail");
        assert_eq!(error.code, LixError::CODE_INVALID_PARAM);
    }
);

simulation_test!(sql_anonymous_placeholders_are_rejected, |sim| async move {
    let engine = sim.boot_engine().await;
    let session = sim.wrap_session(
        engine
            .open_session()
            .await
            .expect("main session should open"),
        &engine,
    );

    let error = session
        .execute("SELECT ? AS anonymous_value", &[Value::Integer(1)])
        .await
        .expect_err("anonymous placeholders should fail");

    assert_eq!(error.code, LixError::CODE_PARSE_ERROR);
});

simulation_test!(
    sql_transaction_execute_accepts_numbered_placeholders,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine
                .open_session()
                .await
                .expect("main session should open"),
            &engine,
        );
        let mut transaction = session
            .begin_transaction()
            .await
            .expect("transaction should begin");

        let result = transaction
            .execute(
                "SELECT $1 AS first, $2 AS second",
                &[Value::Integer(1), Value::Text("two".to_string())],
            )
            .await
            .expect("numbered parameter read in transaction should succeed");
        assert_eq!(
            result.rows()[0].values(),
            &[Value::Integer(1), Value::Text("two".to_string())]
        );

        transaction
            .execute(
                "INSERT INTO lix_file (id, path) VALUES ($1, $2)",
                &[
                    Value::Text("616e6f6e-796d-8f75-832d-7472616e7300".to_string()),
                    Value::Text("/numbered-transaction.txt".to_string()),
                ],
            )
            .await
            .expect("numbered parameter write in transaction should succeed");

        transaction
            .commit()
            .await
            .expect("transaction commit should succeed");

        let result = session
            .execute(
                "SELECT path FROM lix_file WHERE id = $1",
                &[Value::Text(
                    "616e6f6e-796d-8f75-832d-7472616e7300".to_string(),
                )],
            )
            .await
            .expect("committed numbered parameter write should be visible");
        assert_eq!(
            result.rows()[0].values(),
            &[Value::Text("/numbered-transaction.txt".to_string())]
        );
    }
);

simulation_test!(sql_explain_is_read_shaped, |sim| async move {
    let engine = sim.boot_engine().await;
    let session = sim.wrap_session(
        engine
            .open_session()
            .await
            .expect("main session should open"),
        &engine,
    );

    let result = session
        .execute("EXPLAIN SELECT $1 AS explained_value", &[Value::Integer(1)])
        .await
        .expect("EXPLAIN SELECT should return explain rows");
    assert!(!result.columns().is_empty());
    assert!(!result.rows().is_empty());

    let error = session
        .execute(
            "EXPLAIN INSERT INTO lix_file (id, path) VALUES ($1, $2)",
            &[
                Value::Text("explained-write".to_string()),
                Value::Text("/explained-write.txt".to_string()),
            ],
        )
        .await
        .expect_err("EXPLAIN of write statements should not route through write execution");
    assert_eq!(error.code, LixError::CODE_UNSUPPORTED_SQL);
});

simulation_test!(sql_json_function_miss_has_postgres_jsonb_hint, |sim| async move {
    let engine = sim.boot_engine().await;
    let session = sim.wrap_session(
        engine
            .open_session()
            .await
            .expect("main session should open"),
        &engine,
    );

    let error = session
        .execute("SELECT json_extract('{\"a\":1}', '$.a')", &[])
        .await
        .expect_err("non-Lix JSON UDF should fail with a targeted hint");

    assert_eq!(error.code, LixError::CODE_UDF_NOT_FOUND);
    assert!(
        error
            .hint()
            .is_some_and(|hint| hint.contains("PostgreSQL JSONB operators")),
        "expected PostgreSQL JSONB hint: {error}"
    );
});

simulation_test!(
    sql_json_arrow_operator_uses_postgres_semantics,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine
                .open_session()
                .await
                .expect("main session should open"),
            &engine,
        );

        let result = session
            .execute("SELECT CAST('{\"a\":1}' AS JSONB) ->> 'a'", &[])
            .await
            .expect("PostgreSQL JSON arrow operator should be supported");
        assert_eq!(result.rows()[0].values(), &[Value::Text("1".into())]);
    }
);

simulation_test!(
    sql_udf_argument_mismatch_is_public_invalid_param,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine
                .open_session()
                .await
                .expect("main session should open"),
            &engine,
        );

        let error = session
            .execute("SELECT uuidv7('unexpected')", &[])
            .await
            .expect_err("wrong UDF arity should fail as public invalid input");

        assert_eq!(error.code, LixError::CODE_INVALID_PARAM);
    }
);

simulation_test!(
    sql_non_utf8_blob_parameter_has_targeted_error,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine
                .open_session()
                .await
                .expect("main session should open"),
            &engine,
        );

        let error = session
            .execute("SELECT length($1)", &[Value::Blob(vec![0xff].into())])
            .await
            .expect_err("non-UTF-8 blob should fail as text");

        assert_eq!(error.code, LixError::CODE_TYPE_MISMATCH);
        assert!(
            error.message.contains("valid UTF-8 text"),
            "expected targeted UTF-8 message: {error}"
        );
        assert!(
            error
                .hint()
                .is_some_and(|hint| hint.contains("blob") && !hint.contains("lix_json")),
            "expected blob-specific hint without JSON detour: {error}"
        );
    }
);

simulation_test!(
    sql_blob_insert_into_json_row_has_targeted_error,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine
                .open_session()
                .await
                .expect("main session should open"),
            &engine,
        );

        let error = session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('blob-value', $1)",
                &[Value::Blob(vec![1, 2, 3, 255, 0, 128].into())],
            )
            .await
            .expect_err("blob row insert should fail cleanly");

        assert_eq!(error.code, LixError::CODE_INVALID_PARAM);
        assert!(
            error.message.contains("cannot store blob values directly"),
            "expected targeted blob-to-JSON message: {error}"
        );
        assert!(
            !error.message.contains("Binary("),
            "error should not expose Rust/DataFusion debug formatting: {error}"
        );
    }
);

simulation_test!(sql_create_table_returns_error, |sim| async move {
    let engine = sim.boot_engine().await;
    let session = sim.wrap_session(
        engine
            .open_session()
            .await
            .expect("main session should open"),
        &engine,
    );

    let error = session
        .execute("CREATE TABLE scratch (id TEXT)", &[])
        .await
        .expect_err("CREATE TABLE should return an error, not panic");

    assert_eq!(error.code, LixError::CODE_UNSUPPORTED_SQL);
});

simulation_test!(
    sql_recursive_cte_over_commit_views_returns_error,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(
            engine
                .open_session()
                .await
                .expect("main session should open"),
            &engine,
        );

        let error = session
            .execute(
                "WITH RECURSIVE commit_walk(id) AS ( \
                 SELECT id FROM lix_commit \
                 UNION ALL \
                 SELECT lix_commit_edge.child_id \
                 FROM lix_commit_edge \
                 JOIN commit_walk ON lix_commit_edge.parent_id = commit_walk.id \
                 ) \
                 SELECT id FROM commit_walk",
                &[],
            )
            .await
            .expect_err("recursive CTE should return an error, not panic");

        assert_eq!(error.code, LixError::CODE_UNSUPPORTED_SQL, "{error:?}");
    }
);