diff --git a/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java b/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java index 34ffe45204..8b3ca7f0a7 100644 --- a/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java +++ b/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java @@ -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) { @@ -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 diff --git a/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java b/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java index 5c47111db8..ab5d5b2432 100644 --- a/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java +++ b/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java @@ -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(