Skip to content

Commit d4e8bd2

Browse files
authored
add array function (#539)
1 parent 1908100 commit d4e8bd2

6 files changed

Lines changed: 234 additions & 0 deletions

File tree

geaflow/geaflow-dsl/geaflow-dsl-plan/src/main/java/com/antgroup/geaflow/dsl/schema/function/BuildInSqlFunctionTable.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
import com.antgroup.geaflow.dsl.udf.table.agg.PercentileDouble;
3838
import com.antgroup.geaflow.dsl.udf.table.agg.PercentileInteger;
3939
import com.antgroup.geaflow.dsl.udf.table.agg.PercentileLong;
40+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayAppend;
41+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayContains;
42+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayDistinct;
43+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayUnion;
4044
import com.antgroup.geaflow.dsl.udf.table.date.AddMonths;
4145
import com.antgroup.geaflow.dsl.udf.table.date.DateAdd;
4246
import com.antgroup.geaflow.dsl.udf.table.date.DateDiff;
@@ -145,6 +149,12 @@ public class BuildInSqlFunctionTable extends ListSqlOperatorTable {
145149
.add(GeaFlowFunction.of(WeekOfYear.class))
146150
.add(GeaFlowFunction.of(Year.class))
147151

152+
// udf.table.array
153+
.add(GeaFlowFunction.of(ArrayAppend.class))
154+
.add(GeaFlowFunction.of(ArrayContains.class))
155+
.add(GeaFlowFunction.of(ArrayDistinct.class))
156+
.add(GeaFlowFunction.of(ArrayUnion.class))
157+
148158
// udf.table.math
149159
.add(GeaFlowFunction.of(E.class))
150160
.add(GeaFlowFunction.of(Log2.class))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.antgroup.geaflow.dsl.udf.table.array;
21+
22+
import com.antgroup.geaflow.dsl.common.function.Description;
23+
import com.antgroup.geaflow.dsl.common.function.UDF;
24+
25+
@Description(name = "array_append", description = "Append other element to input array.")
26+
public class ArrayAppend extends UDF {
27+
28+
public Object[] eval(Object[] input, Object otherElement) {
29+
Object[] res = new Object[input.length + 1];
30+
for (int i = 0; i < input.length; i++) {
31+
res[i] = input[i];
32+
}
33+
res[res.length - 1] = otherElement;
34+
return res;
35+
}
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.antgroup.geaflow.dsl.udf.table.array;
21+
22+
import com.antgroup.geaflow.dsl.common.function.Description;
23+
import com.antgroup.geaflow.dsl.common.function.UDF;
24+
25+
import java.util.Objects;
26+
27+
@Description(name = "array_contains", description = "Judge if the input element exists in array.")
28+
public class ArrayContains extends UDF {
29+
30+
public Boolean eval(Object[] array, Object e) {
31+
if (array == null || e == null) {
32+
return null;
33+
}
34+
35+
for (Object a : array) {
36+
if (Objects.equals(a, e)) {
37+
return true;
38+
}
39+
}
40+
return false;
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.antgroup.geaflow.dsl.udf.table.array;
21+
22+
import com.antgroup.geaflow.dsl.common.function.Description;
23+
import com.antgroup.geaflow.dsl.common.function.UDF;
24+
25+
import java.util.HashSet;
26+
import java.util.Set;
27+
28+
@Description(name = "array_distinct", description = "Dedup input array.")
29+
public class ArrayDistinct extends UDF {
30+
31+
public Object[] eval(Object[] input) {
32+
Set<Object> set = new HashSet<>();
33+
34+
for (Object o : input) {
35+
set.add(o);
36+
}
37+
return set.toArray(new Object[0]);
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.antgroup.geaflow.dsl.udf.table.array;
21+
22+
import com.antgroup.geaflow.dsl.common.function.Description;
23+
import com.antgroup.geaflow.dsl.common.function.UDF;
24+
import com.google.common.collect.Sets;
25+
26+
import java.util.Arrays;
27+
import java.util.Set;
28+
29+
@Description(name = "array_union", description = "Union two input array.")
30+
public class ArrayUnion extends UDF {
31+
32+
public Object[] eval(Object[] input1, Object[] input2) {
33+
if (input1 == null) {
34+
return input2;
35+
}
36+
if (input2 == null) {
37+
return input1;
38+
}
39+
Set<Object> result = Sets.newHashSet(input1);
40+
result.addAll(Arrays.asList(input2));
41+
return result.toArray();
42+
}
43+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.antgroup.geaflow.dsl.udf.array;
21+
22+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayAppend;
23+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayContains;
24+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayDistinct;
25+
import com.antgroup.geaflow.dsl.udf.table.array.ArrayUnion;
26+
import com.antgroup.geaflow.dsl.udf.table.string.RegExpExtract;
27+
import org.testng.Assert;
28+
import org.testng.annotations.Test;
29+
30+
public class ArrayTest {
31+
32+
@Test
33+
public void testArrayAppend() throws Exception {
34+
ArrayAppend udf = new ArrayAppend();
35+
Object[] input = new Object[]{1, 2, 4, -1};
36+
Object[] res = udf.eval(input, 6);
37+
Assert.assertEquals(res, new Object[]{1, 2, 4, -1, 6});
38+
}
39+
40+
@Test
41+
public void testArrayContains() throws Exception {
42+
ArrayContains udf = new ArrayContains();
43+
Object[] input = new Object[]{1, 2, 4, -1};
44+
Assert.assertTrue(udf.eval(input, 2));
45+
Assert.assertFalse(udf.eval(input, -3));
46+
}
47+
48+
@Test
49+
public void testArrayDistinct() throws Exception {
50+
ArrayDistinct udf = new ArrayDistinct();
51+
Object[] input = new Object[]{1, 2, 4, -1, 2, 1};
52+
Object[] res = udf.eval(input);
53+
Assert.assertEquals(res.length, 4);
54+
}
55+
56+
@Test
57+
public void testArrayUnion() throws Exception {
58+
ArrayUnion udf = new ArrayUnion();
59+
Object[] input1 = new Object[]{1, 2, 4, -1};
60+
Object[] input2 = new Object[]{1, 3, 5, 2, -4, -6};
61+
Object[] res = udf.eval(input1, input2);
62+
Assert.assertEquals(res.length, 8);
63+
}
64+
}

0 commit comments

Comments
 (0)