feat: Add week_start parameter for configurable week boundaries #2577
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add
week_startParameter for Configurable Week BoundariesSummary
This PR adds support for configuring the start day of the week at both source and query levels, providing functionality similar to Looker's
week_start_dayparameter. This allows users to align week-based aggregations with their business requirements (e.g., Monday-start weeks for ISO standards, Sunday-start weeks for US conventions).Motivation
Currently, Malloy uses database defaults for week truncation, which varies by database (e.g., BigQuery defaults to Sunday, DuckDB to Monday). This PR provides explicit control over week boundaries to ensure consistent results across databases and match business requirements.
Implementation
Syntax
Source-level configuration:
Query-level override:
Valid Values
sunday,monday,tuesday,wednesday,thursday,friday,saturdayQuery-level settings override source-level configuration, following the same pattern as the existing
timezone:parameter.Technical Approach
WeekDayunion type andweekStartDayproperty to relevant model interfacesWEEK_STARTtoken andweekStartStatementrulesWeekStartStatementclass with validation logic and helpful error messagesQueryInfoto dialect layerWEEK(WEEKDAY)parameter supportDAYOFWEEK/MODarithmetic approachChanges
Modified Files (20)
malloy_types.ts,field_instance.ts,query_query.ts,to_stable.tsMalloyLexer.g4,MalloyParser.g4dynamic-space.ts,query-spaces.ts,refined-source.ts,source-property.ts,malloy-to-ast.ts,parse-log.ts,index.tsdialect.ts,postgres.ts,duckdb.ts,mysql.ts,snowflake.ts,standardsql.ts,trino.tsNew Files (2)
packages/malloy/src/lang/ast/source-properties/week-start-statement.ts- AST node with validationtest/src/databases/all/week-start.spec.ts- Comprehensive test suiteTesting
I've included a comprehensive test suite (
week-start.spec.ts) that covers:I don't have access to the test database infrastructure, so I wasn't able to run the full test suite locally. The test file is written following the existing patterns in
time.spec.tsand should integrate seamlessly with your CI/CD pipeline. I would appreciate if the maintainers could run the tests and provide feedback on any adjustments needed.Validation
✅ Code Quality
npm run lint✓npm run build✓timezone:implementation)anytypes used)✅ Parser Validation
Backward Compatibility
✅ Fully backward compatible
Example SQL Output
Input (DuckDB with Monday week):
Generated SQL:
Input (BigQuery with Wednesday week):
Generated SQL:
TIMESTAMP_TRUNC(timestamp, WEEK(WEDNESDAY))Documentation for docs.malloydata.dev
Below is ready-to-use documentation in the style of the existing timezone docs. This can be added to the language reference:
Week Start Day
Usage
Default Value
sundayAccepts
A day of the week:
sunday,monday,tuesday,wednesday,thursday,friday, orsaturdayDefinition
You can adjust the day that Malloy considers to be the start of a week with the
week_startparameter. This affects how week-based time dimensions (.week) truncate dates and timestamps.The
week_startparameter can be specified at:Source-level example:
Query-level override:
Use Cases
week_start: mondayweek_start: sunday(default)Relationship to timezone
The
week_startparameter works independently oftimezone:and follows the same override pattern:Cross-database Support
This parameter works consistently across all supported databases (BigQuery, PostgreSQL, MySQL, DuckDB, Snowflake, Trino), automatically generating the appropriate SQL for each dialect.
Questions for Reviewers
Related Issues
This addresses the need for configurable week boundaries similar to Looker's
week_start_dayparameter.Checklist:
Thank you for considering this contribution! I'm happy to make any adjustments based on your feedback.