@@ -55,6 +55,10 @@ public void runBeforeAll() throws Exception {
5555 + "DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES('replication_num'='1')" );
5656 createTable ("CREATE TABLE uuid_topn (id INT, u UUID) DUPLICATE KEY(id) "
5757 + "DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES('replication_num'='1')" );
58+ createTable ("CREATE TABLE topn_nested_fields (id INT, "
59+ + "ts STRUCT<a: TIMESTAMPTZ(0), b: TIMESTAMPTZ(0)>, "
60+ + "si STRUCT<a: INT, b: INT>, sd STRUCT<a: DATE, b: DATE>) DUPLICATE KEY(id) "
61+ + "DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES('replication_num'='1')" );
5862 }
5963
6064 @ Test
@@ -129,6 +133,42 @@ public void testTopNFilterCrossesGroupedKeyAggregation() {
129133 }
130134 }
131135
136+ @ Test
137+ public void testTopNFilterSupportsNestedOrderKeyWithSiblingPredicate () {
138+ double previousRatio = connectContext .getSessionVariable ().topnFilterRatio ;
139+ boolean previousPrune = connectContext .getSessionVariable ().enablePruneNestedColumns ;
140+ String previousDisabledRules = String .join ("," ,
141+ connectContext .getSessionVariable ().getDisableNereidsRuleNames ());
142+ connectContext .getSessionVariable ().topnFilterRatio = 1000000 ;
143+ connectContext .getSessionVariable ().enablePruneNestedColumns = true ;
144+ // Empty unit-test tables must retain scans to exercise nested access-path translation.
145+ connectContext .getSessionVariable ().setDisableNereidsRules (previousDisabledRules + ",PRUNE_EMPTY_PARTITION" );
146+ try {
147+ for (String column : List .of ("ts" , "si" , "sd" )) {
148+ for (String direction : List .of ("ASC" , "DESC" )) {
149+ String sql = "SELECT struct_element(" + column + ", 'a') FROM topn_nested_fields "
150+ + "WHERE struct_element(" + column + ", 'b') IS NOT NULL "
151+ + "ORDER BY 1 " + direction + " NULLS LAST LIMIT 1" ;
152+ PlanChecker .from (connectContext ).checkPlannerResult (sql , planner -> {
153+ String explain = planner .getFragments ().stream ()
154+ .map (fragment -> fragment .getExplainString (TExplainLevel .NORMAL ))
155+ .collect (Collectors .joining ("\n " ));
156+ // TopN must retain its order field when the predicate reads only a sibling's nulls.
157+ Assertions .assertTrue (explain .contains ("TOPN OPT:" ), explain );
158+ Assertions .assertTrue (explain .contains ("all access paths: ["
159+ + column + ".a, " + column + ".b.NULL]" ), explain );
160+ Assertions .assertTrue (explain .contains ("predicate access paths: ["
161+ + column + ".b.NULL]" ), explain );
162+ });
163+ }
164+ }
165+ } finally {
166+ connectContext .getSessionVariable ().topnFilterRatio = previousRatio ;
167+ connectContext .getSessionVariable ().enablePruneNestedColumns = previousPrune ;
168+ connectContext .getSessionVariable ().setDisableNereidsRules (previousDisabledRules );
169+ }
170+ }
171+
132172 @ Test
133173 public void testUuidTopNFilterPreservesOrderingAndNullPlacement () {
134174 double previousRatio = connectContext .getSessionVariable ().topnFilterRatio ;
0 commit comments