Java Code Examples for org.apache.flink.api.common.typeutils.TypeComparator#compare()

The following examples show how to use org.apache.flink.api.common.typeutils.TypeComparator#compare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RowComparator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(Row first, Row second) {
	int i = 0;
	try {
		for (; i < keyPositions.length; i++) {
			int keyPos = keyPositions[i];
			TypeComparator<Object> comparator = comparators[i];
			Object firstElement = first.getField(keyPos);   // element can be null
			Object secondElement = second.getField(keyPos); // element can be null

			int cmp = comparator.compare(firstElement, secondElement);
			if (cmp != 0) {
				return cmp;
			}
		}
	} catch (IndexOutOfBoundsException e) {
		throw new KeyFieldOutOfBoundsException(keyPositions[i]);
	}
	return 0;
}
 
Example 2
Source File: RowComparator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(Row first, Row second) {
	int i = 0;
	try {
		for (; i < keyPositions.length; i++) {
			int keyPos = keyPositions[i];
			TypeComparator<Object> comparator = comparators[i];
			Object firstElement = first.getField(keyPos);   // element can be null
			Object secondElement = second.getField(keyPos); // element can be null

			int cmp = comparator.compare(firstElement, secondElement);
			if (cmp != 0) {
				return cmp;
			}
		}
	} catch (IndexOutOfBoundsException e) {
		throw new KeyFieldOutOfBoundsException(keyPositions[i]);
	}
	return 0;
}
 
Example 3
Source File: RowComparator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(Row first, Row second) {
	int i = 0;
	try {
		for (; i < keyPositions.length; i++) {
			int keyPos = keyPositions[i];
			TypeComparator<Object> comparator = comparators[i];
			Object firstElement = first.getField(keyPos);   // element can be null
			Object secondElement = second.getField(keyPos); // element can be null

			int cmp = comparator.compare(firstElement, secondElement);
			if (cmp != 0) {
				return cmp;
			}
		}
	} catch (IndexOutOfBoundsException e) {
		throw new KeyFieldOutOfBoundsException(keyPositions[i]);
	}
	return 0;
}
 
Example 4
Source File: RowComparator.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public int compareSerialized(
	DataInputView firstSource,
	DataInputView secondSource) throws IOException {

	int len = serializers.length;
	int keyLen = keyPositions.length;

	readIntoNullMask(arity, firstSource, nullMask1);
	readIntoNullMask(arity, secondSource, nullMask2);

	// deserialize
	for (int i = 0; i < len; i++) {
		TypeSerializer<Object> serializer = serializers[i];

		// deserialize field 1
		if (!nullMask1[i]) {
			deserializedKeyFields1[i] = serializer.deserialize(
				deserializedKeyFields1[i],
				firstSource);
		}

		// deserialize field 2
		if (!nullMask2[i]) {
			deserializedKeyFields2[i] = serializer.deserialize(
				deserializedKeyFields2[i],
				secondSource);
		}
	}

	// compare
	for (int i = 0; i < keyLen; i++) {
		int keyPos = keyPositions[i];
		TypeComparator<Object> comparator = comparators[i];

		boolean isNull1 = nullMask1[keyPos];
		boolean isNull2 = nullMask2[keyPos];

		int cmp = 0;
		// both values are null -> equality
		if (isNull1 && isNull2) {
			cmp = 0;
		}
		// first value is null -> inequality
		else if (isNull1) {
			cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
		}
		// second value is null -> inequality
		else if (isNull2) {
			cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
		}
		// no null values
		else {
			cmp = comparator.compare(
				deserializedKeyFields1[keyPos],
				deserializedKeyFields2[keyPos]);
		}

		if (cmp != 0) {
			return cmp;
		}
	}

	return 0;
}
 
