Skip to content

wrong encoding of zero value in numeric #763

@Standing-Man

Description

@Standing-Man
    #[test]
    fn numberic_zero() {
        let decimal_zero = Decimal::from(0);

        let mut binary_result = BytesMut::new();
        decimal_zero.to_sql(&Type::ANY, &mut binary_result).unwrap();
        
        assert_eq!(binary_result.len(), 10);
        assert_eq!(&binary_result[..], &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
    }

But I’m getting the following failed test output, as shown below:

thread 'postgres::driver::test::numberic_zero' (13608077) panicked at src/postgres/driver.rs:477:9:
assertion `left == right` failed
  left: [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
 right: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Detail

As shown in https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/numeric.c

/*
 * zero_var() -
 *
 *	Set a variable to ZERO.
 *	Note: its dscale is not touched.
 */
static void
zero_var(NumericVar *var)
{
	digitbuf_free(var->buf);
	var->buf = NULL;
	var->digits = NULL;
	var->ndigits = 0;
	var->weight = 0;			/* by convention; doesn't really matter */
	var->sign = NUMERIC_POS;	/* anything but NAN... */
}
tempdb=# select numeric_send(0);
    numeric_send    
--------------------
 \x0000000000000000
(1 row)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions