surrealdb / surrealdb

A scalable, distributed, collaborative, document-graph database, for the realtime web
https://surrealdb.com
Other
27.07k stars 866 forks source link

Bug: Can't use record::id(id) inside WHERE #4761

Open lveillard opened 2 weeks ago

lveillard commented 2 weeks ago

Describe the bug

Using filters on optional props might throw errors.

Steps to reproduce

CREATE A:1 SET b=(CREATE ONLY B:1); CREATE A:2 SET b=(CREATE ONLY B:2); CREATE A:3;
SELECT * FROM A WHERE record::id(b.id)==1;
->Incorrect arguments for function record::id(). Argument 1 was the wrong type. Expected a record but found NONE```

Expected behaviour

Ideally, this should work

SELECT * FROM AWHERE b.id() = 1

Alternatives

SELECT * FROM A WHERE b.id && record::id(b.id)==1;

SurrealDB version

2.0.0.beta3

Contact Details

No response

Is there an existing issue for this?

Code of Conduct

DelSkayn commented 1 week ago

This isn't really a bug, but is intended behavior. Functions require a certain type and we don't do anything special for when a value doesn't exist. You will have to check if a value exists if it can be none before handing it to a function that requires a value.

lveillard commented 1 week ago

Yes! The bug was a bit different but im not able define it properly. On the other hand, while the type errors make sense on other places (mainly mutations) the where clause could get a dx boost if the errors where just treated as a false and throw the error silently in the logs as a warning.

But i understand it might be non cannonical behavior

lveillard commented 1 week ago

This is the closest I've been to the bug

DEFINE TABLE UserTag SCHEMAFULL PERMISSIONS FULL;
    DEFINE FIELD name ON TABLE UserTag TYPE option<string>;
    DEFINE FIELD color ON TABLE UserTag VALUE <future> {RETURN SELECT VALUE group.color FROM ONLY $this};
    DEFINE FIELD group ON TABLE UserTag TYPE option<record<UserTagGroup>>;

INSERT INTO ⟨UserTag⟩ [
  { id: "tag-1", group: UserTagGroup:`utg-1`},
    { id: "tag-2", group: UserTagGroup:`utg-1` },
    { id: "tag-3", group: UserTagGroup:`utg-2` },
    { id: "tag-4" }
];

INSERT INTO ⟨Color⟩ [
  { id: "yellow", freeForAll: 7 },
  { id: "blue", freeForAll: "hey" },
    { id: "red", freeForAll: "yay" }
];

INSERT INTO ⟨UserTagGroup⟩ [
  { id: "utg-1", color: Color:yellow },
    { id: "utg-2", color: Color:blue }
];

SELECT color FROM UserTag;
SELECT * FROM UserTag WHERE color; // this should filter the one with NONE
SELECT * FROM UserTag WHERE color[WHERE id == Color:yellow ] //this should show only those connected to utg-1

I would expect tge last two queries to work but they don't. They look like two different issues tho.