Mathias C
Mathias C

Reputation: 125

How to use AQL functions inside user defined functions?

Is it possible to use standard AQL functions inside a user-defined function ?

I tried to call IS_IN_POLYGON() inside a custom function and got this error at execution time :

Query: AQL: in function 'GEO::IS_IN_MULTIPOLYGON()': user function runtime error: ReferenceError: IS_IN_POLYGON is not defined at (…)

Is there any prefix / require() / anything, that should be used to access standard AQL functions ?

ArangoDB version : 3.2.4

Engine : RocksDB

Upvotes: 3

Views: 695

Answers (2)

peak
peak

Reputation: 116650

Yes, one can use AQL functions, including other UDFs, inside UDF functions.

Here is a complete example, using the AQL function LENGTH():

aqlfunctions.register('TEST::test', function(collection) {
    'use strict';
    const db = require('@arangodb').db;
    const AQL_FUNCTION = db._query;
    return (typeof collection == "string") 
      ? AQL_QUERY('RETURN LENGTH(' + collection + ')').toArray()[0]
      : return typeof collection;
}, false);

To use a UDF function, simply include its namespace (e.g. ARRAY::PRODUCT) in the way you'd use in an AQL query.

Caveat: UDFs must be side-effect free. They are not supposed to alter the database state in any way.

The ArangoDB documentation is not entirely clear what complications may arise if the return value of a UDF depends in any way on the database state (as in the above example).

Upvotes: 2

Mathias C
Mathias C

Reputation: 125

Answering my own question here.

One can use AQL functions inside a user defined function this way : (example for fictitious SOME_AQL_FUNCTION() returning a boolean)

let result = AQL_QUERY("RETURN SOME_AQL_FUNCTION(…)").json[0];

Found that reading some test code on ArangoDB's GitHub.

Upvotes: 0

Related Questions