Skip to content

Condition::default() creates an invalid condition that can't be used #205

Closed

Description

@daniel-abramov

The conditions created by Condition::default() are rejected by Qdrant backend with a "Malformed Condition type" error.

Activity

  1. timvisee commented on Dec 23, 2024

    @timvisee
    Member

    Thanks!

    I think we should remove the Default implementation on Condition to prevent misuse of this with provided functions.

    I'm sure you are aware, but in the mean time you can one of the other functions to create the desired condition.

  2. timvisee commented on Dec 23, 2024

    @timvisee
    Member

    After quick inspection - I'm afraid we cannot remove the Default implementation at this time because gRPC framework we use puts it there for us.

    It is implied by ::post::Message:

    #[derive(Clone, PartialEq, ::prost::Message)]

  3. daniel-abramov commented on Dec 23, 2024

    @daniel-abramov
    Author

    I'm sure you are aware, but in the mean time you can one of the other functions to create the desired condition.

    Sure. In my case, the way it occurred is that I had a function that received a structure with optional fields, which I then converted into conditions that were passed to the Filter, e.g.:

    let city_filter = data.location.map(|loc| Condition::matches("city", loc)).unwrap_or_default();
    let radius_filter = data.radius.map(|r| Condition::geo_radius("radius", r.into())).unwrap_or_default();
    let filter = Filter::all(vec![city_filter, radius_filter]);

    So it was a compact, elegant implementation that I assumed would work (typically, if there is a Default implementation, it produces a structure that is valid and can be used), but later I discovered that the function fails in some cases and traced it back to that place in Qdrant that rejects None conditions.

    After quick inspection - I'm afraid we cannot remove the Default implementation at this time because gRPC framework we use puts it there for us.

    There are still several ways to mitigate it. For instance, instead of exposing the low-level "over-the-wire" representation of the Condition, one could expose a convenient high-level type to the user and only convert it to the low-level representations inside a library. This ensures that the client only interacts with a "safe" high-level API that prevents errors.

    Alternatively, there is a "lazy solution", such as making the functions that accept Condition, such as Filter::all or other, to return an error if they receive an invalid condition. It would still be more efficient than letting the user to send an invalid request to the server, only to discover that it's not valid and gets rejected, i.e. we could spare that additional round-trip and return error immediately when we know upfront that the request is invalid and won't be processed anyway.

    However, a better design would simply prevent creating "invalid" structures to avoid bugs and issues 🙂 And to be honest, the Condition type is almost perfect already - it provides lots of useful high-level functions, that's why I think that it just needs to either not have Default, or make Default return something that is valid. If its internal fields could be misused, they should also be made private. I.e. I think there is a simple way to fix the issue by just wrapping the internal 'over-the-wire' Condition into a newtype that prevents such things from happening.

  4. timvisee commented on Dec 23, 2024

    @timvisee
    Member

    Yes, I totally agree. I understand that it's easy to construct this 'broken' variant the way you describe. Thanks for your suggestions.

    I implemented a quick fix to filter out empty conditions, resolving this issue: qdrant/qdrant#5690

  5. timvisee commented on Jan 8, 2025

    @timvisee
    Member

    We've just released Qdrant 1.12.6 which includes the patch for this problem. Thank again for bringing this to our attention!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions