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.
- return count(file:read-text-lines($file, "UTF-8", false()))
Here, file processing will be iterative.
- 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.