Skip to content

Scalar index queries handle NOT and NULL incorrectly #4756

Description

@wjones127

This happens with both BTREE and BITMAP:

import pyarrow as pa
import lance

data = pa.table({"value": [True, False, None]})
ds = lance.write_dataset(data, "memory://")

ds.to_table(filter="NOT value")
value: int64
----
value: [[false]]
ds.create_scalar_index("value", "BTREE")
ds.to_table(filter="NOT value")
pyarrow.Table
value: int64
----
value: [[false,null]]
ds.create_scalar_index("value", "BITMAP", replace=True)
ds.to_table(filter="NOT value")
pyarrow.Table
value: int64
----
value: [[false,null]]

Similar things happen if you use integers and use condition like NOT (value = 1)

Activity

  1. added
    bug
    on Sep 17, 2025
  2. westonpace commented on Sep 18, 2025

    @westonpace
    Member

    Ah, this makes sense because we interpret NOT as the logical inverse. So if value = 1 does not include one then NOT value = 1 will. Not saying this is correct though 😦.

    I'm not sure of a simple way to fix it except maybe push NOT down into the indexes themselves instead of handling it a layer above.

  3. wjones127 commented on Nov 6, 2025

    @wjones127
    ContributorAuthor

    This was accidentally closed by #4815

    This PR doesn't fix #4756

  4. wjones127 commented on Nov 17, 2025

    @wjones127
    ContributorAuthor

    I think what we need to do is make scalar queries return something like a nullable selection vector. That way we can process the nulls properly at all stages. Then at the very end we can resolve it to mask.

    I think this is how filters work in DataFusion: it takes a nullable Boolean array. Then when filter_record_batch is called it drops null and false.

  5. westonpace commented on Nov 18, 2025

    @westonpace
    Member

    What is the physical representation of a nullable selection vector? We use roaring bitmaps today which are sparse. Can we introduce nulls without moving to a dense bitmap? Or should we just bite the bullet and move to dense bitmap? Maybe we can implement it as two sparse bitmaps (one of true/false and one of nulls)?

  6. self-assigned this
    on Nov 18, 2025
  7. wjones127 commented on Nov 18, 2025

    @wjones127
    ContributorAuthor

    We already represent things intermediately as two sparse bitmaps: allow lists and block lists. So I was thinking it would be natural to add a third null list. We’ll see where that lands me.

  8. westonpace commented on Nov 18, 2025

    @westonpace
    Member

    If you're working on this, one other optimization I was thinking of making, is removing the "block list and allow list" case. Anytime we have this case we can simply normalize it into a smaller allow list (allow_list = allow_list - block_list ; block_list = None)

  9. added
    critical-fix
    on Nov 18, 2025
  10. added a commit that references this issue on Nov 18, 2025
    4121147
  11. wjones127 commented on Nov 19, 2025

    @wjones127
    ContributorAuthor

    If you're working on this, one other optimization I was thinking of making, is removing the "block list and allow list" case. Anytime we have this case we can simply normalize it into a smaller allow list (allow_list = allow_list - block_list ; block_list = None)

    Sounds good. I'm thinking:

    /// A selection of rows.
    /// 
    /// This is often a result of a filter, where `selected` represents the rows that
    /// passed the filter, and `nulls` represents the rows where the filter evaluated
    /// to null. For example, in SQL `NULL > 5` evaluates to null. This is distinct
    /// from being deselected to support proper three-valued logic for NOT. 
    /// (`NOT FALSE` is TRUE, `NOT TRUE` is FALSE, but `NOT NULL` is NULL.)
    pub struct RowSelection {
        pub selected: RowIdTreeMap,
        pub nulls: RowIdTreeMap,
    }
    
    /// A mask that selects or deselects rows based on an allow-list or block-list.
    pub enum RowIdMask {
        AllowList(RowSelection),
        BlockList(RowSelection),
    }
  12. added 4 commits that reference this issue on Dec 4, 2025
    772e198
    3fceb97
    ada7686
    7344be4
  13. added a commit that references this issue on Dec 10, 2025
    4a9a533
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcritical-fix

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions