Skip to content

Commit

Permalink
Merge pull request #144 from malloydata/ce/cleanup
Browse files Browse the repository at this point in the history
fix syntax
  • Loading branch information
Carlin Eng authored Jun 12, 2024
2 parents bff6b2e + bc83940 commit 554d29d
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 98 deletions.
58 changes: 29 additions & 29 deletions duckdb-wasm/airports-queries.malloy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -60,22 +60,22 @@ 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
}

// --
// Name: Nesting results
// 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
Expand All @@ -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
Expand All @@ -111,30 +111,30 @@ 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'}
}

// --
// Name: Region Dashboard
// 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:
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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'
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions duckdb-wasm/airports.malloy
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions duckdb-wasm/auto_recalls-queries.malloy
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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`)
Expand All @@ -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
Expand Down
15 changes: 8 additions & 7 deletions duckdb-wasm/auto_recalls.malloy
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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
Expand Down
Loading

0 comments on commit 554d29d

Please sign in to comment.