Skip to content

INTERNAL error when using a macro to expand json[] array (json_each) #14542

@norcalli

Description

@norcalli

What happens?

When I use the macro defined below with a json[] column, it errors out, but text and json types convert correctly.

INTERNAL Error: Failed to cast logical operator to type - logical operator type mismatch
This error signals an assertion failure within DuckDB. This usually occurs due to unexpected conditions or errors in the program's logic.
For more information, see https://duckdb.org/docs/dev/internal_errors

To Reproduce

From the cli:

duckdb -c "
  CREATE OR REPLACE MACRO json_each(input) AS
    TABLE (
      SELECT
          CASE json_type (val::json)
          WHEN 'ARRAY' THEN
              unnest(RANGE (json_array_length(val::json)::bigint)) ::varchar
          ELSE
              unnest(json_keys (val::json))
          END AS key,
          json_extract (val::json, key) AS value
      FROM (SELECT input as val)
    );

  FROM (SELECT '[1,2,3]' as message) CROSS JOIN json_each(message);
  FROM (SELECT '[1,2,3]'::json as message) CROSS JOIN json_each(message);
  FROM (SELECT '[1,2,3]'::json[] as message) CROSS JOIN json_each(message);
  "

Output:

┌─────────┬─────────┬───────┐
│ message │   key   │ value │
│ varchar │ varchar │ json  │
├─────────┼─────────┼───────┤
│ [1,2,3] │ 0       │ 1     │
│ [1,2,3] │ 1       │ 2     │
│ [1,2,3] │ 2       │ 3     │
└─────────┴─────────┴───────┘
┌─────────┬─────────┬───────┐
│ message │   key   │ value │
│  json   │ varchar │ json  │
├─────────┼─────────┼───────┤
│ [1,2,3] │ 0       │ 1     │
│ [1,2,3] │ 1       │ 2     │
│ [1,2,3] │ 2       │ 3     │
└─────────┴─────────┴───────┘
INTERNAL Error: Failed to cast logical operator to type - logical operator type mismatch
This error signals an assertion failure within DuckDB. This usually occurs due to unexpected conditions or errors in the program's logic.
For more information, see https://duckdb.org/docs/dev/internal_errors

OS:

linux

DuckDB Version:

1.0.0, 1.1.0

DuckDB Client:

CLI

Hardware:

No response

Full Name:

Ashkan Kiani

Affiliation:

Jane Street

What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a stable release

Did you include all relevant data sets for reproducing the issue?

Yes

Did you include all code required to reproduce the issue?

  • Yes, I have

Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

  • Yes, I have

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions