Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONB support #513

Merged
merged 15 commits into from
Jan 13, 2025
Merged

JSONB support #513

merged 15 commits into from
Jan 13, 2025

Conversation

ritwizsinha
Copy link
Contributor

@ritwizsinha ritwizsinha commented Dec 31, 2024

Adding support for JSONB and JSONB[]

Fixes #516

@ritwizsinha ritwizsinha marked this pull request as draft January 1, 2025 09:58
Copy link
Collaborator

@JelteF JelteF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! These tests cover some nice ground around JSON support. Sadly half of them result in warnings at the moment, but I think it'd be good to add them already anyway. At least to make sure that we don't crash/error, but fall back gracefully to Postgres execution.

-- JSON
set duckdb.force_execution to true;
-- -> int operator
select '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These queries need to have a FROM clause. Queries that select only constants are not being executed by duckdb currently.

test/regression/expected/json_operator_support.out Outdated Show resolved Hide resolved
-- Test Case 1: Access JSON Object Field (->)
SELECT id, data->'a' AS a_value
FROM test_json;
WARNING: (PGDuckDB/CreatePlan) Prepared query returned an error: 'Binder Error: No function matches the given name and argument types 'json_extract("UnsupportedPostgresType (Oid=3802)", VARCHAR)'. You might need to add explicit type casts.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be really good to start supporting JSONB.

@JelteF JelteF mentioned this pull request Jan 2, 2025
@ritwizsinha ritwizsinha marked this pull request as ready for review January 2, 2025 13:18
-- Create table for JSON testing
CREATE TABLE test_json (
id SERIAL PRIMARY KEY,
data JSONB
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially you had some tests for the JSON type too. I think these tests would be more useful if they used the json type. Since jsonb is currently not supported at all, all the tests return a warning.

Suggested change
data JSONB
data JSON

To be clear I think the jsonb tests are good to have, but they should probably be added together with some basic jsonb support. Having them all throw a warning in the same way is not super useful.

JelteF and others added 9 commits January 11, 2025 19:02
In duckdb#496 we started using the newest clang-format to format our files,
but CI was still installing the old version. This meant that we were not
catching some unformatted files correctly. An example of this being:
duckdb#511 (comment)

This starts using the correct clang-format version in CI too and formats
any incorrectly formatted files.
…b#521)

To construct output string use`AddStringOrBlob` which doesn't require
that input is valid UTF8.
It was [pointed out][1] by @dpxcc that one of our tests was not testing
what it was describing to test. This fixes that. To be clear the
functionality was already working.

[1]: duckdb#241 (comment)
Idea is to reconstruct query based on duckdb filtering information, for
each table and use that information to plan postgres execution. This
plan will, potentially exeucute with parallel workers. If no workers are
available we will scan this local to thread.

This approach has advantage that it also will support all other scan
nodes that are available (and postgresql thinks are best - index/index
only/bitmap scan, also partitioned tables should be possible)

Fixes duckdb#243

---------

Co-authored-by: Yves <[email protected]>
Co-authored-by: Jelte Fennema-Nio <[email protected]>
I noticed two issues with the new `approx_count_distinct`
implementation:

1. If no FROM clause was used it was not possible to use it
2. It would not be detected correctly as duckdb-only without
   `duckdb.force_execution = true` (or some  other mechanism). This
   fixes both of those issues.

Related to duckdb#499
@@ -896,6 +897,7 @@ ConvertPostgresToBaseDuckColumnType(Form_pg_attribute &attribute) {
return duckdb::LogicalTypeId::UUID;
case JSONOID:
case JSONARRAYOID:
case JSONBOID:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also support jsonb insida a postgres array:

Suggested change
case JSONBOID:
case JSONBOID:
case JSONBARRAYOID:

This would need some tests in array_type_support.sql

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

test/regression/schedule Outdated Show resolved Hide resolved
@JelteF JelteF changed the title Add tests for json operators JSONB support Jan 12, 2025
@ritwizsinha
Copy link
Contributor Author

@JelteF your requested changes were made

Copy link
Collaborator

@JelteF JelteF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this work. This looks good now. Next steps in my order of priority would be:

  1. Start supporting some of duckdb its JSON functions
  2. Introduce some of the JSONB operators that postgres supports to DuckDB.
  3. Introduce a PG compatibility mode in duckdb to make existing operators behave exactly the same.

@JelteF JelteF merged commit cf6e6c4 into duckdb:main Jan 13, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support JSONB
4 participants