Search code examples
csvcassandracql

Cassandra: Precision of double values exported with COPY TO in a collection (map)


How do I set the precision of a double value in a collection in the same way as a double column when exporting to a CSV file with COPY TO?

DOUBLEPRECISION works for the column (d below), but not for the key in the map.

CREATE TABLE test.digits (
    name    text,
    d       double,
    digits  map<double, int>,
    PRIMARY KEY ((name))
);

INSERT INTO test.digits (name, d, digits) VALUES ('Fred', 0.1234567890123456789,
    {
        1.1234567890123456789 : 1,
        2.1234567890123456789 : 2,
        3.1234567890123456789 : 3
    }
);

SELECT * from test.digits;

 name | d        | digits
------+----------+--------------------------------------
 Fred | 0.123457 | {1.12346: 1, 2.12346: 2, 3.12346: 3}

COPY test.digits (name, d, digits) TO 'digits.csv' WITH header=true AND DELIMITER='|' AND NULL='' AND DOUBLEPRECISION=15;

cat digits.csv
name|d|digits
Fred|0.1234567890123457|{1.12346: 1, 2.12346: 2, 3.12346: 3}

If it's not possible to set this precision in a collection, is this a bug or a feature?


Solution

  • Try adding: AND FLOATPRECISION=15

    COPY my_keyspace.digits (name, d, digits) TO 'digits.csv' 
    WITH header=true AND DELIMITER='|' AND NULL='' AND DOUBLEPRECISION=15
    AND FLOATPRECISION = 16;
        
    ➜  cat digits.csv
    name|d|digits
    Fred|0.1234567890123457|{1.1234567890123457: 1, 2.1234567890123457: 2, 3.1234567890123457: 3}