lix 0.18.0

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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
use datafusion::arrow::array::{
    Array, ArrayRef, BinaryArray, BooleanArray, Float32Array, Float64Array, Int8Array, Int16Array,
    Int32Array, Int64Array, LargeBinaryArray, LargeListArray, LargeStringArray, ListArray,
    StringArray, StringViewArray, UInt8Array, UInt16Array, UInt32Array, UInt64Array,
};
use datafusion::common::{DataFusionError, Result};
use datafusion::logical_expr::ColumnarValue;
use serde_json::{Number, Value as JsonValue};

/// Parse and normalize the subset of PostgreSQL JSONB represented by Lix.
/// Object order and duplicate spelling are discarded by parsing, JSON numbers
/// are normalized exactly, and NUL is rejected because PostgreSQL's `jsonb`
/// cannot represent it in text values.
pub(crate) fn parse_jsonb(raw: &str) -> std::result::Result<JsonValue, String> {
    let mut value = serde_json::from_str::<JsonValue>(raw).map_err(|error| error.to_string())?;
    normalize_jsonb(&mut value)?;
    Ok(value)
}

pub(crate) fn canonical_jsonb_text(raw: &str) -> std::result::Result<String, String> {
    serde_json::to_string(&parse_jsonb(raw)?).map_err(|error| error.to_string())
}

pub(crate) fn normalize_jsonb(value: &mut JsonValue) -> std::result::Result<(), String> {
    match value {
        JsonValue::String(value) => reject_jsonb_nul(value)?,
        JsonValue::Array(values) => {
            for value in values {
                normalize_jsonb(value)?;
            }
        }
        JsonValue::Object(values) => {
            // DataFusion enables serde_json's `preserve_order` feature across
            // the workspace. Sort the parsed object in place so JSONB keeps
            // the stable key order used by Lix's previous BTreeMap-backed
            // representation without rebuilding every object in an
            // IndexMap.
            values.sort_keys();
            for (key, value) in values.iter_mut() {
                reject_jsonb_nul(key)?;
                normalize_jsonb(value)?;
            }
        }
        JsonValue::Number(number) => {
            *value = JsonValue::Number(normalize_jsonb_number(number)?);
        }
        JsonValue::Null | JsonValue::Bool(_) => {}
    }
    Ok(())
}

