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

Query unable to use expression index that coalesces nulls #114078

Closed
marcrasi opened this issue Nov 8, 2023 · 2 comments · Fixed by #114798
Closed

Query unable to use expression index that coalesces nulls #114078

marcrasi opened this issue Nov 8, 2023 · 2 comments · Fixed by #114798
Assignees
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-queries SQL Queries Team X-blathers-triaged blathers was able to find an owner

Comments

@marcrasi
Copy link

marcrasi commented Nov 8, 2023

Describe the problem

I made a table with a nullable boolean field, and I created an expression index on coalesce(field, false). When I set disallow_full_table_scans and query with a WHERE coalesce(field, false) condition, it tells me that the query contains a full table scan.

Weirdly, WHERE not coalesce(field, false) runs successfully and uses the expression index.

To Reproduce

Here are some commands that repro the problem from scratch:

root@localhost:26257/defaultdb> create database repro_bug;
root@localhost:26257/defaultdb> use repro_bug;
root@localhost:26257/repro_bug> create table testing (id string, field boolean, primary key (id));
root@localhost:26257/repro_bug> create index on testing (coalesce(field, false));
root@localhost:26257/repro_bug> set disallow_full_table_scans=on;
root@localhost:26257/repro_bug> set large_full_scan_rows=0;

Now this query fails:

root@localhost:26257/repro_bug> select * from testing where coalesce(field, false);
ERROR: query `select * from testing where coalesce(field, false)` contains a full table/index scan which is explicitly disallowed
SQLSTATE: P0003
HINT: try overriding the `disallow_full_table_scans` or increasing the `large_full_scan_rows` cluster/session settings

And this query succeeds:

root@localhost:26257/repro_bug> select * from testing where not coalesce(field, false);
  id | field
-----+--------
(0 rows)

Here are the explains for the two queries:

root@localhost:26257/repro_bug> explain select * from testing where coalesce(field, false);
                                        info
------------------------------------------------------------------------------------
  distribution: local
  vectorized: true

  • filter
  │ estimated row count: 0
  │ filter: COALESCE(field, false)
  │
  └── • scan
        estimated row count: 1 (100% of the table; stats collected 10 seconds ago)
        table: testing@testing_pkey
        spans: FULL SCAN
(11 rows)

root@localhost:26257/repro_bug> explain select * from testing where not coalesce(field, false);
                                       info
-----------------------------------------------------------------------------------
  distribution: local
  vectorized: true

  • index join
  │ estimated row count: 0
  │ table: testing@testing_pkey
  │
  └── • scan
        estimated row count: 1 (99% of the table; stats collected 15 seconds ago)
        table: testing@testing_expr_idx
        spans: [/false - /false]
(11 rows)

Expected behavior

The select * from testing where coalesce(field, false) query should succeed and use the index.

Environment:

I'm running cockroach start-single-node on my macbook, and connecting to it with cockroach sql.

% cockroach --version
cockroach version details:
Build Tag:        v23.1.2
Build Time:       2023/05/25 16:20:05
Distribution:     CCL
Platform:         darwin arm64 (aarch64-apple-darwin21.2)
Go Version:       go1.19.4
C Compiler:       Clang 10.0.0
Build Commit ID:  810d4f27a7f02b9cc2750cab654ed1c62ac3e75a
Build Type:       release

Jira issue: CRDB-33326

@marcrasi marcrasi added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Nov 8, 2023
Copy link

blathers-crl bot commented Nov 8, 2023

Hello, I am Blathers. I am here to help you get the issue triaged.

Hoot - a bug! Though bugs are the bane of my existence, rest assured the wretched thing will get the best of care here.

I have CC'd a few people who may be able to assist you:

  • @cockroachdb/sql-queries (found keywords: vectorized)

If we have not gotten back to your issue within a few business days, you can try the following:

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Nov 8, 2023
@yuzefovich yuzefovich added the T-sql-queries SQL Queries Team label Nov 8, 2023
@mgartner
Copy link
Collaborator

Thanks for the report @marcrasi! It's definitely a bit confusing why the not coalesce(field, false) query uses the expression index, but not the coalesce(field, false) query.

One workaround it to index field and alter your query to include or exclude NULL values as needed:

-- This is the same as `... WHERE coalesce(field, false)` and will use an index on `field`.
SELECT * FROM testing WHERE field;

-- This is the same as `... WHERE NOT coalesce(field, false)` and will use an index on `field`.
SELECT * FROM testing WHERE NOT field OR field IS NULL;

craig bot pushed a commit that referenced this issue Jan 5, 2024
114798: opt: generate constrained scans for indexed, boolean expressions r=mgartner a=mgartner

Prior to this commit, the optimizer would not generate constrained scans
for all indexed, boolean computed expressions. This limitation has been
lifted.

Fixes #114078

Release note (performance improvement): The optimizer now generates
constrained scans on indexes containing boolean, computed expressions.


Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com>
@craig craig bot closed this as completed in 83aa7b6 Jan 5, 2024
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. O-community Originated from the community T-sql-queries SQL Queries Team X-blathers-triaged blathers was able to find an owner
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants