DRILL-2403: Print leading zeroes in TimePrintMillis
Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/c2a2377b
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/c2a2377b
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/c2a2377b
Branch: refs/heads/master
Commit: c2a2377bdc2acaf714c19c0cb509f62c8aeffd19
Parents: d32acdb
Author: Mehant Baid <mehantr@gmail.com>
Authored: Thu Jun 11 14:17:26 2015 -0700
Committer: Mehant Baid <mehantr@gmail.com>
Committed: Wed Jun 17 14:43:02 2015 -0700
----------------------------------------------------------------------
.../vector/accessor/sql/TimePrintMillis.java | 22 +++++++++--
.../vector/accessor/TestTimePrintMillis.java | 40 ++++++++++++++++++++
2 files changed, 58 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/drill/blob/c2a2377b/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java
index 349abd5..2611b86 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java
@@ -21,8 +21,12 @@ import java.sql.Time;
import org.apache.drill.exec.expr.fn.impl.DateUtility;
-
public class TimePrintMillis extends Time {
+ private static final String[] leadingZeroes = {"", "0", "00"};
+
+ // Desired length of the milli second portion should be 3
+ private static final int DESIRED_MILLIS_LENGTH = 3;
+
public TimePrintMillis(long time) {
super(time);
}
@@ -30,13 +34,23 @@ public class TimePrintMillis extends Time {
@Override
public String toString () {
int millis = (int) (getTime() % DateUtility.secondsToMillis);
- String baseTime = super.toString();
+ StringBuilder time = new StringBuilder().append(super.toString());
if (millis > 0) {
- baseTime = baseTime + "." + Integer.toString(millis);
+ String millisString = Integer.toString(millis);
+
+ // dot to separate the fractional seconds
+ time.append(".");
+
+ int millisLength = millisString.length();
+ if (millisLength < DESIRED_MILLIS_LENGTH) {
+ // add necessary leading zeroes
+ time.append(leadingZeroes[DESIRED_MILLIS_LENGTH - millisLength]);
+ }
+ time.append(millisString);
}
- return baseTime;
+ return time.toString();
}
}
http://git-wip-us.apache.org/repos/asf/drill/blob/c2a2377b/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java
b/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java
new file mode 100644
index 0000000..df106f2
--- /dev/null
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java
@@ -0,0 +1,40 @@
+/**
+ * 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.drill.exec.vector.accessor;
+
+import org.apache.drill.exec.vector.accessor.sql.TimePrintMillis;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestTimePrintMillis {
+
+ @Test
+ public void testPrintingMillis() {
+ // testing the regular case where the precision of the millisecond is 3
+ TimePrintMillis time = new TimePrintMillis(999);
+ Assert.assertTrue(time.toString().endsWith("999"));
+
+ // test case where one leading zero needs to be added
+ time = new TimePrintMillis(99);
+ Assert.assertTrue(time.toString().endsWith("099"));
+
+ // test case where two leading zeroes needs to be added
+ time = new TimePrintMillis(1);
+ Assert.assertTrue(time.toString().endsWith("001"));
+ }
+}
|