Skip to content

[Feature Request] Support native date.trunc function in standard library to reduce S-string usage #5728

Closed

Description

@sunyeongchoi

Description

Currently, truncating a date or timestamp to a specific grain (e.g., day, month, year) in PRQL requires using S-strings with dialect-specific functions like DATE_TRUNC, DATETRUNC, or TRUNC. For example:

derive [
  order_day = s"DATE_TRUNC('day', {order_date})"
]

While S-strings provide a powerful escape hatch, they have several drawbacks:

  1. Poor Readability: They break the visual flow of PRQL code.
  2. Maintenance: They make the query dialect-dependent, defeating one of PRQL's core strengths (abstraction over SQL dialects).
  3. Developer Experience: Writing s"DATE_TRUNC(...)" is error-prone and feels less intuitive than a native function call.

Proposed Change

I propose adding a native date.trunc function to the PRQL standard library. This would allow users to write:

derive {
  order_day = (order_date | date.trunc 'day'),
  start_of_month = (@2026-03-24 | date.trunc 'month')
}

Instead of:

derive {
  order_day = s"DATE_TRUNC('day', {order_date})"
}

I am willing to contribute to this by implementing the date.trunc function in the standard library if the maintainers agree with this direction.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions