Search code examples
javascriptwebassemblyduckdb

How can I set castDecimalToDouble in duckdb-wasm


I'm using duckdb-wasm to read parquet files containing decimal values. How can I have duckdb have the converted to double automatically? I have found this documentation, but don't know how to apply it:

https://shell.duckdb.org/docs/interfaces/index.DuckDBQueryConfig.html#castDecimalToDouble


Solution

  • The DuckDBQueryConfig interface you've found is used as the type of the input parameter to AsyncDuckDB.open used to provide configuration options when opening a new DuckDB database.

    This is a full example of fetching the bundles from jsDelivr, instantiating the worker, logger, and an AsyncDuckDB binding. The code then opens a new database with the configuration options to do the casting.

    import * as duckdb from '@duckdb/duckdb-wasm';
    
    export async function init(): Promise<duckdb.AsyncDuckDB> {
      const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();
    
      const bundle: duckdb.DuckDBBundle = await duckdb.selectBundle(
          JSDELIVR_BUNDLES
      );
    
      const worker_url = URL.createObjectURL(
        new Blob([`importScripts("${bundle.mainWorker!}");`], {
          type: 'text/javascript',
        })
      );
    
      const worker = new Worker(worker_url);
    
      const logger = new duckdb.ConsoleLogger();
      const db = new duckdb.AsyncDuckDB(logger, worker);
      await this.db.instantiate(bundle.mainModule, bundle.pthreadWorker);
      URL.revokeObjectURL(worker_url);
    
      await db.open({
        query: { castBigIntToDouble: true, castDecimalToDouble: true },
      });
    
      return db;
    }