or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arrays.mdhashing-bitsets.mdindex.mdintervals.mdmemory.mdplatform.mdutf8-strings.md
tile.json

intervals.mddocs/

Calendar Intervals

The CalendarInterval class represents time intervals with separate month and microsecond components, providing precise handling of calendar-based time periods commonly used in SQL operations. This design accounts for the variable length of months while maintaining microsecond precision for sub-day intervals.

Core Imports

import org.apache.spark.unsafe.types.CalendarInterval;

Usage Examples

Creating Calendar Intervals

// Create interval with 2 months and 1.5 days
CalendarInterval interval = new CalendarInterval(2, 1500000000L); // 1.5 days in microseconds

// Create intervals from string representations
CalendarInterval fromString = CalendarInterval.fromString("2 months 3 days");
CalendarInterval caseInsensitive = CalendarInterval.fromCaseInsensitiveString("2 MONTHS 3 DAYS");

// Parse specific formats
CalendarInterval yearMonth = CalendarInterval.fromYearMonthString("1-6"); // 1 year 6 months
CalendarInterval dayTime = CalendarInterval.fromDayTimeString("5 12:30:45.123"); // 5 days, 12:30:45.123

Basic Interval Operations

CalendarInterval interval1 = new CalendarInterval(1, CalendarInterval.MICROS_PER_DAY * 10); // 1 month, 10 days
CalendarInterval interval2 = new CalendarInterval(0, CalendarInterval.MICROS_PER_HOUR * 5);  // 5 hours

// Arithmetic operations
CalendarInterval sum = interval1.add(interval2);         // Add intervals
CalendarInterval diff = interval1.subtract(interval2);  // Subtract intervals
CalendarInterval negated = interval1.negate();          // Negate interval

// Get milliseconds component
long millis = interval1.milliseconds(); // Convert microseconds to milliseconds

Working with Time Constants

// Using predefined time constants
long fiveMinutes = 5 * CalendarInterval.MICROS_PER_MINUTE;
long twoHours = 2 * CalendarInterval.MICROS_PER_HOUR;
long threeDays = 3 * CalendarInterval.MICROS_PER_DAY;
long oneWeek = CalendarInterval.MICROS_PER_WEEK;

CalendarInterval timeInterval = new CalendarInterval(0, fiveMinutes + twoHours);

Parsing Complex Intervals

// Parse intervals with multiple units
CalendarInterval complex = CalendarInterval.fromString("1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds");

// Parse year-month format (SQL standard)
CalendarInterval yearMonthInterval = CalendarInterval.fromYearMonthString("2-5"); // 2 years, 5 months

// Parse day-time format (SQL standard)
CalendarInterval dayTimeInterval = CalendarInterval.fromDayTimeString("10 15:30:45.500"); // 10 days, 15:30:45.500

// Parse single unit intervals
CalendarInterval hours = CalendarInterval.fromSingleUnitString("hour", "24");
CalendarInterval days = CalendarInterval.fromSingleUnitString("day", "7");

API Reference

Constructor and Fields

public final class CalendarInterval {
    /**
     * Number of months in the interval.
     */
    public final int months;
    
    /**
     * Number of microseconds in the interval.
     */
    public final long microseconds;
    
    /**
     * Creates a calendar interval with specified months and microseconds.
     */
    public CalendarInterval(int months, long microseconds);
}

Time Constants

public static final long MICROS_PER_MILLI = 1000L;
public static final long MICROS_PER_SECOND = 1000000L;
public static final long MICROS_PER_MINUTE = 60000000L;
public static final long MICROS_PER_HOUR = 3600000000L;
public static final long MICROS_PER_DAY = 86400000000L;
public static final long MICROS_PER_WEEK = 604800000000L;

Conversion Methods

/**
 * Returns microseconds converted to milliseconds.
 */
public long milliseconds();

Arithmetic Operations

/**
 * Adds two calendar intervals.
 */
public CalendarInterval add(CalendarInterval that);

/**
 * Subtracts one interval from another.
 */
