From 959200d141e11020f9980dc8f5c45da026c584fa Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Wed, 30 Sep 2020 17:45:20 -0400 Subject: [PATCH] Maintain order for uniques() - fixes #3235 Also add tests --- .../refine/expr/functions/arrays/Uniques.java | 26 ++++++-------- .../expr/functions/arrays/UniquesTests.java | 36 ++++++++++++++++++- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/main/src/com/google/refine/expr/functions/arrays/Uniques.java b/main/src/com/google/refine/expr/functions/arrays/Uniques.java index ed78bd370..f5414c485 100644 --- a/main/src/com/google/refine/expr/functions/arrays/Uniques.java +++ b/main/src/com/google/refine/expr/functions/arrays/Uniques.java @@ -33,7 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package com.google.refine.expr.functions.arrays; -import java.util.HashSet; +import java.util.Arrays; +import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; import java.util.Set; @@ -51,25 +52,18 @@ public class Uniques implements Function { public Object call(Properties bindings, Object[] args) { if (args.length == 1) { Object v = args[0]; - + if (v != null) { if (v instanceof ArrayNode) { v = JSONUtilities.toArray((ArrayNode) v); } - - if (v.getClass().isArray() || v instanceof List) { - Set set = null; - - if (v.getClass().isArray()) { - Object[] a = (Object[]) v; - - set = new HashSet(a.length); - for (Object element : a) { - set.add(element); - } - } else { - set = new HashSet(ExpressionUtils.toObjectList(v)); - } + Set set = null; + if (v.getClass().isArray()) { + set = new LinkedHashSet(Arrays.asList((Object[]) v)); + } else if (v instanceof List) { + set = new LinkedHashSet(ExpressionUtils.toObjectList(v)); + } + if (set != null) { return set.toArray(); } } diff --git a/main/tests/server/src/com/google/refine/expr/functions/arrays/UniquesTests.java b/main/tests/server/src/com/google/refine/expr/functions/arrays/UniquesTests.java index ca85c6e37..e592ece17 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/arrays/UniquesTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/arrays/UniquesTests.java @@ -28,13 +28,47 @@ package com.google.refine.expr.functions.arrays; import org.testng.annotations.Test; +import com.google.refine.RefineTest; +import com.google.refine.expr.ParsingException; import com.google.refine.util.TestUtils; -public class UniquesTests { +public class UniquesTests extends RefineTest { @Test public void serializeUniques() { String json = "{\"description\":\"Returns array a with duplicates removed\",\"params\":\"array a\",\"returns\":\"array\"}"; TestUtils.isSerializedTo(new Uniques(), json); } + + public void uniquesJsonArray() throws ParsingException { + String[] test = {"'[2,1,1,3]'.parseJson().uniques().toString()", "[2, 1, 3]"}; + parseEval(bindings, test); + String[] test1 = {"'[2,2,null,null,3,3]'.parseJson().uniques().toString()", "[2, null, 3]"}; + parseEval(bindings, test1); + } + + @Test + public void uniquesArray() throws ParsingException { + String[] test = {"[2,1,1,3].uniques().toString()", "[2, 1, 3]"}; + parseEval(bindings, test); + String[] test1 = {"[2,2,null,null,3,3,3].uniques().toString()", "[2, null, 3]"}; + parseEval(bindings, test1); + + String[] test2 = {"['z','b','c','c','a'].uniques().toString()", "[z, b, c, a]"}; + parseEval(bindings, test2); + String[] test3 = {"['z','z',null,'c','a'].uniques().toString()", "[z, null, c, a]"}; + parseEval(bindings, test3); + + String[] test4 = {"[toDate(2020), '2018-03-02'.toDate(), toDate(2020)].uniques().toString()", "[2020-01-01T00:00Z, 2018-03-02T00:00Z]"}; + parseEval(bindings, test4); + } + + @Test + public void uniquesMixedArray() throws ParsingException { + String[] test = {"[2,1.0,3,1.0].uniques().toString()", "[2, 1.0, 3]"}; + parseEval(bindings, test); + String[] test1 = {"[2,'a',3,3,'a'].uniques().toString()", "[2, a, 3]"}; + parseEval(bindings, test1); + } + }