ClickHouse / clickhouse-js

Official JS client for ClickHouse DB
https://clickhouse.com
Apache License 2.0
213 stars 26 forks source link

ResponseJSON<T> defines incorrect type for "totals" value #274

Closed sanyogitaranade closed 4 months ago

sanyogitaranade commented 4 months ago

Describe the bug

ResponseJSON is defined as

export interface ResponseJSON<T = unknown> {
    data: Array<T>;
    query_id?: string;
    totals?: Record<string, number>;
...
}

But, during our testing we observed that totals values can be "string/boolean" (possibly other data types) too. Here is one such response fragment.

{
  "meta": [
    {
      "name": "totalCount",
      "type": "UInt64"
    },
    {
      "name": "seqID",
      "type": "UInt32"
    },
    {
      "name": "eventHref",
      "type": "String"
    },
    {
      "name": "ip",
      "type": "String"
    },
    {
      "name": "userID",
      "type": "String"
    },
    {
      "name": "userName",
      "type": "String"
    },
    {
      "name": "userEmail",
      "type": "String"
    },
    {
      "name": "userIsAnonymous",
      "type": "Nullable(Bool)"
    },
  ],
  "data": [
    {
      "totalCount": "1",
      "seqID": 6,
      "eventHref": "href1",
      "ip": "::1",
      "userID": "1",
      "userName": "Jane Doe",
      "userEmail": "",
      "userIsAnonymous": false,
    }
  ],
  "totals": {
    "totalCount": "1",
    "seqID": 6,
    "eventHref": "href1",
    "ip": "::1",
    "userID": "1",
    "userName": "Jane Doe",
    "userEmail": "",
    "userIsAnonymous": false,
  },
  "rows": 1,
  "rows_before_limit_at_least": 1,
  "statistics": {
    "elapsed": 0.1502299,
    "rows_read": 69487,
    "bytes_read": 6324936
  }
}

Expected behaviour

  1. Instead of totals being Record<string, number>, it should match the shape of data i.e. the type should be
    data: Array<T>;
    query_id?: string;
    totals?: T;

Code example

      const { stream, query_id } = await this.client.exec({
        query: finalQuery,
      });

      const rs = new ResultSet<'JSON'>(stream, 'JSON', query_id);
      const decoded = await rs.json<Result>();

Error log

Configuration

Environment

ClickHouse server

slvrtrn commented 4 months ago

Fixed in 1.1.0.