use of org.apache.flink.table.data.StringData in project flink by apache.
the class SortUtilTest method testNormalizedKey.
@Test
public void testNormalizedKey() {
int len = 10;
Random random = new Random();
MemorySegment[] segments = new MemorySegment[len];
MemorySegment[] compareSegs = new MemorySegment[len];
for (int i = 0; i < len; i++) {
segments[i] = MemorySegmentFactory.allocateUnpooledSegment(20);
compareSegs[i] = MemorySegmentFactory.allocateUnpooledSegment(20);
}
{
SortUtil.minNormalizedKey(segments[0], 0, 20);
SortUtil.maxNormalizedKey(segments[1], 0, 20);
for (int i = 0; i < len; i++) {
byte[] rndBytes = new byte[20];
random.nextBytes(rndBytes);
segments[2].put(0, rndBytes);
Assert.assertTrue(segments[0].compare(segments[2], 0, 0, 20) <= 0);
Assert.assertTrue(segments[1].compare(segments[2], 0, 0, 20) >= 0);
}
}
{
DecimalData[] arr = new DecimalData[len];
for (int i = 0; i < len; i++) {
arr[i] = DecimalData.fromBigDecimal(new BigDecimal(random.nextInt()), 18, 0);
SortUtil.putDecimalNormalizedKey(arr[i], segments[i], 0, 8);
}
Arrays.sort(arr, DecimalData::compareTo);
for (int i = 0; i < len; i++) {
SortUtil.putDecimalNormalizedKey(arr[i], compareSegs[i], 0, 8);
}
Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 8));
for (int i = 0; i < len; i++) {
Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 8));
}
}
{
Float[] arr = new Float[len];
for (int i = 0; i < len; i++) {
arr[i] = random.nextFloat();
SortUtil.putFloatNormalizedKey(arr[i], segments[i], 0, 4);
}
Arrays.sort(arr, Float::compareTo);
for (int i = 0; i < len; i++) {
SortUtil.putFloatNormalizedKey(arr[i], compareSegs[i], 0, 4);
}
Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 4));
for (int i = 0; i < len; i++) {
Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 4));
}
}
{
Double[] arr = new Double[len];
for (int i = 0; i < len; i++) {
arr[i] = random.nextDouble();
SortUtil.putDoubleNormalizedKey(arr[i], segments[i], 0, 8);
}
Arrays.sort(arr, Double::compareTo);
for (int i = 0; i < len; i++) {
SortUtil.putDoubleNormalizedKey(arr[i], compareSegs[i], 0, 8);
}
Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 8));
for (int i = 0; i < len; i++) {
Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 8));
}
}
{
BinaryStringData[] arr = new BinaryStringData[len];
for (int i = 0; i < len; i++) {
arr[i] = BinaryStringData.fromString(String.valueOf(random.nextLong()));
SortUtil.putStringNormalizedKey(arr[i], segments[i], 0, 8);
}
Arrays.sort(arr, StringData::compareTo);
for (int i = 0; i < len; i++) {
SortUtil.putStringNormalizedKey(arr[i], compareSegs[i], 0, 8);
}
Arrays.sort(segments, (o1, o2) -> o1.compare(o2, 0, 0, 8));
for (int i = 0; i < len; i++) {
Assert.assertTrue(compareSegs[i].equalTo(segments[i], 0, 0, 8));
}
}
}
use of org.apache.flink.table.data.StringData in project flink by apache.
the class EqualiserCodeGeneratorTest method testManyFields.
@Test
public void testManyFields() {
final LogicalType[] fieldTypes = IntStream.range(0, 499).mapToObj(i -> new VarCharType()).toArray(LogicalType[]::new);
final RecordEqualiser equaliser = new EqualiserCodeGenerator(fieldTypes).generateRecordEqualiser("ManyFields").newInstance(Thread.currentThread().getContextClassLoader());
final StringData[] fields = IntStream.range(0, 499).mapToObj(i -> StringData.fromString("Entry " + i)).toArray(StringData[]::new);
assertTrue(equaliser.equals(GenericRowData.of((Object[]) fields), GenericRowData.of((Object[]) fields)));
}
use of org.apache.flink.table.data.StringData in project flink by apache.
the class ListAggWsWithRetractAggFunctionTest method retractValues.
@Override
protected void retractValues(ListAggWsWithRetractAccumulator accumulator, List<StringData> values) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
AggregateFunction<StringData, ListAggWsWithRetractAccumulator> aggregator = getAggregator();
Method retractFunc = getRetractFunc();
Preconditions.checkArgument(values.size() % 2 == 0, "number of values must be an integer multiple of 2.");
for (int i = 0; i < values.size(); i += 2) {
StringData value = values.get(i + 1);
StringData delimiter = values.get(i);
retractFunc.invoke(aggregator, accumulator, delimiter, value);
}
}
use of org.apache.flink.table.data.StringData in project flink by apache.
the class ListAggWsWithRetractAggFunctionTest method accumulateValues.
@Override
protected ListAggWsWithRetractAccumulator accumulateValues(List<StringData> values) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
AggregateFunction<StringData, ListAggWsWithRetractAccumulator> aggregator = getAggregator();
ListAggWsWithRetractAccumulator accumulator = getAggregator().createAccumulator();
Method accumulateFunc = getAccumulateFunc();
Preconditions.checkArgument(values.size() % 2 == 0, "number of values must be an integer multiple of 2.");
for (int i = 0; i < values.size(); i += 2) {
StringData value = values.get(i + 1);
StringData delimiter = values.get(i);
accumulateFunc.invoke(aggregator, accumulator, delimiter, value);
}
return accumulator;
}
Aggregations