ASTERIXDB-1885: Fix printing field separators for record printer

- Fix commas to not appear at the beginning of a record.

Change-Id: I19e5c908367490a64104d961146bad2d870d0c58
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1692
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
BAD: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.1.ddl.sqlpp
new file mode 100644
index 0000000..9970f4e
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.1.ddl.sqlpp
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+/*
+ * Test case name: print-ASTERIXDB-1885
+ * Description: Regression test for 1885
+ * Expected result: success
+ */
+
+drop  dataverse testdvt if exists;
+create  dataverse testdvt;
+
+use testdvt;
+
+
+create type testdvt.testtype as
+{
+  id : string,
+  dateField : date?,
+  timeField : time?,
+  datetimeField : datetime?,
+  durationField : duration?,
+  intervalField : interval?,
+  yearMonthDurationField : `year_month_duration`?,
+  dayTimeDurationField : `day_time_duration`?
+}
+
+create external  dataset testds(testtype) using localfs((`path`=`asterix_nc1://data/temporal/temporalData.json`),(`format`=`adm`));
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.2.update.sqlpp
new file mode 100644
index 0000000..bd4fb21
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.2.update.sqlpp
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+/*
+ * Test case name: print-ASTERIXDB-1885
+ * Description: Regression test for 1885
+ * Expected result: success
+ */
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.3.query.sqlpp
new file mode 100644
index 0000000..9966a5a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.3.query.sqlpp
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+/*
+ * Test case name: print-ASTERIXDB-1885
+ * Description: Regression test for 1885
+ * Expected result: success
+ */
+use testdvt;
+
+
+select element {'date':r.dateField,'time':r.timeField,'datetime':r.datetimeField,'duration':r.durationField,'interval':r.intervalField,'ymduration':r.yearMonthDurationField,'dtduration':r.dayTimeDurationField}
+from  testds as r
+;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.1.adm
new file mode 100644
index 0000000..5d29db0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/null-missing/print-ASTERIXDB-1885/print-ASTERIXDB-1885.1.adm
@@ -0,0 +1,4 @@
+{ "date": date("-2012-12-12"), "time": time("23:49:12.390Z"), "datetime": datetime("2012-12-12T00:00:00.001Z"), "duration": duration("P20Y19DT4H14M23.34S"), "interval": interval(datetime("2012-12-12T00:00:00.001Z"), datetime("2013-08-10T22:10:15.398Z")) }
+{ "time": time("04:12:12.219Z"), "datetime": datetime("1920-12-21T11:29:18.478Z"), "interval": interval(time("04:29:30.000Z"), time("07:59:59.999Z")) }
+{ "datetime": datetime("-0290-03-22T17:59:48.999Z"), "duration": duration("-P27Y148D"), "interval": interval(date("-2012-03-17"), date("2013-04-01")) }
+{ "ymduration": year-month-duration("P31Y2M"), "dtduration": day-time-duration("-P148D") }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/temporal/insert_from_ext_ds/insert_from_ext_ds.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/temporal/insert_from_ext_ds/insert_from_ext_ds.1.adm
index 05e35aa..5d29db0 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/temporal/insert_from_ext_ds/insert_from_ext_ds.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/temporal/insert_from_ext_ds/insert_from_ext_ds.1.adm
@@ -1,4 +1,4 @@
 { "date": date("-2012-12-12"), "time": time("23:49:12.390Z"), "datetime": datetime("2012-12-12T00:00:00.001Z"), "duration": duration("P20Y19DT4H14M23.34S"), "interval": interval(datetime("2012-12-12T00:00:00.001Z"), datetime("2013-08-10T22:10:15.398Z")) }
 { "time": time("04:12:12.219Z"), "datetime": datetime("1920-12-21T11:29:18.478Z"), "interval": interval(time("04:29:30.000Z"), time("07:59:59.999Z")) }
-{ , "datetime": datetime("-0290-03-22T17:59:48.999Z"), "duration": duration("-P27Y148D"), "interval": interval(date("-2012-03-17"), date("2013-04-01")) }
-{ , "ymduration": year-month-duration("P31Y2M"), "dtduration": day-time-duration("-P148D") }
+{ "datetime": datetime("-0290-03-22T17:59:48.999Z"), "duration": duration("-P27Y148D"), "interval": interval(date("-2012-03-17"), date("2013-04-01")) }
+{ "ymduration": year-month-duration("P31Y2M"), "dtduration": day-time-duration("-P148D") }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index c2b0951..8638d83 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -4176,6 +4176,11 @@
         <output-dir compare="Text">query-ASTERIXDB-1689</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="null-missing">
+      <compilation-unit name="print-ASTERIXDB-1885">
+        <output-dir compare="Text">print-ASTERIXDB-1885</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="numeric">
     <test-case FilePath="numeric">
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/printer/ARecordPrinter.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/printer/ARecordPrinter.java
index 1a91d8b..fe9e378 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/printer/ARecordPrinter.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/pointables/printer/ARecordPrinter.java
@@ -52,36 +52,34 @@
 
     public void printRecord(ARecordVisitablePointable recordAccessor, PrintStream ps, IPrintVisitor visitor)
             throws IOException, AsterixException {
-        List<IVisitablePointable> fieldNames = recordAccessor.getFieldNames();
-        List<IVisitablePointable> fieldValues = recordAccessor.getFieldValues();
+        final List<IVisitablePointable> fieldNames = recordAccessor.getFieldNames();
+        final List<IVisitablePointable> fieldValues = recordAccessor.getFieldValues();
 
         nameVisitorArg.first = ps;
         itemVisitorArg.first = ps;
 
         ps.print(startRecord);
+
         final int size = fieldNames.size();
-        IVisitablePointable fieldValue = size > 0 ? fieldValues.get(0) : null;
-        ATypeTag typeTag = fieldValue != null ? EnumDeserializer.ATYPETAGDESERIALIZER
-                .deserialize(fieldValue.getByteArray()[fieldValue.getStartOffset()]) : null;
+        boolean first = true;
         for (int i = 0; i < size; ++i) {
-            IVisitablePointable fieldName = fieldNames.get(i);
+            final IVisitablePointable fieldName = fieldNames.get(i);
+            final IVisitablePointable fieldValue = fieldValues.get(i);
+            final ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER
+                    .deserialize(fieldValue.getByteArray()[fieldValue.getStartOffset()]);
 
             // Prints the current field.
             if (typeTag != ATypeTag.MISSING) {
-                printField(ps, visitor, fieldName, fieldValue, typeTag);
-            }
-
-            // Prints the field separator.
-            if (i < size - 1) {
-                fieldValue = fieldValues.get(i + 1);
-                ATypeTag nextTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER
-                        .deserialize(fieldValue.getByteArray()[fieldValue.getStartOffset()]);
-                if (!(i == 0 && typeTag == ATypeTag.MISSING) && nextTypeTag != ATypeTag.MISSING) {
+                if (first) {
+                    //Skip printing field separator for the first field.
+                    first = false;
+                } else {
                     ps.print(fieldSeparator);
                 }
-                typeTag = nextTypeTag;
+                printField(ps, visitor, fieldName, fieldValue, typeTag);
             }
         }
+
         ps.print(endRecord);
     }