List of usage examples for java.lang Byte compare
public static int compare(byte x, byte y)
From source file:org.apache.flink.table.codegen.SortCodeGeneratorTest.java
private void testInner() throws Exception { List<MemorySegment> segments = new ArrayList<>(); for (int i = 0; i < 100; i++) { segments.add(MemorySegmentFactory.wrap(new byte[32768])); }//from w ww. j a v a 2 s . co m InternalType[] fieldTypes = getFieldTypes(); InternalType[] keyTypes = getKeyTypes(); Tuple2<NormalizedKeyComputer, RecordComparator> tuple2 = getSortBaseWithNulls( this.getClass().getSimpleName(), keyTypes, keys, orders, nullsIsLast); BinaryRowSerializer serializer = new BinaryRowSerializer(fieldTypes.length); BinaryInMemorySortBuffer sortBuffer = BinaryInMemorySortBuffer.createBuffer(tuple2.f0, (AbstractRowSerializer) serializer, serializer, tuple2.f1, segments); BinaryRow[] dataArray = getTestData(); List<BinaryRow> data = Arrays.asList(dataArray.clone()); List<BinaryRow> binaryRows = Arrays.asList(dataArray.clone()); Collections.shuffle(binaryRows); for (BinaryRow row : binaryRows) { if (!sortBuffer.write(row)) { throw new RuntimeException(); } } new QuickSort().sort(sortBuffer); MutableObjectIterator<BinaryRow> iter = sortBuffer.getIterator(); List<BinaryRow> result = new ArrayList<>(); BinaryRow row = serializer.createInstance(); while ((row = iter.next(row)) != null) { result.add(row.copy()); } data.sort((o1, o2) -> { for (int i = 0; i < keys.length; i++) { InternalType t = types[fields[keys[i]]]; boolean order = orders[i]; Object first = null; Object second = null; if (!o1.isNullAt(keys[i])) { first = TypeGetterSetters.get(o1, keys[i], keyTypes[i]); } if (!o2.isNullAt(keys[i])) { second = TypeGetterSetters.get(o2, keys[i], keyTypes[i]); } if (first == null && second == null) { } else if (first == null) { return order ? -1 : 1; } else if (second == null) { return order ? 1 : -1; } else if (first instanceof Comparable) { int ret = ((Comparable) first).compareTo(second); if (ret != 0) { return order ? ret : -ret; } } else if (t instanceof ArrayType) { BinaryArray leftArray = (BinaryArray) first; BinaryArray rightArray = (BinaryArray) second; int minLength = Math.min(leftArray.numElements(), rightArray.numElements()); for (int j = 0; j < minLength; j++) { boolean isNullLeft = leftArray.isNullAt(j); boolean isNullRight = rightArray.isNullAt(j); if (isNullLeft && isNullRight) { // Do nothing. } else if (isNullLeft) { return order ? -1 : 1; } else if (isNullRight) { return order ? 1 : -1; } else { int comp = Byte.compare(leftArray.getByte(j), rightArray.getByte(j)); if (comp != 0) { return order ? comp : -comp; } } } if (leftArray.numElements() < rightArray.numElements()) { return order ? -1 : 1; } else if (leftArray.numElements() > rightArray.numElements()) { return order ? 1 : -1; } } else if (t.equals(InternalTypes.BINARY)) { int comp = org.apache.flink.table.runtime.sort.SortUtil.compareBinary((byte[]) first, (byte[]) second); if (comp != 0) { return order ? comp : -comp; } } else if (t instanceof RowType) { RowType rowType = (RowType) t; int comp; if (rowType.getTypeAt(0).equals(InternalTypes.INT)) { comp = INT_ROW_COMP.compare(INT_ROW_CONV.toExternal(first), INT_ROW_CONV.toExternal(second)); } else { comp = NEST_ROW_COMP.compare(NEST_ROW_CONV.toExternal(first), NEST_ROW_CONV.toExternal(second)); } if (comp != 0) { return order ? comp : -comp; } } else if (t instanceof GenericType) { Integer i1 = BinaryGeneric.getJavaObjectFromBinaryGeneric((BinaryGeneric) first, IntSerializer.INSTANCE); Integer i2 = BinaryGeneric.getJavaObjectFromBinaryGeneric((BinaryGeneric) second, IntSerializer.INSTANCE); int comp = Integer.compare(i1, i2); if (comp != 0) { return order ? comp : -comp; } } else { throw new RuntimeException(); } } return 0; }); StringBuilder builder = new StringBuilder(); for (int i = 0; i < data.size(); i++) { builder.append("\n").append("expect: ").append(data.get(i).toOriginString(fieldTypes)) .append("; actual: ").append(result.get(i).toOriginString(fieldTypes)); } builder.append("\n").append("types: ").append(Arrays.asList(fieldTypes)); builder.append("\n").append("keys: ").append(Arrays.toString(keys)); String msg = builder.toString(); for (int i = 0; i < data.size(); i++) { for (int j = 0; j < keys.length; j++) { boolean isNull1 = data.get(i).isNullAt(keys[j]); boolean isNull2 = result.get(i).isNullAt(keys[j]); Assert.assertEquals(msg, isNull1, isNull2); if (!isNull1 || !isNull2) { Object o1 = TypeGetterSetters.get(data.get(i), keys[j], keyTypes[j]); Object o2 = TypeGetterSetters.get(result.get(i), keys[j], keyTypes[j]); if (keyTypes[j].equals(InternalTypes.BINARY)) { Assert.assertArrayEquals(msg, (byte[]) o1, (byte[]) o2); } else { Assert.assertEquals(msg, o1, o2); } } } } }