Skip to main content

NumericKey

Struct NumericKey 

Source
pub struct NumericKey { /* private fields */ }
Expand description

r1039 — an exact-decimal index key, canonical so that representation equality IS value equality.

That property is the whole reason this is a struct rather than the (scaled, scale) pair the value carries. 1.5 and 1.50 are the same NUMERIC (PG18.4: 1.5::numeric = 1.50::numeric is true) and arrive here as (15, 1) and (150, 2). A B-tree keyed on the raw pair would file them apart, so WHERE n = 1.5 would miss a row stored as 1.50 — an index changing the answer, which is the one thing an index may never do. BigNumeric::cmp carries the same warning and declines to implement Ord for exactly this reason; a KEY cannot decline, so it normalizes instead.

Canonical form: significant decimal digits with no leading and no trailing zeros, most significant first, plus the decimal exponent of the leading digit. Zero is the empty digit vector with neg == false and exp == 0, so there is no -0.

Ordering is PG’s, measured: -Infinity < -1 < 0 < 1 < Infinity < NaN, and NaN = NaN.

Implementations§

Source§

impl NumericKey

Source

pub fn from_numeric(scaled: i128, scale: u16, kind: NumericKind) -> Self

The key for a Value::Numeric’s three fields.

Public because the ORDER BY key wants the same canonical form the index key uses: two sort keys that disagree about which of two NUMERICs is larger is the same class of defect as an index that disagrees with a scan, and one definition is how they stay honest.

Source

pub fn from_i128(n: i128) -> Self

The key for an exact integer — no scale, so no rounding.

Source

pub fn from_big(b: &BigNumeric) -> Self

The key for a mantissa that overflowed i128. The two representations of one value land on one key.

Source

pub fn to_f64(&self) -> f64

The f64 this key means, for the one comparison PG defines that way: numeric against float8 demotes the numeric.

Lossy by construction — that is the point, and it is why nothing else uses it.

Source

pub fn digits(&self) -> Vec<u8>

The significant decimal digits, most significant first — the form the catalog codec writes, and the one from_parts reads back.

Source

pub fn parts(&self) -> (u8, bool, i32)

The wire parts, for the catalog codec.

Source

pub fn from_parts(class: u8, neg: bool, exp: i32, digits: &[u8]) -> Option<Self>

Rebuild from the wire parts. Returns None on parts that are not canonical, so a corrupt catalog cannot smuggle in a key whose Eq and Ord disagree.

Trait Implementations§

Source§

impl Clone for NumericKey

Source§

fn clone(&self) -> NumericKey

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NumericKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for NumericKey

Source§

impl Ord for NumericKey

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for NumericKey

Source§

fn eq(&self, other: &NumericKey) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for NumericKey

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for NumericKey

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.