cancel
Showing results for 
Search instead for 
Did you mean: 

User Defined Function Data Type incompatability

genealogy
Graph Buddy

I'm trying to create a user defined procedure and it compiles okay but Neo4j fails to re-start after introducing the jar file. I'm trying to convert a string of 0's and 1's representing a base 2 number to a base 10 number which is returned as a long integer. I don't understand how to get the long output required by Neo4j. The error message in the log file is:

Caused by: org.neo4j.internal.kernel.api.exceptions.ProcedureException: Argument radix at position 1 in baseConvert with type Integer cannot be converted to a Neo4j type: Don't know how to map java.lang.Integer to the Neo4j Type System.

The code is:

package genealogy;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;

/**
 * This is an example of a simple user-defined function for Neo4j to perform a baseConvert
 */
public class base2to10 {

    @UserFunction
    @Description("example.baseConvert('1101', 2) - Convert the provided base 2 string to a base 10 integer.")
    public Integer baseConvert(
            @Name("s") 
                String s,
            @Name(value = "radix") 
                Integer radix
            ) 
    {
        if (s == null || radix == null) {
            return null;
        }
        return Integer.parseInt(s, radix);
    }
}
2 REPLIES 2

mdfrenchman
Graph Voyager

Need to use Long in place of Integer.

I ran into the same thing yesterday evening and was eventually able to resolve by changing the type. The documentation does include a type mapping entry, but it wasn't immediately obvious in the udf section.

Values and types - Neo4j Java Reference

Mike,

Thanks! So others can see the full final solutions ...

package genealogy;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;

/**
 * This is an example of a simple user-defined function for Neo4j to perform a baseConvert
 */
public class base2to10 {

    @UserFunction
    @Description("example.baseConvert('1101', 2) - Convert the provided base 2 string to a base 10 integer.")
    public Long baseConvert(
            @Name("s") 
                String s
            
            ) 
    {
       
        return Long.parseLong(s,  2);
    }
}

I'm only working with base 2 to produce ahnentafel numbers ....

Then

return genealogy.baseConvert('111101')

produces
61

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.