Example 5
Source File: MergeIteratorTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testInvalidMerge() throws Exception
{
	// iterators
	List<MutableObjectIterator<Tuple2<Integer, String>>> iterators = new ArrayList<>();
	iterators.add(newIterator(new int[] { 1, 2, 17, 23, 23 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 2, 6, 7, 8, 9 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 4, 10, 11, 11, 12 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 3, 6, 10, 7, 12 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 7, 10, 15, 19, 44 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 6, 6, 11, 17, 18 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 1, 2, 4, 5, 10 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 5, 10, 19, 23, 29 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 9, 9, 9, 9, 9 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 8, 8, 14, 14, 15 }, new String[] { "A", "B", "C", "D", "E" }));

	// comparator
	TypeComparator<Integer> comparator = new IntComparator(true);

	// merge iterator
	MutableObjectIterator<Tuple2<Integer, String>> iterator = new MergeIterator<>(iterators, this.comparator);

	boolean violationFound = false;
	
	// check expected order
	Tuple2<Integer, String> rec1 = new Tuple2<>();
	Tuple2<Integer, String> rec2 = new Tuple2<>();
	
	Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
	while ((rec2 = iterator.next(rec2)) != null)
	{			
		if (comparator.compare(rec1.f0, rec2.f0) > 0) {
			violationFound = true;
			break;
		}
		
		Tuple2<Integer, String> tmp = rec1;
		rec1 = rec2;
		rec2 = tmp;
	}
	
	Assert.assertTrue("Merge must have returned a wrong result", violationFound);
}
 
Example 6
Source File: RowComparator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public int compareSerialized(
	DataInputView firstSource,
	DataInputView secondSource) throws IOException {

	int len = serializers.length;
	int keyLen = keyPositions.length;

	readIntoNullMask(arity, firstSource, nullMask1);
	readIntoNullMask(arity, secondSource, nullMask2);

	// deserialize
	for (int i = 0; i < len; i++) {
		TypeSerializer<Object> serializer = serializers[i];

		// deserialize field 1
		if (!nullMask1[i]) {
			deserializedKeyFields1[i] = serializer.deserialize(
				deserializedKeyFields1[i],
				firstSource);
		}

		// deserialize field 2
		if (!nullMask2[i]) {
			deserializedKeyFields2[i] = serializer.deserialize(
				deserializedKeyFields2[i],
				secondSource);
		}
	}

	// compare
	for (int i = 0; i < keyLen; i++) {
		int keyPos = keyPositions[i];
		TypeComparator<Object> comparator = comparators[i];

		boolean isNull1 = nullMask1[keyPos];
		boolean isNull2 = nullMask2[keyPos];

		int cmp = 0;
		// both values are null -> equality
		if (isNull1 && isNull2) {
			cmp = 0;
		}
		// first value is null -> inequality
		else if (isNull1) {
			cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
		}
		// second value is null -> inequality
		else if (isNull2) {
			cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
		}
		// no null values
		else {
			cmp = comparator.compare(
				deserializedKeyFields1[keyPos],
				deserializedKeyFields2[keyPos]);
		}

		if (cmp != 0) {
			return cmp;
		}
	}

	return 0;
}
 
Example 7
Source File: MergeIteratorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testInvalidMerge() throws Exception
{
	// iterators
	List<MutableObjectIterator<Tuple2<Integer, String>>> iterators = new ArrayList<>();
	iterators.add(newIterator(new int[] { 1, 2, 17, 23, 23 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 2, 6, 7, 8, 9 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 4, 10, 11, 11, 12 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 3, 6, 10, 7, 12 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 7, 10, 15, 19, 44 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 6, 6, 11, 17, 18 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 1, 2, 4, 5, 10 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 5, 10, 19, 23, 29 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 9, 9, 9, 9, 9 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 8, 8, 14, 14, 15 }, new String[] { "A", "B", "C", "D", "E" }));

	// comparator
	TypeComparator<Integer> comparator = new IntComparator(true);

	// merge iterator
	MutableObjectIterator<Tuple2<Integer, String>> iterator = new MergeIterator<>(iterators, this.comparator);

	boolean violationFound = false;
	
	// check expected order
	Tuple2<Integer, String> rec1 = new Tuple2<>();
	Tuple2<Integer, String> rec2 = new Tuple2<>();
	
	Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
	while ((rec2 = iterator.next(rec2)) != null)
	{			
		if (comparator.compare(rec1.f0, rec2.f0) > 0) {
			violationFound = true;
			break;
		}
		
		Tuple2<Integer, String> tmp = rec1;
		rec1 = rec2;
		rec2 = tmp;
	}
	
	Assert.assertTrue("Merge must have returned a wrong result", violationFound);
}
 
