Skipping nodes with type mismatch for WHERE clause

I just recently I came across an issue where I haven’t found a solution how to handle the error.

The actual use case/node schema is more complicated, but to make it simple let’s say I have a bunch of nodes :Document with a property title. title of course is always a string and searching for all nodes having 'foo' in the title I’d query with MATCH (d:Document) WHERE d.title CONTAINS 'foo' RETURN d. Everything fine so far.

Now if for any reason a new node gets inserted with title being an object/map or array/list the whole query fails. It’s not just skipping this node and returns the rest as before but just no result with the error “Query failed: ‘contains’ argument at position 1 must be either ‘null’ or ‘string’.”.

Is it possible to somehow handle that? Like in skipping the node that has a wrong type on the property or add something to the WHERE to skip that node explicitly? I couldn’t find any function to do something like type(title) = 'string'. That also means I’m having a hard time finding the nodes that are causing issues, because I can’t match them with a query to quickly fix them.

2 Likes

@nedt Thanks for the very clear description of your issue.

Currently, there isn’t a simple solution to your issue.
The openCypher type function that you mentioned is reserved to return the edge type of the supplied edge so it can’t be used to determine the type of the supplied property.

Currently, the only thing that I can suggest you do to try to find out which are the troublesome nodes is to execute a query like the following: MATCH (d:Document) WHERE size(d.title) < 5 RETURN d.title, id(d). The size function is currently the only function that works both on string and map arguments. You can use the parameter 5 to try to filter out the nodes that have maps for the parameter from those that have strings. Of course, I’m assuming that the strings that are stored in the properties are mostly longer than 5 and that the maps have less than 5 keys in them. You can then manually examine the reduced number of nodes and filter the nodes that have map values.

In the next version of Memgraph we will add a new function to our Cypher implementation that will allow you to do a WHERE valueType(d.title) = "STRING".

The CONTAINS function will still raise an exception when called with a map argument, but you will be able to filter out the troublesome nodes using the new Cypher function.

I hope that this was helpful.

1 Like

@matt Thanks a lot. That really helped.
We will for now check the existing nodes. At least I didn’t fail to find an existing option.
With the valueType() function we can then also easily add a constraint. If not that’s a feature request :wink: