Skip to content

CSHARP-5818: Allow users to generate a hash from a UTF-8 string or binary data#1925

Open
sanych-sun wants to merge 1 commit intomongodb:mainfrom
sanych-sun:csharp5818
Open

CSHARP-5818: Allow users to generate a hash from a UTF-8 string or binary data#1925
sanych-sun wants to merge 1 commit intomongodb:mainfrom
sanych-sun:csharp5818

Conversation

@sanych-sun
Copy link
Member

No description provided.

@sanych-sun sanych-sun added the feature Adds new user-facing functionality. label Mar 25, 2026
@sanych-sun sanych-sun requested a review from a team as a code owner March 25, 2026 01:50
@sanych-sun sanych-sun requested review from Copilot and kyra-rk March 25, 2026 01:50
@sanych-sun sanych-sun requested review from adelinowona and removed request for kyra-rk March 25, 2026 01:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds LINQ (MQL) support for MongoDB’s $hash / $hexHash aggregation expressions, including serializer inference and integration/unit test coverage, gated behind a new server feature flag.

Changes:

  • Introduces Mql.Hash(...) / Mql.HexHash(...) APIs and MqlHashAlgorithm (with server algorithm name mapping).
  • Implements LINQ3 translation + AST nodes for $hash and $hexHash, plus serializer-deduction support.
  • Adds translator, serializer-finder, and integration tests validating translation/rendering and runtime behavior.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/HashMethodToAggregationExpressionTranslatorTests.cs New unit tests validating AST rendering + serializer type for Hash/HexHash.
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/SerializerFinders/MqlHexHashTests.cs New serializer-finder test ensuring Mql.HexHash resolves to StringSerializer.
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/SerializerFinders/MqlHashTests.cs New serializer-finder test ensuring Mql.Hash resolves to BsonBinaryDataSerializer.
tests/MongoDB.Driver.Tests/Linq/Integration/MqlSubtypeTests.cs Adjusts subtype integration expectations/projections (and test data) alongside new formatting.
tests/MongoDB.Driver.Tests/Linq/Integration/MqlHexHashTests.cs New integration tests for $hexHash across queryable/filter/projection/aggregate.
tests/MongoDB.Driver.Tests/Linq/Integration/MqlHashTests.cs New integration tests for $hash across queryable/filter/projection/aggregate.
src/MongoDB.Driver/MqlHashAlgorithm.cs Adds public enum for hash algorithms + internal extension mapping to server strings.
src/MongoDB.Driver/Mql.cs Adds public LINQ extension entry points Hash and HexHash.
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/HashMethodToAggregationExpressionTranslator.cs Adds method-call translation for Mql.Hash / Mql.HexHash.
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodCallExpressionToAggregationExpressionTranslator.cs Routes Hash / HexHash method calls to the new translator.
src/MongoDB.Driver/Linq/Linq3Implementation/SerializerFinders/SerializerFinderVisitMethodCall.cs Adds serializer deduction logic for Hash/HexHash method calls.
src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/MqlMethod.cs Adds reflection handles for Mql.Hash and Mql.HexHash.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs Adds visitor hooks for the new AST expression nodes.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstHexHashExpression.cs New AST node for $hexHash rendering.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstHashExpression.cs New AST node for $hash rendering.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs Adds AST factory helpers for HashExpression and HexHashExpression.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstNodeType.cs Adds node types for hash expressions.
src/MongoDB.Driver/Core/Misc/Feature.cs Adds Feature.HashOperator for server capability gating.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

renderedPipeline,
"{ '$match' : { '$expr' : { '$eq' : [ { '$subtype' : '$Data' }, 4 ] } } }",
"{ '$project' : { '_id' : '$_id', 'Subtype' : { '$subtype' : '$Data' } } }");
"{ $match : { $expr : { $eq : [{ '$subtype' : '$Data' }, 4 ]} } }",
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

In this expected pipeline stage, the $subtype operator key is quoted ({ '$subtype' : ... }) while the rest of this file’s expected renderings use unquoted keys ({ $subtype : ... }). This inconsistency will cause the string comparison to fail if the renderer outputs unquoted keys (as in the other assertions in this test). Update the expected stage to match the renderer’s formatting (use $subtype without quotes).

Suggested change
"{ $match : { $expr : { $eq : [{ '$subtype' : '$Data' }, 4 ]} } }",
"{ $match : { $expr : { $eq : [{ $subtype : '$Data' }, 4 ]} } }",

Copilot uses AI. Check for mistakes.
[Theory]
[InlineData("A12871FEE210FB8619291EAEA194581CBD2531E4B23759D225F6806923F63222", 1)]
[InlineData(null, 3)]
public void MqlSubtype_in_where(string hexHash, int expectedId)
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

The test name MqlSubtype_in_where appears to be a copy/paste mistake in this MqlHexHashTests fixture (it’s testing Mql.HexHash). Renaming it to something like MqlHexHash_in_where (or similar) would make the test intent clear and keep naming consistent with the other methods in this file.

Suggested change
public void MqlSubtype_in_where(string hexHash, int expectedId)
public void MqlHexHash_in_where(string hexHash, int expectedId)

Copilot uses AI. Check for mistakes.
Comment on lines +153 to +163
/// <param name="algorithm">The hashing algorithms.</param>
/// <returns>Generated hash as binary data.</returns>
public static BsonBinaryData Hash<TValue>(TValue value, MqlHashAlgorithm algorithm) =>
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();

/// <summary>
/// Generates a binary hash for an input expression.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="value">The value.</param>
/// <param name="algorithm">The hashing algorithms.</param>
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

The XML docs for Hash/HexHash have a couple of inaccuracies: (1) algorithm is described as "The hashing algorithms" (plural) but the parameter is a single algorithm, and (2) HexHash’s summary says "Generates a binary hash" even though it returns a hex string. Please adjust the docs to accurately describe the parameter and the return type/behavior.

Suggested change
/// <param name="algorithm">The hashing algorithms.</param>
/// <returns>Generated hash as binary data.</returns>
public static BsonBinaryData Hash<TValue>(TValue value, MqlHashAlgorithm algorithm) =>
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
/// <summary>
/// Generates a binary hash for an input expression.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="value">The value.</param>
/// <param name="algorithm">The hashing algorithms.</param>
/// <param name="algorithm">The hashing algorithm.</param>
/// <returns>Generated hash as binary data.</returns>
public static BsonBinaryData Hash<TValue>(TValue value, MqlHashAlgorithm algorithm) =>
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
/// <summary>
/// Generates a hexadecimal hash string for an input expression.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="value">The value.</param>
/// <param name="algorithm">The hashing algorithm.</param>

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds new user-facing functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants