mssql-tds 0.1.0

Rust implementation of the TDS (Tabular Data Stream) protocol for SQL Server
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Cell decryption and denormalization for Always Encrypted (read path).
//!
//! When a column is encrypted, SQL Server sends each cell as an opaque cipher
//! blob (a `varbinary`) instead of the column's real type. To produce the
//! plaintext value the driver must:
//!
//! 1. Decrypt the blob with the column's encryption key using the
//!    [`AeadAes256CbcHmacSha256`] cipher, yielding the *normalized* plaintext
//!    bytes (the form SQL Server encrypts).
//! 2. *Denormalize* those bytes back into a typed [`ColumnValues`] using the
//!    column's base type information.
//!
//! The normalization rules (version 1) are defined by SQL Server and matched
//! against the reference drivers (the JDBC driver's `dtv.denormalizedValue`):
//!
//! * Integer types (`bit`/`tinyint`/`smallint`/`int`/`bigint`) are normalized to
//!   an 8-byte little-endian `bigint`.
//! * `real`/`float` keep their 4- or 8-byte IEEE form.
//! * `smallmoney`/`money` are normalized to the 8-byte `money` form (high dword
//!   then low dword); `smallmoney` is sign-extended into that form.
//! * `decimal`/`numeric` are a sign byte followed by a fixed 16-byte
//!   little-endian magnitude (four 32-bit words, zero-extended).
//! * `binary`/`varbinary` and `uniqueidentifier` keep their raw bytes.
//! * Character types keep their encoded bytes (UTF-16LE for the `n` types, the
//!   collation code page otherwise).

// Consumed by the ROW/NBCROW decode path in the next Always Encrypted phase.
#![allow(dead_code)]

use uuid::Uuid;

use super::aead_aes_256_cbc_hmac_sha256::AeadAes256CbcHmacSha256;
use crate::core::TdsResult;
use crate::datatypes::column_values::{
    ColumnValues, SqlDate, SqlDateTime, SqlDateTime2, SqlDateTimeOffset, SqlMoney,
    SqlSmallDateTime, SqlSmallMoney, SqlTime,
};
use crate::datatypes::decoder::{DECIMAL_MAGNITUDE_BYTES, DecimalParts, decimal_metadata_is_valid};
use crate::datatypes::sql_string::{EncodingType, SqlString};
use crate::datatypes::sqldatatypes::{TdsDataType, TypeInfo, TypeInfoVariant, is_unicode_type};
use crate::datatypes::sqltypes::SqlType;
use crate::error::Error;
use crate::query::metadata::CryptoMetadata;
use crate::security::encryption::ColumnEncryptionType;

/// Cipher algorithm id for `AEAD_AES_256_CBC_HMAC_SHA256`.
///
/// This is the value SQL Server uses to identify the algorithm both in the
/// COLMETADATA crypto metadata and in the `sp_describe_parameter_encryption`
/// result set (`TdsEnums.AEAD_AES_256_CBC_HMAC_SHA256` in the other drivers).
const AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID: u8 = 0x02;
/// The only cell-normalization rule version defined by SQL Server today.
const SUPPORTED_NORMALIZATION_VERSION: u8 = 0x01;
/// Total length of the normalized `decimal`/`numeric` form: 1 sign byte plus the
/// fixed 16-byte magnitude.
const DECIMAL_NORMALIZED_LEN: usize = 1 + DECIMAL_MAGNITUDE_BYTES;

/// Decrypts an encrypted cell blob and denormalizes it into a typed value.
///
/// `plaintext_cek` is the decrypted column encryption key (see
/// [`crate::security::keystore::decrypt_cek`]); `cipher_blob` is the raw cell
/// value read from the ROW token.
///
/// # Errors
///
/// Returns [`Error::ColumnEncryptionError`] if the cipher algorithm is
/// unsupported, decryption fails, or the plaintext cannot be denormalized into
/// the column's base type.
pub(crate) fn decrypt_cell(
    crypto_metadata: &CryptoMetadata,
    plaintext_cek: &[u8],
    cipher_blob: &[u8],
) -> TdsResult<ColumnValues> {
    if crypto_metadata.cipher_algorithm_id != AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID {
        return Err(Error::ColumnEncryptionError(format!(
            "Unsupported cell cipher algorithm id {:#04x} (expected \
             {AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID:#04x})",
            crypto_metadata.cipher_algorithm_id
        )));
    }

    let cipher = AeadAes256CbcHmacSha256::new(plaintext_cek)?;
    let plaintext = cipher.decrypt(cipher_blob)?;

    denormalize(
        &plaintext,
        crypto_metadata.base_data_type,
        &crypto_metadata.base_type_info,
        crypto_metadata.normalization_rule_version,
    )
}

/// Narrows a normalized 8-byte integer to a smaller signed/unsigned width.
///
/// The upper bytes of the normalized form are expected to be zero (or sign
/// extension), so the cast is lossless in practice. If a post-HMAC,
/// server-trusted value does not fit — a wire-format drift or upstream bug — the
/// cast still truncates (hard-failing trusted data would be too aggressive), but
/// we emit a `trace!` breadcrumb so the assumption break is not invisible.
fn narrow_tinyint(value: i64) -> u8 {
    if u8::try_from(value).is_err() {
        tracing::trace!(
            value,
            target = "tinyint(u8)",
            "Always Encrypted integer does not fit target width; truncating"
        );
    }
    value as u8
}

fn narrow_smallint(value: i64) -> i16 {
    if i16::try_from(value).is_err() {
        tracing::trace!(
            value,
            target = "smallint(i16)",
            "Always Encrypted integer does not fit target width; truncating"
        );
    }
    value as i16
}

fn narrow_int(value: i64) -> i32 {
    if i32::try_from(value).is_err() {
        tracing::trace!(
            value,
            target = "int(i32)",
            "Always Encrypted integer does not fit target width; truncating"
        );
    }
    value as i32
}

