Thanks Christian, I will check the code examples you posted tonight, your explanation makes it easier to understand.

I can see there is a list with the deterministic functions in the specs [1] but not so sure about the BaseX specific functions. Is it possible to know if a function is deterministic or not?

I tried file:read-text-lines("/path.txt")  is file:read-text-lines("/path.txt") but it doesn't work.

George.

[1] - https://www.w3.org/TR/xpath-functions-31/#dt-deterministic

On 1/16/19 1:41 PM, Christian Grün wrote:
The reason for that: file:read-text-lines is a non-deterministic
function. Each invocation might yield different results (as the file
contents may change in the background). This is different with
non-deterministic function calls, such as fn:doc('abc.xml'). If you
call such a function repreatedly, it will always access the same
document, which has been opened and parsed by the first call of this
function.

1) return count(file:read-text-lines($file, "UTF-8", false()))
Here, file processing will be iterative.

2) let $data := file:read-text-lines($file, "UTF-8", false())
     return count($data)
The file contents will be bound to $data, and counted in a second
step. If the expression of your let clause was deterministic, the
variable would be inlined, and the resulting query plan would be
identical to the one of your first query.