Skip to content

[Gandiva] Complete timestamp precision support for all datetime functions#129

Closed
telemenar wants to merge 6 commits intodremio:dremio_27.0_20from
telemenar:gandiva-timestamp-precision-details
Closed

[Gandiva] Complete timestamp precision support for all datetime functions#129
telemenar wants to merge 6 commits intodremio:dremio_27.0_20from
telemenar:gandiva-timestamp-precision-details

Conversation

@telemenar
Copy link
Copy Markdown

Summary

This PR completes the implementation of timestamp precision support for all Gandiva datetime functions, building on the infrastructure established in #128.

Changes

Phase 4: Extract Functions

  • Implement extractMonth, extractDay, extractHour, extractMinute, extractSecond for all precisions
  • Implement extractDoy, extractDow, extractQuarter, extractWeek for all precisions
  • Implement extractEpoch, extractMillennium, extractCentury, extractDecade for all precisions

Phase 5: Date Truncation Functions

  • Implement date_trunc_Second/Minute/Hour/Day/Week/Month/Quarter/Year for all precisions
  • Implement date_trunc_Decade/Century/Millennium for all precisions
  • Add new date_trunc_Millisecond (for us/ns) and date_trunc_Microsecond (for ns)

Phase 6: Timestamp Arithmetic Functions

  • Implement timestampaddSecond/Minute/Hour/Day/Week for all precisions
  • Implement timestampaddMonth/Quarter/Year for all precisions
  • Support both argument orders: (int, timestamp) and (timestamp, int)

Phase 7: Cast and Conversion Functions

  • Implement timestamp-to-timestamp precision casts (all 12 combinations)
  • Implement castDATE for all timestamp precisions
  • Implement castTIME for all timestamp precisions
  • Implement datediff for all timestamp precisions

Phase 8: Remaining Functions

  • Implement months_between for all timestamp precisions
  • Implement last_day for all timestamp precisions

Functions Implemented

Category Functions Precisions
Extract 14 functions sec, ms, us, ns
Truncate 11+ functions sec, ms, us, ns
Arithmetic 8 functions sec, ms, us, ns
Cast castTIMESTAMP, castDATE, castTIME sec, ms, us, ns
Other datediff, months_between, last_day sec, ms, us, ns

Testing

  • Comprehensive unit tests for all functions with all precisions
  • Tests for edge cases (year boundaries, negative timestamps, precision conversion)

Dependencies


Co-authored by Augment Code


Pull Request opened by Augment Code with guidance from the PR author

…ractYear POC

Phase 1: Core Infrastructure
- Add MICROS_*, NANOS_*, SECS_* constants to time_constants.h
- Create templated EpochTimePointT<Duration> class for precision-aware time operations
- Add gdv_timestamp_{sec,ms,us,ns} type aliases in types.h
- Update FunctionSignature::Hash() to include TimeUnit for temporal types
- Add timestamp_{sec,ms,us,ns}() helpers and TIMESTAMP_PRECISION_TYPES macro

Phase 2: Proof of Concept - extractYear
- Implement extractYear_timestamp_{sec,ms,us,ns} functions
- Register precision-specific extractYear in function registry
- Add unit tests for EpochTimePointT template
- Add unit tests for precision-specific extractYear
- Add FunctionSignature hash tests for timestamp precisions
- Add integration test for extractYear with all precisions
Phase 4: Remaining Extract Functions
- Add EXTRACT_TIMESTAMP_PRECISION_FNS macro for generating precision-aware extract functions
- Implement extractMonth, extractDay, extractHour, extractMinute, extractSecond
  for all timestamp precisions (sec, ms, us, ns)
- Implement extractDoy, extractDow, extractQuarter for all precisions
- Implement extractWeek for all precisions (converts to millis for week calculation)
- Implement extractEpoch for all precisions (returns seconds)
- Implement extractMillennium, extractCentury, extractDecade for all precisions
- Add DECLARE_EXTRACT_TIMESTAMP_PRECISION macro for function declarations
- Register all precision-specific extract functions in function registry
- Add comprehensive unit tests for all extract functions with all precisions
Phase 5: Date Truncation Functions
- Add precision-aware date_trunc macros (DATE_TRUNC_FIXED_UNIT_PRECISION,
  DATE_TRUNC_WEEK_PRECISION, DATE_TRUNC_MONTH_UNITS_PRECISION, DATE_TRUNC_YEAR_UNITS_PRECISION)
- Implement date_trunc_Second/Minute/Hour/Day for all timestamp precisions
- Implement date_trunc_Week/Month/Quarter/Year for all precisions
- Implement date_trunc_Decade/Century/Millennium for all precisions
- Add new sub-millisecond truncation functions:
  - date_trunc_Millisecond for timestamp[us] and timestamp[ns]
  - date_trunc_Microsecond for timestamp[ns]
- Add DECLARE_DATE_TRUNC_TIMESTAMP_PRECISION macro for declarations
- Register all precision-specific date_trunc functions in function registry
- Add comprehensive unit tests for date truncation with all precisions
Phase 6: Timestamp Arithmetic Functions
- Add precision-aware macros for timestamp add operations:
  - ADD_INT32_TO_TIMESTAMP_PRECISION_FIXED for fixed unit additions
  - ADD_INT64_TO_TIMESTAMP_PRECISION_FIXED for fixed unit additions
  - ADD_INT32_TO_TIMESTAMP_PRECISION_MONTH for month-based additions
  - ADD_INT64_TO_TIMESTAMP_PRECISION_MONTH for month-based additions
- Implement timestampaddSecond/Minute/Hour/Day/Week for all precisions (sec, ms, us, ns)
- Implement timestampaddMonth/Quarter/Year for all precisions
- Support both argument orders: (int, timestamp) and (timestamp, int)
- Add DECLARE_TIMESTAMPADD_PRECISION macros for function declarations
- Add TIMESTAMP_ADD_PRECISION_FNS macro for registry registration
- Register all precision-specific timestampadd functions
- Add comprehensive unit tests for timestamp arithmetic with all precisions
Phase 7 & 8: Cast, Conversion, and Remaining Functions
- Implement timestamp-to-timestamp precision cast functions:
  - castTIMESTAMP_{ms,us,ns}_timestamp_sec
  - castTIMESTAMP_{sec,us,ns}_timestamp_ms
  - castTIMESTAMP_{sec,ms,ns}_timestamp_us
  - castTIMESTAMP_{sec,ms,us}_timestamp_ns
- Implement castDATE for all timestamp precisions
- Implement castTIME for all timestamp precisions
- Implement datediff for all timestamp precisions
- Register all cast and conversion functions in function registry
- Add comprehensive unit tests for:
  - Timestamp precision conversion (upscale and downscale)
  - Timestamp to date64 cast
  - Timestamp to time32 cast
  - datediff with various precisions
Phase 8: Remaining Functions
- Implement months_between for all timestamp precisions
- Implement last_day for all timestamp precisions
- Register months_between and last_day functions in function registry
- Both functions convert to milliseconds internally for calendar calculations
  since these are calendar operations that don't require sub-ms precision

Note: Timezone functions (to_utc_timestamp, from_utc_timestamp) are complex
and would require more extensive changes to gdv_function_stubs.cc. These can
be addressed in a follow-up PR if needed.
@telemenar telemenar force-pushed the gandiva-timestamp-precision-details branch from 519aad5 to 86f537f Compare April 13, 2026 21:46
@telemenar telemenar changed the base branch from dremio to dremio_27.0_20 April 13, 2026 21:47
@telemenar telemenar closed this Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant