QueryScope

Variables in your formula are either taken from columns or the QueryScope, which dictates what variables and methods are accessible within a query. Furthermore, using variables from the QueryScope can reduce execution time. Changing QueryScope variables does not require query strings to be recompiled, while changing literal values in query strings may require the query string to be recompiled.

Consider the following query:

t2=t1.update("X=f.call(A,b)")

X and A are columns, f is a function, and b is some variable. The "X=f.call(A,b)" string is called a "query string".

This query string gets dynamically converted into code, which is compiled to improve execution speed while having an expressive query language. It is obvious that the columns come from the table. It is less obvious where the variable b comes from. These variables come from the "query scope".

Python

If you are at the top level of a Python program, the variables are automatically added to the scope. For example, the following query will work:

If you are in a function, the variables are not automatically added to the binding, so this query will not work:

The variables can be explicitly added to the binding as in this example:

img

Note

The QueryScope is the scope for the query; it does not map to the scope of the programming language. As a result, if you add an item within a code block, the item is still in the query scope after the code block returns (in other words, the variable is still in the query scope after the function that sets it returns).

If you later call a function using the variable a, you could get bad results:

This query will execute because a is already in the query scope from an earlier call to compute(). However, the a set by compute may be totally different than the argument to computeBadScope(), which will produce unintended results.

Groovy

Variables that are in the global Groovy binding are automatically added to the QueryScope, as in the following query:

If you are in a function, the variables are not automatically added to the binding, so this query will not work:

The variables can be explicitly added to the binding as in this example:

img

Note

The QueryScope is the scope for the query; it does not map to the scope of the programming language. As a result, if you add an item within a code block, the item is still in the query scope after the code block returns (in other words, the variable is still in the query scope after the function that sets it returns). For instance, if you later call a function using the variable a, you could get bad results.

Variables defined without "def" inside closures are added to the global Groovy binding, so they are automatically available in the QueryScope:

However, Groovy variables defined with "def" are not part of the global Groovy binding, so they are not available, unless they are added to the QueryScope.

So the following query does not work:

The following query does work:

Groovy variables defined with a type are also not part of the global Groovy binding, so they are not available, unless they are added to the QueryScope.

So the following query does not work:

The following query does work: