Created
April 9, 2023 23:31
-
-
Save carlineng/54f66e73a6e8b9a8f32dcab6d1b4b05d to your computer and use it in GitHub Desktop.
Explaining implicit context in queries in Sources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "model.malloy" | |
/* | |
The file `model.malloy` (imported above) is defined as follows: | |
source: lineitem is table('duckdb:../data/lineitem.parquet') { | |
join_one: | |
orders is table('duckdb:../data/orders.parquet') on orders.o_orderkey = l_orderkey | |
customer is table('duckdb:../data/customer.parquet') on customer.c_custkey = orders.o_custkey | |
measure : | |
_count is count() | |
count_customers is count( distinct customer.c_custkey) | |
query: customers_bought_something is { | |
aggregate: count_customers | |
} | |
} | |
*/ | |
// "customer_bought_something" is a query defined explicitly on the `lineitem` source | |
// It can only be executed in the context of `lineitem`, as follows: | |
query: lineitem -> customers_bought_something | |
// The following syntax throws a compilation error, | |
// since the query is only defined within `lineitem` | |
query: -> customers_bought_something | |
query: customers_bought_something | |
// In Malloy a Source object can contain a `query` as a property | |
// When you click the "Run" codelens button on the `query` inside a Source, | |
// e.g., the `customers_bought_something` query in the above `lineitem` Source, | |
// the Malloy runtime is implicitly translating that to "lineitem -> customers_bought_something" | |
// so even though it appears in VSCode that you're only referencing a measure from the `Customers` table, | |
// the implicit context is still the `lineitem` Source. | |
// In practice, when running Malloy queries, you "import" the model with the joins and measures defined | |
// then construct queries off of those sources. | |
// The "Run" codelens on queries inside of Sources is mostly there for convenience during development. | |
// When executing a Malloy script. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment