neo4j-contrib / neo4j-apoc-procedures

Awesome Procedures On Cypher for Neo4j - codenamed "apoc"                     If you like it, please ★ above ⇧            

Home Page:https://neo4j.com/labs/apoc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Normalize as boolean deletes boolean properties, too.

michael-simons opened this issue · comments

Expected Behavior (Mandatory)

Normalize to boolean should normalise string properties to boolean, not delete existing boolean properties.

Actual Behavior (Mandatory)

When a property on a node is already boolean it will be deleted by the normalisation.

How to Reproduce the Problem

Simple Dataset (where it's possibile)

CREATE (:Person {prop: 'Y', name:'A'}),
       (:Person {prop: 'Yes', name:'B'}),
       (:Person {prop: 'NO', name:'C'}),
       (:Person {prop: 'X', name:'D'}),
       (:Person {prop: false, name:'Valid'});

Steps (Mandatory)

  1. Check 5 people with a property prop and a bunch of values: match (n:Person) where n.prop is not null return n.name, n.prop;
  2. Apply transformation:
MATCH (n)
CALL apoc.refactor.normalizeAsBoolean(n,'prop',['Y','Yes'],['NO'])
WITH n
ORDER BY n.id
RETURN n.prop AS prop
  1. Property is gone on the person named Valid: match (n:Person) where n.prop is not null return n.name, n.prop;

While the labs page say "The other properties that don’t match these possibilities will be set as null." I find this behaviour at least surprising for things that are already boolean. And not in a good way surprising.

@michael-simons I agree this sounds odd, we will investigate

@michael-simons After looking into the code, it seems like this is actually intentional even though it is confusing. What you need to do is

MATCH (n)
CALL apoc.refactor.normalizeAsBoolean(n,'prop',['Y','Yes',true],['NO',false])
WITH n
ORDER BY n.id
RETURN n.prop AS prop

Since there can be people relying on the old behaviour, we decided not to change it, but we will update the documentation to explicitly mention that you need to include true and false in the parameters if you want to keep them