You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by se...@apache.org on 2014/06/13 02:00:36 UTC
[5/5] git commit: Fix number formatting in JSON plans
Fix number formatting in JSON plans
Project: http://git-wip-us.apache.org/repos/asf/incubator-flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-flink/commit/6c6fc897
Tree: http://git-wip-us.apache.org/repos/asf/incubator-flink/tree/6c6fc897
Diff: http://git-wip-us.apache.org/repos/asf/incubator-flink/diff/6c6fc897
Branch: refs/heads/master
Commit: 6c6fc897a284c27dc904de98039814ec786e9fa0
Parents: 3bc9775
Author: StephanEwen <st...@tu-berlin.de>
Authored: Fri Jun 13 01:41:04 2014 +0200
Committer: StephanEwen <st...@tu-berlin.de>
Committed: Fri Jun 13 01:41:04 2014 +0200
----------------------------------------------------------------------
.../stratosphere/client/testjar/WordCount.java | 1 +
.../plandump/PlanJSONDumpGenerator.java | 50 +++++++-------------
.../compiler/plandump/NumberFormattingTest.java | 49 +++++++++++++++++++
3 files changed, 68 insertions(+), 32 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/6c6fc897/stratosphere-clients/src/test/java/eu/stratosphere/client/testjar/WordCount.java
----------------------------------------------------------------------
diff --git a/stratosphere-clients/src/test/java/eu/stratosphere/client/testjar/WordCount.java b/stratosphere-clients/src/test/java/eu/stratosphere/client/testjar/WordCount.java
index 458c848..e827805 100644
--- a/stratosphere-clients/src/test/java/eu/stratosphere/client/testjar/WordCount.java
+++ b/stratosphere-clients/src/test/java/eu/stratosphere/client/testjar/WordCount.java
@@ -70,6 +70,7 @@ public class WordCount {
* FlatMapFunction. The function takes a line (String) and splits it into
* multiple pairs in the form of "(word,1)" (Tuple2<String, Integer>).
*/
+ @SuppressWarnings("serial")
public static final class Tokenizer extends FlatMapFunction<String, Tuple2<String, Integer>> {
@Override
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/6c6fc897/stratosphere-compiler/src/main/java/eu/stratosphere/compiler/plandump/PlanJSONDumpGenerator.java
----------------------------------------------------------------------
diff --git a/stratosphere-compiler/src/main/java/eu/stratosphere/compiler/plandump/PlanJSONDumpGenerator.java b/stratosphere-compiler/src/main/java/eu/stratosphere/compiler/plandump/PlanJSONDumpGenerator.java
index d966488..183ee2f 100644
--- a/stratosphere-compiler/src/main/java/eu/stratosphere/compiler/plandump/PlanJSONDumpGenerator.java
+++ b/stratosphere-compiler/src/main/java/eu/stratosphere/compiler/plandump/PlanJSONDumpGenerator.java
@@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import eu.stratosphere.api.common.operators.CompilerHints;
@@ -600,44 +601,29 @@ public class PlanJSONDumpGenerator {
}
public static final String formatNumber(double number, String suffix) {
- final int fractionalDigits = 2;
-
- StringBuilder bld = new StringBuilder();
- bld.append(number);
-
- int len = bld.length();
-
- // get the power of 10 / 3
- int pot = (len - (bld.charAt(0) == '-' ? 2 : 1)) / 3;
- if (pot >= SIZE_SUFFIXES.length) {
- pot = SIZE_SUFFIXES.length - 1;
- } else if (pot < 0) {
- pot = 0;
+ if (number <= 0.0) {
+ return String.valueOf(number);
}
- int beforeDecimal = len - pot * 3;
- if (len > beforeDecimal + fractionalDigits) {
- bld.setLength(beforeDecimal + fractionalDigits);
- }
+ int power = (int) Math.ceil(Math.log10(number));
- // insert decimal point
- if (pot > 0) {
- bld.insert(beforeDecimal, '.');
+ int group = (power - 1) / 3;
+ if (group >= SIZE_SUFFIXES.length) {
+ group = SIZE_SUFFIXES.length - 1;
+ } else if (group < 0) {
+ group = 0;
}
- // insert number grouping before decimal point
- for (int pos = beforeDecimal - 3; pos > 0; pos -= 3) {
- bld.insert(pos, ',');
- }
-
- // append the suffix
- bld.append(' ');
- if (pot > 0) {
- bld.append(SIZE_SUFFIXES[pot]);
+ // truncate fractional part
+ int beforeDecimal = power - group * 3;
+ if (power > beforeDecimal) {
+ for (int i = power - beforeDecimal; i > 0; i--) {
+ number /= 10;
+ }
}
- bld.append(suffix);
-
- return bld.toString();
+
+ return group > 0 ? String.format(Locale.US, "%.2f %s", number, SIZE_SUFFIXES[group]) :
+ String.format(Locale.US, "%.2f", number);
}
private static final char[] SIZE_SUFFIXES = { 0, 'K', 'M', 'G', 'T' };
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/6c6fc897/stratosphere-compiler/src/test/java/eu/stratosphere/compiler/plandump/NumberFormattingTest.java
----------------------------------------------------------------------
diff --git a/stratosphere-compiler/src/test/java/eu/stratosphere/compiler/plandump/NumberFormattingTest.java b/stratosphere-compiler/src/test/java/eu/stratosphere/compiler/plandump/NumberFormattingTest.java
new file mode 100644
index 0000000..e18f97a
--- /dev/null
+++ b/stratosphere-compiler/src/test/java/eu/stratosphere/compiler/plandump/NumberFormattingTest.java
@@ -0,0 +1,49 @@
+/***********************************************************************************************************************
+ *
+ * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
+ *
+ * Licensed 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 eu.stratosphere.compiler.plandump;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class NumberFormattingTest {
+
+ @Test
+ public void testFormatNumberNoDigit() {
+ assertEquals("0.0", PlanJSONDumpGenerator.formatNumber(0));
+ assertEquals("0.00", PlanJSONDumpGenerator.formatNumber(0.0000000001));
+ assertEquals("-1.0", PlanJSONDumpGenerator.formatNumber(-1.0));
+ assertEquals("1.00", PlanJSONDumpGenerator.formatNumber(1));
+ assertEquals("17.00", PlanJSONDumpGenerator.formatNumber(17));
+ assertEquals("17.44", PlanJSONDumpGenerator.formatNumber(17.44));
+ assertEquals("143.00", PlanJSONDumpGenerator.formatNumber(143));
+ assertEquals("143.40", PlanJSONDumpGenerator.formatNumber(143.4));
+ assertEquals("143.50", PlanJSONDumpGenerator.formatNumber(143.5));
+ assertEquals("143.60", PlanJSONDumpGenerator.formatNumber(143.6));
+ assertEquals("143.45", PlanJSONDumpGenerator.formatNumber(143.45));
+ assertEquals("143.55", PlanJSONDumpGenerator.formatNumber(143.55));
+ assertEquals("143.65", PlanJSONDumpGenerator.formatNumber(143.65));
+ assertEquals("143.66", PlanJSONDumpGenerator.formatNumber(143.655));
+
+ assertEquals("1.13 K", PlanJSONDumpGenerator.formatNumber(1126.0));
+ assertEquals("11.13 K", PlanJSONDumpGenerator.formatNumber(11126.0));
+ assertEquals("118.13 K", PlanJSONDumpGenerator.formatNumber(118126.0));
+
+ assertEquals("1.44 M", PlanJSONDumpGenerator.formatNumber(1435126.0));
+ }
+
+
+}