kimberlite-query 0.5.0

SQL query layer for Kimberlite projections
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
//! Tests for the extended type system (14 types total).
//!
//! Covers encoding, decoding, ordering, and validation for all types.

use crate::key_encoder::{
    decode_date, decode_decimal, decode_integer, decode_real, decode_smallint, decode_time,
    decode_tinyint, decode_uuid, encode_date, encode_decimal, encode_integer, encode_key,
    encode_real, encode_smallint, encode_time, encode_tinyint, encode_uuid,
};
use crate::value::Value;

#[test]
fn test_tinyint_encoding_preserves_order() {
    let values = [i8::MIN, i8::MIN + 1, -1, 0, 1, i8::MAX - 1, i8::MAX];

    let encoded: Vec<_> = values.iter().map(|&v| encode_tinyint(v)).collect();
    let mut sorted = encoded.clone();
    sorted.sort_unstable();

    assert_eq!(encoded, sorted, "TinyInt encoding should preserve ordering");

    // Verify decode round-trips
    for &v in &values {
        assert_eq!(decode_tinyint(encode_tinyint(v)), v);
    }
}

#[test]
fn test_smallint_encoding_preserves_order() {
    let values = [
        i16::MIN,
        i16::MIN + 1,
        -1000,
        -1,
        0,
        1,
        1000,
        i16::MAX - 1,
        i16::MAX,
    ];

    let encoded: Vec<_> = values.iter().map(|&v| encode_smallint(v)).collect();
    let mut sorted = encoded.clone();
    sorted.sort_unstable();

    assert_eq!(
        encoded, sorted,
        "SmallInt encoding should preserve ordering"
    );

    // Verify decode round-trips
    for &v in &values {
        assert_eq!(decode_smallint(encode_smallint(v)), v);
    }
}

#[test]
fn test_integer_encoding_preserves_order() {
    let values = [
        i32::MIN,
        i32::MIN + 1,
        -1000000,
        -1,
        0,
        1,
        1000000,
        i32::MAX - 1,
        i32::MAX,
    ];

    let encoded: Vec<_> = values.iter().map(|&v| encode_integer(v)).collect();
    let mut sorted = encoded.clone();
    sorted.sort_unstable();

    assert_eq!(encoded, sorted, "Integer encoding should preserve ordering");

    // Verify decode round-trips
    for &v in &values {
        assert_eq!(decode_integer(encode_integer(v)), v);
    }
}

#[test]
fn test_real_total_ordering() {
    // Test that encoding preserves ordering for sortable values
    let values = [
        f64::NEG_INFINITY,
        -1000.0,
        -1.0,
        -0.0,
        0.0,
        1.0,
        1000.0,
        f64::INFINITY,
    ];

    let encoded: Vec<_> = values.iter().map(|&v| encode_real(v)).collect();
    let mut sorted = encoded.clone();
    sorted.sort_unstable();

    assert_eq!(
        encoded, sorted,
        "Real encoding should preserve total ordering for non-NaN values"
    );

    // Verify decode round-trips
    for &v in &values {
        let decoded = decode_real(encode_real(v));
        assert_eq!(decoded, v, "Value should round-trip");
    }

    // Test NaN separately
    let nan = f64::NAN;
    let decoded_nan = decode_real(encode_real(nan));
    assert!(decoded_nan.is_nan(), "NaN should decode to NaN");
}

#[test]
fn test_real_negative_zero_vs_positive_zero() {
    let neg_zero = -0.0f64;
    let pos_zero = 0.0f64;

    let encoded_neg = encode_real(neg_zero);
    let encoded_pos = encode_real(pos_zero);

    // Negative zero should sort before positive zero
    assert!(
        encoded_neg < encoded_pos,
        "Negative zero should sort before positive zero"
    );
}

#[test]
fn test_decimal_encoding_preserves_order() {
    let scale = 2;
    let values = [
        (i128::MIN, scale),
        (-1000000, scale),
        (-100, scale),
        (0, scale),
        (100, scale),
        (1000000, scale),
        (i128::MAX, scale),
    ];

    let encoded: Vec<_> = values.iter().map(|&(v, s)| encode_decimal(v, s)).collect();
    let mut sorted = encoded.clone();
    sorted.sort_unstable();

    assert_eq!(encoded, sorted, "Decimal encoding should preserve ordering");

    // Verify decode round-trips
    for &(v, s) in &values {
        assert_eq!(decode_decimal(encode_decimal(v, s)), (v, s));
    }
}

#[test]
fn test_date_encoding_preserves_order() {
    let values = [i32::MIN, -365, -1, 0, 1, 365, 10000, i32::MAX];

    let encoded: Vec<_> = values.iter().map(|&v| encode_date(v)).collect();
    let mut sorted = encoded.clone();
    sorted.sort_unstable();

    assert_eq!(encoded, sorted, "Date encoding should preserve ordering");

    // Verify decode round-trips
    for &v in &values {
        assert_eq!(decode_date(encode_date(v)), v);
    }
}

#[test]
fn test_time_encoding_preserves_order() {
    let values = [
        0i64,
        1_000_000_000,          // 1 second
        3_600_000_000_000,      // 1 hour
        86_400_000_000_000 - 1, // Last nanosecond of day
    ];

    let encoded: Vec<_> = values.iter().map(|&v| encode_time(v)).collect();
    let mut sorted = encoded.clone();
    sorted.sort_unstable();

    assert_eq!(encoded, sorted, "Time encoding should preserve ordering");

    // Verify decode round-trips
    for &v in &values {
        assert_eq!(decode_time(encode_time(v)), v);
    }
}

#[test]
fn test_uuid_encoding_round_trip() {
    let uuids = [
        [0u8; 16],
        [255u8; 16],
        [
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
            0x32, 0x10,
        ],
    ];

    for uuid in &uuids {
        assert_eq!(decode_uuid(encode_uuid(*uuid)), *uuid);
    }
}

