3

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

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

They are simply AQL function calls in AQL queries. It does not make any difference whether it is in a user-defined function or not. The namespace of built-in functions is _aql, but does not need to be specified because it is the default namespace. Documented here: docs.arangodb.com/3.2/AQL/Extending/Conventions.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.