Implementation:Risingwavelabs Risingwave PkComparator
Metadata
| Property | Value |
|---|---|
| File | java/connector-node/connector-api/src/main/java/com/risingwave/connector/api/PkComparator.java
|
| Language | Java |
| Module | connector-api |
| Package | com.risingwave.connector.api
|
| Classes | PkComparator
|
| Lines | 43 |
| Interfaces Implemented | Comparator<List<Comparable<Object>>>
|
Overview
PkComparator implements the Comparator interface for comparing two lists of Comparable values, representing composite primary keys in the RisingWave connector framework. It performs a lexicographic comparison of the primary key columns, checking each column in order and returning the first non-zero comparison result.
If the two primary key lists have different lengths, the comparator throws a gRPC FAILED_PRECONDITION runtime exception, since mismatched primary key lengths indicate a data integrity issue.
Code Reference
Source Location
java/connector-node/connector-api/src/main/java/com/risingwave/connector/api/PkComparator.java
Signature
public class PkComparator implements Comparator<List<Comparable<Object>>> {
@Override
public int compare(List<Comparable<Object>> objects1, List<Comparable<Object>> objects2)
}
Imports
import io.grpc.Status;
import java.util.Comparator;
import java.util.List;
I/O Contract
compare
| Parameter | Type | Description |
|---|---|---|
objects1 |
List<Comparable<Object>> |
First composite primary key (list of column values) |
objects2 |
List<Comparable<Object>> |
Second composite primary key (list of column values) |
| Direction | Type | Description |
|---|---|---|
| Output | int |
Negative if objects1 < objects2, zero if equal, positive if objects1 > objects2
|
Error Handling:
- Throws
StatusRuntimeExceptionwithFAILED_PRECONDITIONstatus if the two lists have different sizes, indicating a primary key length mismatch.
Algorithm:
- Verify both lists have the same length
- Iterate through each column index, comparing corresponding values
- Return the first non-zero comparison result
- If all columns are equal, return 0
Usage Examples
// Create a PkComparator instance
PkComparator comparator = new PkComparator();
// Compare two composite primary keys
List<Comparable<Object>> pk1 = Arrays.asList(castToComparable(1), castToComparable("abc"));
List<Comparable<Object>> pk2 = Arrays.asList(castToComparable(1), castToComparable("def"));
int result = comparator.compare(pk1, pk2);
// result < 0 because "abc" < "def"
// Can be used with sorting collections
TreeMap<List<Comparable<Object>>, SinkRow> sortedRows = new TreeMap<>(new PkComparator());
Related Pages
- TableSchema - Provides primary key column information used with PkComparator
- SinkRow Interface - The row interface whose primary key values are compared
- SinkWriterV1 Interface - Sink writers that may use PkComparator for deduplication and upsert logic