Open anikaks opened 2 years ago
Agree we need functions. Agree this could be a function. If I could write the feature for THIS user, though, I would implement some way to create user defined data sets.
maybe something stupid like
csv: timeZoneMap is csv(region_name,timezone_name) <<
'North America','Pacific Time (US & Canada)',
...
>>
source: myThing is table('thing') + {
join_one: timeZoneMap on region = timeZoneMap.regionName
}
where we would actually generate table ...
or maybe an actual map data type ...
map: tzMap is map<string,string> = {
'North America' : 'Pacific Time (US & Canada)',
...
}
source: myThing is table('thing') + {
dimension: timeZone is tsMap[region]
}
where we might generate that case statement
or maybe something else and we might decide to generate different SQL depending on how you use it?
So I think functions aren't really the right thing. I'm more of a bare join that takes parameters as input. We're going to want code that declares multiple things from the same input (think of a retail analysis package that computes gross margin, cogs, etc from an orders/items table)...
abstract_source: timezone_to_string (input_field) {
dimension: timezone_string is @input_field:
pick 'North America' when 'Pacific Time (US & Canada)'
pick 'North America' when 'Eastern Time (US & Canada)'
}
source: my_tabe is table(...) {
// creates a user_time name space like a join.
extend: user_time is timezone_to_string(user.timezone)
query: foo is {
group_by: user_time.timezone_string
}
}
agree with Michael...
You could also do this today as:
sql: timezone_map_sql is ||
SELECT * FROM UNNEST([
STRUCT('North America' as region, 'Pacific Time (US & Canada)' as zone_name),
STRUCT('North America', 'Central Time (US & Canada)')
-- ...
])
;;
source: foo ... {
join_one: user_region is from_sql(timezone_map_sql) on users.timezone = user_region.zone_name
}
@anikaks ^^
I'll share this! Maybe not the ideal example for this request; I'm still very interested in functions !
I actually looked deeper at the code and realized there was no reason to duplicate this particular example. Ultimately, the same table was joined multiple times and that's where the above code was applied. I moved it to the original table and then there was no reason to duplicate it.
However, I still think some kind of function would be useful
The ability to define custom functions in Malloy would be super powerful.
Here's one example, provided by Mike on Slack: