Dimension_group w/ type "time" doesn't work with datatype: datetime for BigQuery

vfx1
New Member

Consider the following code:

view: datetime {
  derived_table: {
    sql: SELECT CURRENT_DATETIME as dt;; # Bigquery dialect
  }

  dimension_group: dt {
    type: time
    timeframes: [
      date,
      month
    ]
    sql: ${TABLE}.dt ;;
    datatype: datetime
  }
}

explore: datetime {}

This is supposed to be pretty straightforward - a dimension group with datetime as underlying data type.

Looker generates the following SQL:

WITH datetime AS (SELECT CURRENT_DATETIME as dt)
SELECT 
	CAST(TIMESTAMP(FORMAT_TIMESTAMP('%F %T', datetime.dt , 'Asia/Kolkata')) AS DATE) AS datetime_dt_date
FROM datetime

GROUP BY 1

Which produces an error as the underlying type is not timestamp.

Failed to retrieve data - No matching signature for function FORMAT_TIMESTAMP for argument types: STRING, DATETIME, STRING. Supported signature: FORMAT_TIMESTAMP(STRING, TIMESTAMP, [STRING]) at [4:24]

This happens with other datatypes too. There is a way to overcome it by casting the datetime to timestamp in the โ€˜sql:โ€™ element but this is a hack and produces redundant transformation.

Is it a bug?

Solved Solved
1 20 3,943
1 ACCEPTED SOLUTION

Youโ€™ll be pleased to know (especially @vfx1) that weโ€™ve just fixed this issue. It should be present in the next version of Looker.

Weโ€™ll be introducing a legacy feature flag as well as a model-level keyword parameter to control this, to make sure that folks can get the proper experience without breaking any code that has been relying on this incorrect behavior.

Iโ€™m being intentionally vague as thereโ€™s still time for things to change, so keep your eyes peeled for the details in the release notes for 7.14!

View solution in original post

20 REPLIES 20

I think this is just a discrepancy in nomenclature. โ€œDatetimeโ€ in LookML represents a concept which may have different names in different SQL dialects.

On the other hand, the โ€œdatetimeโ€ type in BigQuery is actually rather clunky and difficult to work with - it represents a local time with no timezone information, which is (in my experience) not so useful in practice. is there a reason youโ€™re not using a BigQuery timestamp instead?

Looking at our docs a bit more, I guess we have LookML datatypes of both datetime and timestamp. I think Iโ€™ll have to read into it a bit more!

vfx1
New Member

Hi Fabio,

Thanks for taking the time to look at this. The reason why we are using DATETIME datatype is because the underlying Bigquery tables use fields of this type and they come from the original data sources.

Of course DATETIME fields can be converted back to timestamp while creating the views in Looker but then what is the use for the โ€œdatatypeโ€ parameter if it has to be converted to TIMESTAMP?

On a side note, the automatic conversion to TIMESTAMP (by using create view from table) also doesnโ€™t seem to work properly with timezones, so I reported this separately as a potential bug here.

Cheers

vfx1
New Member

I found more details.

The documentation mentions at least at two places that for dimension_group of type time the default datatype is datetime.

The default value for datatype is datetime.

By observing how looker writes SQL it seems the assumed default type (at least for BigQuery) is in fact timestamp. And even if datatype parameter is specified to be datetime itโ€™s being ignored, which produces invalid SQL.

vfx1
New Member

Guys,

I was just wondering if there is some progress on this. It seems somewhat foundational to meโ€ฆ

I think youโ€™re right and that this is an issue with our default generated SQLโ€” Iโ€™ll pass it along to engineering!

vfx1
New Member

Great news! Thanks!

vfx1
New Member

Hi Izzy,

Is there some news about this?

Hi hi,

Our engineers agreed that this is something to be fixedโ€” Specifically, they thought it has to do with the way that bigquery treats the word โ€œdatetimeโ€. In a lot of dialects, itโ€™s synonymous with timestamp, and we treat it as such. In BigQuery, though, it appears to be different, and thatโ€™s the hitch.

Weโ€™ve prioritized it, but not urgently, since there is a workaround (casting) thatโ€™s not too in depthโ€” Which is to say it will get fixed, but there are currently other projects prioritized higher in this development cycle.

This topic is linked to the bug, so Iโ€™ll know if thereโ€™s any progress made and update here. Thanks for checking in!

Hi, the workaround works and isnโ€™t too painful, but the bug isnโ€™t very obvious and puzzled me for quite a while. So itโ€™d be nice to have a fix.

Cheers,

Alex

A fix will definitely come, since itโ€™s been triaged & prioritized by engineering. Itโ€™s just a matter of when, or if there are other things in the queue ahead of it. Thanks for checking in on it! Iโ€™ll drop a line where when itโ€™s fixed.

RobG
New Member

Just adding a +1 in looking for a fix for this.

jtopp
New Member

+1 From me as well!

As a bonus, you could do something like this:

TIMESTAMP(<datetime_column>, <database_timezone>)

(where <database_timezone> is the timezone specified in admin).

That way, youโ€™d be enriching our datetime columns with timezone info! I think thatโ€™s actually a great value add, as well as a fix for an annoying bug. ๐Ÿ˜€

I have found that when trying to create a time dimension group parameter based on a date type that is atypical (not based on a datetime field), the best way to overcome this issue is to apply a datatype parameter as such:

dimension_group: created {
type: time
**datatype: date**
timeframes: [
  raw,
  time,
  date,
  week,
  month,
  quarter,
  year
]
sql: ${TABLE}._created_date ;;

}

This alleviates the error above.

vfx1
New Member

Hi Kate,

It goes half way there ๐Ÿ™‚ Doing the above makes looker to write the following:

CAST(CAST(<some_datetime> AS TIMESTAMP) AS DATE)

Itโ€™s great as:

  • doesnโ€™t break

Itโ€™s not so great as:

  • removes the time from the datetime
  • only works in UTC - in every other timezone produces potentially wrong date

So all in all - doesnโ€™t seem to be a workaround for the issue.

Youโ€™ll be pleased to know (especially @vfx1) that weโ€™ve just fixed this issue. It should be present in the next version of Looker.

Weโ€™ll be introducing a legacy feature flag as well as a model-level keyword parameter to control this, to make sure that folks can get the proper experience without breaking any code that has been relying on this incorrect behavior.

Iโ€™m being intentionally vague as thereโ€™s still time for things to change, so keep your eyes peeled for the details in the release notes for 7.14!

mwalle
New Member

This was the only thread I could find with any mention of 7.14 at this time, so Iโ€™m hijacking it just to ask @izzymiller if you know when the 7.14 relnotes will be published to https://docs.looker.com/relnotes

Itโ€™s July 30th which is the expected final release date for 7.12; but there are fixes weโ€™ve been informed of in 7.14 that weโ€™d love to see the published details of.

@izzymiller didnโ€™t see this feature in 7.14. Am I missing it or did it get bumped to the next release? Thanks!

@Jason_Comp_Thre . This was indeed addressed in 7.14. See the changelog here:

Fixed: Incorrect SQL for datetime fields for BigQuery dialects is no longer being generated.

Top Labels in this Space
Top Solution Authors