malloy icon indicating copy to clipboard operation
malloy copied to clipboard

`LAST_DAY` function in BigQuery not working

Open carlineng opened this issue 1 year ago • 4 comments

Bug report from Slack.

The LAST_DAY function in BigQuery takes a date_part as the second argument, but fails when you pass in a string. Some examples:

The following query fails with a syntax error, A valid date part name is required:

query: table('malloy-data.ecomm.order_items') -> {
    project:
        created_at
        created_date is created_at.day
        last_day_of_week is LAST_DAY(created_at.day, 'WEEK')
    limit: 24
}

The following query does not compile:

query: table('malloy-data.ecomm.order_items') -> {
    project:
        created_at
        created_date is created_at.day
        last_day_of_week is LAST_DAY(created_at.day, WEEK)
    limit: 24
}

image

carlineng avatar May 03 '23 19:05 carlineng

@christopherswenson will any of your recent work on functions be able to help with this? How should we handle arguments that need the "date_part" type?

carlineng avatar May 03 '23 19:05 carlineng

Not sure how we are going to handle this and similar functions. We would need some special new syntax for "this looks like a field name but it is really a keyword in the underlying database".

last_day_of_week is created_at.week + 6 days ?

mtoy-googly-moogly avatar May 05 '23 15:05 mtoy-googly-moogly

This issue came up again in Slack: https://malloy-community.slack.com/archives/C025JAK8G0N/p1691111355523209

Perhaps we do some kind of special quoting or escaping to indicate that a keyword should be passed through directly, and not interpreted as a field?

carlineng avatar Aug 04 '23 13:08 carlineng

It should be possible now, using the sql_functions experiment, to do something like sql_date("LAST_DAY(${created_at_day}, WEEK)"), which currently requires that created_at_day be a predefined field. Probably some day we'll support something more like sql date("LAST_DAY(${created_at.day}, WEEK)") but unclear on the path forward to get there.

I think SQL expressions is our (possibly final) solution to the "weird function invocation case."

christopherswenson avatar Feb 07 '24 16:02 christopherswenson