Search code examples
orientdb

OrientDB Register Custom Function for Aggregation Java


As written at the bottom in this page http://orientdb.com/docs/2.0/orientdb.wiki/SQL-Functions.html , we can build our custom function in OrientDB. However, this example just shows a custom function to perform an inline operation. Is there any way to write an OrientDB function that performs an aggregation operation? Something like sum(fieldName) . So the question is more towards how we can access the 'current' record value for that field inside our custom function so that we can perform an aggregation operation.


Solution

  • I suggest you to take a look at this: OSQLFunctionSum

    The code is quite simple, but the important concepts are following:

    1. aggregateResults() has to return true to let OrientDB know that it's an aggregate function
    2. OrientDB will aggregate records for you and will return a an instance of your function for every aggregation.
    3. you have to keep aggregate data in your function instance (see private Number sum; in the example)
    4. execute() method is invoked for every record, during query
    5. getResult() must return the aggregate result