feat: Add week_start parameter for configurable week boundaries#2577
Open
kayvink wants to merge 8 commits intomalloydata:mainfrom
Open
feat: Add week_start parameter for configurable week boundaries#2577kayvink wants to merge 8 commits intomalloydata:mainfrom
kayvink wants to merge 8 commits intomalloydata:mainfrom
Conversation
Adds support for configuring the start day of the week at both source
and query levels, similar to Looker's week_start_day parameter.
- Added WeekDay type and weekStartDay property across model types
- Extended grammar with WEEK_START token and parser rules
- Implemented dialect-specific SQL generation for all databases
- Follows existing timezone: parameter pattern
- Comprehensive test suite included
- Fully backward compatible (defaults to Sunday)
Syntax:
source: data extend { week_start: monday }
run: data -> { week_start: friday; ... }
Valid values: sunday, monday, tuesday, wednesday, thursday, friday, saturday
Signed-off-by: kayvink <kmvink@gmail.com>
Signed-off-by: kayvink <kmvink@gmail.com>
Fixes test failures in week-start.spec.ts across all database backends. Changes: - BigQuery/StandardSQL: Fixed syntax to use WEEK(DAYNAME) format instead of passing day name as a separate parameter - Snowflake: Changed from session-level WEEK_START parameter to per-query offset calculation for dynamic week start support - Tests: Fixed reserved keyword conflict by renaming 'week' field to 'week_value' in group_by clause - Tests: Updated date assertions to use toISOString() for timezone-independent comparisons This ensures week_start configuration works correctly at source-level and query-level across Postgres, MySQL, DuckDB, Trino, Presto, BigQuery, and Snowflake.
I, kayvink <kmvink@gmail.com>, hereby add my Signed-off-by to this commit: 30766fc Signed-off-by: kayvink <kmvink@gmail.com>
Changed Jan 17 to Jan 11 so that two dates fall in the same week. Jan 17 is a Wednesday, which starts a new week with wednesday start, causing 3 groups instead of the expected 2. Now: Jan 11 (Thu) and Jan 15 (Mon) both fall in week starting Jan 10 (Wed) and Jan 24 (Wed) starts its own week, giving the expected 2 groups. Signed-off-by: kayvink <kayvink@users.noreply.github.com>
Fixed linting errors caused by trailing spaces on blank lines. Signed-off-by: kayvink <kayvink@users.noreply.github.com>
Snowflake's DATE_TRUNC('week') defaults to Monday (WEEK_START=0), not Sunday.
Updated the offset calculation to work from Monday as the baseline instead of
Sunday. This fixes the 29 Snowflake test failures.
Signed-off-by: kayvink <kayvink@users.noreply.github.com>
Keep WEEK_START=7 (Sunday) as the session default because it affects both
DATE_TRUNC('week') AND DAYOFWEEK extraction. Removing it broke many tests
that depend on Sunday-based day-of-week numbering.
For custom week starts, we still calculate offsets dynamically from Sunday
in the dialect's sqlTruncate method.
Signed-off-by: kayvink <kayvink@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.