In addition to views and materialized views, RAW has packages, which are a more advanced mechanism to organize queries in RAW.
The raw_magic extension is already loaded. To reload it, use: %reload_ext raw_magic
Packages are used to organize queries together and have them accessible under a single name.
The Jupyter magic
%%package is used to create a package.
Packages can export multiple declarations or functions.
%%package package1 airports := SELECT * FROM READ("https://raw-tutorial.s3.amazonaws.com/airports.csv"); airports_by_country := SELECT Country, * FROM airports GROUP BY Country; airports_in_city(city: string) := SELECT * FROM airports WHERE City = city;
Package "package1" was replaced
To use a package in RQL, use the
from <package name> import <declaration>, <declaration> syntax.
%%rql FROM package1 IMPORT airports_in_city; SELECT * FROM airports_in_city("Lisbon")
|7752||Lisbon Cruise Terminal||Lisbon||Portugal||N||38.712606||-9.122483||0||0.0||E||Europe/Lisbon|
When defining a package, declarations that are marked as private are not exported.
For example in the following package,
a is exported but
b is not.
%%package package2 a(v: int) := v + 1; private b(v: int) := v * 2;
%%rql FROM package2 IMPORT a; a(1)
%%rql FROM package2 IMPORT b; b(1)
b is not declared. Positions: 3:22 to 3:23 3: FROM package2 IMPORT b; ^^
b is not available.
If a package includes an expression, this can also be imported.
For instance, the following package defines one function but also a final expression:
%%package package3 f(v: string) := "Hello " + v; "World"
Package "package3" was replaced
%%rql FROM package3 IMPORT f; IMPORT package3; f(package3)
This is because
IMPORT package3 assigns
package3 identifier to the expression in end of
package3, which is “World”.