observablehq / feedback

Customer submitted bugs and feature requests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SQL cell casts timestamps as dates

nachocab opened this issue · comments

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

Yep, that's an open bug in DuckDB: duckdb/duckdb-wasm#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;
}