/// Converts normalized plaintext bytes back into a typed [`ColumnValues`].
pub(crate) fn denormalize(
    plaintext: &[u8],
    base_data_type: TdsDataType,
    base_type_info: &TypeInfo,
    normalization_rule_version: u8,
) -> TdsResult<ColumnValues> {
    if normalization_rule_version != SUPPORTED_NORMALIZATION_VERSION {
        return Err(Error::ColumnEncryptionError(format!(
            "Unsupported Always Encrypted normalization rule version {normalization_rule_version} \
             (expected {SUPPORTED_NORMALIZATION_VERSION})"
        )));
    }

    match base_data_type {
        // Integer family: normalized to an 8-byte little-endian bigint.
        TdsDataType::Bit | TdsDataType::BitN => Ok(ColumnValues::Bit(read_i64(plaintext)? != 0)),
        TdsDataType::Int1 => Ok(ColumnValues::TinyInt(narrow_tinyint(read_i64(plaintext)?))),
        TdsDataType::Int2 => Ok(ColumnValues::SmallInt(narrow_smallint(read_i64(
            plaintext,
        )?))),
        TdsDataType::Int4 => Ok(ColumnValues::Int(narrow_int(read_i64(plaintext)?))),
        TdsDataType::Int8 => Ok(ColumnValues::BigInt(read_i64(plaintext)?)),
        TdsDataType::IntN => {
            let value = read_i64(plaintext)?;
            match base_type_info.length {
                1 => Ok(ColumnValues::TinyInt(narrow_tinyint(value))),
                2 => Ok(ColumnValues::SmallInt(narrow_smallint(value))),
                4 => Ok(ColumnValues::Int(narrow_int(value))),
                8 => Ok(ColumnValues::BigInt(value)),
                other => Err(Error::ColumnEncryptionError(format!(
                    "Invalid IntN base length {other} for encrypted column"
                ))),
            }
        }

        // Floating point: keeps its 4- or 8-byte IEEE form.
        TdsDataType::Flt4 => Ok(ColumnValues::Real(read_f32(plaintext)?)),
        TdsDataType::Flt8 => Ok(ColumnValues::Float(read_f64(plaintext)?)),
        TdsDataType::FltN => match plaintext.len() {
            4 => Ok(ColumnValues::Real(read_f32(plaintext)?)),
            8 => Ok(ColumnValues::Float(read_f64(plaintext)?)),
            other => Err(Error::ColumnEncryptionError(format!(
                "Invalid FltN plaintext length {other} (expected 4 or 8)"
            ))),
        },

        // Money: normalized to the 8-byte money form (high dword then low dword).
        TdsDataType::Money => Ok(ColumnValues::Money(read_money(plaintext)?)),
        TdsDataType::Money4 => Ok(ColumnValues::SmallMoney(read_small_money(plaintext)?)),
        TdsDataType::MoneyN => match base_type_info.length {
            4 => Ok(ColumnValues::SmallMoney(read_small_money(plaintext)?)),
            8 => Ok(ColumnValues::Money(read_money(plaintext)?)),
            other => Err(Error::ColumnEncryptionError(format!(
                "Invalid MoneyN base length {other} for encrypted column"
            ))),
        },

        // Decimal/numeric: sign byte followed by the little-endian magnitude.
        TdsDataType::Decimal | TdsDataType::DecimalN => Ok(ColumnValues::Decimal(read_decimal(
            plaintext,
            base_type_info,
        )?)),
        TdsDataType::Numeric | TdsDataType::NumericN => Ok(ColumnValues::Numeric(read_decimal(
            plaintext,
            base_type_info,
        )?)),

        // Binary: raw bytes.
        TdsDataType::Binary
        | TdsDataType::VarBinary
        | TdsDataType::BigBinary
        | TdsDataType::BigVarBinary
        | TdsDataType::Image => Ok(ColumnValues::Bytes(plaintext.to_vec())),

        // uniqueidentifier: 16 little-endian bytes.
        TdsDataType::Guid => {
            let uuid = Uuid::from_slice_le(plaintext).map_err(|e| {
                Error::ColumnEncryptionError(format!("Invalid uniqueidentifier plaintext: {e}"))
            })?;
            Ok(ColumnValues::Uuid(uuid))
        }

        // Character types: keep the encoded bytes; the encoding follows the base type.
        TdsDataType::Char
        | TdsDataType::VarChar
        | TdsDataType::BigChar
        | TdsDataType::BigVarChar
        | TdsDataType::Text
        | TdsDataType::NChar
        | TdsDataType::NVarChar
        | TdsDataType::NText => {
            let encoding = string_encoding(base_data_type, base_type_info)?;
            Ok(ColumnValues::String(SqlString::new(
                plaintext.to_vec(),
                encoding,
            )))
        }

        // Temporal types: the normalized form matches the TDS row value layout
        // (without the length prefix). Scale comes from the base TYPE_INFO.
        TdsDataType::DateN => Ok(ColumnValues::Date(read_date(plaintext)?)),
        TdsDataType::TimeN => {
            let scale = temporal_scale(base_type_info)?;
            Ok(ColumnValues::Time(read_time(plaintext, scale)?))
        }
        TdsDataType::DateTime2N => {
            let scale = temporal_scale(base_type_info)?;
            Ok(ColumnValues::DateTime2(read_datetime2(plaintext, scale)?))
        }
        TdsDataType::DateTimeOffsetN => {
            let scale = temporal_scale(base_type_info)?;
            Ok(ColumnValues::DateTimeOffset(read_datetime_offset(
                plaintext, scale,
            )?))
        }
        TdsDataType::DateTim4 => Ok(ColumnValues::SmallDateTime(read_small_datetime(plaintext)?)),
        TdsDataType::DateTime => Ok(ColumnValues::DateTime(read_datetime(plaintext)?)),
        TdsDataType::DateTimeN => match base_type_info.length {
            4 => Ok(ColumnValues::SmallDateTime(read_small_datetime(plaintext)?)),
            8 => Ok(ColumnValues::DateTime(read_datetime(plaintext)?)),
            other => Err(Error::ColumnEncryptionError(format!(
                "Invalid DateTimeN base length {other} for encrypted column"
            ))),
        },

        // Anything else is not yet denormalized.
        other => Err(Error::ColumnEncryptionError(format!(
            "Denormalization is not yet implemented for encrypted base type {other:?}"
        ))),
    }
}

/// Reads an exact 8-byte little-endian `i64` (the normalized integer form).
fn read_i64(bytes: &[u8]) -> TdsResult<i64> {
    let array: [u8; 8] = bytes
        .try_into()
        .map_err(|_| length_error("8-byte integer", bytes.len()))?;
    Ok(i64::from_le_bytes(array))
}

/// Reads an exact 4-byte little-endian `f32`.
fn read_f32(bytes: &[u8]) -> TdsResult<f32> {
    let array: [u8; 4] = bytes
        .try_into()
        .map_err(|_| length_error("4-byte real", bytes.len()))?;
    Ok(f32::from_le_bytes(array))
}

/// Reads an exact 8-byte little-endian `f64`.
fn read_f64(bytes: &[u8]) -> TdsResult<f64> {
    let array: [u8; 8] = bytes
        .try_into()
        .map_err(|_| length_error("8-byte float", bytes.len()))?;
    Ok(f64::from_le_bytes(array))
}

/// Reads a little-endian `i32` from a 4-byte slice.
fn read_i32_at(bytes: &[u8], offset: usize) -> TdsResult<i32> {
    let slice = bytes
        .get(offset..offset + 4)
        .ok_or_else(|| length_error("4-byte money component", bytes.len()))?;
    let array: [u8; 4] = slice.try_into().expect("slice is exactly 4 bytes");
    Ok(i32::from_le_bytes(array))
}

/// Reads the 8-byte normalized `money` form (high dword then low dword).
fn read_money(bytes: &[u8]) -> TdsResult<SqlMoney> {
    if bytes.len() != 8 {
        return Err(length_error("8-byte money", bytes.len()));
    }
    Ok(SqlMoney {
        msb_part: read_i32_at(bytes, 0)?,
        lsb_part: read_i32_at(bytes, 4)?,
    })
}

/// Reads a normalized `smallmoney` (the low dword of the 8-byte money form).
fn read_small_money(bytes: &[u8]) -> TdsResult<SqlSmallMoney> {
    if bytes.len() != 8 {
        return Err(length_error("8-byte smallmoney", bytes.len()));
    }
    Ok(SqlSmallMoney {
        int_val: read_i32_at(bytes, 4)?,
    })
}

/// Reads the normalized decimal/numeric form: a sign byte (`1` = positive)
/// followed by the little-endian magnitude in 32-bit groups.
fn read_decimal(bytes: &[u8], base_type_info: &TypeInfo) -> TdsResult<DecimalParts> {
    let (precision, scale) = match base_type_info.type_info_variant {
        TypeInfoVariant::VarLenPrecisionScale(_, _, precision, scale) => (precision, scale),
        ref other => {
            return Err(Error::ColumnEncryptionError(format!(
                "Invalid base type info for decimal/numeric: {other:?}"
            )));
        }
    };

    if !decimal_metadata_is_valid(precision, scale) {
        return Err(Error::ColumnEncryptionError(format!(
            "Invalid decimal precision {precision} / scale {scale}"
        )));
    }

    let (sign, magnitude) = bytes
        .split_first()
        .ok_or_else(|| length_error("decimal sign byte", bytes.len()))?;

    // A valid decimal/numeric magnitude (precision <= 38) fits in 128 bits, so
    // it is at most 16 bytes (four 32-bit words). Reject anything longer: it
    // signals wire-format drift or corrupted plaintext.
    if magnitude.len() > DECIMAL_MAGNITUDE_BYTES {
        return Err(Error::ColumnEncryptionError(format!(
            "Invalid decimal magnitude length {} (max {DECIMAL_MAGNITUDE_BYTES} bytes for \
             precision <= 38)",
            magnitude.len()
        )));
    }

    // Reference AE readers are length-tolerant: .NET reads `length >> 2` 32-bit
    // groups and ignores any trailing partial group, and JDBC uses the actual
    // length (its bulk-copy path writes a 15-byte magnitude). Zero-filling the
    // fixed-width buffer pads a short trailing group into a full 32-bit word
    // rather than rejecting it and — unlike .NET — preserves those trailing
    // bytes instead of dropping them, so no significant magnitude bits are lost.
    // This lets us read magnitudes produced by every reference driver.
    let mut bytes = [0u8; DECIMAL_MAGNITUDE_BYTES];
    bytes[..magnitude.len()].copy_from_slice(magnitude);

    Ok(DecimalParts::new(
        *sign == 1,
        precision,
        scale,
        u128::from_le_bytes(bytes),
    ))
}

/// Determines the encoding for an encrypted character column from its base type.
fn string_encoding(
    base_data_type: TdsDataType,
    base_type_info: &TypeInfo,
) -> TdsResult<EncodingType> {
    if is_unicode_type(base_data_type) {
        return Ok(EncodingType::Utf16);
    }

    let collation = match base_type_info.type_info_variant {
        TypeInfoVariant::VarLenString(_, _, collation) => collation,
        TypeInfoVariant::PartialLen(_, _, collation, _, _) => collation,
        _ => None,
    };

    match collation {
        Some(collation) if collation.utf8() => Ok(EncodingType::Utf8),
        Some(collation) => Ok(EncodingType::LcidBased(collation)),
        None => Err(Error::ColumnEncryptionError(
            "Encrypted character column is missing collation information".to_string(),
        )),
    }
}

/// Builds a length-mismatch error.
fn length_error(expected: &str, actual: usize) -> Error {
    Error::ColumnEncryptionError(format!(
        "Invalid normalized plaintext length for {expected}: got {actual} bytes"
    ))
}

/// Extracts the fractional-seconds scale from a temporal column's base type.
fn temporal_scale(base_type_info: &TypeInfo) -> TdsResult<u8> {
    match base_type_info.type_info_variant {
        TypeInfoVariant::VarLenScale(_, scale) => Ok(scale),
        ref other => Err(Error::ColumnEncryptionError(format!(
            "Encrypted temporal column is missing scale information: {other:?}"
        ))),
    }
}

/// Reads a little-endian unsigned integer (1..=8 bytes) into a `u64`.
fn read_uint_le(bytes: &[u8], width: usize, what: &str) -> TdsResult<u64> {
    if bytes.len() != width {
        return Err(length_error(what, bytes.len()));
    }
    let mut value: u64 = 0;
    for (index, byte) in bytes.iter().enumerate() {
        value |= (*byte as u64) << (8 * index);
    }
    Ok(value)
}

/// Reads a `date`: a 3-byte little-endian day count since 0001-01-01.
fn read_date(bytes: &[u8]) -> TdsResult<SqlDate> {
    let days = read_uint_le(bytes, 3, "3-byte date")? as u32;
    Ok(SqlDate::unchecked_create(days))
}

/// Reads a `time(scale)`: a 3/4/5-byte little-endian scaled value, converted to
/// 100-nanosecond units (matching the row decoder).
fn read_time(bytes: &[u8], scale: u8) -> TdsResult<SqlTime> {
    let scaled_value = match bytes.len() {
        3 => read_uint_le(bytes, 3, "time")?,
        4 => read_uint_le(bytes, 4, "time")?,
        5 => read_uint_le(bytes, 5, "time")?,
        other => return Err(length_error("3/4/5-byte time", other)),
    };
    let multiplier = if scale <= 7 {
        10u64.pow(u32::from(7 - scale))
    } else {
        1
    };
    Ok(SqlTime {
        time_nanoseconds: scaled_value * multiplier,
        scale,
    })
}

/// Reads a `datetime2(scale)`: the time component followed by a 3-byte date.
fn read_datetime2(bytes: &[u8], scale: u8) -> TdsResult<SqlDateTime2> {
    let date_start = bytes
        .len()
        .checked_sub(3)
        .ok_or_else(|| length_error("datetime2 (time + 3-byte date)", bytes.len()))?;
    let time = read_time(&bytes[..date_start], scale)?;
    let date = read_date(&bytes[date_start..])?;
    Ok(SqlDateTime2 {
        days: date.get_days(),
        time,
    })
}

/// Reads a `datetimeoffset(scale)`: a `datetime2` followed by a 2-byte signed
/// UTC offset in minutes.
fn read_datetime_offset(bytes: &[u8], scale: u8) -> TdsResult<SqlDateTimeOffset> {
    let offset_start = bytes
        .len()
        .checked_sub(2)
        .ok_or_else(|| length_error("datetimeoffset (datetime2 + 2-byte offset)", bytes.len()))?;
    let datetime2 = read_datetime2(&bytes[..offset_start], scale)?;
    let offset = i16::from_le_bytes(
        bytes[offset_start..]
            .try_into()
            .expect("slice is exactly 2 bytes"),
    );
    Ok(SqlDateTimeOffset { datetime2, offset })
}

/// Reads a legacy `smalldatetime`: 2-byte day count and 2-byte minute count.
fn read_small_datetime(bytes: &[u8]) -> TdsResult<SqlSmallDateTime> {
    if bytes.len() != 4 {
        return Err(length_error("4-byte smalldatetime", bytes.len()));
    }
    Ok(SqlSmallDateTime {
        days: read_uint_le(&bytes[0..2], 2, "smalldatetime days")? as u16,
        time: read_uint_le(&bytes[2..4], 2, "smalldatetime minutes")? as u16,
    })
}

/// Reads a legacy `datetime`: a 4-byte signed day count and 4-byte tick count.
fn read_datetime(bytes: &[u8]) -> TdsResult<SqlDateTime> {
    if bytes.len() != 8 {
        return Err(length_error("8-byte datetime", bytes.len()));
    }
    let days = i32::from_le_bytes(bytes[0..4].try_into().expect("slice is exactly 4 bytes"));
    let time = u32::from_le_bytes(bytes[4..8].try_into().expect("slice is exactly 4 bytes"));
    Ok(SqlDateTime { days, time })
}

/// Encrypts a plaintext parameter value for Always Encrypted.
///
/// Normalizes `value` into the canonical plaintext byte form, then encrypts it
/// with the column encryption key using `AEAD_AES_256_CBC_HMAC_SHA256`.
/// Returns `Ok(None)` when the parameter value is SQL `NULL` (a NULL parameter
/// is sent with no ciphertext).
///
/// # Errors
///
/// Returns [`Error::ColumnEncryptionError`] if the cipher algorithm, encryption
/// type, or normalization rule version is unsupported, if the value's type is
/// not normalizable, or if encryption fails.
pub(crate) fn encrypt_parameter(
    value: &SqlType,
    plaintext_cek: &[u8],
    cipher_algorithm_id: u8,
    encryption_type: u8,
    normalization_rule_version: u8,
) -> TdsResult<Option<Vec<u8>>> {
    if cipher_algorithm_id != AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID {
        return Err(Error::ColumnEncryptionError(format!(
            "Unsupported parameter cipher algorithm id {cipher_algorithm_id:#04x} (expected \
             {AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID:#04x})"
        )));
    }
    if normalization_rule_version != SUPPORTED_NORMALIZATION_VERSION {
        return Err(Error::ColumnEncryptionError(format!(
            "Unsupported Always Encrypted normalization rule version {normalization_rule_version} \
             (expected {SUPPORTED_NORMALIZATION_VERSION})"
        )));
    }

    let column_encryption_type = match encryption_type {
        1 => ColumnEncryptionType::Deterministic,
        2 => ColumnEncryptionType::Randomized,
        other => {
            return Err(Error::ColumnEncryptionError(format!(
                "Unsupported Always Encrypted encryption type {other} (expected 1 or 2)"
            )));
        }
    };

    let Some(normalized) = normalize(value)? else {
        return Ok(None);
    };

    let cipher = AeadAes256CbcHmacSha256::new(plaintext_cek)?;
    Ok(Some(cipher.encrypt(&normalized, column_encryption_type)?))
}

/// Normalizes a plaintext parameter value into the canonical byte form that
/// SQL Server encrypts (the inverse of [`denormalize`]).
///
/// Returns `Ok(None)` when the value is SQL `NULL`.
pub(crate) fn normalize(value: &SqlType) -> TdsResult<Option<Vec<u8>>> {
    let bytes = match value {
        // Integer family: an 8-byte little-endian bigint.
        SqlType::Bit(v) => v.map(|b| i64::from(b).to_le_bytes().to_vec()),
        SqlType::TinyInt(v) => v.map(|n| i64::from(n).to_le_bytes().to_vec()),
        SqlType::SmallInt(v) => v.map(|n| i64::from(n).to_le_bytes().to_vec()),
        SqlType::Int(v) => v.map(|n| i64::from(n).to_le_bytes().to_vec()),
        SqlType::BigInt(v) => v.map(|n| n.to_le_bytes().to_vec()),

        // Floating point: native IEEE form.
        SqlType::Real(v) => v.map(|f| f.to_le_bytes().to_vec()),
        SqlType::Float(v) => v.map(|f| f.to_le_bytes().to_vec()),

        // Money: the 8-byte money form (high dword then low dword).
        SqlType::Money(v) => v.as_ref().map(normalize_money),
        SqlType::SmallMoney(v) => v.as_ref().map(normalize_small_money),

        // Decimal/numeric: sign byte followed by the little-endian magnitude.
        SqlType::Decimal(v) | SqlType::Numeric(v) => v.as_ref().map(normalize_decimal),

        // uniqueidentifier: 16 little-endian bytes.
        SqlType::Uuid(v) => v.map(|u| u.to_bytes_le().to_vec()),

        // Binary: raw bytes.
        SqlType::Binary(v, _) | SqlType::VarBinary(v, _) | SqlType::VarBinaryMax(v) => v.clone(),

        // Character types: the already-encoded bytes (UTF-16LE for `n` types,
        // the collation code page otherwise).
        SqlType::NVarchar(v, _)
        | SqlType::NVarcharMax(v)
        | SqlType::NChar(v, _)
        | SqlType::NText(v)
        | SqlType::Varchar(v, _)
        | SqlType::VarcharMax(v)
        | SqlType::Char(v, _)
        | SqlType::Text(v) => v.as_ref().map(|s| s.bytes.clone()),

        // Temporal types: the row-value layout (without the length prefix).
        SqlType::Date(v) => v.as_ref().map(normalize_date),
        SqlType::Time(v) => return v.as_ref().map(normalize_time).transpose(),
        SqlType::DateTime2(v) => return v.as_ref().map(normalize_datetime2).transpose(),
        SqlType::DateTimeOffset(v) => {
            return v.as_ref().map(normalize_datetime_offset).transpose();
        }
        SqlType::SmallDateTime(v) => v.as_ref().map(normalize_small_datetime),
        SqlType::DateTime(v) => v.as_ref().map(normalize_datetime),

        other => {
            return Err(Error::ColumnEncryptionError(format!(
                "Always Encrypted normalization is not implemented for parameter type {other:?}"
            )));
        }
    };
    Ok(bytes)
}

/// Normalizes and encrypts a plaintext bulk-copy cell value for an encrypted
/// destination column. This is the bulk-copy counterpart to
/// [`encrypt_parameter`]: bulk copy works with [`ColumnValues`] rather than the
/// RPC [`SqlType`] representation.
///
/// Returns `Ok(None)` when the value is SQL `NULL`.
pub(crate) fn encrypt_cell_value(
    value: &ColumnValues,
    plaintext_cek: &[u8],
    cipher_algorithm_id: u8,
    encryption_type: u8,
    normalization_rule_version: u8,
) -> TdsResult<Option<Vec<u8>>> {
    if cipher_algorithm_id != AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID {
        return Err(Error::ColumnEncryptionError(format!(
            "Unsupported cell cipher algorithm id {cipher_algorithm_id:#04x} (expected \
             {AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID:#04x})"
        )));
    }
    if normalization_rule_version != SUPPORTED_NORMALIZATION_VERSION {
        return Err(Error::ColumnEncryptionError(format!(
            "Unsupported Always Encrypted normalization rule version {normalization_rule_version} \
             (expected {SUPPORTED_NORMALIZATION_VERSION})"
        )));
    }

    let column_encryption_type = match encryption_type {
        1 => ColumnEncryptionType::Deterministic,
        2 => ColumnEncryptionType::Randomized,
        other => {
            return Err(Error::ColumnEncryptionError(format!(
                "Unsupported Always Encrypted encryption type {other} (expected 1 or 2)"
            )));
        }
    };

    let Some(normalized) = normalize_column_value(value)? else {
        return Ok(None);
    };

    let cipher = AeadAes256CbcHmacSha256::new(plaintext_cek)?;
    Ok(Some(cipher.encrypt(&normalized, column_encryption_type)?))
}

/// Normalizes a plaintext bulk-copy cell value into the canonical byte form that
/// SQL Server encrypts. This is the bulk-copy counterpart to [`normalize`]
/// (which operates on RPC [`SqlType`] values) and the inverse of the
/// denormalization performed by [`denormalize`].
///
/// Returns `Ok(None)` when the value is SQL `NULL`.
pub(crate) fn normalize_column_value(value: &ColumnValues) -> TdsResult<Option<Vec<u8>>> {
    let bytes = match value {
        ColumnValues::Null => None,

        // Integer family: an 8-byte little-endian bigint.
        ColumnValues::Bit(v) => Some(i64::from(*v).to_le_bytes().to_vec()),
        ColumnValues::TinyInt(v) => Some(i64::from(*v).to_le_bytes().to_vec()),
        ColumnValues::SmallInt(v) => Some(i64::from(*v).to_le_bytes().to_vec()),
        ColumnValues::Int(v) => Some(i64::from(*v).to_le_bytes().to_vec()),
        ColumnValues::BigInt(v) => Some(v.to_le_bytes().to_vec()),

        // Floating point: native IEEE form.
        ColumnValues::Real(v) => Some(v.to_le_bytes().to_vec()),
        ColumnValues::Float(v) => Some(v.to_le_bytes().to_vec()),

        // Money: the 8-byte money form (high dword then low dword).
        ColumnValues::Money(v) => Some(normalize_money(v)),
        ColumnValues::SmallMoney(v) => Some(normalize_small_money(v)),

        // Decimal/numeric: sign byte followed by the little-endian magnitude.
        ColumnValues::Decimal(v) | ColumnValues::Numeric(v) => Some(normalize_decimal(v)),

        // uniqueidentifier: 16 little-endian bytes.
        ColumnValues::Uuid(v) => Some(v.to_bytes_le().to_vec()),

        // Binary: raw bytes.
        ColumnValues::Bytes(v) => Some(v.clone()),

        // Character types: the already-encoded bytes (UTF-16LE for `n` types,
        // the collation code page otherwise).
        ColumnValues::String(s) => Some(s.bytes.clone()),

        // Temporal types: the row-value layout (without the length prefix).
        ColumnValues::Date(v) => Some(normalize_date(v)),
        ColumnValues::Time(v) => Some(normalize_time(v)?),
        ColumnValues::DateTime2(v) => Some(normalize_datetime2(v)?),
        ColumnValues::DateTimeOffset(v) => Some(normalize_datetime_offset(v)?),
        ColumnValues::SmallDateTime(v) => Some(normalize_small_datetime(v)),
        ColumnValues::DateTime(v) => Some(normalize_datetime(v)),

        other => {
            return Err(Error::ColumnEncryptionError(format!(
                "Always Encrypted normalization is not implemented for bulk-copy value {other:?}"
            )));
        }
    };
    Ok(bytes)
}

/// Normalizes a `money` value to the 8-byte form (high dword then low dword).
fn normalize_money(m: &SqlMoney) -> Vec<u8> {
    let mut out = Vec::with_capacity(8);
    out.extend_from_slice(&m.msb_part.to_le_bytes());
    out.extend_from_slice(&m.lsb_part.to_le_bytes());
    out
}

/// Normalizes a `smallmoney` value to the 8-byte money form. `smallmoney` is a
/// 4-byte scaled signed integer; the 8-byte money form is `[high dword][low
/// dword]`, so the value must be sign-extended — negative values fill the high
/// dword with `0xFFFFFFFF` — to match SQL Server and the other AE clients.
fn normalize_small_money(m: &SqlSmallMoney) -> Vec<u8> {
    let value = i64::from(m.int_val);
    let mut out = Vec::with_capacity(8);
    out.extend_from_slice(&((value >> 32) as i32).to_le_bytes());
    out.extend_from_slice(&(value as i32).to_le_bytes());
    out
}

/// Normalizes a `decimal`/`numeric` value: a sign byte (`1` = positive) followed
/// by a fixed 16-byte little-endian magnitude (four 32-bit words, zero-extended).
///
/// The fixed-width magnitude matches SQL Server's canonical form and the
/// reference drivers (.NET `SerializeDecimal`/`SerializeSqlDecimal`, msodbcsql),
/// so deterministically-encrypted decimal values are byte-compatible across
/// drivers. The value's magnitude is scaled to its own `scale`, which the RPC
/// parameter also declares on the wire, so both sides agree on the scale.
fn normalize_decimal(d: &DecimalParts) -> Vec<u8> {
    let mut out = Vec::with_capacity(DECIMAL_NORMALIZED_LEN);
    out.push(u8::from(d.is_positive));
    out.extend_from_slice(&d.magnitude().to_le_bytes());
    out
}

/// Returns the on-wire byte width of a `time(scale)` value.
fn time_byte_width(scale: u8) -> usize {
    match scale {
        0..=2 => 3,
        3..=4 => 4,
        _ => 5,
    }
}

/// Normalizes a `date` value to a 3-byte little-endian day count.
fn normalize_date(d: &SqlDate) -> Vec<u8> {
    d.get_days().to_le_bytes()[..3].to_vec()
}

/// Normalizes a `time(scale)` value to its 3/4/5-byte scaled form.
fn normalize_time(t: &SqlTime) -> TdsResult<Vec<u8>> {
    let scale = t.scale;
    if scale > 7 {
        return Err(Error::ColumnEncryptionError(format!(
            "Invalid time scale {scale} (expected 0..=7)"
        )));
    }
    let multiplier = 10u64.pow(u32::from(7 - scale));
    let scaled = t.time_nanoseconds / multiplier;
    Ok(scaled.to_le_bytes()[..time_byte_width(scale)].to_vec())
}

/// Normalizes a `datetime2(scale)` value: the time component followed by the
/// 3-byte date.
fn normalize_datetime2(dt: &SqlDateTime2) -> TdsResult<Vec<u8>> {
    let mut out = normalize_time(&dt.time)?;
    out.extend_from_slice(&dt.days.to_le_bytes()[..3]);
    Ok(out)
}

/// Normalizes a `datetimeoffset(scale)` value: the `datetime2` form followed by
/// a 2-byte signed UTC offset in minutes.
fn normalize_datetime_offset(dto: &SqlDateTimeOffset) -> TdsResult<Vec<u8>> {
    let mut out = normalize_datetime2(&dto.datetime2)?;
    out.extend_from_slice(&dto.offset.to_le_bytes());
    Ok(out)
}

/// Normalizes a `smalldatetime` value: 2-byte day count and 2-byte minute count.
fn normalize_small_datetime(sdt: &SqlSmallDateTime) -> Vec<u8> {
    let mut out = Vec::with_capacity(4);
    out.extend_from_slice(&sdt.days.to_le_bytes());
    out.extend_from_slice(&sdt.time.to_le_bytes());
    out
}