fn normalize_jsonb_number(number: &Number) -> std::result::Result<Number, String> {
    const MAX_INTEGER_DIGITS: i64 = 131_072;
    const MAX_FRACTIONAL_DIGITS: i64 = 16_383;

    let raw = number.as_str();
    let (negative, raw) = raw
        .strip_prefix('-')
        .map_or((false, raw), |raw| (true, raw));
    let exponent_index = raw.find(['e', 'E']);
    let (mantissa, exponent) = match exponent_index {
        Some(index) => (&raw[..index], &raw[index + 1..]),
        None => (raw, "0"),
    };
    let exponent = exponent.parse::<i64>().map_err(|_| {
        "JSONB numeric exponent is outside PostgreSQL's supported range".to_owned()
    })?;
    let (integer, fraction) = mantissa.split_once('.').unwrap_or((mantissa, ""));

    // PostgreSQL applies NUMERIC's scale limit to the input spelling before
    // insignificant zeroes are stripped. Check it first so values such as
    // 1.<16,384 zeroes> cannot evade the limit by normalizing to `1`.
    let input_scale = i64::try_from(fraction.len())
        .ok()
        .and_then(|scale| scale.checked_sub(exponent))
        .ok_or_else(|| "JSONB numeric exponent is outside PostgreSQL's supported range".to_owned())?
        .max(0);
    if input_scale > MAX_FRACTIONAL_DIGITS {
        return Err("JSONB number exceeds PostgreSQL numeric precision or scale limits".to_owned());
    }

    let mut digits = String::with_capacity(integer.len() + fraction.len());
    digits.push_str(integer);
    digits.push_str(fraction);

    let leading_zeroes = digits.bytes().take_while(|digit| *digit == b'0').count();
    if leading_zeroes == digits.len() {
        return Ok(Number::from_string_unchecked("0".to_owned()));
    }
    digits.drain(..leading_zeroes);

    let decimal_position = i64::try_from(integer.len())
        .ok()
        .and_then(|position| position.checked_add(exponent))
        .and_then(|position| position.checked_sub(i64::try_from(leading_zeroes).ok()?))
        .ok_or_else(|| "JSONB numeric exponent is outside PostgreSQL's supported range".to_owned())?;

    let integer_digits = decimal_position.max(0);
    let fractional_digits = i64::try_from(digits.len())
        .ok()
        .and_then(|length| length.checked_sub(decimal_position))
        .ok_or_else(|| "JSONB numeric exponent is outside PostgreSQL's supported range".to_owned())?
        .max(0);
    if integer_digits > MAX_INTEGER_DIGITS || fractional_digits > MAX_FRACTIONAL_DIGITS {
        return Err("JSONB number exceeds PostgreSQL numeric precision or scale limits".to_owned());
    }

    let sign_length = if negative { 1 } else { 0 };
    let display_length = if decimal_position <= 0 {
        sign_length + 2 + fractional_digits
    } else {
        sign_length + integer_digits + if fractional_digits > 0 {
            1 + fractional_digits
        } else {
            0
        }
    };
    let mut canonical = String::with_capacity(usize::try_from(display_length).unwrap_or_default());
    if negative {
        canonical.push('-');
    }
    if decimal_position <= 0 {
        canonical.push_str("0.");
        canonical.extend(std::iter::repeat('0').take((-decimal_position) as usize));
        canonical.push_str(&digits);
    } else if decimal_position >= i64::try_from(digits.len()).unwrap_or(i64::MAX) {
        canonical.push_str(&digits);
        let integer_zeroes = decimal_position - i64::try_from(digits.len()).unwrap_or(i64::MAX);
        canonical.extend(std::iter::repeat('0').take(integer_zeroes as usize));
    } else {
        let split = decimal_position as usize;
        canonical.push_str(&digits[..split]);
        canonical.push('.');
        canonical.push_str(&digits[split..]);
    }

    // JSONB is stored as normalized UTF8 in Lix. Strip decimal display scale
    // so DataFusion's native equality, hash, DISTINCT, and set-operation
    // kernels retain JSONB numeric equality (1, 1.0, and 1.00 compare equal).
    if let Some(decimal_point) = canonical.find('.') {
        while canonical.ends_with('0') {
            canonical.pop();
        }
        if canonical.len() == decimal_point + 1 {
            canonical.pop();
        }
    }

    // The normalized spelling is generated from a valid JSON number and only
    // changes its decimal point and insignificant zeroes.
    Ok(Number::from_string_unchecked(canonical))
}

fn reject_jsonb_nul(value: &str) -> std::result::Result<(), String> {
    if value.contains('\0') {
        Err("PostgreSQL JSONB does not support the Unicode NUL escape (\\u0000)".to_owned())
    } else {
        Ok(())
    }
}

pub(super) fn scalar_inputs(args: &[ColumnarValue]) -> bool {
    args.iter()
        .all(|value| matches!(value, ColumnarValue::Scalar(_)))
}

pub(super) fn json_value_to_serde(array: &dyn Array, row: usize) -> Result<Option<JsonValue>> {
    let Some(raw) = text_like_value(array, row)? else {
        return Ok(None);
    };
    parse_jsonb(&raw).map(Some).map_err(|error| {
        DataFusionError::Execution(format!(
            "JSON function expected valid JSON text in its first argument, got error: {error}"
        ))
    })
}

pub(super) fn text_like_value(array: &dyn Array, row: usize) -> Result<Option<String>> {
    if matches!(
        array.data_type(),
        datafusion::arrow::datatypes::DataType::Null
    ) {
        return Ok(None);
    }
    if let Some(array) = array.as_any().downcast_ref::<StringArray>() {
        return Ok((!array.is_null(row)).then(|| array.value(row).to_string()));
    }
    if let Some(array) = array.as_any().downcast_ref::<LargeStringArray>() {
        return Ok((!array.is_null(row)).then(|| array.value(row).to_string()));
    }
    if let Some(array) = array.as_any().downcast_ref::<StringViewArray>() {
        return Ok((!array.is_null(row)).then(|| array.value(row).to_string()));
    }
    if let Some(value) = numeric_value(array, row)? {
        return Ok(Some(value));
    }
    if let Some(array) = array.as_any().downcast_ref::<BooleanArray>() {
        return Ok((!array.is_null(row)).then(|| {
            if array.value(row) {
                "true".to_string()
            } else {
                "false".to_string()
            }
        }));
    }
    if let Some(array) = array.as_any().downcast_ref::<BinaryArray>() {
        return (!array.is_null(row))
            .then(|| utf8_text(array.value(row)).map(str::to_owned))
            .transpose();
    }
    if let Some(array) = array.as_any().downcast_ref::<LargeBinaryArray>() {
        return (!array.is_null(row))
            .then(|| utf8_text(array.value(row)).map(str::to_owned))
            .transpose();
    }
    Err(DataFusionError::Execution(format!(
        "unsupported argument type for JSON/text function: {:?}",
        array.data_type()
    )))
}

pub(crate) fn utf8_text(bytes: &[u8]) -> Result<&str> {
    std::str::from_utf8(bytes).map_err(|error| {
        DataFusionError::Execution(format!("binary text input is not valid UTF-8: {error}"))
    })
}

pub(crate) fn parse_uuid(raw: &str) -> Result<uuid::Uuid> {
    uuid::Uuid::parse_str(raw)
        .map_err(|error| DataFusionError::Execution(format!("invalid UUID value: {error}")))
}

pub(super) fn numeric_value(array: &dyn Array, row: usize) -> Result<Option<String>> {
    macro_rules! numeric_array {
        ($ty:ty) => {
            if let Some(array) = array.as_any().downcast_ref::<$ty>() {
                return Ok((!array.is_null(row)).then(|| array.value(row).to_string()));
            }
        };
    }

    numeric_array!(Int8Array);
    numeric_array!(Int16Array);
    numeric_array!(Int32Array);
    numeric_array!(Int64Array);
    numeric_array!(UInt8Array);
    numeric_array!(UInt16Array);
    numeric_array!(UInt32Array);
    numeric_array!(UInt64Array);
    numeric_array!(Float32Array);
    numeric_array!(Float64Array);
    Ok(None)
}

pub(super) fn extract_json_path(
    fn_name: &str,
    arrays: &[ArrayRef],
    row: usize,
    constant_path: Option<&[Option<String>]>,
) -> Result<Option<JsonValue>> {
    let Some(mut current) = json_value_to_serde(arrays[0].as_ref(), row)? else {
        return Ok(None);
    };

    if fn_name.contains("path_get") {
        let parsed_path;
        let path = if let Some(path) = constant_path {
            path
        } else {
            let Some(path) = text_array_path(arrays[1].as_ref(), row)? else {
                return Ok(None);
            };
            parsed_path = path;
            &parsed_path
        };
        for segment in path {
            let Some(segment) = segment else {
                return Ok(None);
            };
            let Some(next) = dynamic_path_get(&current, &segment) else {
                return Ok(None);
            };
            current = next;
        }
        return Ok(Some(current));
    }

    for path in &arrays[1..] {
        let Some(segment) = json_path_segment(fn_name, path.as_ref(), row)? else {
            return Ok(None);
        };
        let next = match segment {
            JsonPathSegment::Key(key) => current.get(&key).cloned(),
            JsonPathSegment::Index(index) => current.as_array().and_then(|values| {
                let index = if index < 0 {
                    i64::try_from(values.len()).ok()?.checked_add(index)?
                } else {
                    index
                };
                usize::try_from(index)
                    .ok()
                    .and_then(|index| values.get(index))
                    .cloned()
            }),
        };
        let Some(value) = next else {
            return Ok(None);
        };
        current = value;
    }

    Ok(Some(current))
}

pub(super) fn constant_text_array_path(
    value: &ColumnarValue,
) -> Result<Option<Vec<Option<String>>>> {
    let ColumnarValue::Scalar(value) = value else {
        return Ok(None);
    };
    let array = ColumnarValue::Scalar(value.clone()).into_array_of_size(1)?;
    text_array_path(array.as_ref(), 0)
}