public CalendarInterval subtract(CalendarInterval that);

/**
 * Returns the negated interval.
 */
public CalendarInterval negate();

String Parsing Methods

/**
 * Parses interval from string representation (case-sensitive).
 * Supports formats like "1 year 2 months 3 days 4 hours 5 minutes 6 seconds".
 */
public static CalendarInterval fromString(String s);

/**
 * Parses interval from string representation (case-insensitive).
 */
public static CalendarInterval fromCaseInsensitiveString(String s);

/**
 * Parses year-month format interval (e.g., "1-6" for 1 year 6 months).
 */
public static CalendarInterval fromYearMonthString(String s);

/**
 * Parses day-time format interval (e.g., "5 12:30:45.123").
 */
public static CalendarInterval fromDayTimeString(String s);

/**
 * Parses single unit interval (e.g., unit="day", s="5").
 */
public static CalendarInterval fromSingleUnitString(String unit, String s);

Utility Methods

/**
 * Parses long value with range validation.
 */
public static long toLongWithRange(String fieldName, String s, long minValue, long maxValue);

/**
 * Parses seconds with nanosecond precision, returns microseconds.
 */
public static long parseSecondNano(String secondNano);

Object Methods

/**
 * Compares intervals for equality.
 */
public boolean equals(Object other);

/**
 * Returns hash code for the interval.
 */
public int hashCode();

/**
 * Returns string representation of the interval.
 */
public String toString();

Supported String Formats

General Format

The general string format supports combinations of the following units:

  • year, years
  • month, months
  • week, weeks
  • day, days
  • hour, hours
  • minute, minutes
  • second, seconds
  • millisecond, milliseconds
  • microsecond, microseconds

Examples:

CalendarInterval.fromString("1 year 2 months")
CalendarInterval.fromString("3 days 4 hours 30 minutes")
CalendarInterval.fromString("2 weeks 5 seconds 500 milliseconds")

Year-Month Format

SQL standard format for year-month intervals:

  • Format: "Y-M" where Y is years and M is months
  • Examples: "1-6" (1 year 6 months), "0-18" (18 months)

Day-Time Format

SQL standard format for day-time intervals:

  • Format: "D H:M:S.f" where D is days, H is hours, M is minutes, S is seconds, f is fractional seconds
  • Examples: "5 12:30:45", "10 15:30:45.500"

Single Unit Format

For parsing single units with a specific value:

CalendarInterval.fromSingleUnitString("day", "5")    // 5 days
CalendarInterval.fromSingleUnitString("hour", "24")  // 24 hours

Usage Notes

  1. Month Precision: Months are stored separately because they have variable lengths (28-31 days).

  2. Microsecond Precision: Sub-day intervals are stored with microsecond precision for maximum accuracy.

  3. Immutability: CalendarInterval objects are immutable; arithmetic operations return new instances.

  4. SQL Compatibility: String parsing follows SQL standard formats for INTERVAL types.

  5. Range Limitations: Be aware of overflow when working with very large intervals.

  6. Parsing Errors: Invalid string formats will throw exceptions during parsing.

  7. Case Sensitivity: Use fromCaseInsensitiveString() for case-insensitive parsing.

Common Patterns

Creating Common Intervals

// Common time periods
CalendarInterval oneDay = new CalendarInterval(0, CalendarInterval.MICROS_PER_DAY);
CalendarInterval oneWeek = new CalendarInterval(0, CalendarInterval.MICROS_PER_WEEK);
CalendarInterval oneMonth = new CalendarInterval(1, 0);
CalendarInterval oneYear = new CalendarInterval(12, 0);

// Mixed intervals
CalendarInterval quarter = new CalendarInterval(3, 0); // 3 months
CalendarInterval businessWeek = new CalendarInterval(0, 5 * CalendarInterval.MICROS_PER_DAY);

Validation and Error Handling

try {
    CalendarInterval interval = CalendarInterval.fromString("invalid format");
} catch (IllegalArgumentException e) {
    // Handle parsing errors
    System.err.println("Invalid interval format: " + e.getMessage());
}