/// Normalizes a legacy `datetime` value: 4-byte signed day count and 4-byte
/// tick count.
fn normalize_datetime(dt: &SqlDateTime) -> Vec<u8> {
    let mut out = Vec::with_capacity(8);
    out.extend_from_slice(&dt.days.to_le_bytes());
    out.extend_from_slice(&dt.time.to_le_bytes());
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::datatypes::sqldatatypes::{FixedLengthTypes, TypeInfoVariant, VariableLengthTypes};
    use crate::security::encryption::ColumnEncryptionType;

    #[test]
    fn normalize_small_money_sign_extends() {
        // Positive: high dword is zero, low dword is the scaled integer.
        let pos = normalize_small_money(&SqlSmallMoney { int_val: 123_450 });
        assert_eq!(&pos[0..4], &0i32.to_le_bytes());
        assert_eq!(&pos[4..8], &123_450i32.to_le_bytes());

        // Negative: high dword is 0xFFFFFFFF (sign-extended), not zero. A zero
        // high dword would produce a bogus large-positive money whose
        // deterministic ciphertext would not match SQL Server / other AE clients.
        let neg = normalize_small_money(&SqlSmallMoney { int_val: -123_450 });
        assert_eq!(&neg[0..4], &(-1i32).to_le_bytes());
        assert_eq!(&neg[4..8], &(-123_450i32).to_le_bytes());

        // A negative `smallmoney` normalizes to the same 8 bytes as the
        // equivalent `money` value.
        let money = normalize_money(&SqlMoney {
            msb_part: -1,
            lsb_part: -123_450,
        });
        assert_eq!(neg, money);
    }

    /// A fixed 32-byte CEK used to round-trip through the cipher.
    const CEK: [u8; 32] = [
        0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
        0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
        0x48, 0x49,
    ];

    fn type_info(tds_type: TdsDataType, length: usize, variant: TypeInfoVariant) -> TypeInfo {
        TypeInfo {
            tds_type,
            length,
            type_info_variant: variant,
        }
    }

    fn crypto(base_data_type: TdsDataType, base_type_info: TypeInfo) -> CryptoMetadata {
        CryptoMetadata {
            cek_table_ordinal: 0,
            base_data_type,
            base_type_info,
            cipher_algorithm_id: AEAD_AES_256_CBC_HMAC_SHA256_ALGORITHM_ID,
            cipher_algorithm_name: None,
            encryption_type: 1,
            normalization_rule_version: 1,
        }
    }

    /// Encrypts `normalized` so it can be fed back through [`decrypt_cell`].
    fn encrypt(normalized: &[u8]) -> Vec<u8> {
        AeadAes256CbcHmacSha256::new(&CEK)
            .unwrap()
            .encrypt(normalized, ColumnEncryptionType::Randomized)
            .unwrap()
    }

    #[test]
    fn denormalize_int_family() {
        // int normalized to 8-byte bigint.
        let info = type_info(
            TdsDataType::IntN,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let value = denormalize(&1234i64.to_le_bytes(), TdsDataType::IntN, &info, 1).unwrap();
        assert_eq!(value, ColumnValues::Int(1234));

        // tinyint
        let info = type_info(
            TdsDataType::IntN,
            1,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let value = denormalize(&200i64.to_le_bytes(), TdsDataType::IntN, &info, 1).unwrap();
        assert_eq!(value, ColumnValues::TinyInt(200));

        // bigint via fixed Int8
        let info = type_info(
            TdsDataType::Int8,
            8,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int8),
        );
        let value = denormalize(&(-5i64).to_le_bytes(), TdsDataType::Int8, &info, 1).unwrap();
        assert_eq!(value, ColumnValues::BigInt(-5));
    }

    #[test]
    fn denormalize_bit() {
        let info = type_info(
            TdsDataType::BitN,
            1,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Bit),
        );
        assert_eq!(
            denormalize(&1i64.to_le_bytes(), TdsDataType::BitN, &info, 1).unwrap(),
            ColumnValues::Bit(true)
        );
        assert_eq!(
            denormalize(&0i64.to_le_bytes(), TdsDataType::BitN, &info, 1).unwrap(),
            ColumnValues::Bit(false)
        );
    }

    #[test]
    fn denormalize_float() {
        let info = type_info(
            TdsDataType::FltN,
            8,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Flt8),
        );
        let value = denormalize(&1.5f64.to_le_bytes(), TdsDataType::FltN, &info, 1).unwrap();
        assert_eq!(value, ColumnValues::Float(1.5));

        let info = type_info(
            TdsDataType::FltN,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Flt4),
        );
        let value = denormalize(&2.5f32.to_le_bytes(), TdsDataType::FltN, &info, 1).unwrap();
        assert_eq!(value, ColumnValues::Real(2.5));
    }

    #[test]
    fn denormalize_money_forms() {
        // money: high dword then low dword. Represent 1.2345 (123450 / 10000).
        let amount: i64 = 12345;
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&((amount >> 32) as i32).to_le_bytes());
        bytes.extend_from_slice(&((amount & 0xFFFF_FFFF) as i32).to_le_bytes());
        let info = type_info(
            TdsDataType::MoneyN,
            8,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Money),
        );
        let value = denormalize(&bytes, TdsDataType::MoneyN, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::Money(SqlMoney {
                msb_part: 0,
                lsb_part: 12345,
            })
        );

        // smallmoney: low dword at offset 4.
        let info = type_info(
            TdsDataType::MoneyN,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Money4),
        );
        let value = denormalize(&bytes, TdsDataType::MoneyN, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::SmallMoney(SqlSmallMoney { int_val: 12345 })
        );
    }

    #[test]
    fn denormalize_decimal() {
        // value 12345 with scale 2 => 123.45, precision 5.
        let info = type_info(
            TdsDataType::DecimalN,
            5,
            TypeInfoVariant::VarLenPrecisionScale(VariableLengthTypes::DecimalN, 17, 5, 2),
        );
        let mut bytes = vec![1u8]; // positive sign
        bytes.extend_from_slice(&12345i32.to_le_bytes());
        let value = denormalize(&bytes, TdsDataType::DecimalN, &info, 1).unwrap();
        match value {
            ColumnValues::Decimal(parts) => {
                assert!(parts.is_positive);
                assert_eq!(parts.scale, 2);
                assert_eq!(parts.precision, 5);
                assert_eq!(parts.magnitude(), 12345);
            }
            other => panic!("expected Decimal, got {other:?}"),
        }
    }

    #[test]
    fn denormalize_binary_and_guid() {
        let info = type_info(
            TdsDataType::BigVarBinary,
            8000,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let value = denormalize(&[1, 2, 3, 4], TdsDataType::BigVarBinary, &info, 1).unwrap();
        assert_eq!(value, ColumnValues::Bytes(vec![1, 2, 3, 4]));

        let raw = [
            0x10u8, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
            0x1e, 0x1f,
        ];
        let info = type_info(
            TdsDataType::Guid,
            16,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let value = denormalize(&raw, TdsDataType::Guid, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::Uuid(Uuid::from_slice_le(&raw).unwrap())
        );
    }

    #[test]
    fn denormalize_unicode_string() {
        let info = type_info(
            TdsDataType::NVarChar,
            65535,
            TypeInfoVariant::VarLenString(VariableLengthTypes::NVarChar, 20, None),
        );
        let utf16: Vec<u8> = "Hi".encode_utf16().flat_map(u16::to_le_bytes).collect();
        let value = denormalize(&utf16, TdsDataType::NVarChar, &info, 1).unwrap();
        match value {
            ColumnValues::String(s) => assert_eq!(s.to_utf8_string(), "Hi"),
            other => panic!("expected String, got {other:?}"),
        }
    }

    #[test]
    fn denormalize_rejects_unknown_normalization_version() {
        let info = type_info(
            TdsDataType::Int4,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let error = denormalize(&0i64.to_le_bytes(), TdsDataType::Int4, &info, 2).unwrap_err();
        assert!(matches!(error, Error::ColumnEncryptionError(_)));
    }

    #[test]
    fn decrypt_cell_round_trips_through_cipher() {
        let info = type_info(
            TdsDataType::IntN,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let crypto_metadata = crypto(TdsDataType::IntN, info);
        let blob = encrypt(&987654i64.to_le_bytes());

        let value = decrypt_cell(&crypto_metadata, &CEK, &blob).unwrap();
        assert_eq!(value, ColumnValues::Int(987654));
    }

    #[test]
    fn decrypt_cell_rejects_unknown_algorithm() {
        let info = type_info(
            TdsDataType::Int4,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let mut crypto_metadata = crypto(TdsDataType::Int4, info);
        crypto_metadata.cipher_algorithm_id = 0x00;

        let error = decrypt_cell(&crypto_metadata, &CEK, &[0u8; 64]).unwrap_err();
        assert!(matches!(error, Error::ColumnEncryptionError(_)));
    }

    /// Builds a temporal base type carrying the given scale.
    fn temporal_type_info(
        tds_type: TdsDataType,
        variant_type: VariableLengthTypes,
        scale: u8,
    ) -> TypeInfo {
        type_info(
            tds_type,
            0,
            TypeInfoVariant::VarLenScale(variant_type, scale),
        )
    }

    #[test]
    fn denormalize_date() {
        // 2024-01-01 is day 739_251 since 0001-01-01.
        let days: u32 = 739_251;
        let bytes = &days.to_le_bytes()[0..3];
        let info = type_info(
            TdsDataType::DateN,
            3,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let value = denormalize(bytes, TdsDataType::DateN, &info, 1).unwrap();
        match value {
            ColumnValues::Date(date) => assert_eq!(date.get_days(), days),
            other => panic!("expected Date, got {other:?}"),
        }
    }

    #[test]
    fn denormalize_time() {
        // 01:02:03.0000000 = 3723 seconds = 37_230_000_000 100ns units.
        // Scale 7 keeps the 100ns value directly (5-byte scaled value).
        let scaled: u64 = 37_230_000_000;
        let bytes = &scaled.to_le_bytes()[0..5];
        let info = temporal_type_info(TdsDataType::TimeN, VariableLengthTypes::TimeN, 7);
        let value = denormalize(bytes, TdsDataType::TimeN, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::Time(SqlTime {
                time_nanoseconds: 37_230_000_000,
                scale: 7,
            })
        );
    }

    #[test]
    fn denormalize_time_scaled() {
        // Scale 0 stores seconds in 3 bytes; denormalize multiplies by 10^7.
        let seconds: u32 = 3723;
        let bytes = &seconds.to_le_bytes()[0..3];
        let info = temporal_type_info(TdsDataType::TimeN, VariableLengthTypes::TimeN, 0);
        let value = denormalize(bytes, TdsDataType::TimeN, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::Time(SqlTime {
                time_nanoseconds: 37_230_000_000,
                scale: 0,
            })
        );
    }

    #[test]
    fn denormalize_datetime2() {
        let days: u32 = 739_251;
        let scaled: u64 = 37_230_000_000; // scale 7, 100ns units
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&scaled.to_le_bytes()[0..5]);
        bytes.extend_from_slice(&days.to_le_bytes()[0..3]);
        let info = temporal_type_info(TdsDataType::DateTime2N, VariableLengthTypes::DateTime2N, 7);
        let value = denormalize(&bytes, TdsDataType::DateTime2N, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::DateTime2(SqlDateTime2 {
                days,
                time: SqlTime {
                    time_nanoseconds: 37_230_000_000,
                    scale: 7,
                },
            })
        );
    }

    #[test]
    fn denormalize_datetime_offset() {
        let days: u32 = 739_251;
        let scaled: u64 = 37_230_000_000;
        let offset: i16 = -330; // UTC-05:30
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&scaled.to_le_bytes()[0..5]);
        bytes.extend_from_slice(&days.to_le_bytes()[0..3]);
        bytes.extend_from_slice(&offset.to_le_bytes());
        let info = temporal_type_info(
            TdsDataType::DateTimeOffsetN,
            VariableLengthTypes::DateTimeOffsetN,
            7,
        );
        let value = denormalize(&bytes, TdsDataType::DateTimeOffsetN, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::DateTimeOffset(SqlDateTimeOffset {
                datetime2: SqlDateTime2 {
                    days,
                    time: SqlTime {
                        time_nanoseconds: 37_230_000_000,
                        scale: 7,
                    },
                },
                offset,
            })
        );
    }

    #[test]
    fn denormalize_legacy_datetime() {
        // datetime: 4-byte signed days + 4-byte ticks.
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&45_000i32.to_le_bytes());
        bytes.extend_from_slice(&1_080_000u32.to_le_bytes());
        let info = type_info(
            TdsDataType::DateTimeN,
            8,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let value = denormalize(&bytes, TdsDataType::DateTimeN, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::DateTime(SqlDateTime {
                days: 45_000,
                time: 1_080_000,
            })
        );

        // smalldatetime: 2-byte days + 2-byte minutes.
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&45_000u16.to_le_bytes());
        bytes.extend_from_slice(&720u16.to_le_bytes());
        let info = type_info(
            TdsDataType::DateTimeN,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let value = denormalize(&bytes, TdsDataType::DateTimeN, &info, 1).unwrap();
        assert_eq!(
            value,
            ColumnValues::SmallDateTime(SqlSmallDateTime {
                days: 45_000,
                time: 720,
            })
        );
    }

    #[test]
    fn normalize_integer_is_8_byte_bigint() {
        assert_eq!(
            normalize(&SqlType::Int(Some(1234))).unwrap().unwrap(),
            1234i64.to_le_bytes().to_vec()
        );
        assert_eq!(
            normalize(&SqlType::TinyInt(Some(200))).unwrap().unwrap(),
            200i64.to_le_bytes().to_vec()
        );
        assert_eq!(
            normalize(&SqlType::Bit(Some(true))).unwrap().unwrap(),
            1i64.to_le_bytes().to_vec()
        );
    }

    #[test]
    fn normalize_null_returns_none() {
        assert!(normalize(&SqlType::Int(None)).unwrap().is_none());
        assert!(normalize(&SqlType::NVarcharMax(None)).unwrap().is_none());
    }

    #[test]
    fn normalize_string_keeps_encoded_bytes() {
        let s = SqlString::from_utf8_string("hi".to_string());
        let expected = s.bytes.clone();
        let normalized = normalize(&SqlType::NVarchar(Some(s), 10)).unwrap().unwrap();
        assert_eq!(normalized, expected);
    }

    #[test]
    fn normalize_money_layout() {
        let m = SqlMoney {
            msb_part: 1,
            lsb_part: 2,
        };
        let mut expected = 1i32.to_le_bytes().to_vec();
        expected.extend_from_slice(&2i32.to_le_bytes());
        assert_eq!(
            normalize(&SqlType::Money(Some(m))).unwrap().unwrap(),
            expected
        );
    }

    #[test]
    fn normalize_decimal_layout() {
        // The magnitude is emitted as a fixed 16-byte (four 32-bit words),
        // zero-extended form, so two significant words are followed by two zero
        // words. Total length is 1 sign byte + 16 magnitude bytes = 17.
        let d = DecimalParts::new(false, 10, 2, 1 | (2 << 32));
        let mut expected = vec![0u8];
        expected.extend_from_slice(&1i32.to_le_bytes());
        expected.extend_from_slice(&2i32.to_le_bytes());
        expected.extend_from_slice(&0i32.to_le_bytes());
        expected.extend_from_slice(&0i32.to_le_bytes());
        let actual = normalize(&SqlType::Decimal(Some(d))).unwrap().unwrap();
        assert_eq!(actual.len(), 17);
        assert_eq!(actual, expected);
    }

    #[test]
    fn normalize_decimal_pads_small_value_to_fixed_width() {
        // A value that needs only one 32-bit word must still be emitted as the
        // canonical 17-byte form (sign + four words) so deterministic ciphertext
        // matches .NET/msodbcsql, which always zero-extend to 16 magnitude bytes.
        let d = DecimalParts::new(true, 5, 2, 12345);
        let mut expected = vec![1u8];
        expected.extend_from_slice(&12345i32.to_le_bytes());
        expected.extend_from_slice(&0i32.to_le_bytes());
        expected.extend_from_slice(&0i32.to_le_bytes());
        expected.extend_from_slice(&0i32.to_le_bytes());
        assert_eq!(
            normalize(&SqlType::Numeric(Some(d))).unwrap().unwrap(),
            expected
        );
    }

    #[test]
    fn read_decimal_is_length_tolerant() {
        // A 15-byte magnitude (JDBC bulk-copy writes this form) must be accepted,
        // zero-padding the short trailing group into a full 32-bit word rather
        // than being rejected. Value 12345 with scale 2 => 123.45.
        let info = type_info(
            TdsDataType::DecimalN,
            5,
            TypeInfoVariant::VarLenPrecisionScale(VariableLengthTypes::DecimalN, 17, 5, 2),
        );
        let mut bytes = vec![1u8]; // positive sign
        bytes.extend_from_slice(&12345i32.to_le_bytes()); // first 32-bit word
        bytes.extend_from_slice(&[0u8; 11]); // 11 more bytes => 15-byte magnitude
        let value = denormalize(&bytes, TdsDataType::DecimalN, &info, 1).unwrap();
        match value {
            ColumnValues::Decimal(parts) => {
                assert!(parts.is_positive);
                assert_eq!(parts.scale, 2);
                assert_eq!(parts.to_string(), "123.45");
            }
            other => panic!("expected Decimal, got {other:?}"),
        }
    }

    #[test]
    fn read_decimal_rejects_oversized_magnitude() {
        // A valid decimal magnitude is at most 16 bytes (128 bits). A longer
        // magnitude must be rejected rather than silently dropping the bytes
        // that do not fit.
        let info = type_info(
            TdsDataType::DecimalN,
            5,
            TypeInfoVariant::VarLenPrecisionScale(VariableLengthTypes::DecimalN, 17, 5, 2),
        );
        let mut bytes = vec![1u8]; // positive sign
        bytes.extend_from_slice(&[0u8; 17]); // 17-byte magnitude => too long
        let error = denormalize(&bytes, TdsDataType::DecimalN, &info, 1).unwrap_err();
        assert!(matches!(error, Error::ColumnEncryptionError(_)));
    }

    #[test]
    fn read_decimal_rejects_invalid_precision_and_scale() {
        for (precision, scale) in [(0, 0), (39, 0), (18, 19)] {
            let info = type_info(
                TdsDataType::DecimalN,
                5,
                TypeInfoVariant::VarLenPrecisionScale(
                    VariableLengthTypes::DecimalN,
                    17,
                    precision,
                    scale,
                ),
            );
            let error = denormalize(&[1], TdsDataType::DecimalN, &info, 1).unwrap_err();
            assert!(matches!(error, Error::ColumnEncryptionError(_)));
        }
    }

    #[test]
    fn normalize_then_read_decimal_roundtrips() {
        // The fixed-width write form must read back to the same value through the
        // (now length-tolerant) reader.
        let d = DecimalParts::new(false, 12, 3, 987654);
        let normalized = normalize(&SqlType::Decimal(Some(d))).unwrap().unwrap();
        let info = type_info(
            TdsDataType::DecimalN,
            12,
            TypeInfoVariant::VarLenPrecisionScale(VariableLengthTypes::DecimalN, 17, 12, 3),
        );
        let value = denormalize(&normalized, TdsDataType::DecimalN, &info, 1).unwrap();
        match value {
            ColumnValues::Decimal(parts) => {
                assert!(!parts.is_positive);
                assert_eq!(parts.to_string(), "-987.654");
            }
            other => panic!("expected Decimal, got {other:?}"),
        }
    }

    #[test]
    fn encrypt_parameter_null_returns_none() {
        assert!(
            encrypt_parameter(&SqlType::Int(None), &CEK, 2, 1, 1)
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn encrypt_parameter_rejects_unknown_algorithm() {
        let error = encrypt_parameter(&SqlType::Int(Some(1)), &CEK, 0, 1, 1).unwrap_err();
        assert!(matches!(error, Error::ColumnEncryptionError(_)));
    }

    #[test]
    fn encrypt_parameter_rejects_unknown_encryption_type() {
        let error = encrypt_parameter(&SqlType::Int(Some(1)), &CEK, 2, 9, 1).unwrap_err();
        assert!(matches!(error, Error::ColumnEncryptionError(_)));
    }

    #[test]
    fn encrypt_parameter_roundtrip_int() {
        let cipher = encrypt_parameter(&SqlType::Int(Some(987654)), &CEK, 2, 2, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::IntN,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let mut meta = crypto(TdsDataType::IntN, info);
        meta.encryption_type = 2;
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Int(987654)
        );
    }

    #[test]
    fn encrypt_parameter_roundtrip_binary() {
        let cipher = encrypt_parameter(&SqlType::VarBinary(Some(vec![9, 8, 7]), 10), &CEK, 2, 2, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::BigVarBinary,
            10,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let mut meta = crypto(TdsDataType::BigVarBinary, info);
        meta.encryption_type = 2;
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Bytes(vec![9, 8, 7])
        );
    }

    #[test]
    fn encrypt_parameter_roundtrip_guid() {
        let value = uuid::Uuid::from_u128(0x0123_4567_89ab_cdef_0123_4567_89ab_cdef);
        let cipher = encrypt_parameter(&SqlType::Uuid(Some(value)), &CEK, 2, 1, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::Guid,
            16,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let meta = crypto(TdsDataType::Guid, info);
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Uuid(value)
        );
    }

    #[test]
    fn encrypt_parameter_roundtrip_date() {
        let date = SqlDate::unchecked_create(739_251);
        let cipher = encrypt_parameter(&SqlType::Date(Some(date)), &CEK, 2, 2, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::DateN,
            3,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let mut meta = crypto(TdsDataType::DateN, info);
        meta.encryption_type = 2;
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Date(SqlDate::unchecked_create(739_251))
        );
    }

    #[test]
    fn normalize_column_value_null_returns_none() {
        assert!(
            normalize_column_value(&ColumnValues::Null)
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn normalize_column_value_integer_is_8_byte_bigint() {
        assert_eq!(
            normalize_column_value(&ColumnValues::Int(1234)).unwrap(),
            Some(1234i64.to_le_bytes().to_vec())
        );
        assert_eq!(
            normalize_column_value(&ColumnValues::TinyInt(200)).unwrap(),
            Some(200i64.to_le_bytes().to_vec())
        );
        assert_eq!(
            normalize_column_value(&ColumnValues::Bit(true)).unwrap(),
            Some(1i64.to_le_bytes().to_vec())
        );
    }

    #[test]
    fn encrypt_cell_value_null_returns_none() {
        assert!(
            encrypt_cell_value(&ColumnValues::Null, &CEK, 2, 1, 1)
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn encrypt_cell_value_rejects_unknown_algorithm() {
        let error = encrypt_cell_value(&ColumnValues::Int(1), &CEK, 0, 1, 1).unwrap_err();
        assert!(matches!(error, Error::ColumnEncryptionError(_)));
    }

    #[test]
    fn encrypt_cell_value_rejects_unknown_encryption_type() {
        let error = encrypt_cell_value(&ColumnValues::Int(1), &CEK, 2, 9, 1).unwrap_err();
        assert!(matches!(error, Error::ColumnEncryptionError(_)));
    }

    #[test]
    fn encrypt_cell_value_roundtrip_int() {
        let cipher = encrypt_cell_value(&ColumnValues::Int(987654), &CEK, 2, 2, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::IntN,
            4,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let mut meta = crypto(TdsDataType::IntN, info);
        meta.encryption_type = 2;
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Int(987654)
        );
    }

    #[test]
    fn encrypt_cell_value_roundtrip_binary() {
        let cipher = encrypt_cell_value(&ColumnValues::Bytes(vec![9, 8, 7]), &CEK, 2, 2, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::BigVarBinary,
            10,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let mut meta = crypto(TdsDataType::BigVarBinary, info);
        meta.encryption_type = 2;
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Bytes(vec![9, 8, 7])
        );
    }

    #[test]
    fn encrypt_cell_value_roundtrip_guid() {
        let value = uuid::Uuid::from_u128(0x0123_4567_89ab_cdef_0123_4567_89ab_cdef);
        let cipher = encrypt_cell_value(&ColumnValues::Uuid(value), &CEK, 2, 1, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::Guid,
            16,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let meta = crypto(TdsDataType::Guid, info);
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Uuid(value)
        );
    }

    #[test]
    fn encrypt_cell_value_roundtrip_date() {
        let date = SqlDate::unchecked_create(739_251);
        let cipher = encrypt_cell_value(&ColumnValues::Date(date), &CEK, 2, 2, 1)
            .unwrap()
            .unwrap();
        let info = type_info(
            TdsDataType::DateN,
            3,
            TypeInfoVariant::FixedLen(FixedLengthTypes::Int4),
        );
        let mut meta = crypto(TdsDataType::DateN, info);
        meta.encryption_type = 2;
        assert_eq!(
            decrypt_cell(&meta, &CEK, &cipher).unwrap(),
            ColumnValues::Date(SqlDate::unchecked_create(739_251))
        );
    }
}