Skip to content

Commit 6b156fb

Browse files
committed
Some WIP work on prefix comparison.
1 parent 7f875f9 commit 6b156fb

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

core/src/main/java/org/apache/spark/util/collection/unsafe/sort/PrefixComparator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
package org.apache.spark.util.collection.unsafe.sort;
1919

20+
import org.apache.spark.annotation.Private;
21+
2022
/**
2123
* Compares 8-byte key prefixes in prefix sort. Subclasses may implement type-specific
2224
* comparisons, such as lexicographic comparison for strings.
2325
*/
26+
@Private
2427
public abstract class PrefixComparator {
2528
public abstract int compare(long prefix1, long prefix2);
2629
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.util.collection.unsafe.sort;
19+
20+
import org.apache.spark.annotation.Private;
21+
22+
@Private
23+
public class PrefixComparators {
24+
private PrefixComparators() {}
25+
26+
public static final IntPrefixComparator INTEGER = new IntPrefixComparator();
27+
28+
static final class IntPrefixComparator extends PrefixComparator {
29+
@Override
30+
public int compare(long aPrefix, long bPrefix) {
31+
int a = (int) aPrefix;
32+
int b = (int) bPrefix;
33+
return (a < b) ? -1 : (a > b) ? 1 : 0;
34+
}
35+
36+
public long computePrefix(int value) {
37+
return value & 0xffffffffL;
38+
}
39+
}
40+
41+
static final class LongPrefixComparator extends PrefixComparator {
42+
@Override
43+
public int compare(long a, long b) {
44+
return (a < b) ? -1 : (a > b) ? 1 : 0;
45+
}
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.util.collection.unsafe.sort;
19+
20+
import org.junit.Test;
21+
import static org.junit.Assert.*;
22+
23+
public class PrefixComparatorsSuite {
24+
25+
private static int genericComparison(Comparable a, Comparable b) {
26+
return a.compareTo(b);
27+
}
28+
29+
@Test
30+
public void intPrefixComparator() {
31+
int[] testData = new int[] { 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 1, 2, -1, -2, 1024};
32+
for (int a : testData) {
33+
for (int b : testData) {
34+
long aPrefix = PrefixComparators.INTEGER.computePrefix(a);
35+
long bPrefix = PrefixComparators.INTEGER.computePrefix(b);
36+
assertEquals(
37+
"Wrong prefix comparison results for a=" + a + " b=" + b,
38+
genericComparison(a, b),
39+
PrefixComparators.INTEGER.compare(aPrefix, bPrefix));
40+
41+
}
42+
}
43+
}
44+
45+
}

sql/core/src/main/scala/org/apache/spark/sql/execution/basicOperators.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.spark.sql.execution
1919

2020
import org.apache.spark.sql.types.StructType
21-
import org.apache.spark.util.collection.unsafe.sort.PrefixComparator
2221
import org.apache.spark.annotation.DeveloperApi
2322
import org.apache.spark.rdd.{RDD, ShuffledRDD}
2423
import org.apache.spark.shuffle.sort.SortShuffleManager
@@ -28,6 +27,7 @@ import org.apache.spark.sql.catalyst.expressions._
2827
import org.apache.spark.sql.Row
2928
import org.apache.spark.sql.catalyst.plans.physical._
3029
import org.apache.spark.util.collection.ExternalSorter
30+
import org.apache.spark.util.collection.unsafe.sort.PrefixComparator
3131
import org.apache.spark.util.{CompletionIterator, MutablePair}
3232
import org.apache.spark.{HashPartitioner, SparkEnv}
3333

0 commit comments

Comments
 (0)