@@ -564,6 +564,54 @@ order by SessionId;)", "%Y-%m-%d %H:%M:%S %Z", sessionsSet.front().GetId().data(
564564 CompareYson (expected, StreamResultToYson (it));
565565 }
566566
567+ Y_UNIT_TEST (NodesOrderByDesc) {
568+ // Test to reproduce issue #12585: ORDER BY DESC doesn't work for sys views
569+ // The sys view actors ignore the direction flag and don't guarantee order
570+ TKikimrRunner kikimr (" " , KikimrDefaultUtDomainRoot, 5 );
571+ auto client = kikimr.GetQueryClient ();
572+ auto session = client.GetSession ().GetValueSync ().GetSession ();
573+
574+ ui32 offset = kikimr.GetTestServer ().GetRuntime ()->GetNodeId (0 );
575+
576+ auto result = session.ExecuteQuery (R"( --!syntax_v1
577+ SELECT NodeId, Host
578+ FROM `/Root/.sys/nodes`
579+ ORDER BY NodeId DESC
580+ )" , NYdb::NQuery::TTxControl::NoTx ()).GetValueSync ();
581+
582+ UNIT_ASSERT_C (result.IsSuccess (), result.GetIssues ().ToString ());
583+
584+ // Collect all results
585+ TVector<ui32> nodeIds;
586+ auto resultSet = result.GetResultSet (0 );
587+ NYdb::TResultSetParser parser (resultSet);
588+ while (parser.TryNextRow ()) {
589+ auto nodeId = parser.ColumnParser (" NodeId" ).GetOptionalUint32 ().value ();
590+ nodeIds.push_back (nodeId);
591+ }
592+
593+ // Verify we got all 5 nodes
594+ UNIT_ASSERT_VALUES_EQUAL (nodeIds.size (), 5 );
595+
596+ // Verify results are in descending order (this should fail if the bug exists)
597+ // According to issue #12585, sys view actors ignore the direction flag
598+ // and don't guarantee order, so this assertion should fail
599+ for (size_t i = 1 ; i < nodeIds.size (); ++i) {
600+ UNIT_ASSERT_C (nodeIds[i - 1 ] >= nodeIds[i],
601+ TStringBuilder () << " Results not in descending order: "
602+ << " nodeIds[" << (i - 1 ) << " ] = " << nodeIds[i - 1 ]
603+ << " < nodeIds[" << i << " ] = " << nodeIds[i]
604+ << " . ORDER BY DESC is being ignored by sys view actors." );
605+ }
606+
607+ // Verify exact expected order: offset+4, offset+3, offset+2, offset+1, offset
608+ UNIT_ASSERT_VALUES_EQUAL (nodeIds[0 ], offset + 4 );
609+ UNIT_ASSERT_VALUES_EQUAL (nodeIds[1 ], offset + 3 );
610+ UNIT_ASSERT_VALUES_EQUAL (nodeIds[2 ], offset + 2 );
611+ UNIT_ASSERT_VALUES_EQUAL (nodeIds[3 ], offset + 1 );
612+ UNIT_ASSERT_VALUES_EQUAL (nodeIds[4 ], offset);
613+ }
614+
567615 Y_UNIT_TEST (QueryStatsSimple) {
568616 auto checkTable = [&] (const TStringBuf tableName) {
569617 TKikimrRunner kikimr (" " , KikimrDefaultUtDomainRoot, 3 );
@@ -920,15 +968,15 @@ order by SessionId;)", "%Y-%m-%d %H:%M:%S %Z", sessionsSet.front().GetId().data(
920968 );
921969 )" ).ExtractValueSync ();
922970 UNIT_ASSERT_VALUES_EQUAL_C (createResult.GetStatus (), EStatus::SUCCESS, createResult.GetIssues ());
923-
971+
924972 auto insertResult = session.ExecuteDataQuery (R"(
925973 INSERT INTO TestTable (Key, Value) VALUES
926974 (42, "val"), (NULL, "val1");
927975 )" , TTxControl::BeginTx ().CommitTx ()).GetValueSync ();
928976 UNIT_ASSERT_C (insertResult.IsSuccess (), insertResult.GetIssues ().ToString ());
929977 }
930-
931-
978+
979+
932980 auto session = db.CreateSession ().GetValueSync ().GetSession ();
933981 TString query = R"(
934982 SELECT * FROM TestTable WHERE Key in [42, NULL];
0 commit comments