-
Q: How to call BigQuery native functions?I've discovered Logica just today, went through tutorial and already thinking about using it to replace some damn ugly unmaintainable pieces of SQL... Sadly, I haven't found anywhere HOW can I call a BigQuery functions? Is there some additional Logica documentation other than examples in repo and tutorial? Example: I'd like to use %%logica MonthlyValues
MyTimestamp("2023-11-01 02:00:00+00:00", 1);
MyTimestamp("2023-11-10 09:00:00+00:00", 1);
MyTimestamp("2023-10-30 07:00:00+00:00", 2);
MyTimestamp("2023-10-20 02:00:00+00:00", 2);
MyTimestamp("2023-10-10 11:00:00+00:00", 7);
MyTimestamp("2023-10-01 05:00:00+00:00", 2);
MonthlyValues(month:, agg? Set= value) distinct :-
MyTimestamp(date, value),
month = Substr(date, 0, 7);
#QuarterValues(quarter:, agg? Set= value) distinct :-
# MyTimestamp(date, value),
# quarter = ??? how to call FORMAT_TIMESTAMP("%Y-%Q", date) ??? Where
and
The only way I found is abusing TypedTimestamp(str:, timestamp:) :-
timestamp == SqlExpr("CAST({str_ts} AS TIMESTAMP)", {str_ts: str});
QuarterValues(quarter:, agg? Set= value) distinct :-
MyTimestamp(date, value),
TypedTimestamp(str: date, timestamp:),
quarter == SqlExpr("FORMAT_TIMESTAMP('%Y-%m', {ts})", {ts: timestamp}); Continuing with this, I'd like to compute the difference between maximum and minimum in the DiffValues(time_period:, diff:) :-
MonthlyValues(month: time_period, agg:),
diff == SqlExpr("(SELECT MAX(ma) FROM UNNEST({agg}) as ma) - (SELECT MIN(mi) FROM UNNEST({agg}) as mi)", {agg: agg}); And result like
I believe that there must be a better way. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Indeed, function calling was totally missing from tutorail. Updated. Basically for functions with simple signature like So this works:
As for You will need an aggregating expression. And you can either define an aggregate function and apply it:
Or you can compute
Let me know if you have further questions! :-) |
Beta Was this translation helpful? Give feedback.
-
You're very welcome! For the followup: (1) No, you were right about looking at If you know what functions were added and you need them, feel free to create an issue, or list them here, it is easy to add them. (2) This looks doable, but I am sure what exactly needs to happen. Will there be For example we can simply add this constraint as conjunct in the body of the clause. Is this not what we want for some reason? (3) We can use
|
Beta Was this translation helpful? Give feedback.
Indeed, function calling was totally missing from tutorail. Updated.
Basically for functions with simple signature like
FORMAT_TIMESTAMP
we simply need to camel-case it, e.g. intoFormatTimestamp
.So this works:
As for
d…