16
16
17
17
package com .amazon .opendistroforelasticsearch .sql .analysis ;
18
18
19
+ import static com .amazon .opendistroforelasticsearch .sql .ast .tree .Sort .NullOrder .NULL_FIRST ;
20
+ import static com .amazon .opendistroforelasticsearch .sql .ast .tree .Sort .NullOrder .NULL_LAST ;
19
21
import static com .amazon .opendistroforelasticsearch .sql .ast .tree .Sort .SortOption .DEFAULT_ASC ;
20
22
import static com .amazon .opendistroforelasticsearch .sql .ast .tree .Sort .SortOption .DEFAULT_DESC ;
23
+ import static com .amazon .opendistroforelasticsearch .sql .ast .tree .Sort .SortOrder .ASC ;
24
+ import static com .amazon .opendistroforelasticsearch .sql .ast .tree .Sort .SortOrder .DESC ;
21
25
import static com .amazon .opendistroforelasticsearch .sql .data .type .ExprCoreType .INTEGER ;
22
26
import static com .amazon .opendistroforelasticsearch .sql .data .type .ExprCoreType .STRING ;
23
27
import static org .junit .jupiter .api .Assertions .assertEquals ;
24
28
25
29
import com .amazon .opendistroforelasticsearch .sql .ast .dsl .AstDSL ;
30
+ import com .amazon .opendistroforelasticsearch .sql .ast .expression .Alias ;
31
+ import com .amazon .opendistroforelasticsearch .sql .ast .tree .Sort .SortOption ;
26
32
import com .amazon .opendistroforelasticsearch .sql .expression .DSL ;
27
33
import com .amazon .opendistroforelasticsearch .sql .expression .config .ExpressionConfig ;
28
34
import com .amazon .opendistroforelasticsearch .sql .expression .window .WindowDefinition ;
29
35
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalPlan ;
30
36
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalPlanDSL ;
31
37
import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalRelation ;
38
+ import com .amazon .opendistroforelasticsearch .sql .planner .logical .LogicalSort ;
32
39
import com .google .common .collect .ImmutableList ;
40
+ import com .google .common .collect .ImmutableMap ;
41
+ import java .util .Collections ;
33
42
import org .apache .commons .lang3 .tuple .ImmutablePair ;
34
43
import org .junit .jupiter .api .BeforeEach ;
35
44
import org .junit .jupiter .api .DisplayNameGeneration ;
@@ -76,7 +85,7 @@ void should_wrap_child_with_window_and_sort_operator_if_project_item_windowed()
76
85
AstDSL .function ("row_number" ),
77
86
ImmutableList .of (AstDSL .qualifiedName ("string_value" )),
78
87
ImmutableList .of (
79
- ImmutablePair .of ("DESC" , AstDSL .qualifiedName ("integer_value" ))))),
88
+ ImmutablePair .of (DEFAULT_DESC , AstDSL .qualifiedName ("integer_value" ))))),
80
89
analysisContext ));
81
90
}
82
91
@@ -91,4 +100,35 @@ void should_return_original_child_if_project_item_not_windowed() {
91
100
analysisContext ));
92
101
}
93
102
103
+ @ Test
104
+ void can_analyze_sort_options () {
105
+ // Mapping from input option to expected option after analysis
106
+ ImmutableMap <SortOption , SortOption > expects =
107
+ ImmutableMap .<SortOption , SortOption >builder ()
108
+ .put (new SortOption (null , null ), DEFAULT_ASC )
109
+ .put (new SortOption (ASC , null ), DEFAULT_ASC )
110
+ .put (new SortOption (DESC , null ), DEFAULT_DESC )
111
+ .put (new SortOption (null , NULL_FIRST ), DEFAULT_ASC )
112
+ .put (new SortOption (null , NULL_LAST ), new SortOption (ASC , NULL_LAST ))
113
+ .put (new SortOption (ASC , NULL_FIRST ), DEFAULT_ASC )
114
+ .put (new SortOption (DESC , NULL_FIRST ), new SortOption (DESC , NULL_FIRST ))
115
+ .put (new SortOption (DESC , NULL_LAST ), DEFAULT_DESC )
116
+ .build ();
117
+
118
+ expects .forEach ((option , expect ) -> {
119
+ Alias ast = AstDSL .alias (
120
+ "row_number" ,
121
+ AstDSL .window (
122
+ AstDSL .function ("row_number" ),
123
+ Collections .emptyList (),
124
+ ImmutableList .of (
125
+ ImmutablePair .of (option , AstDSL .qualifiedName ("integer_value" )))));
126
+
127
+ LogicalPlan plan = analyzer .analyze (ast , analysisContext );
128
+ LogicalSort sort = (LogicalSort ) plan .getChild ().get (0 );
129
+ assertEquals (expect , sort .getSortList ().get (0 ).getLeft (),
130
+ "Assertion failed on input option: " + option );
131
+ });
132
+ }
133
+
94
134
}
0 commit comments