13. Packages

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

[3]:
%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:

[9]:
%%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.

[7]:
%%rql

FROM package1 IMPORT airports_in_city;

SELECT * FROM airports_in_city("Lisbon")
[7]:
AirportIDNameCityCountryIATA_FAAICAOLatitudeLongitudeAltitudeTimezoneDSTTZ
1638LisboaLisbonPortugalLISLPPT38.781311-9.1359193740.0EEurope/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.

[10]:
%%package package2

a(v: int) := v + 1;

private b(v: int) := v * 2;
[11]:
%%rql

FROM package2 IMPORT a;

a(1)
[11]:
int
2
[12]:
%%rql

FROM package2 IMPORT b;

b(1)
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:

[15]:
%%package package3

f(v: string) := "Hello " + v;

"World"
Package "package3" was replaced
[16]:
%%rql

FROM package3 IMPORT f;
IMPORT package3;

f(package3)
[16]:
string
Hello World

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