deno icon indicating copy to clipboard operation
deno copied to clipboard

Panic in NAPI when using pl.format() with expressions in Polars Node.js

Open drewbitt opened this issue 1 year ago • 0 comments

Version: Deno 1.42.4

The Node.js version of the Polars library panics when using pl.format() with expressions inside a select operation. This issue occurs even in a minimal example and prevents the execution of the script. It should just error.

Reproduction

Minimial reproduction with documentation:

import pl from "npm:nodejs-polars";

/**
 * Create a sample DataFrame with a single column "ds" containing date values.
 */
const df = pl.DataFrame({
  ds: [
    new Date("2023-01-01"),
    new Date("2023-01-02"),
    new Date("2023-01-03"),
    new Date("2023-01-04"),
  ],
});

/**
 * Perform a simple operation on the DataFrame that causes a panic.
 *
 * The operation:
 * 1. Selects the "ds" column and aliases it as "ds1" and "ds2".
 * 2. Filters the rows where "ds1" is less than "ds2".
 * 3. Selects a new column "cadence" using pl.format() to format a string.
 *    The string includes placeholders {} and expressions that calculate the
 *    difference between "ds2" and "ds1" in days and adds a plural "s" if the
 *    difference is greater than 1.
 */
const cadences = df
  .select(pl.col("ds").alias("ds1"), pl.col("ds").alias("ds2"))
  .filter(pl.col("ds1").lessThan(pl.col("ds2")))
  .select(
    pl
      .format(
        "{} day{}",
        pl.col("ds2").cast(pl.Int64).sub(pl.col("ds1").cast(pl.Int64)),
        pl
          .when(
            pl
              .col("ds2")
              .cast(pl.Int64)
              .sub(pl.col("ds1").cast(pl.Int64))
              .greaterThan(1)
          )
          .then("s")
          .otherwise("")
      )
      .alias("cadence")
  );

console.log(cadences);

Panics with:

❯ deno run -A find-cadence-3.ts

============================================================
Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.

Platform: macos aarch64
Version: 1.42.4
Args: ["deno", "run", "-A", "find-cadence-3.ts"]

thread 'main' panicked at cli/napi/js_native_api.rs:2749:47:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Issue that is not being handled

The issue in the code is that

          .then("s")
          .otherwise("")

should be something like:

          .then(pl.lit("s"))
          .otherwise(pl.lit(""))

and I would expect the error to be handled.

Other runtimes

Node

 tsx find-cadence-3.ts    
/Users/drewbitt/Repos/x/scripts/find-cadences-imports/node_modules/nodejs-polars/bin/lazy/whenthen.js:33
        then: ({ _expr }) => Then(_when.then(_expr)),
                                        ^

Error: Failed to recover `JsExpr` type from napi value
    at Object.then (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/node_modules/nodejs-polars/bin/lazy/whenthen.js:33:41)
    at pl (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/find-cadence-3.ts:42:12)
    at Object.<anonymous> (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/find-cadence-3.ts:54:21)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/Users/drewbitt/.local/share/mise/installs/npm-tsx/4.7.1/lib/node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:202:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:336:24) {
  code: 'InvalidArg'
}

bun

 bun find-cadence-3.ts
28 |  * Utility function.
29 |  * @see {@link when}
30 |  */
31 | function When(_when) {
32 |     return {
33 |         then: ({ _expr }) => Then(_when.then(_expr)),
                                                  ^
error: Failed to recover `JsExpr` type from napi value
 code: "ObjectExpected"

      at then (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/node_modules/.deno/[email protected]/node_modules/nodejs-polars/bin/lazy/whenthen.js:33:46)
      at /Users/drewbitt/Repos/x/scripts/find-cadences-imports/find-cadence-3.ts:34:9

drewbitt avatar Apr 22 '24 17:04 drewbitt