Value semantics
The shared value-semantics crate gives query, planning, and storage code one exact model for numeric values. It keeps comparison and index behavior stable when the same value crosses AST and database value types. This page complements Graph computation and value semantics and Graph algorithms.
System role
The crate is a small contract layer between caller-specific property values and behavior that depends on value meaning. The planner and database each adapt their own value type at the boundary, so the shared model does not depend on AST or storage code.
The main path is:
flowchart LR
A[AST property value] --> B[Planner adapter]
C[Stored property value] --> D[Database adapter]
B --> E[Canonical numeric value]
D --> E
E --> F[Query comparison]
E --> G[Planner range bound]
E --> H[Equality key]
E --> I[Range key]
Shared numeric contract
CanonicalNumber is the shared exact form for every non-NaN integer or floating-point value. It has separate classes for negative infinity, negative finite values, zero, positive finite values, and positive infinity. A finite value uses an odd significand and a binary exponent, which removes trailing powers of two without changing the value (crates/…/lib.rs• L11-16, crates/…/lib.rs• L49-76).
The contract treats equal mathematical values as equal across i64, f32, and f64. It also maps positive and negative zero to one zero value. NaN has no canonical value, so floating-point conversion returns no result (crates/…/lib.rs• L78-109, crates/…/lib.rs• L111-153).
References: crates/…/lib.rs• L11-16 crates/…/lib.rs• L49-76 crates/…/lib.rs• L78-153
Numeric comparison
The shared order is an exact mathematical order. It places negative infinity first and positive infinity last. It compares finite magnitudes without first converting integers to floating point, so values near the precision limit remain distinct (crates/…/lib.rs• L155-176).
The query-facing database value uses this contract for all numeric pairs. It compares datetimes only with datetimes and strings only with strings. A NaN or a pair from different non-numeric domains produces no comparison instead of an invented result (crates/…/property_value.rs• L121-139).
References: crates/…/lib.rs• L135-176 crates/…/property_value.rs• L121-139 crates/…/value.rs• L203-241
Value representations
The repository has caller-specific PropertyValue types because AST values and stored values serve different jobs. Both can represent null, booleans, integers, dates, floats, strings, bytes, typed arrays, nested arrays, and objects. The shared crate intentionally handles only primitive numeric inputs; callers retain control of their wider value rules (crates/…/value.rs• L12-43, crates/…/lib.rs• L1-5).
Semantic equality
Semantic equality means that values match under query rules. Numeric variants match by exact numeric value, while different domains such as a string and an integer do not match. NaN does not match anything because it is non-reflexive: it cannot be treated as equal to itself (crates/…/property_value.rs• L201-227).
This rule is different from persisted representation identity. Storage can preserve float sign bits and NaN payloads when it must decide whether two old records have the same bytes. That stricter check also applies inside arrays and objects, so representation identity must not replace query equality (crates/…/property_value.rs• L52-118).
References: crates/…/property_value.rs• L52-118 crates/…/property_value.rs• L201-227
Total value order
total_order supplies a deterministic order for a full scan when no index supplies the order. It first orders value domains as null, boolean, numeric, datetime, string, bytes, typed arrays, nested arrays, and objects. It then compares values within the same domain, using the canonical numeric order for valid numbers and a deterministic fallback for NaN (crates/…/property_value.rs• L142-199).
This order is broader than predicate comparison. Predicate comparison can return no result for incompatible domains, while a full scan still needs every row to have a stable position. Nested arrays compare element by element, and objects compare sorted key-value pairs (crates/…/property_value.rs• L176-199, crates/…/property_value.rs• L346-389).
References: crates/…/property_value.rs• L142-199 crates/…/property_value.rs• L346-389
Query and index contracts
The same numeric meaning reaches queries through two related paths. Query predicates use semantic comparison and equality. Index planning and storage use a narrower set of values that can be represented safely in an index key.
Range value conversion
A range bound accepts an integer, datetime, non-NaN float, or string literal. A runtime parameter is also accepted because its value is checked later. Null, booleans, bytes, arrays, objects, and NaN are not valid range literals (crates/…/value.rs• L10-60, crates/…/value.rs• L163-201).
The planner preserves the value domain during conversion. Integers and floats share the canonical numeric comparison, but a datetime is not an integer even when both use an i64 internally. This prevents a range bound from crossing domains by accident (crates/…/value.rs• L203-241).
References: crates/…/value.rs• L10-60 crates/…/value.rs• L163-241 crates/…/value.rs• L14-39
Equality value conversion
Equality indexing uses typed canonical values. The canonical form keeps booleans, numbers, dates, strings, bytes, and supported typed arrays in separate domains. Exact numeric equivalents share one canonical number, while values such as integer 42 and string "42" stay distinct (crates/…/equality_index_value.rs• L16-30, crates/…/equality_index_value.rs• L92-173).
The equality path classifies every input as indexable, authoritative null, non-reflexive, unsupported, or oversized. Nested arrays and objects are not equality-index values in this path. A short digest speeds scans, but the full canonical bytes remain the exact identity and protect against digest collisions (crates/…/equality_index_value.rs• L32-57, crates/…/equality_index_value.rs• L82-90).
References: crates/…/equality_index_value.rs• L32-57 crates/…/equality_index_value.rs• L82-173 crates/…/canonical_number.rs• L7-24
Range key conversion
Range storage maps supported values into one order-preserving domain. Numeric values come first, datetimes come next, and strings come last. The encoded form keeps ascending key order aligned with semantic order. Descending keys reverse that order (crates/…/range_index_value.rs• L1-5, crates/…/range_index_value.rs• L88-140).
The range key also keeps the entity identifier as a tie-breaker after the value. This lets multiple entities with the same property value remain separate while the value portion still controls the scan order (crates/…/range_index_value.rs• L34-75).
References: crates/…/range_index_value.rs• L34-75 crates/…/range_index_value.rs• L79-140 crates/…/range_index_value.rs• L143-164
Normalization boundaries
Normalization must happen before code compares values or builds an index key. The planner turns AST literals into range values. The database turns stored property values into canonical numeric values and then into equality or range projections. These boundaries keep the shared contract in one place while allowing each caller to reject values that its own feature cannot support.
Planner boundary
The planner accepts only literal or parameter operands for an index bound. It turns a valid literal into a typed range value and reports a non-literal expression separately from a literal that is simply not indexable (crates/…/value.rs• L14-39). Comparison operators then map the value to a lower or upper bound while preserving inclusive or exclusive behavior (crates/…/range.rs• L15-77, crates/…/range.rs• L102-141).
For membership predicates, the planner converts collection members to scalar values and removes duplicate semantic values. NaN members are excluded from the usable equality set, while an entirely unusable set becomes empty rather than a false broad match (crates/…/equality.rs• L130-189).
References: crates/…/value.rs• L14-39 crates/…/range.rs• L81-141 crates/…/equality.rs• L130-189
Storage boundary
The database adapter converts its property-value variants into CanonicalNumber only for integer and floating-point values. It returns no numeric value for other domains or NaN, which lets equality and range projections fail closed (crates/…/canonical_number.rs• L1-24).
Stored keys validate their canonical frames when they are reconstructed. This protects query and maintenance paths from malformed bytes, invalid numeric forms, trailing data, and digest mismatches (crates/…/equality_index_value.rs• L48-57, crates/…/equality_index_value.rs• L218-269, crates/…/range_index_value.rs• L166-239).
References: crates/…/canonical_number.rs• L1-24 crates/…/equality_index_value.rs• L48-57 crates/…/equality_index_value.rs• L218-269 crates/…/range_index_value.rs• L166-239
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the numeric class set and exact normalization rules aligned with query comparison and both index projections.
- If a property domain changes, review planner range-literal acceptance and database equality/range projection support together.
- If canonical numeric fields change, review persisted equality frames and range-key validation because both depend on the same finite-value facts.
- Preserve the difference between semantic equality, total scan order, and persisted representation identity.
- When index key order changes, review ascending and descending scans as one contract and retain the entity-ID tie-breaker.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A NaN enters numeric normalization | Query comparison has no result and equality or range indexing rejects the value | Use the Option result from numeric conversion and the projection classifications for NaN (crates/…/lib.rs• L87-109, crates/…/range_index_value.rs• L93-100) |
| An integer is compared after conversion to a float | Distinct integers near the float precision limit can become equal | Compare through CanonicalNumber (crates/…/lib.rs• L184-193, crates/…/value.rs• L249-265) |
| A datetime is treated as a numeric bound | A query can cross a value domain that the range index keeps separate | Keep datetime in its own typed variant and domain (crates/…/value.rs• L183-200, crates/…/range_index_value.rs• L94-105) |
| A digest is treated as the full equality identity | A digest collision can select the wrong stored value | Compare and persist the length-delimited canonical bytes after the digest scan (crates/…/equality_index_value.rs• L1-4, crates/…/equality_index_value.rs• L39-57) |
| A malformed canonical key is accepted | Storage-facing lookup or maintenance can read invalid value data | Validate domain, length, numeric form, string framing, and trailing bytes before use (crates/…/range_index_value.rs• L166-239, crates/…/equality_index_value.rs• L218-269) |
Key files
| File | Why to read it |
|---|---|
| crates/…/lib.rs | Defines the shared exact numeric representation, normalization, and total numeric order. |
| crates/…/value.rs | Defines the AST-side property values that the planner adapts. |
| crates/…/value.rs | Defines which literals and parameters can serve as range bounds and how domains compare. |
| crates/…/range.rs | Shows how predicate operators become typed range bounds. |
| crates/…/equality.rs | Shows how equality and membership predicates become usable value domains. |
| crates/…/property_value.rs | Defines database-side semantic equality, predicate comparison, total order, and representation identity. |
| crates/…/canonical_number.rs | Adapts stored property values to the shared numeric contract. |
| crates/…/equality_index_value.rs | Defines typed equality projections, exact canonical identity, and validation. |
| crates/…/range_index_value.rs | Defines order-preserving range projections and ascending or descending key behavior. |