Better handling of data types in buckets

Uses cts.referenceScalarType() to get the type of a reference (i.e.
range index) and does the correct casting to pass to cts.valueRanges.