Skip to content

GODRIVER-3986: Implement date and timestamp expression operators and tests#15

Merged
matthewdale merged 3 commits into
mongodb-labs:mainfrom
linborland:godriver3986
Jul 17, 2026
Merged

GODRIVER-3986: Implement date and timestamp expression operators and tests#15
matthewdale merged 3 commits into
mongodb-labs:mainfrom
linborland:godriver3986

Conversation

@linborland

@linborland linborland commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

GODRIVER-3986

Implements the types DateResolver, TimestampResolver, DateExpr, and TimestampExpr.

Implements the following operators and their corresponding tests: $dateAdd, $dateSubtract, $dateDiff, $dateFromParts, $dateFromString, $dateToParts, $dateToString, $dateTrunc, $dayOfMonth, $dayOfWeek, $dayOfYear, $hour, $isoDayOfWeek, $isoWeek, $isoWeekYear, $millisecond, $minute, $month, $second, $week, $year, $toDate, $tsIncrement, $tsSecond

@linborland
linborland marked this pull request as ready for review July 14, 2026 14:14
@linborland
linborland requested a review from a team as a code owner July 14, 2026 14:14
@linborland
linborland requested review from prestonvasquez and removed request for a team July 14, 2026 14:14
@matthewdale
matthewdale requested review from matthewdale and removed request for prestonvasquez July 14, 2026 17:19
Comment thread mql/agg/operator.go Outdated

// Year returns the year for a date ($year).
// date may resolve to a Date, Timestamp, or ObjectID.
func Year(date Expr, opts ...Option[datePartOptions]) NumberExpr {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The date param should use a type constraint that aligns with the described valid values. We should add something like ObjectIDResolver because the MQL spec for operators like $year describe a resolvesToObjectId.

This suggestion applies to all operator functions added in this PR that can accept a date param with the same valid types.

E.g.

type ObjectIDResolver interface {
	AnyExpr | bson.ObjectID | string
}

func Year[T DateResolver | TimestampResolver | ObjectIDResolver](date T, opts ...Option[datePartOptions])

@linborland linborland Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ToObjectId operator function currently returns AnyExpr, but the spec says it should return resolvesToObjectId (our equivalent of ObjectIdExpr). Do you think we should create type ObjectIdExpr for it to return, or should it return bson.ObjectID?
There's also a CreateObjectId operator function in the next PR that this decision would apply to.

Comment thread mql/agg/expr.go Outdated
return byte(typ), b, err
}

type TimestampExpr struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any operators that return a TimestampExpr (i.e. return resolvesToTimestamp in the MQL spec)? If not, we should remove TimestampExpr since it won't be used.

@linborland
linborland requested a review from matthewdale July 17, 2026 17:26

@matthewdale matthewdale left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

@matthewdale
matthewdale merged commit 088bf24 into mongodb-labs:main Jul 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants