Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use AQL Variables e.g. for counting (LET sum = sum + 1)

Tags:

arangodb

According to https://www.arangodb.com/2014/07/13/arangodb-2-2-0-released it shall be possible to use statements like this:

LET sum = 0
FOR v IN values
  SORT v.year
  LET sum = sum + v.value
  RETURN { year: v.year, value: v.value, sum: sum }

I currently use version 2.4 but am not able to use it, e.g. in such a statement:

LET sum = 0
FOR i in memoryColl
    LET sum = sum + 1
    // sum = sum + 1
    RETURN { "i": i, "sum": sum }

I got the error [1511] variable 'sum' is assigned multiple times (while parsing)

Can somebody tell me if such a statemtn should in principle work, and how exactly?

like image 924
augustin-s Avatar asked Nov 01 '22 11:11

augustin-s


2 Answers

As explained in the upgrading docs for 2.3, it's no longer possible to update variables in queries:

Previous versions of ArangoDB allowed the modification of variables inside AQL queries [...]

While this is admittedly a convenient feature, the new query optimizer design did not allow to keep it.

Additionally, updating variables inside a query would prevent a lot of optimizations to queries that we would like the optimizer to make. Additionally, updating variables in queries that run on different nodes in a cluster would like cause non-deterministic behavior because queries are not executed linearly.

To enumerate documents, you could do

LET range = 0..LENGTH(memoryColl)-1

FOR i IN range
    RETURN {i: i+1, doc: memoryColl[i]}

but it looks like a really bad idea to me. Better return the documents and let the client enumerate them.

If you actually want to count the number of documents, you may use a sub-query:

LET result = (
    FOR doc IN memoryColl
        FILTER True // add some condition here for instance
        RETURN doc
)

RETURN LENGTH(result)

In 2.4, it is also possible to count more efficiently:
http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/

like image 135
CodeManX Avatar answered Dec 07 '22 03:12

CodeManX


On arango 3.7 in 2020 you could do something like described here

LET values = [
    { year: 2019, value: 35 },
    { year: 2017, value: 8 },
    { year: 2018, value: 17 },
    { year: 2020, value: 84 }
]

LET sortedValues = (FOR v IN values SORT v.year RETURN v)
FOR i IN 0..LENGTH(sortedValues)-1
    LET v = sortedValues[i]
    LET sum = sortedValues[i].value + SUM(SLICE(sortedValues, 0, i)[*].value)
    RETURN {year:v.year,value:v.value,sum:sum}

This returned

[
  {
    "year": 2017,
    "value": 8,
    "sum": 8
  },
  {
    "year": 2018,
    "value": 17,
    "sum": 25
  },
  {
    "year": 2019,
    "value": 35,
    "sum": 60
  },
  {
    "year": 2020,
    "value": 84,
    "sum": 144
  }
]
like image 29
raisercostin Avatar answered Dec 07 '22 01:12

raisercostin