Name resolving

    In PRQL’s compiler, a scope is the collection of all names one can reference from a specific point in the program.

    In PRQL, names in the scope are composed from namespace and variable name which are separated by a dot, similar to SQL. Namespaces can contain many dots, but variable names cannot.

    Example

    Name is a variable some_column from namespace my_table.

    When processing a query, a scope is maintained and updated for each point in the query.

    It start with only namespace , which is the standard library. It contains common functions like sum or count, along with all transform functions such as derive and group.

    In pipelines (or rather in transform functions), scope is also injected with namespaces of tables which may have been referenced with from or transforms. These namespaces contain simply all the columns of the table and possibly a wildcard variable, which matches any variable (see the algorithm below). Within transforms, there is also a special namespace that does not have a name. It is called a “frame” and it contains columns of the current table the transform is operating on.

    For each ident we want to resolve, we search the scope’s items in order. One of three things can happen:

    • Otherwise, the nothing is matched and an error is raised.

    When translating into an SQL statement which references only one table, there is no need to reference column names with table prefix.

    But when there are multiple tables and we don’t have complete knowledge of all table columns, a column without a prefix (i.e. first_name) may actually reside in multiple tables. Because of this, we have to use table prefixes for all column names.