fn text_array_path(array: &dyn Array, row: usize) -> Result<Option<Vec<Option<String>>>> {
    if array.is_null(row) {
        return Ok(None);
    }
    let (values, start, end): (&dyn Array, usize, usize) =
        if let Some(array) = array.as_any().downcast_ref::<ListArray>() {
            let offsets = array.value_offsets();
            (
                array.values().as_ref(),
                offsets[row] as usize,
                offsets[row + 1] as usize,
            )
        } else if let Some(array) = array.as_any().downcast_ref::<LargeListArray>() {
            let offsets = array.value_offsets();
            (
                array.values().as_ref(),
                usize::try_from(offsets[row]).map_err(|error| {
                    DataFusionError::Execution(format!("invalid JSONB path offset: {error}"))
                })?,
                usize::try_from(offsets[row + 1]).map_err(|error| {
                    DataFusionError::Execution(format!("invalid JSONB path offset: {error}"))
                })?,
            )
        } else {
            let Some(path) = text_like_value(array, row)? else {
                return Ok(None);
            };
            return postgres_text_array_path(&path).map(Some);
        };

    let mut path = Vec::with_capacity(end.saturating_sub(start));
    if let Some(values) = values.as_any().downcast_ref::<StringArray>() {
        for index in start..end {
            path.push((!values.is_null(index)).then(|| values.value(index).to_owned()));
        }
    } else if let Some(values) = values.as_any().downcast_ref::<LargeStringArray>() {
        for index in start..end {
            path.push((!values.is_null(index)).then(|| values.value(index).to_owned()));
        }
    } else if let Some(values) = values.as_any().downcast_ref::<StringViewArray>() {
        for index in start..end {
            path.push((!values.is_null(index)).then(|| values.value(index).to_owned()));
        }
    } else {
        return Err(DataFusionError::Execution(format!(
            "JSONB path array must contain text values, got {:?}",
            values.data_type()
        )));
    }
    Ok(Some(path))
}

pub(super) fn json_text_value(value: &JsonValue) -> Result<String> {
    match value {
        JsonValue::String(text) => Ok(text.clone()),
        JsonValue::Number(number) => Ok(number.to_string()),
        JsonValue::Bool(boolean) => Ok(if *boolean {
            "true".to_string()
        } else {
            "false".to_string()
        }),
        JsonValue::Array(_) | JsonValue::Object(_) => {
            serde_json::to_string(value).map_err(|error| {
                DataFusionError::Execution(format!(
                    "JSONB ->> could not render JSON value: {error}"
                ))
            })
        }
        JsonValue::Null => Ok("null".to_string()),
    }
}

pub(super) fn json_json_value(value: &JsonValue) -> Result<String> {
    serde_json::to_string(value).map_err(|error| {
        DataFusionError::Execution(format!("JSONB -> could not render JSON value: {error}"))
    })
}

enum JsonPathSegment {
    Key(String),
    Index(i64),
}

fn dynamic_path_get(value: &JsonValue, segment: &str) -> Option<JsonValue> {
    match value {
        JsonValue::Object(value) => value.get(segment).cloned(),
        JsonValue::Array(value) => {
            let index = segment.parse::<i64>().ok()?;
            let index = if index < 0 {
                i64::try_from(value.len()).ok()?.checked_add(index)?
            } else {
                index
            };
            usize::try_from(index)
                .ok()
                .and_then(|index| value.get(index))
                .cloned()
        }
        _ => None,
    }
}

fn postgres_text_array_path(value: &str) -> Result<Vec<Option<String>>> {
    let value = value.trim();
    let Some(inner) = value
        .strip_prefix('{')
        .and_then(|value| value.strip_suffix('}'))
    else {
        return Err(DataFusionError::Execution(format!(
            "JSONB path must use one-dimensional PostgreSQL text-array syntax such as '{{user,name}}', got '{value}'"
        )));
    };
    if inner.is_empty() {
        return Ok(Vec::new());
    }

    let mut elements = Vec::new();
    let mut chars = inner.chars().peekable();
    loop {
        while chars.peek().is_some_and(|character| character.is_whitespace()) {
            chars.next();
        }
        let mut element = String::new();
        let mut quoted = false;
        let mut escaped = false;
        if chars.peek() == Some(&'"') {
            quoted = true;
            chars.next();
            let mut closed = false;
            while let Some(character) = chars.next() {
                match character {
                    '\\' => {
                        let Some(escaped_character) = chars.next() else {
                            return Err(malformed_postgres_text_array(value));
                        };
                        element.push(escaped_character);
                        escaped = true;
                    }
                    '"' => {
                        closed = true;
                        break;
                    }
                    character => element.push(character),
                }
            }
            if !closed {
                return Err(malformed_postgres_text_array(value));
            }
            while chars.peek().is_some_and(|character| character.is_whitespace()) {
                chars.next();
            }
        } else {
            let mut started = false;
            let mut last_significant_length = 0;
            while let Some(character) = chars.peek().copied() {
                match character {
                    ',' => break,
                    '"' | '{' | '}' => return Err(malformed_postgres_text_array(value)),
                    '\\' => {
                        chars.next();
                        let Some(escaped_character) = chars.next() else {
                            return Err(malformed_postgres_text_array(value));
                        };
                        element.push(escaped_character);
                        started = true;
                        escaped = true;
                        last_significant_length = element.len();
                    }
                    character => {
                        chars.next();
                        if !started && character.is_whitespace() {
                            continue;
                        }
                        element.push(character);
                        started = true;
                        if !character.is_whitespace() {
                            last_significant_length = element.len();
                        }
                    }
                }
            }
            element.truncate(last_significant_length);
        }

        if !quoted && element.is_empty() {
            return Err(malformed_postgres_text_array(value));
        }
        elements.push(if !quoted && !escaped && element.eq_ignore_ascii_case("NULL") {
            None
        } else {
            Some(element)
        });

        match chars.next() {
            Some(',') => {
                if chars.peek().is_none() {
                    return Err(malformed_postgres_text_array(value));
                }
            }
            None => break,
            _ => return Err(malformed_postgres_text_array(value)),
        }
    }

    Ok(elements)
}

fn malformed_postgres_text_array(value: &str) -> DataFusionError {
    DataFusionError::Execution(format!("malformed PostgreSQL text-array JSONB path '{value}'"))
}

fn json_path_segment(
    fn_name: &str,
    array: &dyn Array,
    row: usize,
) -> Result<Option<JsonPathSegment>> {
    if let Some(array) = array.as_any().downcast_ref::<StringArray>() {
        if array.is_null(row) {
            return Ok(None);
        }
        let value = array.value(row).to_string();
        return Ok(Some(JsonPathSegment::Key(value)));
    }
    if let Some(array) = array.as_any().downcast_ref::<LargeStringArray>() {
        if array.is_null(row) {
            return Ok(None);
        }
        let value = array.value(row).to_string();
        return Ok(Some(JsonPathSegment::Key(value)));
    }
    if let Some(array) = array.as_any().downcast_ref::<StringViewArray>() {
        if array.is_null(row) {
            return Ok(None);
        }
        let value = array.value(row).to_string();
        return Ok(Some(JsonPathSegment::Key(value)));
    }
    macro_rules! index_array {
        ($ty:ty) => {
            if let Some(array) = array.as_any().downcast_ref::<$ty>() {
                if array.is_null(row) {
                    return Ok(None);
                }
                let value = array.value(row);
                let index = i64::try_from(value).map_err(|_| {
                    DataFusionError::Execution(format!(
                        "{fn_name}() path index is outside the supported integer range"
                    ))
                })?;
                return Ok(Some(JsonPathSegment::Index(index)));
            }
        };
    }
    index_array!(UInt8Array);
    index_array!(UInt16Array);
    index_array!(UInt32Array);
    index_array!(UInt64Array);
    index_array!(Int8Array);
    index_array!(Int16Array);
    index_array!(Int32Array);
    index_array!(Int64Array);
    Err(DataFusionError::Execution(format!(
        "{fn_name}() path arguments must be strings or integers, got {:?}",
        array.data_type()
    )))
}

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

    #[test]
    fn canonical_jsonb_collapses_equivalent_numeric_spellings() {
        assert_eq!(canonical_jsonb_text("[42]").unwrap(), "[42]");
        assert_eq!(canonical_jsonb_text("[42.0]").unwrap(), "[42]");
        assert_eq!(canonical_jsonb_text("[4.2e1]").unwrap(), "[42]");
        assert_eq!(canonical_jsonb_text("[ 42 ]").unwrap(), "[42]");
    }

    #[test]
    fn jsonb_numeric_scale_limit_is_checked_before_zero_normalization() {
        let fractional_zeroes = "0".repeat(16_384);
        assert!(canonical_jsonb_text(&format!("1.{fractional_zeroes}")).is_err());
        assert!(canonical_jsonb_text(&format!("0.{fractional_zeroes}")).is_err());
        assert!(canonical_jsonb_text("1e-16384").is_err());
        assert_eq!(canonical_jsonb_text("1e-16383").unwrap().len(), 16_385);
    }

    #[test]
    fn canonical_jsonb_rejects_nul_everywhere() {
        assert!(canonical_jsonb_text(r#"["a\u0000b"]"#).is_err());
        assert!(canonical_jsonb_text(r#"{"a\u0000b":1}"#).is_err());
    }
}