Data Types

The eight data types a field can declare, and which warehouse types they map to.

Reference

data_typeWarehouse typesUse forNotes
stringVARCHAR, TEXT, CHARnames, descriptions, codes, categories, status valuesKeep order numbers and SKUs as strings unless you do math on them
integerINT, INTEGER, SMALLINTcounts, quantities, years, small IDs32-bit range
bigintBIGINTlarge IDs, epoch timestamps, large counts64-bit range
decimalDECIMAL, NUMERIC, FLOAT, DOUBLEmoney, percentages, ratios, averages, measurementsPrecision follows the warehouse; use it for every monetary value
dateDATEdates without a time partDeclares grains: day, week, month, quarter, year
date_timeTIMESTAMP, DATETIMEtimestamps, event timesDeclares grains, plus raw, second, minute, hour
booleanBOOLEAN, BOOLflags, yes/no fieldsValues true, false, NULL
binaryBLOB, BINARY, BYTEAbinary payloadsRare; store a URL or path as a string instead

A measure’s data_type describes the aggregate’s result: count(*) is an integer, sum(amount) a decimal.

Examples

- type: dimension
  name: Order ID
  data_type: integer
  expression:
    primary_key: true
    sql: order_id

- type: dimension
  name: Order Date
  data_type: date
  grains: [day, week, month, quarter, year]
  expression:
    sql: order_date

- type: dimension
  name: Created At
  data_type: date_time
  grains: [raw, hour, day, week, month]
  expression:
    sql: created_at

- type: measure
  name: Total Revenue
  data_type: decimal
  format: currency:2
  expression:
    sql: sum(amount)

Grains

date and date_time fields list the granularities callers can group and filter by. When a spec asks for a grain with a truncate decorator, 0sql truncates the column to it in the generated SQL; the list advertises which grains make sense for the field.

GrainApplies to
rawdate_time only: the untruncated value
millisecond, second, minute, hourdate_time only
day, week, month, quarter, yearboth

Guidelines

  • Match the column. Mismatches surface as cast errors when your application runs the SQL, not at deploy.
  • decimal for money, never a float column type name. Currency formatting is a format hint for your client, not the type. See Field format.
  • List only useful grains. A daily inventory table has no business offering hour.
  • Prefer integer for IDs unless the column is a BIGINT.

Next Steps