Example 8
Source File: RowComparator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public int compareSerialized(
		DataInputView firstSource,
		DataInputView secondSource) throws IOException {
	final int len = serializers.length;
	final int keyLen = keyPositions.length;

	// read bitmask
	readIntoMask(firstSource, mask1);
	readIntoMask(secondSource, mask2);

	// deserialize fields
	for (int fieldPos = 0; fieldPos < len; fieldPos++) {
		final TypeSerializer<Object> serializer = serializers[fieldPos];

		// deserialize field 1
		if (!mask1[ROW_KIND_OFFSET + fieldPos]) {
			deserializedKeyFields1[fieldPos] = serializer.deserialize(
				deserializedKeyFields1[fieldPos],
				firstSource);
		}

		// deserialize field 2
		if (!mask2[ROW_KIND_OFFSET + fieldPos]) {
			deserializedKeyFields2[fieldPos] = serializer.deserialize(
				deserializedKeyFields2[fieldPos],
				secondSource);
		}
	}

	// compare
	for (int fieldPos = 0; fieldPos < keyLen; fieldPos++) {
		final int keyPos = keyPositions[fieldPos];
		final TypeComparator<Object> comparator = comparators[fieldPos];

		final boolean isNull1 = mask1[ROW_KIND_OFFSET + keyPos];
		final boolean isNull2 = mask2[ROW_KIND_OFFSET + keyPos];

		int cmp;
		// both values are null -> equality
		if (isNull1 && isNull2) {
			cmp = 0;
		}
		// first value is null -> inequality
		else if (isNull1) {
			cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
		}
		// second value is null -> inequality
		else if (isNull2) {
			cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
		}
		// no null values
		else {
			cmp = comparator.compare(
				deserializedKeyFields1[keyPos],
				deserializedKeyFields2[keyPos]);
		}

		if (cmp != 0) {
			return cmp;
		}
	}

	return 0;
}
 
Example 9
Source File: MergeIteratorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testInvalidMerge() throws Exception
{
	// iterators
	List<MutableObjectIterator<Tuple2<Integer, String>>> iterators = new ArrayList<>();
	iterators.add(newIterator(new int[] { 1, 2, 17, 23, 23 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 2, 6, 7, 8, 9 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 4, 10, 11, 11, 12 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 3, 6, 10, 7, 12 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 7, 10, 15, 19, 44 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 6, 6, 11, 17, 18 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 1, 2, 4, 5, 10 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 5, 10, 19, 23, 29 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 9, 9, 9, 9, 9 }, new String[] { "A", "B", "C", "D", "E" }));
	iterators.add(newIterator(new int[] { 8, 8, 14, 14, 15 }, new String[] { "A", "B", "C", "D", "E" }));

	// comparator
	TypeComparator<Integer> comparator = new IntComparator(true);

	// merge iterator
	MutableObjectIterator<Tuple2<Integer, String>> iterator = new MergeIterator<>(iterators, this.comparator);

	boolean violationFound = false;
	
	// check expected order
	Tuple2<Integer, String> rec1 = new Tuple2<>();
	Tuple2<Integer, String> rec2 = new Tuple2<>();
	
	Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
	while ((rec2 = iterator.next(rec2)) != null)
	{			
		if (comparator.compare(rec1.f0, rec2.f0) > 0) {
			violationFound = true;
			break;
		}
		
		Tuple2<Integer, String> tmp = rec1;
		rec1 = rec2;
		rec2 = tmp;
	}
	
	Assert.assertTrue("Merge must have returned a wrong result", violationFound);
}