Skip to main content

Primitive Types

Hamelin provides several primitive types that serve as building blocks for more complex data structures. These types map cleanly to SQL types while providing a simplified interface for type checking and operations.

Boolean

The boolean type translates directly to SQL's boolean type.

Examples: true, false

Int

The int type is an umbrella for all integer types, from tinyint (8 bits) to bigint (64 bits). All integers are treated the same for type checking purposes.

Examples: 1, 42, -17, 1000000

Double (Floating Point)

The double type represents floating point numbers with variable precision. Use doubles for calculations where approximate values are acceptable.

Bare decimal literals like 3.14 are doubles. Scientific notation like 1.5e0 also produces doubles.

Examples: 3.14, 1.5, 0.75, 1.23e-4, -9.87e2

Decimal (Fixed Point)

The decimal type represents exact numeric values with specified precision and scale, written as decimal(precision, scale). Use the m suffix to create decimal literals when you need exact fixed-point arithmetic (e.g., for financial calculations).

Examples: 100.50m, 0.075m, 999.99m

String

The string type covers char, varchar (of any length). String concatenation uses the + operator.

Examples: 'hello world', "error message", '[email protected]'

Binary

The binary type translates to varbinary in SQL for handling binary data.

Examples: Binary data representations for file contents, encrypted data, or raw bytes.

Timestamp

The timestamp type is an umbrella for date, timestamp, and all their variants (precision and timezone). Hamelin follows SQL's assumption that timestamps with and without zones can be compared.

Examples: ts('2024-01-15'), ts('2024-01-15 14:30:00'), ts('2024-01-15T14:30:00Z')

Interval

The interval type covers interval day to second for time duration calculations with fixed durations. These intervals represent exact amounts of time that can be directly compared and calculated. You can multiply and divide intervals by both integers and floating-point numbers because they represent fixed durations.

Examples: 2h, 30min, 5d, 2h * 2.5, 6h / 2

Calendar Interval

The calendar interval type covers interval year to month for calendar-based durations. Calendar intervals like years and months don't represent a fixed number of days because months have different lengths and years can be leap years. These intervals cannot be directly compared to day-based intervals.

Calendar interval arithmetic only supports integer multipliers and divisors. You cannot multiply or divide calendar intervals by floating-point numbers because fractional months or years don't have clear meaning in calendar arithmetic.

Valid examples: 3mon, 2y, 1q, 6mon * 2, 2y / 4

Invalid examples:

  • 1y * 2.5 → Error: Calendar intervals require integer arithmetic
  • 3mon / 1.5 → Error: Calendar intervals require integer arithmetic

Range

The range type represents spans between two values of any type. Ranges are created by the .. operator. Ranges can be bounded (with both start and end points) or unbounded (extending infinitely in one direction). You see them most commonly in Hamelin as range(timestamp) or range(interval), and every query generally operates under the constraints of a range(timestamp).

Examples: -2hr..-1hr, ts('2024-01-15')..now(), -1hr.., ..now(), 1..10, 'a'..'z'

Rows

The rows type represents a number of rows and is only useful when declaring window frames as a certain number of rows rather than a time-based frame.

Examples: 5r, 10r, 0r