Malloy Documentation
search

In order to reuse or extend a source from another file, you can include all the exported sources from another file using import "path/to/some/file.malloy".

For example, if you wanted to create a file flights_by_carrier.malloy with a query from the flights source, you could write:

document
import "flights.malloy"

run: flights -> { limit: 5; group_by: carrier; aggregate: flight_count }
QUERY RESULTS
carrierflight_​count
WN88,751
US37,683
AA34,577
NW33,580
UA32,757
[
  {
    "carrier": "WN",
    "flight_count": 88751
  },
  {
    "carrier": "US",
    "flight_count": 37683
  },
  {
    "carrier": "AA",
    "flight_count": 34577
  },
  {
    "carrier": "NW",
    "flight_count": 33580
  },
  {
    "carrier": "UA",
    "flight_count": 32757
  }
]
SELECT 
   flights."carrier" as "carrier",
   COUNT( 1) as "flight_count"
FROM '../data/flights.parquet' as flights
GROUP BY 1
ORDER BY 2 desc NULLS LAST
LIMIT 5

Import Locations

Imported files may be specified with relative or absolute URLs.

Import Statement Meaning from "file:///f1/a.malloy"
import "b.malloy" "file:///f1/b.malloy"
import "./c.malloy" "file:///f1/c.malloy"
import "/f1/d.malloy" "file:///f1/d.malloy"
import "file:///f1/e.malloy" "file:///f1/e.malloy"
import "../f2/f.malloy" "file:///f2/f.malloy"
import "https://example.com/g.malloy" "https://example.com/g.malloy"

Selective Imports

The default is to import all objects from the referenced file. You can also use {} from to select (and optionally rename) specific objects to be imported.

document
import { airports, spaceports is airports } from "airports.malloy"

run: airports -> { aggregate: airport_count is count() }
run: spaceports -> { aggregate: spaceport_count is count() }
QUERY RESULTS
spaceport_​count
19,793
[
  {
    "spaceport_count": 19793
  }
]
SELECT 
   COUNT( 1) as "spaceport_count"
FROM '../data/airports.parquet' as spaceports