observablehq / feedback

Customer submitted bugs and feature requests
42 stars 3 forks source link

SQL cell casts timestamps as dates #610

Open nachocab opened 6 months ago

nachocab commented 6 months ago

I was hoping there was a way to query an array of objects without losing the time. Here's an example notebook.

CleanShot 2023-12-08 at 17 41 52@2x

mootari commented 6 months ago

Yep, that's an open bug in DuckDB: https://github.com/duckdb/duckdb-wasm/issues/1231

The workaround is to ingest dates as ISO date strings, then change the column type:

{
  const data = [
    {create_time: new Date()}
  ];

  const client = await DuckDBClient.of({
    // Ingest dates as ISO strings
    my_data: data.map(d => ({
      ...d,
      create_time: d.create_time.toISOString()
    }))
  });

  // Changes the column type from Utf8 to Date64
  await client.sql`ALTER TABLE my_data ALTER COLUMN create_time SET DATA TYPE DATETIME`;

  // Should read "Date64<MILLISECOND>"
  const columnType = (await client.query("SELECT create_time FROM my_data")).schema[0].databaseType;

  return client;
}