Skip to content
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

CSHARP-5515: Implement $convert binData <-> int, long #1637

Closed
wants to merge 49 commits into from

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Mar 18, 2025

No description provided.

@papafe papafe requested a review from rstam March 18, 2025 10:12
@papafe
Copy link
Contributor Author

papafe commented Mar 18, 2025

@rstam This is a draft, as there are a couple of things missing, but I wanted to get some initial feedback.
Regarding the ConvertToBinDataFromType(Type value, BsonBinarySubType subtype, string format, BsonBinaryData onError, BsonBinaryData onNull) methods, I'm unsure if we should allow onError and onNull to be nulls.
What do you think?

@papafe papafe marked this pull request as ready for review March 24, 2025 14:51
@papafe papafe requested a review from a team as a code owner March 24, 2025 14:51
{
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

Copy link
Contributor

@rstam rstam Mar 24, 2025

Choose a reason for hiding this comment

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

By the way, in the comment before the previous one (with the 3 scenarios), the Optional<T> class was designed precisely to tell the difference between an explicit null value and not passing a value at all.

When you have a method like foo(int? arg = null) you can't tell the difference between the following two:

foo(); // arg omitted and defaults to `null`
foo(null); // explicit null provided

But it the method is declared foo(Optional<int?> arg = default(Optional<int?>)) you can:

foo(); // arg omitted and defaults to `default(Optional<int?>)` (which is not the same as `null`)
foo(null); // explicit null provided

But... Boris doesn't like Optional<T> so that might be a non-starter.

@papafe papafe requested a review from rstam March 25, 2025 17:49
/// <param name="subtype">The BsonBinaryData subtype of the result value.</param>
/// <param name="format">The format string.</param>
/// <returns>The converted value.</returns>
public static BsonBinaryData ToBinData(string value, BsonBinarySubType subtype, string format)
Copy link
Contributor

Choose a reason for hiding this comment

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

I've been calling this method ToBsonBinaryData but you keep using ToBinData.

I understand it's shorter, but it's not accurate. There is no data type called BinData.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

/// <param name="subtype">The BsonBinaryData subtype of the result value.</param>
/// <param name="byteOrder">The byte order of BsonBinaryData.</param>
/// <returns>The converted value.</returns>
public static BsonBinaryData ToBinData(int? value, BsonBinarySubType subtype, ByteOrder byteOrder)
Copy link
Contributor

Choose a reason for hiding this comment

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

We need separate overloads for int and int?.

A property in a user's POCO might be an int or it might be an int?.

I realize there is an automatic conversion from int to int? but I don't know if that's how we would want to handle that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for long and double.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

/// <param name="value">The value.</param>
/// <param name="byteOrder">The byte ordering of BsonBinaryData.</param>
/// <returns>The converted value.</returns>
public static int? ToNullableInt(BsonBinaryData value, ByteOrder byteOrder)
Copy link
Contributor

Choose a reason for hiding this comment

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

We need separate ToInt32 and ToNullableInt32 methods so that the user can control the type of the result.

They might not want the result to be nullable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

{
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ferdinando and I discussed in Slack an approach with an options class that would allow the user to write something like the following:

Mql.ToInt(x.BinaryField, ByteOrder.LittleEndian))
Mql.ToInt(x.BinaryField, ByteOrder.LittleEndian, new() { OnError. = 0 })
Mql.ToInt(x.BinaryField, ByteOrder.LittleEndian, new() { OnNull = 0 })
Mql.ToInt(x.BinaryField, ByteOrder.LittleEndian, new() { OnError = 0, OnNull = 0 }))

This would require only two overloads. One with options and one without.

The options class can keep track whether OnError or OnNull were set or not.

public BsonBinaryData BinaryProperty { get; set; }
public double? DoubleProperty { get; set; }
public int? IntProperty { get; set; }
public long? LongProperty { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

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

These properties are named incorrectly because they are nullable. They should be named:

public double? NullableDoubleProperty { get; set; }     
public int? NullableIntProperty { get; set; }
public long? NullableLongProperty { get; set; }

At the same time, we need to add the following three NON-nullable properties to test against:

public double DoubleProperty { get; set; }     
public int IntProperty { get; set; }
public long LongProperty { get; set; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@papafe papafe requested a review from rstam March 26, 2025 15: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.

Quick initial pass. More thorough review later today.

@@ -44,6 +44,8 @@ public class Feature
private static readonly Feature __clientBulkWrite = new Feature("ClientBulkWrite", WireVersion.Server80);
private static readonly Feature __clientSideEncryption = new Feature("ClientSideEncryption", WireVersion.Server42);
private static readonly Feature __clusteredIndexes = new Feature("ClusteredIndexes", WireVersion.Server53);
private static readonly Feature __convertOperatorBinDataToFromNumeric = new Feature("ConvertBinDataToFromNumeric", WireVersion.Server81);
private static readonly Feature __convertOperatorBinDataToFromString= new Feature("ConvertBinDataToFromString", WireVersion.Server80);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: change string constants to match feature name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

private readonly AstExpression _onError;
private readonly AstExpression _onNull;
private readonly string _format;
private readonly ConvertOptions _options;
Copy link
Contributor

Choose a reason for hiding this comment

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

Undo all these changes at the AST level.

These changes are not necessary and the AST level should be self contained and not depend on any classes like ConvertOptions defined at a higher level in the public API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see your point, I restored it to the older version. I wasn't sure of at what level we wanted to make the conversion.

@papafe papafe closed this Apr 2, 2025
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