Skip to main content

Using libraries

Libraries are RAW SQL code that can be shared and used across many queries.

Libraries consist of individual RAW SQL files, hosted in a remote location, which are then imported by a given query.

Types of libraries

Libraries consist of either code declarations or of data declarations (expressions).

For instance:

github://raw-labs/example/temperature_conversion.rql

fahrenheit_to_celsius(t: double) := (t - 32) * 0.5556;

celsius_to_fahrenheit(t: double) := (t * 1.8) + 32;

This library consists of two code declarations: fahrenheit_to_celsius and celsius_to_fahrenheit.

Now assume the following library:

github://raw-labs/example/countries.rql

PRIVATE data := READ("http://example.org/countries");

SELECT * FROM countries

This library consists of a data declaration (a single expression).

Using a library with code declarations

To use a library with code declarations, you must import the individual declarations needed.

For instance:

FROM `github://raw-labs/example/temperature_conversion.rql` IMPORT fahrenheit_to_celsius;

fahrenheit_to_celsius(100)

This imports the declaration fahrenheit_to_celsius, which is then made available for use.

You can import multiple declarations and aliases them (rename them to prevent conflicts with other names using AS) as shown:

For instance:

FROM `github://raw-labs/example/temperature_conversion.rql`
IMPORT fahrenheit_to_celsius AS f_to_c, celsius_to_fahrenheit AS c_to_f;

c_to_f(23)

Using a library with data declarations

To use a library with data declaration, you must import the library.

For instance:

IMPORT `github://raw-labs/example/temperature_conversion.rql` AS list_of_countries;

SELECT * FROM list_of_countries LIMIT 10

Creating a library

To create a library, you simply need to create a file with RAW SQL code, and host it in an accessible location.

An accessible location is any URL that can be read by RAW. These include HTTP locations, GitHub repositories, S3 buckets, Dropbox files, etc. If the file is stored in a secure location, then its access credentials must be registered similarly with other sources of data. To learn which locations are supported, please refer to the I/O guide.