Github user cestella commented on a diff in the pull request:
https://github.com/apache/metron/pull/652#discussion_r127240904
--- Diff:
metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/FunctionalFunctionsTest.java
---
@@ -24,13 +24,124 @@
import org.junit.Assert;
import org.junit.Test;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import static
org.apache.metron.stellar.common.utils.StellarProcessorUtils.run;
public class FunctionalFunctionsTest {
@Test
+ public void testZipLongest_boundary() {
+ for (String expr : ImmutableList.of( "ZIP_LONGEST()"
+ , "ZIP_LONGEST( null, null )"
+ , "ZIP_LONGEST( [], null )"
+ , "ZIP_LONGEST( [], [] )"
+ , "ZIP_LONGEST( null, [] )"
+ )
+ )
+ {
+ List<List<Object>> o = (List<List<Object>>) run(expr, new
HashMap<>());
+ Assert.assertEquals(0, o.size());
+ }
+ }
+
+ @Test
+ public void testZip_longest() {
+ Map<String, Object> variables = ImmutableMap.of(
+ "list1" , ImmutableList.of(1, 2, 3)
+ ,"list2", ImmutableList.of(4, 5, 6, 7)
+ );
+ for (String expr : ImmutableList.of( "ZIP_LONGEST(list1, list2)"
+ , "ZIP_LONGEST( [1, 2, 3], [4, 5, 6, 7] )"
+ )
+ )
+ {
+ List<List<Object>> o = (List<List<Object>>) run(expr, variables);
+ Assert.assertEquals(4, o.size());
+ for (int i = 0; i < 3; ++i) {
+ List l = o.get(i);
+ Assert.assertEquals(2, l.size());
+ Assert.assertEquals(i+1, l.get(0));
+ Assert.assertEquals(i+4, l.get(1));
+ }
+ {
+ int i = 3;
+ List l = o.get(i);
+ Assert.assertEquals(2, l.size());
+ Assert.assertNull(l.get(0));
+ Assert.assertEquals(i+4, l.get(1));
+ }
+ }
+
+
+ for (String expr : ImmutableList.of(
+ "REDUCE(ZIP_LONGEST(list2, list1), (s, x) -> s + GET_FIRST(x)
* GET_LAST(x), 0)"
+ , "REDUCE(ZIP_LONGEST( [1, 2, 3], [4, 5, 6, 7] ), (s, x) -> s
+ GET_FIRST(x) * GET_LAST(x), 0)"
+ , "REDUCE(ZIP_LONGEST(list1, list2), (s, x) -> s +
GET_FIRST(x) * GET_LAST(x), 0)" //this works because stellar treats nulls as 0
in arithmetic operations.
+ , "REDUCE(ZIP_LONGEST(list1, list2), (s, x) -> s +
(GET_FIRST(x) == null?0:GET_FIRST(x)) * (GET_LAST(x) == null?0:GET_LAST(x)),
0)" //with proper guarding NOT assuming stellar peculiarities
+ )
+ )
+ {
+ int o = (int) run(expr, variables);
+ Assert.assertEquals(1*4 + 2*5 + 3*6, o, 1e-7);
--- End diff --
`reduce(zip(a, reversed(b)), (s, x) -> s + x[0]*x[1], 0)` would yield `1*6
+ 2*5 + 3*1 == 19` which is not `1*4 + 2*5 + 3*6 == 32`, so it is ensuring that
the lists align. The previous case involving evaluating jsut the output of
`ZIP([1,2,3], [4, 5, 6])` takes care of the ordering per-element (`1` is in the
first spot and `4` is in the second spot, etc)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---