Import Statements
Import Statements
Import statements bring concepts and definitions from other Trilogy files into the current scope.
Syntax
import_statement: "import" (".")* IDENTIFIER ("." IDENTIFIER)* ("as" IDENTIFIER)?
selective_import_statement: "from" import_path ("as" IDENTIFIER)? "import" IDENTIFIER ("," IDENTIFIER)*
self_import_statement: "self" "import" "as" IDENTIFIER
Basic Import
Import a module by name:
import customer;
Without as, definitions enter the current namespace (for example, id and name from customer.preql). Use an alias to give them an explicit prefix.
Relative Imports
Use dots to import from relative paths:
import .local_module;
import ..parent_module;
Nested Module Import
Import from nested module paths:
import models.customer;
import data.warehouse.sales;
Aliased Import
Use as to give the import a local alias. All concepts from the imported module will be namespaced under this alias:
import customer as c;
import models.order as ord;
After aliasing, reference concepts like c.id or ord.total.
Selective Imports
Limit the public concepts brought into scope:
from customer as c import id, name;
select c.id, c.name;
Other concepts from the module remain hidden, while dependencies needed for lineage resolution and planning are retained internally.
Self Imports
Use self import as manager; inside a model to reuse that same model in a second role. Save the following as employee.preql:
self import as manager;
key id int;
property id.name string;
datasource employees (
employee_id: id,
employee_name: name,
manager_id: ~?manager.id,
)
grain (id)
address employees;
Then query both roles from a separate script:
import employee as employee;
where employee.id is not null
select employee.id, employee.name, employee.manager.name;
~?manager.id declares a partial, nullable binding into the manager role. The filter keeps rows from the employee population, including employees with no manager, and excludes rows contributed only by the manager side. The physical source may be used more than once in the generated SQL, with the model supplying the relationship. Self imports require a file context; an anonymous Environment.parse(...) string has no file to import. In the SDK, import the saved file from an environment with the appropriate working_path.
Example
import customer as customer;
import order as order;
import std.money;
select
order.customer.id,
order.customer.name,
sum(order.total) -> customer_total
;
This example assumes the order model imports and connects its customer model. Use that connected path, or add a query-scoped join when combining independent imports.