Merge pull request #1535 from metamx/alphanum-docs-tests

Update alphanumeric sort docs + more tests / examples
This commit is contained in:
Himanshu 2015-07-16 22:09:41 -05:00
commit 19af3bc9bc
2 changed files with 44 additions and 20 deletions

View File

@ -47,7 +47,7 @@ The grammar for dimension values sorted lexicographically is as follows:
## AlphaNumeric TopNMetricSpec
Sort dimension values in alpha-numeric order, i.e treating numbers differently from other characters in sorting the values.
See [http://www.davekoelle.com/alphanum.html](http://www.davekoelle.com/alphanum.html) for details on how the algorithm works.
The algorithm is based on [https://github.com/amjjd/java-alphanum](https://github.com/amjjd/java-alphanum).
```json
"metric": {

View File

@ -18,14 +18,16 @@
package io.druid.query.topn;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import io.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
public class AlphaNumericTopNMetricSpecTest
{
@ -36,33 +38,55 @@ public class AlphaNumericTopNMetricSpecTest
final Comparator<String> comparator = AlphaNumericTopNMetricSpec.comparator;
// equality
assertEquals(0, comparator.compare("", ""));
assertEquals(0, comparator.compare("abc", "abc"));
assertEquals(0, comparator.compare("123", "123"));
assertEquals(0, comparator.compare("abc123", "abc123"));
Assert.assertEquals(0, comparator.compare("", ""));
Assert.assertEquals(0, comparator.compare("abc", "abc"));
Assert.assertEquals(0, comparator.compare("123", "123"));
Assert.assertEquals(0, comparator.compare("abc123", "abc123"));
// empty strings < non-empty
assertTrue(comparator.compare("", "abc") < 0);
assertTrue(comparator.compare("abc", "") > 0);
Assert.assertTrue(comparator.compare("", "abc") < 0);
Assert.assertTrue(comparator.compare("abc", "") > 0);
// numbers < non numeric
assertTrue(comparator.compare("123", "abc") < 0);
assertTrue(comparator.compare("abc", "123") > 0);
Assert.assertTrue(comparator.compare("123", "abc") < 0);
Assert.assertTrue(comparator.compare("abc", "123") > 0);
// numbers ordered numerically
assertTrue(comparator.compare("2", "11") < 0);
assertTrue(comparator.compare("a2", "a11") < 0);
Assert.assertTrue(comparator.compare("2", "11") < 0);
Assert.assertTrue(comparator.compare("a2", "a11") < 0);
// leading zeroes
assertTrue(comparator.compare("02", "11") < 0);
assertTrue(comparator.compare("02", "002") < 0);
Assert.assertTrue(comparator.compare("02", "11") < 0);
Assert.assertTrue(comparator.compare("02", "002") < 0);
// decimal points ...
assertTrue(comparator.compare("1.3", "1.5") < 0);
Assert.assertTrue(comparator.compare("1.3", "1.5") < 0);
// ... don't work too well
assertTrue(comparator.compare("1.3", "1.15") < 0);
Assert.assertTrue(comparator.compare("1.3", "1.15") < 0);
// but you can sort ranges
List<String> sorted = Lists.newArrayList("1-5", "11-15", "16-20", "21-25", "26-30", "6-10", "Other");
Collections.sort(sorted, comparator);
Assert.assertEquals(
ImmutableList.of("1-5", "6-10", "11-15", "16-20", "21-25", "26-30", "Other"),
sorted
);
List<String> sortedFixedDecimal = Lists.newArrayList(
"Other", "[0.00-0.05)", "[0.05-0.10)", "[0.10-0.50)", "[0.50-1.00)",
"[1.00-5.00)", "[5.00-10.00)", "[10.00-20.00)"
);
Collections.sort(sortedFixedDecimal, comparator);
Assert.assertEquals(
ImmutableList.of(
"[0.00-0.05)", "[0.05-0.10)", "[0.10-0.50)", "[0.50-1.00)",
"[1.00-5.00)", "[5.00-10.00)", "[10.00-20.00)", "Other"
),
sortedFixedDecimal
);
}
@Test
@ -79,7 +103,7 @@ public class AlphaNumericTopNMetricSpecTest
ObjectMapper jsonMapper = new DefaultObjectMapper();
TopNMetricSpec actualMetricSpec = jsonMapper.readValue(jsonMapper.writeValueAsString(jsonMapper.readValue(jsonSpec, TopNMetricSpec.class)), AlphaNumericTopNMetricSpec.class);
TopNMetricSpec actualMetricSpec1 = jsonMapper.readValue(jsonMapper.writeValueAsString(jsonMapper.readValue(jsonSpec1, TopNMetricSpec.class)), AlphaNumericTopNMetricSpec.class);
assertEquals(expectedMetricSpec, actualMetricSpec);
assertEquals(expectedMetricSpec1, actualMetricSpec1);
Assert.assertEquals(expectedMetricSpec, actualMetricSpec);
Assert.assertEquals(expectedMetricSpec1, actualMetricSpec1);
}
}