Path: redb/redb.Tests.Integration/Tests/Unit/SqlStatementTextTests.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Unit/SqlStatementTextTests.cs ---- using redb.Core.Pro.Query; namespace redb.Tests.Integration.Tests.Unit; /// /// A tree count drops the ordering of the query it counts by cutting at the statement's ORDER BY. An ORDER BY inside an /// aggregate (the ordered fold of a collection field), a subquery or a CTE body is not the statement's: cutting there /// left the statement unfinished. /// public class SqlStatementTextTests { [Fact] public void TheStatementsOwnOrderBy_IsFound_AfterOnesInsideAggregatesAndSubqueries() { const string sql = "WITH t AS (SELECT a FROM x ORDER BY a) SELECT json_group_array(v.c ORDER BY v.i) FROM t ORDER BY 1 LIMIT 5"; SqlStatementText.LastTopLevelIndexOf(sql, "ORDER BY").Should().Be(sql.LastIndexOf("ORDER BY 1", StringComparison.Ordinal)); } [Fact] public void AStatementWithoutItsOwnOrderBy_GivesMinusOne_ThoughAnAggregateIsOrdered() { SqlStatementText.LastTopLevelIndexOf("SELECT array_agg(v.c ORDER BY v.i) FROM v", "ORDER BY").Should().Be(-1); SqlStatementText.LastTopLevelIndexOf("SELECT STRING_AGG(c, ',') WITHIN GROUP (ORDER BY i) FROM v", "ORDER BY").Should().Be(-1); } [Fact] public void QuotedTextAndParenthesesInLiterals_DoNotCount() { const string sql = "SELECT 'ORDER BY (' AS s, [a)b] FROM v WHERE n = 'it''s' ORDER BY s"; SqlStatementText.LastTopLevelIndexOf(sql, "ORDER BY").Should().Be(sql.LastIndexOf("ORDER BY s", StringComparison.Ordinal)); } [Fact] public void TheStartBoundsTheSearch() { const string sql = "SELECT a FROM v ORDER BY a"; SqlStatementText.LastTopLevelIndexOf(sql, "ORDER BY", sql.Length - 3).Should().Be(-1); } }