22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
44import static org .junit .jupiter .api .Assertions .assertNotNull ;
5+ import static org .junit .jupiter .api .Assertions .assertThrows ;
56import static org .mockito .quality .Strictness .LENIENT ;
67
78import java .util .List ;
1213import org .hypertrace .core .grpcutils .context .RequestContext ;
1314import org .hypertrace .entity .query .service .converter .accessor .ExpressionOneOfAccessor ;
1415import org .hypertrace .entity .query .service .converter .accessor .OneOfAccessor ;
16+ import org .hypertrace .entity .query .service .converter .identifier .FunctionAliasProvider ;
1517import org .hypertrace .entity .query .service .converter .identifier .IdentifierAliasProvider ;
1618import org .hypertrace .entity .query .service .v1 .ColumnIdentifier ;
1719import org .hypertrace .entity .query .service .v1 .Expression ;
1820import org .hypertrace .entity .query .service .v1 .Expression .ValueCase ;
21+ import org .hypertrace .entity .query .service .v1 .Function ;
1922import org .hypertrace .entity .query .service .v1 .OrderByExpression ;
2023import org .junit .jupiter .api .BeforeEach ;
2124import org .junit .jupiter .api .Test ;
@@ -35,6 +38,7 @@ class OrderByConverterTest {
3538 private Converter <List <OrderByExpression >, Sort > orderByConverter ;
3639
3740 private final AliasProvider <ColumnIdentifier > aliasProvider = new IdentifierAliasProvider ();
41+ private final AliasProvider <Function > functionAliasProvider = new FunctionAliasProvider ();
3842 private final ColumnIdentifier columnIdentifier =
3943 ColumnIdentifier .newBuilder ().setColumnName ("Planet_Mars" ).build ();
4044 private final IdentifierExpression identifierExpression = IdentifierExpression .of ("Planet_Mars" );
@@ -47,7 +51,8 @@ class OrderByConverterTest {
4751 @ BeforeEach
4852 void setup () {
4953 OneOfAccessor <Expression , ValueCase > expressionAccessor = new ExpressionOneOfAccessor ();
50- orderByConverter = new OrderByConverter (expressionAccessor , aliasProvider );
54+ orderByConverter =
55+ new OrderByConverter (expressionAccessor , aliasProvider , functionAliasProvider );
5156 }
5257
5358 @ Test
@@ -57,6 +62,32 @@ void testConvert() throws ConversionException {
5762 assertEquals (expected , orderByConverter .convert (List .of (orderByExpression ), requestContext ));
5863 }
5964
65+ @ Test
66+ void testConvert_functionExpression () throws ConversionException {
67+ IdentifierExpression aliasExpression = IdentifierExpression .of ("agg_count" );
68+ OrderByExpression orderByExpressionWithAlias =
69+ OrderByExpression .newBuilder ()
70+ .setExpression (
71+ Expression .newBuilder ()
72+ .setFunction (Function .newBuilder ().setAlias ("agg_count" ).build ()))
73+ .setOrder (org .hypertrace .entity .query .service .v1 .SortOrder .ASC )
74+ .build ();
75+
76+ Sort expected =
77+ Sort .builder ().sortingSpec (SortingSpec .of (aliasExpression , SortOrder .ASC )).build ();
78+ assertEquals (
79+ expected , orderByConverter .convert (List .of (orderByExpressionWithAlias ), requestContext ));
80+
81+ OrderByExpression orderByExpressionWithoutAlias =
82+ OrderByExpression .newBuilder ()
83+ .setExpression (Expression .newBuilder ().setFunction (Function .newBuilder ().build ()))
84+ .setOrder (org .hypertrace .entity .query .service .v1 .SortOrder .ASC )
85+ .build ();
86+ assertThrows (
87+ IllegalArgumentException .class ,
88+ () -> orderByConverter .convert (List .of (orderByExpressionWithoutAlias ), requestContext ));
89+ }
90+
6091 @ ParameterizedTest
6192 @ EnumSource (
6293 value = org .hypertrace .entity .query .service .v1 .SortOrder .class ,
0 commit comments