ArcadeData / arcadedb

ArcadeDB Multi-Model Database, one DBMS that supports SQL, Cypher, Gremlin, HTTP/JSON, MongoDB and Redis. ArcadeDB is a conceptual fork of OrientDB, the first Multi-Model DBMS. ArcadeDB supports Vector Embeddings.
https://arcadedb.com
Apache License 2.0
486 stars 60 forks source link

Error in variable scope? #1646

Closed finduspedersen closed 2 months ago

finduspedersen commented 2 months ago

ArcadeDB Version:

24.5.1

OS and JDK Version:

Official Docker image

Expected behavior

The script should always return "List element detected".

Actual behavior

If the seemingly redundant LET statement is removed, the script returns "List is empty". This only happens when the variable is assigned within an IF statement nested in a FOREACH loop.

Please explain scoping rules for ArcadeDB variables within (possibly nested) IF/WHILE/FOREACH statements. Is the intention that all variables should be globally scoped in ArcadeDB? This is not documented in the manual.

Steps to reproduce

LET $list = [];

FOREACH ($i IN [1, 2, 3]) {
    IF ($i = 3) {
        LET $list = ['HELLO'];
    }

    /* Removing this seemingly redundant LET statement changes the result :-( */
    LET $list = $list;
}

IF ($list.size() > 0) {
  RETURN "List element detected";
}

RETURN "List is empty";
lvca commented 2 months ago

@finduspedersen Thanks for the test case, I was able to reproduce the issue. The LET statement should lookup for the context that declared the variable and use that instead of setting the variable on the local context (in this case the one for the IF statement). Working on a fix.

lvca commented 2 months ago

Fixed.