Skip to content

Numeric fields with NaN being converted to null or "NaN" (String) #1943

Open
@emartinez-usgs

Description

@emartinez-usgs

I have a database with a numeric array which sometimes contains NaN values. When querying these data, the resulting rows instead contain array of null values.

PostgreSQL Command Line

postgres=# \d data
                           Table "deterministic.data"
  Column   |   Type    | Collation | Nullable |             Default              
-----------+-----------+-----------+----------+----------------------------------
 id        | integer   |           | not null | nextval('data_id_seq'::regclass)
 region_id | integer   |           | not null | 
 latitude  | numeric   |           | not null | 
 longitude | numeric   |           | not null | 
 pgad      | numeric   |           |          | 
 sad       | numeric[] |           |          | 

postgres=# select * from data limit 1;
    id    | region_id | latitude | longitude | pgad |                                            sad                                            
----------+-----------+----------+-----------+------+-------------------------------------------------------------------------------------------
 35747804 |        14 |       50 |     -76.1 |  NaN | {NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN}

Node (node-postgres)

'use strict';

const { Client } = require('pg').native;
const DB_CONFIG = require('./db-config.js');

const db = new Client(DB_CONFIG);

db.connect(err => {
  if (err) {
    process.stderr.write(err + '\n');
  } else {
    db.query('SELECT * FROM deterministic.data LIMIT 1', (err, result) => {
      if (err) {
        process.stderr.write(err + '\n');
      } else {
        process.stdout.write(JSON.stringify(result.rows[0], null, 2) + '\n');
        process.exit(0);
      }
    });
  }
});

Node Output

$ node index.js 
{
  "id": 35747804,
  "region_id": 14,
  "latitude": "50",
  "longitude": "-76.1",
  "pgad": "NaN",
  "sad": [
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions