azure_data_cosmos_driver 0.6.0

Core implementation layer for Azure Cosmos DB - provides transport, routing, and protocol handling for cross-language SDK reuse
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

//! Backend query plan models.
//!
//! These types model the response from the Cosmos DB Gateway when issuing a
//! query plan request (`OperationType::QueryPlan`). The planner uses them to
//! determine partition targeting, detect unsupported query features, and build
//! the dataflow pipeline.

use std::collections::HashMap;

use serde::Deserialize;
use serde::Serialize;

use crate::error::{CosmosError, CosmosStatus, Result};
use crate::models::{EffectivePartitionKey, PartitionKeyDefinition, PartitionKeyValue};

/// Deserializes a boolean from either a JSON boolean (`true`/`false`) or
/// an integer (`0`/`1`). The native QueryPlanInterop library serializes
/// booleans as integers, while the Gateway uses standard JSON booleans.
fn bool_from_int_or_bool<'de, D>(deserializer: D) -> std::result::Result<bool, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de;

    struct BoolVisitor;

    impl<'de> de::Visitor<'de> for BoolVisitor {
        type Value = bool;

        fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            formatter.write_str("a boolean or an integer (0/1)")
        }

        fn visit_bool<E: de::Error>(self, v: bool) -> std::result::Result<bool, E> {
            Ok(v)
        }

        fn visit_i64<E: de::Error>(self, v: i64) -> std::result::Result<bool, E> {
            Ok(v != 0)
        }

        fn visit_u64<E: de::Error>(self, v: u64) -> std::result::Result<bool, E> {
            Ok(v != 0)
        }
    }

    deserializer.deserialize_any(BoolVisitor)
}

/// Deserializes a value as a String. If the JSON value is already a string,
/// it is used directly. Otherwise (array, object, number, etc.), it is
/// serialized back to a JSON string. This handles the difference between
/// Gateway responses (hex EPK strings) and native DLL responses (structured
/// partition key values like arrays).
fn string_or_json<'de, D>(deserializer: D) -> std::result::Result<String, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = serde_json::Value::deserialize(deserializer)?;
    match value {
        serde_json::Value::String(s) => Ok(s),
        other => Ok(other.to_string()),
    }
}

/// Raw query plan as deserialized off the wire.
///
/// The standard Gateway returns each `queryRanges[].min` / `max` as a hex EPK
/// string (`""`, `"FF"`, or a specific hash hex). The Gateway V2 thin-client
/// proxy returns the raw `PartitionKeyInternal` JSON shape — an array of
/// component values or the special sentinels `[]` (MIN) and
/// `[{"type":"Infinity"}]` (MAX). To accept both shapes we keep the bounds
/// as opaque `serde_json::Value` here and resolve them to canonical EPK hex
/// strings via [`RawQueryPlan::resolve`].
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
#[serde(rename_all = "camelCase")]
pub(crate) struct RawQueryPlan {
    #[serde(default)]
    pub partitioned_query_execution_info_version: usize,

    #[serde(default)]
    pub query_info: Option<QueryInfo>,

    #[serde(default)]
    pub query_ranges: Vec<RawQueryRange>,

    pub hybrid_search_query_info: Option<HybridSearchQueryInfo>,
}

/// A raw query range as deserialized off the wire, with `min` / `max` still
/// in proxy or Gateway form. See [`RawQueryPlan`] for the resolution path.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct RawQueryRange {
    pub min: serde_json::Value,
    pub max: serde_json::Value,
    #[serde(deserialize_with = "bool_from_int_or_bool")]
    pub is_min_inclusive: bool,
    #[serde(deserialize_with = "bool_from_int_or_bool")]
    pub is_max_inclusive: bool,
}

impl RawQueryPlan {
    /// Resolves the wire-format ranges into a planner-ready [`QueryPlan`].
    ///
    /// Walks each `queryRanges[].min` / `max`. Strings are accepted as-is
    /// (with the literal `"Infinity"` collapsed to the `"FF"` sentinel).
    /// PartitionKeyInternal JSON arrays are decoded against `pk_definition`:
    /// the empty array becomes the MIN sentinel; `[{"type":"Infinity"}]`
    /// becomes MAX; concrete components are hashed via
    /// [`EffectivePartitionKey::compute`].
    ///
    /// After conversion the ranges are sorted by `min` so the planner can
    /// match them against the routing map.
    pub(crate) fn resolve(self, pk_definition: &PartitionKeyDefinition) -> Result<QueryPlan> {
        let mut query_ranges = Vec::with_capacity(self.query_ranges.len());
        for raw in self.query_ranges {
            let min = resolve_epk_bound(&raw.min, pk_definition)?;
            let max = resolve_epk_bound(&raw.max, pk_definition)?;
            query_ranges.push(QueryRange {
                min,
                max,
                is_min_inclusive: raw.is_min_inclusive,
                is_max_inclusive: raw.is_max_inclusive,
            });
        }
        query_ranges.sort_by(|a, b| a.min.cmp(&b.min));
        Ok(QueryPlan {
            partitioned_query_execution_info_version: self.partitioned_query_execution_info_version,
            query_info: self.query_info,
            query_ranges,
            hybrid_search_query_info: self.hybrid_search_query_info,
        })
    }
}

/// Converts a single `queryRanges[].min|max` JSON value into the canonical
/// EPK hex string the planner expects.
fn resolve_epk_bound(
    value: &serde_json::Value,
    pk_definition: &PartitionKeyDefinition,
) -> Result<String> {
    use serde_json::Value;
    match value {
        Value::String(s) if s == "Infinity" => Ok("FF".to_owned()),
        Value::String(s) => Ok(s.clone()),
        Value::Array(items) => {
            // Empty PartitionKeyInternal array = MIN sentinel.
            if items.is_empty() {
                return Ok(String::new());
            }
            // Single `{"type":"Infinity"}` element = MAX sentinel.
            if items.len() == 1 {
                if let Value::Object(obj) = &items[0] {
                    if matches!(obj.get("type"), Some(Value::String(t)) if t == "Infinity") {
                        return Ok("FF".to_owned());
                    }
                }
            }
            let pk_values: Vec<PartitionKeyValue> = items
                .iter()
                .map(pki_component_to_pk_value)
                .collect::<Result<_>>()?;
            let epk = EffectivePartitionKey::compute(
                &pk_values,
                pk_definition.kind(),
                pk_definition.version(),
            );
            Ok(epk.to_hex())
        }
        other => Err(CosmosError::builder()
            .with_status(CosmosStatus::SERIALIZATION_RESPONSE_BODY_INVALID)
            .with_message(format!(
                "queryRanges bound has unsupported shape (expected string or PartitionKeyInternal array, got {})",
                json_type_name(other)
            ))
            .build()),
    }
}

/// Converts one component of a PartitionKeyInternal array (proxy form) into
/// a [`PartitionKeyValue`] suitable for [`EffectivePartitionKey::compute`].
fn pki_component_to_pk_value(value: &serde_json::Value) -> Result<PartitionKeyValue> {
    use serde_json::Value;
    match value {
        Value::Null => Ok(PartitionKeyValue::NULL),
        Value::Bool(b) => Ok(PartitionKeyValue::from(*b)),
        Value::Number(n) => {
            let f = n.as_f64().ok_or_else(|| {
                CosmosError::builder()
                    .with_status(CosmosStatus::SERIALIZATION_RESPONSE_BODY_INVALID)
                    .with_message(format!(
                        "queryRanges component number {n} cannot be represented as f64"
                    ))
                    .build()
            })?;
            Ok(PartitionKeyValue::from(f))
        }
        Value::String(s) => Ok(PartitionKeyValue::from(s.clone())),
        Value::Object(obj) => {
            // PartitionKeyInternal serializes its special sentinels as
            // `{"type":"Infinity"}` and `{"type":"Undefined"}`. Map the
            // former to the EPK MAX sentinel and the latter to the SDK's
            // UNDEFINED value.
            match obj.get("type").and_then(|t| t.as_str()) {
                Some("Infinity") => Ok(PartitionKeyValue::INFINITY),
                Some("Undefined") => Ok(PartitionKeyValue::UNDEFINED),
                _ => Err(CosmosError::builder()
                    .with_status(CosmosStatus::SERIALIZATION_RESPONSE_BODY_INVALID)
                    .with_message(format!(
                        "queryRanges component object is not a recognized PartitionKeyInternal sentinel: {value}"
                    ))
                    .build()),
            }
        }
        Value::Array(_) => Err(CosmosError::builder()
            .with_status(CosmosStatus::SERIALIZATION_RESPONSE_BODY_INVALID)
            .with_message("queryRanges component cannot be a nested array")
            .build()),
    }
}

fn json_type_name(v: &serde_json::Value) -> &'static str {
    match v {
        serde_json::Value::Null => "null",
        serde_json::Value::Bool(_) => "bool",
        serde_json::Value::Number(_) => "number",
        serde_json::Value::String(_) => "string",
        serde_json::Value::Array(_) => "array",
        serde_json::Value::Object(_) => "object",
    }
}

/// The resolved query plan the planner consumes.
///
/// Built from a [`RawQueryPlan`] via [`RawQueryPlan::resolve`] on the Gateway
/// path. The native QueryPlanInterop path deserializes JSON directly into
/// this type (it does not need the EPK-hashing step the Gateway path needs,
/// because the native DLL emits ranges in a form `string_or_json` can flatten
/// in place).
#[derive(Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(default)]
#[serde(rename_all = "camelCase")]
pub(crate) struct QueryPlan {
    /// The version of the query plan format.
    pub partitioned_query_execution_info_version: usize,

    /// Detailed query information (ordering, aggregates, rewrites, etc.).
    pub query_info: Option<QueryInfo>,

    /// The EPK ranges that the query references.
    ///
    /// Used by the planner to limit which physical partitions get queried.
    pub query_ranges: Vec<QueryRange>,

    /// Information about hybrid search queries, if applicable.
    pub hybrid_search_query_info: Option<HybridSearchQueryInfo>,
}

/// Information about a hybrid search query.
#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)] // Wire-format fields; hybrid search isn't fully wired yet.
pub(crate) struct HybridSearchQueryInfo {
    /// The query used for global statistics gathering.
    pub global_statistics_query: String,

    /// Individual component queries that make up the hybrid search.
    pub component_query_infos: Vec<QueryInfo>,

    /// Weights assigned to each component query.
    #[serde(default)]
    pub component_weights: Vec<f64>,

    /// Number of results to skip.
    pub skip: Option<u64>,

    /// Number of results to take (always present for hybrid search).
    pub take: Option<u64>,

    /// Whether global statistics are required.
    #[serde(deserialize_with = "bool_from_int_or_bool")]
    pub requires_global_statistics: bool,
}

/// The kind of DISTINCT tracking required by the query.
#[derive(Debug, Clone, Deserialize, Default, PartialEq, Eq, Serialize)]
pub(crate) enum DistinctType {
    /// No deduplication required.
    #[default]
    None,

    /// Order-preserving deduplication.
    Ordered,

    /// Order-independent deduplication.
    Unordered,
}

/// Detailed query plan information.
#[derive(Debug, Deserialize, Default, Serialize, PartialEq)]
#[serde(default)]
#[serde(rename_all = "camelCase")]
pub(crate) struct QueryInfo {
    /// The kind of DISTINCT clause, if any.
    pub distinct_type: DistinctType,

    /// `TOP` clause limit.
    pub top: Option<u64>,

    /// `OFFSET` clause value.
    pub offset: Option<u64>,

    /// `LIMIT` clause value (from `OFFSET`/`LIMIT`).
    pub limit: Option<u64>,

    /// Sort orders for `ORDER BY` expressions.
    pub order_by: Vec<SortOrder>,

    /// Expressions used by `ORDER BY` clauses.
    pub order_by_expressions: Vec<String>,

    /// Expressions used by `GROUP BY` clauses.
    pub group_by_expressions: Vec<String>,

    /// Aliases used by `GROUP BY` clauses.
    pub group_by_aliases: Vec<String>,

    /// Aggregates used in the `SELECT` portion of a `GROUP BY` query.
    pub aggregates: Vec<String>,

    /// Mapping from GROUP BY aliases to aggregate types.
    /// Values may be null or empty strings in the native DLL output.
    #[serde(default)]
    pub group_by_alias_to_aggregate_type: HashMap<String, serde_json::Value>,

    /// Rewritten form of the query for single-partition sub-queries.
    ///
    /// When non-empty, this should be used instead of the original query text
    /// for individual partition requests. The native DLL may return null.
    #[serde(default)]
    pub rewritten_query: Option<String>,

    /// Whether the query contains a `SELECT VALUE` clause.
    #[serde(deserialize_with = "bool_from_int_or_bool")]
    pub has_select_value: bool,

    /// Whether the query contains a non-streaming `ORDER BY`.
    #[serde(deserialize_with = "bool_from_int_or_bool")]
    pub has_non_streaming_order_by: bool,
}

/// Sort order for an `ORDER BY` expression.
#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq, Serialize)]
pub(crate) enum SortOrder {
    Ascending,
    Descending,
}

/// An EPK range covered by the query.
///
/// On the Gateway path, ranges arrive in canonical hex EPK form (`""`, `"FF"`,
/// or a hash hex). On the native QueryPlanInterop path and the Gateway V2
/// thin-client proxy path, ranges arrive as `PartitionKeyInternal` JSON values
/// (arrays); `string_or_json` flattens those to the JSON text. The Gateway
/// path additionally runs each `RawQueryRange` through
/// [`RawQueryPlan::resolve`] to hash structured PK values into proper EPK hex.
#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)] // Inclusivity flags are wire-format; planner treats ranges uniformly.
pub(crate) struct QueryRange {
    /// The minimum EPK value (hex string, `""` for MIN, `"FF"` for MAX).
    #[serde(deserialize_with = "string_or_json")]
    pub min: String,

    /// The maximum EPK value (hex string, `""` for MIN, `"FF"` for MAX).
    #[serde(deserialize_with = "string_or_json")]
    pub max: String,

    /// Whether the minimum value is inclusive.
    #[serde(deserialize_with = "bool_from_int_or_bool")]
    pub is_min_inclusive: bool,

    /// Whether the maximum value is inclusive.
    #[serde(deserialize_with = "bool_from_int_or_bool")]
    pub is_max_inclusive: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::borrow::Cow;

    /// Single-path Hash/V2 partition key definition used by the parser tests
    /// that don't care about concrete-value EPK computation.
    fn default_pk_def() -> PartitionKeyDefinition {
        PartitionKeyDefinition::new(vec![Cow::Borrowed("/pk")])
    }

    fn parse(json: &str) -> QueryPlan {
        let raw: RawQueryPlan = serde_json::from_str(json).expect("valid raw JSON");
        raw.resolve(&default_pk_def()).expect("resolution succeeds")
    }

    #[test]
    fn deserializes_minimal_query_plan() {
        let json = r#"{
            "partitionedQueryExecutionInfoVersion": 1,
            "queryRanges": [
                {
                    "min": "",
                    "max": "FF",
                    "isMinInclusive": true,
                    "isMaxInclusive": false
                }
            ]
        }"#;
        let plan = parse(json);
        assert_eq!(plan.partitioned_query_execution_info_version, 1);
        assert!(plan.query_info.is_none());
        assert!(plan.hybrid_search_query_info.is_none());
        assert_eq!(plan.query_ranges.len(), 1);
        assert_eq!(plan.query_ranges[0].min, "");
        assert_eq!(plan.query_ranges[0].max, "FF");
        assert!(plan.query_ranges[0].is_min_inclusive);
        assert!(!plan.query_ranges[0].is_max_inclusive);
    }

    #[test]
    fn deserializes_query_plan_with_order_by() {
        let json = r#"{
            "partitionedQueryExecutionInfoVersion": 2,
            "queryInfo": {
                "orderBy": ["Ascending", "Descending"],
                "orderByExpressions": ["c.name", "c.age"],
                "rewrittenQuery": "SELECT c.name, c.age FROM c ORDER BY c.name ASC, c.age DESC"
            },
            "queryRanges": []
        }"#;
        let plan = parse(json);
        let info = plan.query_info.unwrap();
        assert_eq!(
            info.order_by,
            vec![SortOrder::Ascending, SortOrder::Descending]
        );
        assert_eq!(info.order_by_expressions, vec!["c.name", "c.age"]);
    }

    #[test]
    fn deserializes_query_plan_with_top_and_aggregates() {
        let json = r#"{
            "partitionedQueryExecutionInfoVersion": 1,
            "queryInfo": {
                "top": 10,
                "aggregates": ["Count"],
                "distinctType": "Ordered"
            },
            "queryRanges": []
        }"#;
        let plan = parse(json);
        let info = plan.query_info.unwrap();
        assert_eq!(info.top, Some(10));
        assert_eq!(info.aggregates, vec!["Count"]);
        assert_eq!(info.distinct_type, DistinctType::Ordered);
    }

    #[test]
    fn deserializes_query_plan_with_hybrid_search() {
        let json = r#"{
            "partitionedQueryExecutionInfoVersion": 1,
            "queryRanges": [],
            "hybridSearchQueryInfo": {
                "globalStatisticsQuery": "SELECT COUNT(1) FROM c",
                "componentQueryInfos": [],
                "componentWeights": [0.5, 0.5],
                "skip": null,
                "take": 10,
                "requiresGlobalStatistics": true
            }
        }"#;
        let plan = parse(json);
        let hybrid = plan.hybrid_search_query_info.unwrap();
        assert_eq!(hybrid.global_statistics_query, "SELECT COUNT(1) FROM c");
        assert_eq!(hybrid.component_weights, vec![0.5, 0.5]);
        assert_eq!(hybrid.take, Some(10));
        assert!(hybrid.requires_global_statistics);
    }

    #[test]
    fn deserializes_query_plan_with_offset_limit() {
        let json = r#"{
            "partitionedQueryExecutionInfoVersion": 1,
            "queryInfo": {
                "offset": 5,
                "limit": 20
            },
            "queryRanges": []
        }"#;
        let plan = parse(json);
        let info = plan.query_info.unwrap();
        assert_eq!(info.offset, Some(5));
        assert_eq!(info.limit, Some(20));
    }

    #[test]
    fn deserializes_multiple_query_ranges() {
        let json = r#"{
            "partitionedQueryExecutionInfoVersion": 1,
            "queryRanges": [
                { "min": "", "max": "40", "isMinInclusive": true, "isMaxInclusive": false },
                { "min": "80", "max": "FF", "isMinInclusive": true, "isMaxInclusive": false }
            ]
        }"#;
        let plan = parse(json);
        assert_eq!(plan.query_ranges.len(), 2);
        assert_eq!(plan.query_ranges[0].max, "40");
        assert_eq!(plan.query_ranges[1].min, "80");
    }

    /// The Gateway V2 thin-client proxy returns the QueryPlan response with
    /// `hasSelectValue` / `hasNonStreamingOrderBy` as integers (0/1) instead
    /// of booleans, with `queryRanges[].min` as the empty `PartitionKeyInternal`
    /// array (`[]`), and with `queryRanges[].max` as the literal string
    /// `"Infinity"`. Pin that we resolve this shape into the same canonical
    /// EPK form (`""` / `"FF"`) the planner already handles.
    #[test]
    fn deserializes_thin_client_proxy_response_shape() {
        let json = r#"{
            "queryInfo": {
                "distinctType": "None",
                "groupByExpressions": [],
                "groupByAliases": [],
                "orderBy": [],
                "orderByExpressions": [],
                "aggregates": [],
                "hasSelectValue": 0,
                "rewrittenQuery": "",
                "groupByAliasToAggregateType": {},
                "hasNonStreamingOrderBy": 0
            },
            "queryRanges": [
                {
                    "min": [],
                    "max": "Infinity",
                    "isMinInclusive": true,
                    "isMaxInclusive": false
                }
            ]
        }"#;
        let plan = parse(json);
        let info = plan.query_info.expect("queryInfo present");
        assert!(!info.has_select_value);
        assert!(!info.has_non_streaming_order_by);
        assert_eq!(plan.query_ranges.len(), 1);
        assert_eq!(plan.query_ranges[0].min, "");
        assert_eq!(plan.query_ranges[0].max, "FF");
        assert!(plan.query_ranges[0].is_min_inclusive);
        assert!(!plan.query_ranges[0].is_max_inclusive);
    }

    #[test]
    fn deserializes_thin_client_infinity_sentinel_in_array() {
        let json = r#"{
            "queryRanges": [
                {
                    "min": [],
                    "max": [{"type":"Infinity"}],
                    "isMinInclusive": 1,
                    "isMaxInclusive": 0
                }
            ]
        }"#;
        let plan = parse(json);
        assert_eq!(plan.query_ranges[0].min, "");
        assert_eq!(plan.query_ranges[0].max, "FF");
        assert!(plan.query_ranges[0].is_min_inclusive);
        assert!(!plan.query_ranges[0].is_max_inclusive);
    }

    /// Proxy returns a concrete partition-key value in the PKI array; the
    /// resolver must hash it to the same EPK the SDK would compute client-side
    /// for that value.
    #[test]
    fn resolves_concrete_single_path_pk_value() {
        let json = r#"{
            "queryRanges": [
                {
                    "min": ["myKey"],
                    "max": ["myKey"],
                    "isMinInclusive": true,
                    "isMaxInclusive": true
                }
            ]
        }"#;
        let raw: RawQueryPlan = serde_json::from_str(json).unwrap();
        let pk_def = PartitionKeyDefinition::new(vec![Cow::Borrowed("/pk")]);
        let plan = raw.resolve(&pk_def).expect("resolve succeeds");

        let expected = EffectivePartitionKey::compute(
            &[PartitionKeyValue::from("myKey")],
            pk_def.kind(),
            pk_def.version(),
        );
        assert_eq!(plan.query_ranges[0].min, expected.to_hex());
        assert_eq!(plan.query_ranges[0].max, expected.to_hex());
        assert!(plan.query_ranges[0].is_min_inclusive);
        assert!(plan.query_ranges[0].is_max_inclusive);
        // Non-empty EPK string (the actual hash hex).
        assert!(!plan.query_ranges[0].min.is_empty());
        assert!(!plan.query_ranges[0].min.starts_with("FF"));
    }

    /// Multi-path (HPK) container: a prefix PKI array resolves via MultiHash
    /// V2 — each component hashed and concatenated.
    #[test]
    fn resolves_concrete_multi_hash_prefix() {
        let json = r#"{
            "queryRanges": [
                {
                    "min": ["tenantA"],
                    "max": ["tenantA"],
                    "isMinInclusive": true,
                    "isMaxInclusive": true
                }
            ]
        }"#;
        let raw: RawQueryPlan = serde_json::from_str(json).unwrap();
        let pk_def =
            PartitionKeyDefinition::new(vec![Cow::Borrowed("/tenantId"), Cow::Borrowed("/userId")]);
        let plan = raw.resolve(&pk_def).expect("resolve succeeds");

        let expected = EffectivePartitionKey::compute(
            &[PartitionKeyValue::from("tenantA")],
            pk_def.kind(),
            pk_def.version(),
        );
        assert_eq!(plan.query_ranges[0].min, expected.to_hex());
        assert_eq!(plan.query_ranges[0].max, expected.to_hex());
    }

    /// The resolver sorts ranges by `min` so the planner sees a stable order
    /// regardless of how the proxy emits them.
    #[test]
    fn resolves_sorts_ranges_by_min() {
        let json = r#"{
            "queryRanges": [
                { "min": "80", "max": "FF", "isMinInclusive": true, "isMaxInclusive": false },
                { "min": "",   "max": "40", "isMinInclusive": true, "isMaxInclusive": false },
                { "min": "40", "max": "80", "isMinInclusive": true, "isMaxInclusive": false }
            ]
        }"#;
        let plan = parse(json);
        assert_eq!(plan.query_ranges.len(), 3);
        assert_eq!(plan.query_ranges[0].min, "");
        assert_eq!(plan.query_ranges[1].min, "40");
        assert_eq!(plan.query_ranges[2].min, "80");
    }

    /// An object that is not the `Infinity` sentinel is malformed and must
    /// error out (no silent collapse to the full keyspace).
    #[test]
    fn resolves_rejects_unknown_pki_object() {
        let json = r#"{
            "queryRanges": [
                { "min": [{"weird": true}], "max": "FF", "isMinInclusive": true, "isMaxInclusive": false }
            ]
        }"#;
        let raw: RawQueryPlan = serde_json::from_str(json).unwrap();
        let err = raw.resolve(&default_pk_def()).unwrap_err();
        assert!(format!("{err:?}").contains("PartitionKeyInternal"));
    }
}