flink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject flink git commit: [FLINK-4736] [core] Don't duplicate fields in Ordering
Date Thu, 06 Oct 2016 21:10:37 GMT
Repository: flink
Updated Branches:
  refs/heads/master 0dac7ad00 -> 6efb7c266


[FLINK-4736] [core] Don't duplicate fields in Ordering

Duplicate fields should not be appended to an ordering. In an ordering
each subsequent field is only used as a comparison when all prior fields
test equal; therefore, a repeated field cannot contribute to the
ordering.

This closes #2601


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/6efb7c26
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/6efb7c26
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/6efb7c26

Branch: refs/heads/master
Commit: 6efb7c2666d6b75b686e30a44f0acec8f69d1ed0
Parents: 0dac7ad
Author: Greg Hogan <code@greghogan.com>
Authored: Tue Oct 4 11:37:37 2016 -0400
Committer: Greg Hogan <code@greghogan.com>
Committed: Thu Oct 6 17:08:48 2016 -0400

----------------------------------------------------------------------
 .../flink/api/common/operators/Ordering.java    | 15 ++++--
 .../api/common/operators/OrderingTest.java      | 49 ++++++++++++++++++++
 2 files changed, 59 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/6efb7c26/flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java
b/flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java
index afc659a..83fcf18 100644
--- a/flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java
+++ b/flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java
@@ -55,6 +55,8 @@ public class Ordering implements Cloneable {
 	
 	/**
 	 * Extends this ordering by appending an additional order requirement.
+	 * If the index has been previously appended then the unmodified Ordering
+	 * is returned.
 	 * 
 	 * @param index Field index of the appended order requirement.
 	 * @param type Type of the appended order requirement.
@@ -63,7 +65,7 @@ public class Ordering implements Cloneable {
 	 * @return This ordering with an additional appended order requirement.
 	 */
 	public Ordering appendOrdering(Integer index, Class<? extends Comparable<?>>
type, Order order) {
-		if (index.intValue() < 0) {
+		if (index < 0) {
 			throw new IllegalArgumentException("The key index must not be negative.");
 		}
 		if (order == null) {
@@ -72,10 +74,13 @@ public class Ordering implements Cloneable {
 		if (order == Order.NONE) {
 			throw new IllegalArgumentException("An ordering must not be created with a NONE order.");
 		}
-		
-		this.indexes = this.indexes.addField(index);
-		this.types.add(type);
-		this.orders.add(order);
+
+		if (!this.indexes.contains(index)) {
+			this.indexes = this.indexes.addField(index);
+			this.types.add(type);
+			this.orders.add(order);
+		}
+
 		return this;
 	}
 	

http://git-wip-us.apache.org/repos/asf/flink/blob/6efb7c26/flink-core/src/test/java/org/apache/flink/api/common/operators/OrderingTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/api/common/operators/OrderingTest.java
b/flink-core/src/test/java/org/apache/flink/api/common/operators/OrderingTest.java
new file mode 100644
index 0000000..8e1690e
--- /dev/null
+++ b/flink-core/src/test/java/org/apache/flink/api/common/operators/OrderingTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.common.operators;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class OrderingTest {
+
+	@Test
+	public void testNewOrdering() {
+		Ordering ordering = new Ordering();
+
+		// add a field
+		ordering.appendOrdering(3, Integer.class, Order.ASCENDING);
+		assertEquals(1, ordering.getNumberOfFields());
+
+		// add a second field
+		ordering.appendOrdering(1, Long.class, Order.DESCENDING);
+		assertEquals(2, ordering.getNumberOfFields());
+
+		// duplicate field index does not change Ordering
+		ordering.appendOrdering(1, String.class, Order.ASCENDING);
+		assertEquals(2, ordering.getNumberOfFields());
+
+		// verify field positions, types, and orderings
+		assertArrayEquals(new int[]{3, 1}, ordering.getFieldPositions());
+		assertArrayEquals(new Class[]{Integer.class, Long.class}, ordering.getTypes());
+		assertArrayEquals(new boolean[]{true, false}, ordering.getFieldSortDirections());
+	}
+}


Mime
View raw message