Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,13 @@ boolean isValid() {
* @return the location of the point
*/
Location compare(Comparable point) {
int minCompare = point.compareTo(lower);
int minCompare = compareKeys(point, lower);
if (minCompare < 0) {
return Location.BEFORE;
} else if (minCompare == 0) {
return onlyLowerBound ? Location.BEFORE : Location.MIN;
}
int maxCompare = point.compareTo(upper);
int maxCompare = compareKeys(point, upper);
if (maxCompare > 0) {
return Location.AFTER;
} else if (maxCompare == 0) {
Expand All @@ -542,6 +542,30 @@ Location compare(Comparable point) {
return Location.MIDDLE;
}

private static int compareKeys(Comparable point, Comparable bound) {
if (!(point instanceof String left) || !(bound instanceof String right)) {
return point.compareTo(bound);
}
// UTF-8 byte order agrees with Unicode code point order for well-formed strings.
int limit = Math.min(left.length(), right.length());
for (int i = 0; i < limit; ) {
int leftCodePoint = left.codePointAt(i);
int rightCodePoint = right.codePointAt(i);
if (leftCodePoint != rightCodePoint) {
if (isUnpairedSurrogate(leftCodePoint) || isUnpairedSurrogate(rightCodePoint)) {
return new Text(left).compareTo(new Text(right));
}
return Integer.compare(leftCodePoint, rightCodePoint);
}
i += Character.charCount(leftCodePoint);
}
return Integer.compare(left.length(), right.length());
}

private static boolean isUnpairedSurrogate(int codePoint) {
return codePoint >= Character.MIN_SURROGATE && codePoint <= Character.MAX_SURROGATE;
}

/**
* Is this range a single point?
* @return true if min == max
Expand Down
19 changes: 19 additions & 0 deletions java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,25 @@ public void testPredEvalWithStringStats() throws Exception {
}
}

@Test
public void testStringStatisticsUseUtf8Order() {
String bmp = "\uE100";
String supplementary = new String(Character.toChars(0x10437));

PredicateLeaf atMostBmp = createPredicateLeaf(PredicateLeaf.Operator.LESS_THAN_EQUALS,
PredicateLeaf.Type.STRING, "x", bmp, null);
TypeDescription type = TypeDescription.createString();
assertEquals(TruthValue.NO, RecordReaderImpl.evaluatePredicateProto(
createStringStats(supplementary, supplementary, false), atMostBmp, null,
null, null, OrcFile.WriterVersion.ORC_135, type));

PredicateLeaf equalToSupplementary = createPredicateLeaf(PredicateLeaf.Operator.EQUALS,
PredicateLeaf.Type.STRING, "x", supplementary, null);
assertEquals(TruthValue.YES_NO, RecordReaderImpl.evaluatePredicateProto(
createStringStats(bmp, supplementary, false), equalToSupplementary, null,
null, null, OrcFile.WriterVersion.ORC_135, type));
}

@Test
public void testPredEvalWithDateStats() throws Exception {
PredicateLeaf pred = createPredicateLeaf(
Expand Down