Skip to content

Commit 84f2677

Browse files
committed
test: Add E2E tests for array functions
1 parent 52ad58a commit 84f2677

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.facebook.presto.nativetests;
15+
16+
import com.facebook.presto.nativeworker.PrestoNativeQueryRunnerUtils;
17+
import com.facebook.presto.scalar.sql.SqlInvokedFunctionsPlugin;
18+
import com.facebook.presto.testing.QueryRunner;
19+
import com.facebook.presto.tests.AbstractTestQueryFramework;
20+
import org.testng.annotations.BeforeClass;
21+
import org.testng.annotations.Test;
22+
23+
import static com.facebook.presto.sidecar.NativeSidecarPluginQueryRunnerUtils.setupNativeSidecarPlugin;
24+
import static java.lang.Boolean.parseBoolean;
25+
26+
public class TestPrestoNativeArrayFunctionQueries
27+
extends AbstractTestQueryFramework
28+
{
29+
private String storageFormat;
30+
private boolean sidecarEnabled;
31+
32+
@BeforeClass
33+
@Override
34+
public void init()
35+
throws Exception
36+
{
37+
storageFormat = System.getProperty("storageFormat", "PARQUET");
38+
sidecarEnabled = parseBoolean(System.getProperty("sidecarEnabled", "true"));
39+
super.init();
40+
}
41+
42+
@Override
43+
protected QueryRunner createQueryRunner()
44+
throws Exception
45+
{
46+
QueryRunner queryRunner = PrestoNativeQueryRunnerUtils.nativeHiveQueryRunnerBuilder()
47+
.setStorageFormat(storageFormat)
48+
.setAddStorageFormatToPath(true)
49+
.setUseThrift(true)
50+
.setCoordinatorSidecarEnabled(sidecarEnabled)
51+
.build();
52+
if (sidecarEnabled) {
53+
setupNativeSidecarPlugin(queryRunner);
54+
}
55+
else {
56+
queryRunner.installPlugin(new SqlInvokedFunctionsPlugin());
57+
}
58+
return queryRunner;
59+
}
60+
61+
@Override
62+
protected QueryRunner createExpectedQueryRunner()
63+
throws Exception
64+
{
65+
QueryRunner queryRunner = PrestoNativeQueryRunnerUtils.javaHiveQueryRunnerBuilder()
66+
.setStorageFormat(storageFormat)
67+
.setAddStorageFormatToPath(true)
68+
.build();
69+
queryRunner.installPlugin(new SqlInvokedFunctionsPlugin());
70+
return queryRunner;
71+
}
72+
73+
@Test
74+
public void testArrayMaxBy()
75+
{
76+
assertQuery("SELECT array_max_by(a, x -> length(x)) from (values(ARRAY['a', 'bbb', 'cc'])) as t(a)");
77+
assertQuery("SELECT array_max_by(a, x -> length(x)) from (values(ARRAY['aa', 'bb', 'c'])) as t(a)");
78+
assertQuery("SELECT array_max_by(a, x -> length(x)) from (values(ARRAY['a', NULL, 'bbb'])) as t(a)");
79+
assertQuery("SELECT array_max_by(a, x -> length(x)) from (values(ARRAY[NULL, NULL])) as t(a)");
80+
assertQuery("SELECT array_max_by(a, x -> length(x)) from (values(ARRAY['aa', 'bb', 'c'])) as t(a)");
81+
assertQuery("SELECT array_max_by(a, x -> x) from (values(ARRAY[])) as t(a)");
82+
assertQuery("SELECT array_max_by(a, x -> abs(x)) from (values(ARRAY[-10, 5, 7])) as t(a)");
83+
assertQuery("SELECT array_max_by(a, x -> IF(x = 2, NULL, x)) from (values(ARRAY[1, 2, 3])) as t(a)");
84+
assertQuery("SELECT array_max_by(a, x -> x) from (values(CAST(NULL AS ARRAY(INTEGER)))) as t(a)");
85+
}
86+
87+
@Test
88+
public void testArrayMinBy()
89+
{
90+
assertQuery("SELECT array_min_by(a, x -> length(x)) from (values(ARRAY['a', 'bbb', 'cc'])) as t(a)");
91+
assertQuery("SELECT array_min_by(a, x -> length(x)) from (values(ARRAY['aa', 'bb', 'c'])) as t(a)");
92+
assertQuery("SELECT array_min_by(a, x -> length(x)) from (values(ARRAY['a', NULL, 'bbb'])) as t(a)");
93+
assertQuery("SELECT array_min_by(a, x -> length(x)) from (values(ARRAY[NULL, NULL])) as t(a)");
94+
assertQuery("SELECT array_min_by(a, x -> length(x)) from (values(ARRAY['aa', 'bb', 'c'])) as t(a)");
95+
assertQuery("SELECT array_min_by(a, x -> x) from (values(ARRAY[])) as t(a)");
96+
assertQuery("SELECT array_min_by(a, x -> abs(x)) from (values(ARRAY[-10, 5, 7])) as t(a)");
97+
assertQuery("SELECT array_min_by(a, x -> IF(x = 2, NULL, x)) from (values(ARRAY[1, 2, 3])) as t(a)");
98+
assertQuery("SELECT array_min_by(a, x -> x) from (values(CAST(NULL AS ARRAY(INTEGER)))) as t(a)");
99+
}
100+
101+
@Test
102+
public void testArrayTopN()
103+
{
104+
assertQuery("SELECT array_top_n(a, b) FROM (VALUES(ARRAY[1, 5, 3, 9, 2],3)) as t(a,b)");
105+
assertQuery("SELECT array_top_n(a, b) FROM (VALUES(ARRAY[1, 2], 5)) as t(a,b)");
106+
assertQuery("SELECT array_top_n(a, b) FROM (VALUES(ARRAY[5, 1, 5, 3], 2)) as t(a,b)");
107+
assertQuery("SELECT array_top_n(a, b) FROM (VALUES(ARRAY[1, NULL, 3, 2], 2)) as t(a,b)");
108+
assertQuery("SELECT array_top_n(a, b) FROM (VALUES(ARRAY[1, 2, 3], 0)) as t(a,b)");
109+
assertQuery("SELECT array_top_n(a, b) FROM (VALUES(ARRAY[], 2)) as t(a,b)");
110+
assertQuery("SELECT array_top_n(a, b) FROM (VALUES(CAST(NULL AS ARRAY(INTEGER)), 2)) as t(a,b)");
111+
assertQueryFails("SELECT array_top_n(a, b) FROM (VALUES(ARRAY[1, 2, 3], -2)) as t(a,b)",
112+
"n >= 0 \\(-2 vs\\. 0\\) Parameter n: -2 to ARRAY_TOP_N is negative Top-level Expression: (presto|native)\\.default\\.array_top_n\\(field, field_0\\)");
113+
}
114+
}

0 commit comments

Comments
 (0)