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

unable to encode table key: *tree.DJSON error during CREATE STATISTICS #63387

Closed
gaoxk opened this issue Apr 9, 2021 · 2 comments
Closed

unable to encode table key: *tree.DJSON error during CREATE STATISTICS #63387

gaoxk opened this issue Apr 9, 2021 · 2 comments
Assignees
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team

Comments

@gaoxk
Copy link
Contributor

gaoxk commented Apr 9, 2021

Describe the problem
Running CREATE STATISTICS ... results in the error ERROR: unable to encode table key: *tree.DJSON when partial indexes get involved.

To Reproduce

  1. Set up CockroachDB
  2. Create the following table
CREATE TABLE public.policies (
    id UUID NOT NULL,
    principal_user_id UUID NOT NULL,
    serialized JSONB NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now():::TIMESTAMPTZ,
    expires_at TIMESTAMPTZ NULL,
    revoked_at TIMESTAMPTZ NULL,
    CONSTRAINT "primary" PRIMARY KEY (id ASC),
    CONSTRAINT fk_principal_user_id_ref_users FOREIGN KEY (principal_user_id) REFERENCES public.users(id) ON DELETE CASCADE,
    INDEX policies_principal_user_id_expires_at_idx (principal_user_id ASC, expires_at ASC),
    INDEX policies_superuser (principal_user_id ASC, expires_at ASC, revoked_at ASC) WHERE (serialized->>'type':::STRING) LIKE 'SUPERUSER_%':::STRING,
    FAMILY "primary" (id, principal_user_id, serialized, created_at, expires_at, revoked_at)
)
  1. Run CREATE STATISTICS policy_stats FROM policies;
  2. See error ERROR: unable to encode table key: *tree.DJSON

Expected behavior
I expect no error to occur.

Additional data / screenshots
Output of SHOW STATISTICS FOR TABLE policies;:

 statistics_name |          column_names          |             created              | row_count | distinct_count | null_count |    histogram_id
------------------+--------------------------------+----------------------------------+-----------+----------------+------------+---------------------
  __auto__        | {id}                           | 2021-03-09 16:57:22.255134+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {principal_user_id}            | 2021-03-09 16:57:22.255134+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {expires_at}                   | 2021-03-09 16:57:22.255134+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {principal_user_id,expires_at} | 2021-03-09 16:57:22.255134+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {serialized}                   | 2021-03-09 16:57:22.255134+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {created_at}                   | 2021-03-09 16:57:22.255134+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {id}                           | 2021-04-05 13:50:50.501794+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {principal_user_id}            | 2021-04-05 13:50:50.501794+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {expires_at}                   | 2021-04-05 13:50:50.501794+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {principal_user_id,expires_at} | 2021-04-05 13:50:50.501794+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {serialized}                   | 2021-04-05 13:50:50.501794+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {created_at}                   | 2021-04-05 13:50:50.501794+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {revoked_at}                   | 2021-04-05 18:32:02.452387+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {id}                           | 2021-04-05 18:32:02.452387+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {expires_at}                   | 2021-04-05 18:32:02.452387+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {principal_user_id,expires_at} | 2021-04-05 18:32:02.452387+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {serialized}                   | 2021-04-05 18:32:02.452387+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {created_at}                   | 2021-04-05 18:32:02.452387+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {principal_user_id}            | 2021-04-05 18:32:02.452387+00:00 |         0 |              0 |          0 |               NULL
  __auto__        | {id}                           | 2021-04-07 15:10:15.273846+00:00 |         1 |              1 |          0 | 647947273111896065
  __auto__        | {principal_user_id}            | 2021-04-07 15:10:15.273846+00:00 |         1 |              1 |          0 | 647947273276719105
  __auto__        | {expires_at}                   | 2021-04-07 15:10:15.273846+00:00 |         1 |              1 |          1 | 647947273439674369
  __auto__        | {principal_user_id,expires_at} | 2021-04-07 15:10:15.273846+00:00 |         1 |              1 |          0 |               NULL
  __auto__        | {serialized}                   | 2021-04-07 15:10:15.273846+00:00 |         1 |              1 |          0 |               NULL
  __auto__        | {created_at}                   | 2021-04-07 15:10:15.273846+00:00 |         1 |              1 |          0 | 647947273714663425
  __auto__        | {revoked_at}                   | 2021-04-07 15:10:15.273846+00:00 |         1 |              1 |          1 | 647947273802940417
(26 rows)

Environment:

  • CockroachDB CCL v20.2.3 (x86_64-apple-darwin14, built 2020/12/14 18:37:58, go1.13.14) (same version as client)
  • Server OS: Mac Big Sur
    -cockroach sql
@gaoxk gaoxk added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team labels Apr 9, 2021
@mgartner
Copy link
Collaborator

Thanks for the report! It looks like this has been fixed on master, so I'll bisect to figure out which commit fixed it and try to backport that commit to 20.2.

mgartner added a commit to mgartner/cockroach that referenced this issue Apr 26, 2021
This commit fixes a bug that caused errors when creating stats for a
table with a partial index predicate containing references to
inverted-type columns, like JSON and ARRAY.

Fixes cockroachdb#63387

Release note (bug fix): The `CREATE STATISTICS` command no longer fails
when creating statistics on a table with a partial index predicate
containing references to an inverted-type column, such as `JSON`,
`ARRAY`, `GEOMETRY`, or `GEOGRAPHY`. This bug was present since partial
indexes were introduced in version 20.2.0.
@mgartner
Copy link
Collaborator

mgartner commented May 7, 2021

Fixed by #64226.

@mgartner mgartner closed this as completed May 7, 2021
@mgartner mgartner moved this to Done in SQL Queries Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team
Projects
Archived in project
Development

No branches or pull requests

2 participants