diff --git a/duckdb-wasm/airports-queries.malloy b/duckdb-wasm/airports-queries.malloy index bd28f721..294b8eb7 100644 --- a/duckdb-wasm/airports-queries.malloy +++ b/duckdb-wasm/airports-queries.malloy @@ -4,42 +4,42 @@ import "./airports.malloy" // Name: Show all Data // The Equivqalent of a SELECT * in SQL. -query: airports -> { - project: * +run: airports -> { + select: * } // -- // Name: Filtering Data // New York City District Airports -query: airports -> { +run: airports -> { where: faa_dist = 'NYC' - project: * + select: * } // -- // Name: Named query // Runs the query declared in the model below -query: airports->by_state +run: airports->by_state // -- // Name: Named query with a filter applied to the source // Filters query to major airports -query: airports {where: major='Y'}->by_state +run: airports extend {where: major='Y'}->by_state // -- // Name: Aggregates: Simple Query with Aggregates // Calculations can be written into the query -query: airports->{ +run: airports->{ group_by: state aggregate: - airport_count is count(*) + airport_count is count() avg_elevation is elevation.avg() - percentage_of_all_airports_in_usa is count(*)/all(count(*))*100 + percentage_of_all_airports_in_usa is count()/all(count())*100 order_by: avg_elevation desc } @@ -49,7 +49,7 @@ query: airports->{ // Calculations can come from the model. In this case all the // calculations are from the model below. -query: airports->{ +run: airports->{ group_by: state aggregate: airport_count @@ -60,14 +60,14 @@ query: airports->{ // -- // Name: Filtered aggregate expressions -query: airports->{ +run: airports->{ group_by: state aggregate: airport_count gliderport_count is airport_count {where: fac_type = 'GLIDERPORT'} avg_heliport_elevation is elevation.avg() {where: fac_type = 'HELIPORT'} - percent_major is airport_count {? major = 'Y'}/airport_count * 100 + percent_major is airport_count {where: major = 'Y'}/airport_count * 100 } // -- @@ -75,7 +75,7 @@ query: airports->{ // Queries can be nested in one another. The outer query essentially // becomes the filter for the nested query. -query: airports->{ +run: airports->{ group_by: faa_region aggregate: airport_count @@ -100,7 +100,7 @@ query: airports->{ // Modeled queries can be changed and expanded. In this case // we expand the reult limit to 50 and add a couple of measures -query: airports->by_state + { +run: airports->by_state + { limit: 50 aggregate: avg_elevation @@ -111,22 +111,22 @@ query: airports->by_state + { // Name: Named nested queries and refinements // We can simply nest one query in another by refining it. -query: airports-> by_state + { +run: airports-> by_state + { nest: by_facility_type } // -- // Name: Rendering results // Changing the name of a query can change how it is rendered. -query: airports-> by_state + { +run: airports-> by_state + { nest: fac_type_bar_chart is by_facility_type } // -- // Name: Filtering nested Queries // You can apply filters to nested queries -query: airports-> by_state + { - nest: fac_type_bar_chart is by_facility_type + {? fac_type = 'AIRPORT' | 'HELIPORT'} +run: airports-> by_state + { + nest: fac_type_bar_chart is by_facility_type + {where: fac_type = 'AIRPORT' | 'HELIPORT'} } // -- @@ -134,7 +134,7 @@ query: airports-> by_state + { // You can nest things by name. The name of the column // determines how it is rendered (in this case, shape_map) -query: airports -> { +run: airports -> { group_by: faa_region aggregate: airport_count nest: @@ -146,7 +146,7 @@ query: airports -> { // Name: Mapping Data - Strings // We often want Map values to other value. Malloy's 'pick' statement // is powerful and readable. You only have to mention the source once -query: airports -> { +run: airports -> { group_by: faa_region_name is faa_region ? pick 'Southwest' when 'ASW' pick 'Northwest Mountain' when 'ANM' @@ -165,7 +165,7 @@ query: airports -> { // -- // Name: Mapping Data - Multiple Values // We can map multiple values to a single value -query: airports -> { +run: airports -> { group_by: east_west is faa_region ? pick 'West' when 'ASW' | 'ANM' | 'AWP' | 'AAL' pick 'Central' when 'AGL' | 'ACE' | 'ANM' @@ -178,7 +178,7 @@ query: airports -> { // -- // Name: Mapping Data - numbers // We can map numeric ranges to strings. -query: airports -> { +run: airports -> { group_by: elevation_string is elevation ? pick 'low' when < 300 pick 'medium' when < 900 @@ -193,7 +193,7 @@ query: airports -> { // function. A takes an aggregate calculation (modelled or expplicit) // and runs without dimensional grouping. -query: airports-> { +run: airports-> { group_by: fac_type state @@ -210,7 +210,7 @@ query: airports-> { // -- // Name: Pipelines Step 1 // Let's start with this query -query: airports-> { +run: airports-> { group_by: state nest: by_county is { group_by: county @@ -223,7 +223,7 @@ query: airports-> { // Name: Pipelines Step 2 // Find the top two counties in each state. // See the previous query to run the first part -query: airports-> { +run: airports-> { group_by: state nest: by_county is { group_by: county @@ -232,7 +232,7 @@ query: airports-> { } } -> { - project: state, by_county.county, by_county.airport_count + select: state, by_county.county, by_county.airport_count order_by: state, county } @@ -242,10 +242,10 @@ query: airports-> { // add a filter so the source only contain major airports // the '+' means add parameters // -source: major_airports is airports + { +source: major_airports is airports extend { where: major = 'Y' measure: - count_with_control_tower is airport_count {? cntl_twr = 'Y'} + count_with_control_tower is airport_count {where: cntl_twr = 'Y'} dimension: faa_region_name is faa_region ? pick 'Southwest' when 'ASW' pick 'Northwest Mountain' when 'ANM' @@ -259,7 +259,7 @@ source: major_airports is airports + { } // Look at major airports by district and state -query: major_airports-> { +run: major_airports-> { group_by: faa_region_name aggregate: airport_count, count_with_control_tower nest: by_state is { diff --git a/duckdb-wasm/airports.malloy b/duckdb-wasm/airports.malloy index daba3ff4..12dadbb9 100644 --- a/duckdb-wasm/airports.malloy +++ b/duckdb-wasm/airports.malloy @@ -1,18 +1,18 @@ -source: airports is table('duckdb:airports.parquet') { +source: airports is duckdb.table('airports.parquet') extend { measure: airport_count is count() percent_of_all_airports is airport_count/all(airport_count)*100 avg_elevation is elevation.avg() - heliport_count is airport_count {? fac_type = 'HELIPORT'} + heliport_count is airport_count { where: fac_type = 'HELIPORT' } - query: by_state is { + view: by_state is { where: state != null group_by: state aggregate: airport_count } - query: by_facility_type is { + view: by_facility_type is { group_by: fac_type aggregate: airport_count diff --git a/duckdb-wasm/auto_recalls-queries.malloy b/duckdb-wasm/auto_recalls-queries.malloy index c2444cf5..4fcdcab8 100644 --- a/duckdb-wasm/auto_recalls-queries.malloy +++ b/duckdb-wasm/auto_recalls-queries.malloy @@ -2,17 +2,17 @@ import "./auto_recalls.malloy" // -- // Name: Recent Honda Recalls -query: recalls { +run: recalls + { where: Manufacturer ~ r'Honda' } -> recent_recalls // -- // Name: Overall Recall Dashboard -query: recalls->recall_dashboard +run: recalls->recall_dashboard // -- // Name: Brake Recall Dashboard -query: recalls { +run: recalls + { where:`Recall Description` ~r'brake' } -> recall_dashboard @@ -21,7 +21,7 @@ query: recalls { // Name: Manufacturer year // If a query name ends in _line_chart is is rendered as one -query: recalls-> { +run: recalls-> { where: Manufacturer ~ r'General Motors|Ford Motor|Chry.*US' nest: _line_chart is { group_by: recall_year is year(`Report Received Date`) @@ -34,14 +34,14 @@ query: recalls-> { // -- // Name: By Component -query: recalls-> { +run: recalls-> { group_by: Component aggregate: recall_count, percent_of_recalls } // -- // Name: Component Dashboard -query: recalls-> { +run: recalls-> { group_by: Component aggregate: recall_count, percent_of_recalls nest: by_year_line_chart is by_year diff --git a/duckdb-wasm/auto_recalls.malloy b/duckdb-wasm/auto_recalls.malloy index 19da9249..5c84d14e 100644 --- a/duckdb-wasm/auto_recalls.malloy +++ b/duckdb-wasm/auto_recalls.malloy @@ -1,35 +1,36 @@ // Auto Recalls: CSV example -source: recalls is table('duckdb:auto_recalls.csv') { - declare: +source: recalls is duckdb.table('auto_recalls.csv') extend { + measure: recall_count is count() percent_of_recalls is recall_count/all(recall_count)*100 + dimension: recall_url is concat( 'https://www.nhtsa.gov/recalls?ntsaid=', `NHTSA ID` ) - query: by_manufacturer is { + view: by_manufacturer is { group_by: `Manufacturer` aggregate: recall_count percent_of_recalls } - query: by_type is { + view: by_type is { group_by: `Recall Type` aggregate: recall_count percent_of_recalls } - query: by_year is { + view: by_year is { group_by: recall_year is year(`Report Received Date`) aggregate: recall_count order_by: recall_year } - query: recent_recalls is { + view: recent_recalls is { group_by: recall_date is `Report Received Date`::string `NHTSA ID` @@ -39,7 +40,7 @@ source: recalls is table('duckdb:auto_recalls.csv') { order_by: 1 desc limit: 10 } - query: recall_dashboard is by_manufacturer + { + view: recall_dashboard is by_manufacturer + { nest: by_year_line_chart is by_year + {group_by: `Recall Type`} nest: by_type nest: recent_recalls diff --git a/duckdb-wasm/flights-queries.malloy b/duckdb-wasm/flights-queries.malloy index 833fdc0b..90a8896c 100644 --- a/duckdb-wasm/flights-queries.malloy +++ b/duckdb-wasm/flights-queries.malloy @@ -2,47 +2,47 @@ import "./flights.malloy" // -- // Name: Carrier Table -query: carriers -> { - project: * +run: carriers -> { + select: * } // -- // Name: Flights Table -query: flights -> { - project: * +run: flights -> { + select: * } // -- // Name: Named query -query: flights -> by_carrier +run: flights -> by_carrier // -- // Name: Named query with filter: SFO carriers -query: flights { +query: sfo_carriers is flights extend { where: orig.code = 'SFO' } -> by_carrier // -- -// Name: Named Query: By Origin -query: flights -> by_origin +// Name: Named run: By Origin +run: flights -> by_origin // -- -// Name: Named Query: By Origin with Top 3 Carrier Nested -query: flights-> by_origin + { +// Name: Named run: By Origin with Top 3 Carrier Nested +run: flights-> by_origin + { nest: by_carrier + {limit: 3} limit: 30 } // -- -// Name: Named Query: By Carrier with Top 3 Origins Nested -query: flights-> by_carrier + { +// Name: Named run: By Carrier with Top 3 Origins Nested +run: flights-> by_carrier + { nest: by_origin + {limit: 3} limit: 30 } // -- // Name: Simple Barchart: Carriers by Flights -query: flights-> { +run: flights-> { nest: by_carrier_bar_chart is { group_by: carriers.nickname aggregate: flight_count @@ -51,7 +51,7 @@ query: flights-> { // -- // Name: Barchart with two measures: Carriers by Flights -query: flights-> { +run: flights-> { group_by: orig.faa_region aggregate: flight_count nest: by_carrier_bar_chart is { @@ -64,7 +64,7 @@ query: flights-> { // -- // Name: Barchart with two dimensions: Carriers by Flights and Distance -query: flights-> { +run: flights-> { nest: by_carrier_bar_chart is { group_by: carriers.nickname aggregate: flight_count @@ -74,7 +74,7 @@ query: flights-> { // -- // Name: Line Chart Simple: flights by month -query: flights-> { +run: flights-> { group_by: carriers.nickname nest: by_month_line_chart is { group_by: dep_qtr is dep_time.quarter @@ -84,7 +84,7 @@ query: flights-> { // -- // Name: Line Chart with two dimension: Flights by Month and Length -query: flights-> { +run: flights-> { group_by: carriers.nickname nest: by_month_line_chart is { group_by: dep_qtr is dep_time.quarter @@ -95,7 +95,7 @@ query: flights-> { // -- // Name: Line Chart with two dimension: Flights by Month and Length -query: flights-> { +run: flights-> { group_by: carriers.nickname nest: by_month_line_chart is { group_by: dep_qtr is dep_time.quarter @@ -106,7 +106,7 @@ query: flights-> { // -- // Name: Lists: Origins and the list of carriers -query: flights-> { +run: flights-> { group_by: orig.code, orig.city nest: carrier_list is { group_by: carriers.nickname @@ -118,7 +118,7 @@ query: flights-> { // -- // Name: List Detail: Origins and the list of carriers // shows the count for each member of the list -query: flights-> { +run: flights-> { group_by: orig.code, orig.city nest: carrier_list_detail is { group_by: carriers.nickname @@ -130,7 +130,7 @@ query: flights-> { // -- // Name: Shape Map: Carriers Origin by State -query: flights-> { +run: flights-> { group_by: carriers.nickname aggregate: flight_count nest: by_state_shape_map is { @@ -148,8 +148,8 @@ query: flights-> { // we can run two queries simultaneously and // join the results. -source: my_flights is flights + { - query: top_carrier_line_chart is { +source: my_flights is flights extend { + view: top_carrier_line_chart is { nest: c is { group_by: qtr is dep_time.quarter aggregate: flight_count @@ -162,29 +162,32 @@ source: my_flights is flights + { } } -> { - project: c.* + select: c.* where: c.nickname = top_carriers.nickname } - query: origin_top_carriers is { + view: origin_top_carriers is { group_by: origin, orig.city aggregate: flight_count nest: top_carrier_line_chart } } -query: my_flights->origin_top_carriers +run: my_flights->origin_top_carriers // -- // Name: Using one query as a filter for another // Filter by carriers that fly to SFO. -query: sfo_carriers is flights-> { +query: sfo_carriers2 is flights-> { where: orig.code = 'SFO' group_by: carrier } -query: flights -> { - join_one: sfo is from(->sfo_carriers) on carrier = sfo.carrier +run: flights -> { + extend: { + join_one: sfo is sfo_carriers2 on carrier = sfo.carrier + } + where: sfo.carrier != null group_by: carriers.name aggregate: flight_count, origin_count is orig.airport_count diff --git a/duckdb-wasm/flights.malloy b/duckdb-wasm/flights.malloy index 1bef89ab..eedacd6c 100644 --- a/duckdb-wasm/flights.malloy +++ b/duckdb-wasm/flights.malloy @@ -1,14 +1,14 @@ --! styles flights.styles.json -source: carriers is table('duckdb:carriers.parquet'){ +source: carriers is duckdb.table('carriers.parquet') extend { measure: carrier_count is count() } -source: airports is table('duckdb:airports.parquet'){ +source: airports is duckdb.table('airports.parquet') extend { measure: airport_count is count() } -source: flights is table('duckdb:flights.parquet') { +source: flights is duckdb.table('flights.parquet') extend { join_one: carriers on carrier=carriers.code join_one: dest is airports on destination=dest.code join_one: orig is airports on origin=orig.code @@ -16,23 +16,23 @@ source: flights is table('duckdb:flights.parquet') { measure: flight_count is count() total_distance is distance.sum() - aircraft_count is count(distinct tail_num) + aircraft_count is count(tail_num) dest_count is dest.airport_count - carrier_count is count(distinct carrier) + carrier_count is count(carrier) - dimension: flight_length is distance: + dimension: flight_length is distance ? pick 'short' when < 200 pick 'medium' when < 800 pick 'regional' when < 1500 else 'long' - query: by_carrier is { + view: by_carrier is { group_by: carriers.nickname aggregate: flight_count } - query: by_origin is { + view: by_origin is { group_by: orig.code, orig.city aggregate: flight_count diff --git a/duckdb-wasm/names-queries.malloy b/duckdb-wasm/names-queries.malloy index 6ab0d6db..d3d335d1 100644 --- a/duckdb-wasm/names-queries.malloy +++ b/duckdb-wasm/names-queries.malloy @@ -3,4 +3,4 @@ import "./names.malloy" // Name: "J" Names // named querys // see: https://malloydata.github.io/documentation/language/query.html -query: j_names is names -> name_dashboard {? name ~ r'J'} +query: j_names is names -> name_dashboard + {where: name ~ r'J'} diff --git a/duckdb-wasm/names.malloy b/duckdb-wasm/names.malloy index 51371bfd..70f64224 100644 --- a/duckdb-wasm/names.malloy +++ b/duckdb-wasm/names.malloy @@ -11,53 +11,53 @@ // // source, see: https://malloydata.github.io/documentation/language/source.html -source: names is table('duckdb:data/usa_names.parquet') { +source: names is duckdb.table('usa_names.parquet') extend { measure: population is `number`.sum() dimension: decade is floor(`year`/10)*10 - // query, see: https://malloydata.github.io/documentation/language/query.html - query: by_name is { + // view, see: https://malloydata.github.io/documentation/language/query.html + view: by_name is { group_by: name aggregate: population limit: 10 } - query: by_state is { + view: by_state is { group_by: state aggregate: population } - query: by_gender is { + view: by_gender is { group_by: gender aggregate: population } - query: by_year is { + view: by_year is { group_by: `year` aggregate: population order_by: 1 asc } - query: by_decade is { + view: by_decade is { group_by: decade aggregate: population order_by: 1 asc } // filters, see: https://malloydata.github.io/documentation/language/filters.html - query: male_names is by_name {? gender = 'M'} - query: female_names is by_name {? gender = 'F'} + view: male_names is by_name + {where: gender = 'M'} + view: female_names is by_name + {where: gender = 'F'} - query: top_names_by_state_ea_gender is by_state { + view: top_names_by_state_ea_gender is by_state + { nest: male_names nest: female_names limit: 10 } - query: name_dashboard is by_name { + view: name_dashboard is by_name + { nest: by_decade by_state @@ -65,6 +65,6 @@ source: names is table('duckdb:data/usa_names.parquet') { } } -// named querys -// see: https://malloydata.github.io/documentation/language/query.html -query: j_names is names -> name_dashboard {? name ~ r'J'} +// named queries +// see: https://malloydata.github.io/documentation/language/view.html +query: j_names is names -> name_dashboard + {where: name ~ r'J'} diff --git a/duckdb-wasm/usa_names.parquet b/duckdb-wasm/usa_names.parquet new file mode 100644 index 00000000..6d6926a4 Binary files /dev/null and b/duckdb-wasm/usa_names.parquet differ