Here's the mechanism for putting part of the query into a separate attached file:
Export
EXPORT $my_symbol1, $my_symbol2, ...;
lists the names of named expressions in the library that are available for import.Import
IMPORT my_library SYMBOLS $my_symbol1, $my_symbol2, ...;
makes the listed named expressions available for further use.{% note info %}
You can use the library to include lambdas, actions, named subqueries, constants and expressions, but not subqueries or aggregate functions.
{% endnote %}
{% note warning %}
The file linked by the PRAGMA Library must be attached to the query. You can't use a PRAGMA File for this purpose.
{% endnote %}
my_lib.sql:
$Square = ($x) -> { RETURN $x * $x; };
$Sqrt = ($x) -> { RETURN Math::Sqrt($x); };
-- Aggregate functions created by
-- AggregationFactory, it makes sense to add it to the library
$Agg_sum = AggregationFactory("SUM");
$Agg_max = AggregationFactory("MAX");
EXPORT $Square, $Sqrt, $Agg_sum, $Agg_max;
Query:
PRAGMA Library("my_lib.sql");
IMPORT my_lib SYMBOLS $Square, $Sqrt, $Agg_sum, $Agg_max;
SELECT
$Square(2), -- 4
$Sqrt(4); -- 2
SELECT
AGGREGATE_BY(x, $Agg_sum), -- 5
AGGREGATE_BY(x, $Agg_max) -- 3
FROM (
SELECT 2 AS x
UNION ALL
SELECT 3 AS x
)