#[test]
fn test_composite_key_with_new_types() {
    let values = vec![
        Value::TinyInt(42),
        Value::SmallInt(1000),
        Value::Integer(100000),
        Value::Real(3.14159),
        Value::Decimal(12345, 2), // 123.45
        Value::Uuid([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
        Value::Date(19000),             // Days since epoch
        Value::Time(43200_000_000_000), // Noon
    ];

    let key = encode_key(&values);

    // Verify key is non-empty
    assert!(!key.as_bytes().is_empty());

    // Verify key length is reasonable
    // 1 byte tag + data for each: 1+1 + 1+2 + 1+4 + 1+8 + 1+17 + 1+16 + 1+4 + 1+8 = 66 bytes
    // But Text and Bytes also need length prefix, so may vary
    assert!(
        key.as_bytes().len() >= 64,
        "Key should be at least 64 bytes"
    );
}

#[test]
fn test_value_compare_tinyint() {
    assert_eq!(
        Value::TinyInt(-1).compare(&Value::TinyInt(0)),
        Some(std::cmp::Ordering::Less)
    );
    assert_eq!(
        Value::TinyInt(0).compare(&Value::TinyInt(0)),
        Some(std::cmp::Ordering::Equal)
    );
    assert_eq!(
        Value::TinyInt(1).compare(&Value::TinyInt(0)),
        Some(std::cmp::Ordering::Greater)
    );
}

#[test]
fn test_value_compare_real_with_special_values() {
    let neg_inf = Value::Real(f64::NEG_INFINITY);
    let zero = Value::Real(0.0);
    let inf = Value::Real(f64::INFINITY);

    // -Inf < 0 < Inf
    assert_eq!(neg_inf.compare(&zero), Some(std::cmp::Ordering::Less));
    assert_eq!(zero.compare(&inf), Some(std::cmp::Ordering::Less));
    assert_eq!(neg_inf.compare(&inf), Some(std::cmp::Ordering::Less));

    // Test NaN comparison (NaN uses total ordering in our implementation)
    let nan = Value::Real(f64::NAN);
    assert_eq!(nan.compare(&nan), Some(std::cmp::Ordering::Equal));
}

#[test]
fn test_value_compare_decimal_same_scale() {
    let a = Value::Decimal(12345, 2); // 123.45
    let b = Value::Decimal(12346, 2); // 123.46

    assert_eq!(a.compare(&b), Some(std::cmp::Ordering::Less));
}

#[test]
fn test_value_compare_decimal_different_scale() {
    let a = Value::Decimal(12345, 2); // 123.45
    let b = Value::Decimal(12345, 3); // 12.345

    // Different scales are incomparable
    assert_eq!(a.compare(&b), None);
}

#[test]
fn test_value_compare_different_types() {
    let a = Value::Integer(42);
    let b = Value::BigInt(42);

    // Different types are incomparable (strict typing)
    assert_eq!(a.compare(&b), None);
}

#[test]
fn test_value_to_json_all_types() {
    use serde_json::json;

    assert_eq!(Value::TinyInt(42).to_json(), json!(42));
    assert_eq!(Value::SmallInt(1000).to_json(), json!(1000));
    assert_eq!(Value::Integer(100000).to_json(), json!(100000));
    assert_eq!(Value::Real(3.14).to_json(), json!(3.14));
    assert_eq!(Value::Decimal(12345, 2).to_json(), json!("123.45"));

    let uuid_str = Value::Uuid([
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
        0x10,
    ])
    .to_json();
    assert_eq!(uuid_str, json!("01234567-89ab-cdef-fedc-ba9876543210"));

    assert_eq!(Value::Date(19000).to_json(), json!(19000));
    assert_eq!(
        Value::Time(43200_000_000_000).to_json(),
        json!(43200000000000i64)
    );

    let json_val = Value::Json(json!({"key": "value"}));
    assert_eq!(json_val.to_json(), json!({"key": "value"}));
}

#[test]
fn test_value_display() {
    assert_eq!(format!("{}", Value::TinyInt(42)), "42");
    assert_eq!(format!("{}", Value::SmallInt(1000)), "1000");
    assert_eq!(format!("{}", Value::Integer(100000)), "100000");
    assert_eq!(format!("{}", Value::Real(3.14)), "3.14");
    assert_eq!(format!("{}", Value::Decimal(12345, 2)), "123.45");
    assert_eq!(
        format!(
            "{}",
            Value::Uuid([
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
                0x32, 0x10
            ])
        ),
        "01234567-89ab-cdef-fedc-ba9876543210"
    );
    assert_eq!(format!("{}", Value::Date(19000)), "DATE(19000)");
    assert_eq!(
        format!("{}", Value::Time(43200_000_000_000)),
        "TIME(43200000000000)"
    );
}

#[test]
fn test_null_comparison_with_all_types() {
    // NULL < all non-NULL values
    assert_eq!(
        Value::Null.compare(&Value::TinyInt(0)),
        Some(std::cmp::Ordering::Less)
    );
    assert_eq!(
        Value::Null.compare(&Value::Real(0.0)),
        Some(std::cmp::Ordering::Less)
    );
    assert_eq!(
        Value::Null.compare(&Value::Uuid([0; 16])),
        Some(std::cmp::Ordering::Less)
    );

    // NULL == NULL
    assert_eq!(
        Value::Null.compare(&Value::Null),
        Some(std::cmp::Ordering::Equal)
    );
}

#[test]
fn test_value_equality() {
    // Same type, same value
    assert_eq!(Value::Integer(42), Value::Integer(42));
    assert_ne!(Value::Integer(42), Value::Integer(43));

    // Different types, same underlying value
    assert_ne!(Value::Integer(42), Value::BigInt(42));

    // Float equality with total ordering
    assert_eq!(Value::Real(3.14), Value::Real(3.14));
    assert_eq!(Value::Real(f64::NAN), Value::Real(f64::NAN)); // NaN == NaN with total ordering
}

#[test]
#[should_panic(expected = "JSON values cannot be used in primary keys")]
fn test_json_cannot_be_encoded_as_key() {
    let values = vec![Value::Json(serde_json::json!({"key": "value"}))];
    encode_key(&values);
}

#[test]
fn test_all_integer_types_distinct() {
    // Verify that TinyInt, SmallInt, Integer, BigInt are distinct types
    let tiny = Value::TinyInt(42);
    let small = Value::SmallInt(42);
    let int = Value::Integer(42);
    let big = Value::BigInt(42);

    // All should be incomparable (different types)
    assert_eq!(tiny.compare(&small), None);
    assert_eq!(tiny.compare(&int), None);
    assert_eq!(tiny.compare(&big), None);
    assert_eq!(small.compare(&int), None);
    assert_eq!(small.compare(&big), None);
    assert_eq!(int.compare(&big), None);
}