Merge pull request #36 from DataSketches/quantiles-to-string-udfs
quantiles sketch to string UDFs
diff --git a/src/main/java/com/yahoo/sketches/hive/quantiles/DoublesSketchToStringUDF.java b/src/main/java/com/yahoo/sketches/hive/quantiles/DoublesSketchToStringUDF.java
new file mode 100644
index 0000000..a3a63c5
--- /dev/null
+++ b/src/main/java/com/yahoo/sketches/hive/quantiles/DoublesSketchToStringUDF.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2019, Verizon Media.
+ * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
+ */
+
+package com.yahoo.sketches.hive.quantiles;
+
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.UDF;
+import org.apache.hadoop.io.BytesWritable;
+
+import com.yahoo.memory.Memory;
+import com.yahoo.sketches.quantiles.DoublesSketch;
+
+@Description(name = "ToString", value = "_FUNC_(sketch)",
+extended = " Returns a human-readable summary of a given DoublesSketch.")
+public class DoublesSketchToStringUDF extends UDF {
+
+ /**
+ * Returns a human-readable summary of a given sketch
+ * @param serializedSketch serialized sketch
+ * @return text summary
+ */
+ public String evaluate(final BytesWritable serializedSketch) {
+ if (serializedSketch == null) { return null; }
+ final DoublesSketch sketch = DoublesSketch.wrap(Memory.wrap(serializedSketch.getBytes()));
+ return sketch.toString();
+ }
+
+}
diff --git a/src/main/java/com/yahoo/sketches/hive/quantiles/StringsSketchToStringUDF.java b/src/main/java/com/yahoo/sketches/hive/quantiles/StringsSketchToStringUDF.java
new file mode 100644
index 0000000..b3d9fd4
--- /dev/null
+++ b/src/main/java/com/yahoo/sketches/hive/quantiles/StringsSketchToStringUDF.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2019, Verizon Media.
+ * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
+ */
+
+package com.yahoo.sketches.hive.quantiles;
+
+import java.util.Comparator;
+
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.UDF;
+import org.apache.hadoop.io.BytesWritable;
+
+import com.yahoo.memory.Memory;
+import com.yahoo.sketches.ArrayOfStringsSerDe;
+import com.yahoo.sketches.quantiles.ItemsSketch;
+
+@Description(name = "ToString", value = "_FUNC_(sketch)",
+extended = " Returns a human-readable summary of a given ItemsSketch<String>.")
+public class StringsSketchToStringUDF extends UDF {
+
+ /**
+ * Returns a human-readable summary of a given sketch
+ * @param serializedSketch serialized sketch
+ * @return text summary
+ */
+ public String evaluate(final BytesWritable serializedSketch) {
+ if (serializedSketch == null) { return null; }
+ final ItemsSketch<String> sketch = ItemsSketch.getInstance(
+ Memory.wrap(serializedSketch.getBytes()),
+ Comparator.naturalOrder(),
+ new ArrayOfStringsSerDe()
+ );
+ return sketch.toString();
+ }
+
+}
diff --git a/src/test/java/com/yahoo/sketches/hive/quantiles/DoublesSektchToStringUDFTest.java b/src/test/java/com/yahoo/sketches/hive/quantiles/DoublesSektchToStringUDFTest.java
new file mode 100644
index 0000000..895d0df
--- /dev/null
+++ b/src/test/java/com/yahoo/sketches/hive/quantiles/DoublesSektchToStringUDFTest.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2019, Verizon Media.
+ * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
+ */
+
+package com.yahoo.sketches.hive.quantiles;
+
+import com.yahoo.sketches.quantiles.DoublesSketch;
+
+import org.testng.annotations.Test;
+import org.apache.hadoop.io.BytesWritable;
+import org.testng.Assert;
+
+public class DoublesSektchToStringUDFTest {
+
+ @Test
+ public void nullSketch() {
+ final String result = new DoublesSketchToStringUDF().evaluate(null);
+ Assert.assertNull(result);
+ }
+
+ @Test
+ public void normalCase() {
+ final DoublesSketch sketch = DoublesSketch.builder().build();
+ final String result = new DoublesSketchToStringUDF().evaluate(new BytesWritable(sketch.toByteArray()));
+ Assert.assertNotNull(result);
+ Assert.assertTrue(result.length() > 0);
+ }
+
+}
diff --git a/src/test/java/com/yahoo/sketches/hive/quantiles/StringsSketchToStringUDFTest.java b/src/test/java/com/yahoo/sketches/hive/quantiles/StringsSketchToStringUDFTest.java
new file mode 100644
index 0000000..0e70ad6
--- /dev/null
+++ b/src/test/java/com/yahoo/sketches/hive/quantiles/StringsSketchToStringUDFTest.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2019, Verizon Media.
+ * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
+ */
+
+package com.yahoo.sketches.hive.quantiles;
+
+import java.util.Comparator;
+
+import org.apache.hadoop.io.BytesWritable;
+
+import com.yahoo.sketches.ArrayOfItemsSerDe;
+import com.yahoo.sketches.ArrayOfStringsSerDe;
+import com.yahoo.sketches.quantiles.ItemsSketch;
+
+import org.testng.annotations.Test;
+import org.testng.Assert;
+
+public class StringsSketchToStringUDFTest {
+
+ static final Comparator<String> COMPARATOR = Comparator.naturalOrder();
+ static final ArrayOfItemsSerDe<String> SERDE = new ArrayOfStringsSerDe();
+
+ @Test
+ public void nullSketch() {
+ final String result = new StringsSketchToStringUDF().evaluate(null);
+ Assert.assertNull(result);
+ }
+
+ @Test
+ public void normalCase() {
+ final ItemsSketch<String> sketch = ItemsSketch.getInstance(COMPARATOR);
+ final String result = new StringsSketchToStringUDF().evaluate(new BytesWritable(sketch.toByteArray(SERDE)));
+ Assert.assertNotNull(result);
+ Assert.assertTrue(result.length() > 0);
+ }
+
+}