Skip to content

CSHARP-5672: Support sorting by value in PushEach operation #1748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

adelinowona
Copy link
Contributor

No description provided.

Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

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

Looks good. Minor changes requested.

{
SortDirection.Ascending => 1,
SortDirection.Descending => -1,
_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".")
Copy link
Contributor

Choose a reason for hiding this comment

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

I would have done this:

_ => throw new InvalidOperationException($"Invalid sort direction: {_direction}.")

but I suppose then we might want to change DirectionalSortDefinition also...

_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".")
};

return new BsonDocument("direction", value);
Copy link
Contributor

Choose a reason for hiding this comment

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

When we use "magic" values like this we usually enclose them in angle brackets to make it visually apparent that something is special about this value:

return new BsonDocument("<direction>", value);

We only have a field name here because Render is already declared to return BsonDocument and changing it to return BsonValue would be a breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

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

So what happens in other cases (which are not UpdateDefinitionBuilder), will it be rendered as
sort: { direction: ... } and do we want that?

We could add
internal virtual BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => Render(args);
to SortDefinition, and refactor Render to return BsonValue in 4.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, I like this! I thought of something similar but thought adding something to SortDefinition would be a breaking change but I suppose adding an internal virtual method is not? Learning some library maintainer tricks :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With this updated approach, I am also thinking of throwing an exception when a call to Render is made on a ValueDirectionalSortDefinition. A call to render for ValueDirectionalSortDefinition is suggesting someone is trying to use this where a field-based sort is expected which is invalid.

var newArgs = args.WithNewDocumentType((IBsonSerializer<TItem>)itemSerializer);

var renderedSort = _sort is NoFieldDirectionalSortDefinition<TItem>
? _sort.Render(newArgs)["direction"]
Copy link
Contributor

Choose a reason for hiding this comment

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

? _sort.Render(newArgs)["<direction>"]

Note the added angle brackets.


public override BsonDocument Render(RenderArgs<TDocument> args)
{
BsonValue value = _direction switch
Copy link
Contributor

Choose a reason for hiding this comment

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

We do the SortDirection to BsonValue conversion in 2 different places already.
Consider extorting this to internal SortDirectionExtensions or similar?

_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".")
};

return new BsonDocument("direction", value);
Copy link
Contributor

Choose a reason for hiding this comment

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

So what happens in other cases (which are not UpdateDefinitionBuilder), will it be rendered as
sort: { direction: ... } and do we want that?

We could add
internal virtual BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => Render(args);
to SortDefinition, and refactor Render to return BsonValue in 4.0.


internal override BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => _direction.ToBsonValue();

public override BsonDocument Render(RenderArgs<TDocument> args)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, I'm using the exception thrown during rendering as the mechanism to inform users that value-based sorts aren't supported. Since I've only updated call sites where value-based sorts are explicitly supported, any Render call that returns a BsonDocument will naturally fail in unsupported contexts, keeping this PR's changes minimal. However, Alex raised a point that when we update Render to return BsonValue in 4.0, we'll need to add validation at all call sites to check for value-based sort support before rendering. The question is whether to implement this validation now or defer it to 4.0. Implementing it now would distribute some of the 4.0 migration work and provide users with cleaner exceptions higher in the stack trace (and potentially a better exception type). I suppose we'll probably want to do that but what's the preference? @BorisDog @rstam

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the way you have it now is fine.

We can address whatever changes (if any) are needed later.

Most places that currently call Render should work with a BsonValue also. If not there is some work to be done a the CALL site, not here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @rstam, we can defer the work to 4.0.

Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

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

Minor changes requested.

@@ -47,6 +47,9 @@ public abstract class SortDefinition<TDocument>
/// <returns>A <see cref="BsonDocument"/>.</returns>
public abstract BsonDocument Render(RenderArgs<TDocument> args);

// TODO: remove this and refactor Render to return a BsonValue in 4.0
internal virtual BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => Render(args);
Copy link
Contributor

Choose a reason for hiding this comment

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

The RenderAsBsonValue method should probably be placed after the Render method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It already is? Maybe you got a defective diff

Copy link
Contributor

Choose a reason for hiding this comment

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

I tagged the wrong one. It's the one on line 311 of SortDefinitionBuilder.cs that's out of order.

{
internal static class SortDirectionExtensions
{
internal static BsonValue ToBsonValue(this SortDirection direction)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this method should probably be named Render.


internal override BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => _direction.ToBsonValue();

public override BsonDocument Render(RenderArgs<TDocument> args)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the way you have it now is fine.

We can address whatever changes (if any) are needed later.

Most places that currently call Render should work with a BsonValue also. If not there is some work to be done a the CALL site, not here.


internal override BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => _direction.ToBsonValue();

public override BsonDocument Render(RenderArgs<TDocument> args)
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @rstam, we can defer the work to 4.0.

{
internal static class SortDirectionExtensions
{
internal static BsonValue ToBsonValue(this SortDirection direction)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, would be good to use it in other places as well, like DirectionalIndexKeyDefinition.

@adelinowona adelinowona requested review from rstam and BorisDog August 13, 2025 00:43
{
internal static BsonValue Render(this SortDirection direction)
{
return direction switch
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: you can use expression body definition.

{
var subject = CreateSubject<BsonDocument>();

Assert(subject.Ascending(), "{direction: 1}");
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix the tests, and add new one to test exception?

@adelinowona adelinowona requested a review from BorisDog August 14, 2025 07:22
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

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

Looks good with one minor change requested.

@@ -47,6 +47,9 @@ public abstract class SortDefinition<TDocument>
/// <returns>A <see cref="BsonDocument"/>.</returns>
public abstract BsonDocument Render(RenderArgs<TDocument> args);

// TODO: remove this and refactor Render to return a BsonValue in 4.0
internal virtual BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => Render(args);
Copy link
Contributor

Choose a reason for hiding this comment

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

I tagged the wrong one. It's the one on line 311 of SortDefinitionBuilder.cs that's out of order.

_direction = direction;
}

internal override BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => _direction.Render();
Copy link
Contributor

Choose a reason for hiding this comment

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

Move RenderAsBsonValue to after Render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@adelinowona adelinowona requested a review from rstam August 14, 2025 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants