Skip to content

Commit c5b7231

Browse files
committed
proper tests
1 parent 4d5003a commit c5b7231

File tree

12 files changed

+335
-138
lines changed

12 files changed

+335
-138
lines changed

table/src/main/java/tech/ydb/table/values/DecimalValue.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,33 @@ public int compareTo(Value<?> other) {
274274
if (other == null) {
275275
throw new IllegalArgumentException("Cannot compare with null value");
276276
}
277-
277+
278+
// Handle comparison with OptionalValue
279+
if (other instanceof OptionalValue) {
280+
OptionalValue otherOptional = (OptionalValue) other;
281+
282+
// Check that the item type matches this decimal type
283+
if (!getType().equals(otherOptional.getType().getItemType())) {
284+
throw new IllegalArgumentException(
285+
"Cannot compare DecimalValue with OptionalValue of different item type: " +
286+
getType() + " vs " + otherOptional.getType().getItemType());
287+
}
288+
289+
// Non-empty value is greater than empty optional
290+
if (!otherOptional.isPresent()) {
291+
return 1;
292+
}
293+
294+
// Compare with the wrapped value
295+
return compareTo(otherOptional.get());
296+
}
297+
278298
if (!(other instanceof DecimalValue)) {
279299
throw new IllegalArgumentException("Cannot compare DecimalValue with " + other.getClass().getSimpleName());
280300
}
281-
301+
282302
DecimalValue otherDecimal = (DecimalValue) other;
283-
303+
284304
// Handle special values first
285305
if (isNan() || otherDecimal.isNan()) {
286306
// NaN is not comparable, but we need to provide a consistent ordering
@@ -292,7 +312,7 @@ public int compareTo(Value<?> other) {
292312
}
293313
return -1;
294314
}
295-
315+
296316
if (isInf() && otherDecimal.isInf()) {
297317
return 0;
298318
}
@@ -302,7 +322,7 @@ public int compareTo(Value<?> other) {
302322
if (otherDecimal.isInf()) {
303323
return -1;
304324
}
305-
325+
306326
if (isNegativeInf() && otherDecimal.isNegativeInf()) {
307327
return 0;
308328
}
@@ -312,18 +332,18 @@ public int compareTo(Value<?> other) {
312332
if (otherDecimal.isNegativeInf()) {
313333
return 1;
314334
}
315-
335+
316336
// Compare finite values
317337
if (isNegative() != otherDecimal.isNegative()) {
318338
return isNegative() ? -1 : 1;
319339
}
320-
340+
321341
// Both have the same sign, compare magnitudes
322342
int highComparison = Long.compareUnsigned(high, otherDecimal.high);
323343
if (highComparison != 0) {
324344
return isNegative() ? -highComparison : highComparison;
325345
}
326-
346+
327347
int lowComparison = Long.compareUnsigned(low, otherDecimal.low);
328348
return isNegative() ? -lowComparison : lowComparison;
329349
}

table/src/main/java/tech/ydb/table/values/DictValue.java

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,43 @@ public int compareTo(Value<?> other) {
124124
if (other == null) {
125125
throw new IllegalArgumentException("Cannot compare with null value");
126126
}
127-
127+
128+
// Handle comparison with OptionalValue
129+
if (other instanceof OptionalValue) {
130+
OptionalValue otherOptional = (OptionalValue) other;
131+
132+
// Check that the item type matches this dict type
133+
if (!getType().equals(otherOptional.getType().getItemType())) {
134+
throw new IllegalArgumentException(
135+
"Cannot compare DictValue with OptionalValue of different item type: " +
136+
getType() + " vs " + otherOptional.getType().getItemType());
137+
}
138+
139+
// Non-empty value is greater than empty optional
140+
if (!otherOptional.isPresent()) {
141+
return 1;
142+
}
143+
144+
// Compare with the wrapped value
145+
return compareTo(otherOptional.get());
146+
}
147+
128148
if (!(other instanceof DictValue)) {
129149
throw new IllegalArgumentException("Cannot compare DictValue with " + other.getClass().getSimpleName());
130150
}
131-
151+
132152
DictValue otherDict = (DictValue) other;
133-
153+
134154
// Compare sizes first
135155
int sizeComparison = Integer.compare(items.size(), otherDict.items.size());
136156
if (sizeComparison != 0) {
137157
return sizeComparison;
138158
}
139-
159+
140160
// Convert to sorted lists for lexicographical comparison
141161
List<Map.Entry<Value<?>, Value<?>>> thisEntries = new ArrayList<>(items.entrySet());
142162
List<Map.Entry<Value<?>, Value<?>>> otherEntries = new ArrayList<>(otherDict.items.entrySet());
143-
163+
144164
// Sort entries by key first, then by value
145165
thisEntries.sort((e1, e2) -> {
146166
int keyComparison = compareValues(e1.getKey(), e2.getKey());
@@ -149,54 +169,62 @@ public int compareTo(Value<?> other) {
149169
}
150170
return compareValues(e1.getValue(), e2.getValue());
151171
});
152-
172+
153173
otherEntries.sort((e1, e2) -> {
154174
int keyComparison = compareValues(e1.getKey(), e2.getKey());
155175
if (keyComparison != 0) {
156176
return keyComparison;
157177
}
158178
return compareValues(e1.getValue(), e2.getValue());
159179
});
160-
180+
161181
// Compare sorted entries
162182
for (int i = 0; i < thisEntries.size(); i++) {
163183
Map.Entry<Value<?>, Value<?>> thisEntry = thisEntries.get(i);
164184
Map.Entry<Value<?>, Value<?>> otherEntry = otherEntries.get(i);
165-
185+
166186
int keyComparison = compareValues(thisEntry.getKey(), otherEntry.getKey());
167187
if (keyComparison != 0) {
168188
return keyComparison;
169189
}
170-
190+
171191
int valueComparison = compareValues(thisEntry.getValue(), otherEntry.getValue());
172192
if (valueComparison != 0) {
173193
return valueComparison;
174194
}
175195
}
176-
196+
177197
return 0;
178198
}
179-
199+
180200
private static int compareValues(Value<?> a, Value<?> b) {
181201
// Handle null values
182-
if (a == null && b == null) return 0;
183-
if (a == null) return -1;
184-
if (b == null) return 1;
185-
202+
if (a == null && b == null) {
203+
return 0;
204+
}
205+
if (a == null) {
206+
return -1;
207+
}
208+
if (b == null) {
209+
return 1;
210+
}
211+
186212
// Check that the types are the same
187213
if (!a.getType().equals(b.getType())) {
188-
throw new IllegalArgumentException("Cannot compare values of different types: " +
214+
throw new IllegalArgumentException("Cannot compare values of different types: " +
189215
a.getType() + " vs " + b.getType());
190216
}
191-
217+
192218
// Use the actual compareTo method of the values
193219
if (a instanceof Comparable && b instanceof Comparable) {
194220
try {
195221
return ((Comparable<Value<?>>) a).compareTo((Value<?>) b);
196-
} catch (ClassCastException e) {}
222+
} catch (ClassCastException e) {
223+
// Fall back to error
224+
}
197225
}
198-
199-
throw new IllegalArgumentException("Cannot compare values of different types: " +
226+
227+
throw new IllegalArgumentException("Cannot compare values of different types: " +
200228
a.getClass().getSimpleName() + " vs " + b.getClass().getSimpleName());
201229
}
202230
}

table/src/main/java/tech/ydb/table/values/ListValue.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,50 +119,78 @@ public int compareTo(Value<?> other) {
119119
if (other == null) {
120120
throw new IllegalArgumentException("Cannot compare with null value");
121121
}
122-
122+
123+
// Handle comparison with OptionalValue
124+
if (other instanceof OptionalValue) {
125+
OptionalValue otherOptional = (OptionalValue) other;
126+
127+
// Check that the item type matches this list type
128+
if (!getType().equals(otherOptional.getType().getItemType())) {
129+
throw new IllegalArgumentException(
130+
"Cannot compare ListValue with OptionalValue of different item type: " +
131+
getType() + " vs " + otherOptional.getType().getItemType());
132+
}
133+
134+
// Non-empty value is greater than empty optional
135+
if (!otherOptional.isPresent()) {
136+
return 1;
137+
}
138+
139+
// Compare with the wrapped value
140+
return compareTo(otherOptional.get());
141+
}
142+
123143
if (!(other instanceof ListValue)) {
124144
throw new IllegalArgumentException("Cannot compare ListValue with " + other.getClass().getSimpleName());
125145
}
126-
146+
127147
ListValue otherList = (ListValue) other;
128-
148+
129149
// Compare elements lexicographically
130150
int minLength = Math.min(items.length, otherList.items.length);
131151
for (int i = 0; i < minLength; i++) {
132152
Value<?> thisItem = items[i];
133153
Value<?> otherItem = otherList.items[i];
134-
154+
135155
int itemComparison = compareValues(thisItem, otherItem);
136156
if (itemComparison != 0) {
137157
return itemComparison;
138158
}
139159
}
140-
160+
141161
// If we reach here, one list is a prefix of the other
142162
// The shorter list comes first
143163
return Integer.compare(items.length, otherList.items.length);
144164
}
145-
165+
146166
private static int compareValues(Value<?> a, Value<?> b) {
147167
// Handle null values
148-
if (a == null && b == null) return 0;
149-
if (a == null) return -1;
150-
if (b == null) return 1;
151-
168+
if (a == null && b == null) {
169+
return 0;
170+
}
171+
if (a == null) {
172+
return -1;
173+
}
174+
if (b == null) {
175+
return 1;
176+
}
177+
152178
// Check that the types are the same
153179
if (!a.getType().equals(b.getType())) {
154-
throw new IllegalArgumentException("Cannot compare values of different types: " +
180+
throw new IllegalArgumentException("Cannot compare values of different types: " +
155181
a.getType() + " vs " + b.getType());
156182
}
157-
183+
158184
// Use the actual compareTo method of the values
159185
if (a instanceof Comparable && b instanceof Comparable) {
160186
try {
161187
return ((Comparable<Value<?>>) a).compareTo((Value<?>) b);
162-
} catch (ClassCastException e) {}
188+
} catch (ClassCastException e) {
189+
// Fall back to error
190+
}
163191
}
164-
165-
throw new IllegalArgumentException("Cannot compare values of different types: " +
192+
193+
throw new IllegalArgumentException("Cannot compare values of different types: " +
166194
a.getClass().getSimpleName() + " vs " + b.getClass().getSimpleName());
167195
}
168196
}

table/src/main/java/tech/ydb/table/values/NullValue.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,31 @@ public int compareTo(Value<?> other) {
3838
if (other == null) {
3939
throw new IllegalArgumentException("Cannot compare with null value");
4040
}
41-
41+
42+
// Handle comparison with OptionalValue
43+
if (other instanceof OptionalValue) {
44+
OptionalValue otherOptional = (OptionalValue) other;
45+
46+
// Check that the item type matches this null type
47+
if (!getType().equals(otherOptional.getType().getItemType())) {
48+
throw new IllegalArgumentException(
49+
"Cannot compare NullValue with OptionalValue of different item type: " +
50+
getType() + " vs " + otherOptional.getType().getItemType());
51+
}
52+
53+
// Non-empty value is greater than empty optional
54+
if (!otherOptional.isPresent()) {
55+
return 1;
56+
}
57+
58+
// Compare with the wrapped value
59+
return compareTo(otherOptional.get());
60+
}
61+
4262
if (!(other instanceof NullValue)) {
4363
throw new IllegalArgumentException("Cannot compare NullValue with " + other.getClass().getSimpleName());
4464
}
45-
65+
4666
// All NullValue instances are equal
4767
return 0;
4868
}

0 commit comments

Comments
 (0)