Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@JKamsker
Copy link
Collaborator

This pull request introduces support for multi-key sorting in queries by allowing multiple OrderBy and ThenBy clauses. The changes update the query structure and pipeline to handle complex ordering, add new methods to the queryable interface, and extend SQL parsing for multiple sort keys. Comprehensive tests have also been added to verify correct behavior. The most important changes are grouped below.

Query Structure and Pipeline Updates

  • The Query class now supports multiple sort keys by replacing the single OrderBy/Order properties with a list of QueryOrder objects. This enables multi-key sorting in queries. (LiteDB/Engine/Query/Query.cs, [LiteDB/Engine/Query/Query.csL20-R20](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-97611a73f837562092daf228e86fdccda3742a76e9e702ccb56856b690f3e344L20-R20))
  • The query pipeline (BasePipe, GroupByPipe, QueryPipe) is updated to process multiple sort keys, supporting both single and multi-key sorting logic. (LiteDB/Engine/Query/Pipeline/BasePipe.cs, [[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-6461767771fdec0b3edd95e9e2606bfcdbaf495ec3b4e8989ba07a41d857962bL157-R200); LiteDB/Engine/Query/Pipeline/GroupByPipe.cs, [[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-c886a057f90e8eafb7fb2d8e1d214a8dfbbd7904d6febc27efc5ce544d401c6bL42-R42); LiteDB/Engine/Query/Pipeline/QueryPipe.cs, [[3]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-7e4950815f9584c929339777670c8257ae513f2d3218d4029602618621b530baL49-R49))

Queryable Interface and LINQ-style Methods

  • The ILiteQueryable<T> interface and its implementation now include ThenBy and ThenByDescending methods for chaining multiple sort keys, matching LINQ conventions. Error handling is improved for multiple OrderBy calls. (LiteDB/Client/Database/ILiteQueryable.cs, [[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-a07a996a9d353dc6e776893fa3488c5cf7918c0dac423762c1ad2fb3b213659eR23-R26); LiteDB/Client/Database/LiteQueryable.cs, [[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-abeb070f07ebf2ff584f4e6ffdd108467ff216610c0fc2fe25d7330c87ec4523L106-R112), [[3]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-abeb070f07ebf2ff584f4e6ffdd108467ff216610c0fc2fe25d7330c87ec4523L126-R171))

SQL Parser Enhancements

  • The SQL parser now supports parsing multiple ORDER BY keys, handling comma-separated sort expressions and their respective orders. (LiteDB/Client/SqlParser/Commands/Select.cs, [[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-5e935952ff4227268c8e10fe4d4975a71b01830f62ca156e362623de0abcbd70R129-R130), [[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-5e935952ff4227268c8e10fe4d4975a71b01830f62ca156e362623de0abcbd70L139-R152))

Testing and Validation

  • New and updated tests verify multi-key sorting logic, including correct ordering and query plan validation for chained OrderBy/ThenBy clauses. (LiteDB.Tests/Query/OrderBy_Tests.cs, [LiteDB.Tests/Query/OrderBy_Tests.csR109-R182](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-a375641c4901acfdc058526aa00a1470e3c2f181198d394d6339723548fe7201R109-R182))

Miscellaneous Improvements

  • Supporting changes to utility classes and query optimization ensure compatibility with the new multi-key sorting, including updates to SQL output and index selection logic. (LiteDB/Engine/Query/Query.cs, [[1]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-97611a73f837562092daf228e86fdccda3742a76e9e702ccb56856b690f3e344L87-R91); LiteDB/Engine/Query/QueryOptimization.cs, [[2]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-c820145a8c73cc470681110e42d7aa416ef2a4d3259f7ee61df3b54e944dd36bL153-R156), [[3]](https://github.com/litedb-org/LiteDB/pull/2632/files#diff-c820145a8c73cc470681110e42d7aa416ef2a4d3259f7ee61df3b54e944dd36bL280-R288))

Let me know if you want a deeper walkthrough of any part of the new multi-key sorting implementation!

@JKamsker JKamsker changed the title Feat/composite sorting Add Composite sorting: OrderBy(...).ThenBy(...) Sep 21, 2025
@JKamsker JKamsker merged commit fa00f17 into litedb-org:dev Sep 21, 2025
1 check passed
@JKamsker JKamsker deleted the feat/composite-sorting branch September 21, 2025 20:47
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.

1 participant