Maintain order for uniques() - fixes #3235

Also add tests
This commit is contained in:
Tom Morris 2020-09-30 17:45:20 -04:00
parent bb4fc50f17
commit 959200d141
2 changed files with 45 additions and 17 deletions

View File

@ -33,7 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.expr.functions.arrays; 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.List;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@ -56,20 +57,13 @@ public class Uniques implements Function {
if (v instanceof ArrayNode) { if (v instanceof ArrayNode) {
v = JSONUtilities.toArray((ArrayNode) v); v = JSONUtilities.toArray((ArrayNode) v);
} }
if (v.getClass().isArray() || v instanceof List<?>) {
Set<Object> set = null; Set<Object> set = null;
if (v.getClass().isArray()) { if (v.getClass().isArray()) {
Object[] a = (Object[]) v; set = new LinkedHashSet<Object>(Arrays.asList((Object[]) v));
} else if (v instanceof List<?>) {
set = new HashSet<Object>(a.length); set = new LinkedHashSet<Object>(ExpressionUtils.toObjectList(v));
for (Object element : a) {
set.add(element);
}
} else {
set = new HashSet<Object>(ExpressionUtils.toObjectList(v));
} }
if (set != null) {
return set.toArray(); return set.toArray();
} }
} }

View File

@ -28,13 +28,47 @@ package com.google.refine.expr.functions.arrays;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.refine.RefineTest;
import com.google.refine.expr.ParsingException;
import com.google.refine.util.TestUtils; import com.google.refine.util.TestUtils;
public class UniquesTests { public class UniquesTests extends RefineTest {
@Test @Test
public void serializeUniques() { public void serializeUniques() {
String json = "{\"description\":\"Returns array a with duplicates removed\",\"params\":\"array a\",\"returns\":\"array\"}"; String json = "{\"description\":\"Returns array a with duplicates removed\",\"params\":\"array a\",\"returns\":\"array\"}";
TestUtils.isSerializedTo(new Uniques(), json); 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);
}
} }