Github user ottobackwards commented on a diff in the pull request: https://github.com/apache/metron/pull/652#discussion_r127245624 --- 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 -- math fight!
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---