13. Packages

In addition to views and materialized views, RAW has packages, which are a more advanced mechanism to organize queries in RAW.

%load_ext raw_magic
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.

For example:

%%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.


FROM package1 IMPORT airports_in_city;

SELECT * FROM airports_in_city("Lisbon")
7752Lisbon Cruise TerminalLisbonPortugalN38.712606-9.12248300.0EEurope/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;

FROM package2 IMPORT a;


FROM package2 IMPORT b;

b is not declared. Positions: 3:22 to 3:23
  3: FROM package2 IMPORT b;

As expected, 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;

Package "package3" was replaced

FROM package3 IMPORT f;
IMPORT package3;

Hello World

This is because IMPORT package3 assigns package3 identifier to the expression in end of package3, which is “World”.