grafana-flightsql-datasource icon indicating copy to clipboard operation
grafana-flightsql-datasource copied to clipboard

Add support for different time units

Open hammsvietro opened this issue 1 year ago • 0 comments

Hi there! I stumbled upon an issue today while using the plugin, the data I was querying for used timestamps with microseconds as the time unit, and the date displayed in the plugin was not correct.

Currently, when loading timestamp columns, the unit used to convert timestamps to time is fixed to arrow.Nanoseconds, and this may lead to incorrect conversions. Instead, it is possible to use arrow.TimestampType to infer what is the desired unit.

I tried the following and it seems to be working correctly:

func getTimeUnit(col arrow.Array) arrow.TimeUnit {
	switch t := col.DataType().(type) {
	        case *arrow.TimestampType:
		        return t.TimeUnit()
	        default:
		        return arrow.Nanosecond
	}
}
case arrow.TIMESTAMP:
	v := array.NewTimestampData(data)
	unit := getTimeUnit(col)
	for i := 0; i < v.Len(); i++ {
		if field.Nullable() {
			if v.IsNull(i) {
				var t *time.Time
				field.Append(t)
				continue
			}
			t := v.Value(i).ToTime(unit)
			field.Append(&t)
			continue
		}
		field.Append(v.Value(i).ToTime(unit))
	}

hammsvietro avatar Jun 16 '23 19:06 hammsvietro