You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@carbondata.apache.org by GitBox <gi...@apache.org> on 2020/10/21 05:14:14 UTC

[GitHub] [carbondata] Karan980 commented on a change in pull request #3970: [CARBONDATA-4007] Fix multiple issues in SDK

Karan980 commented on a change in pull request #3970:
URL: https://github.com/apache/carbondata/pull/3970#discussion_r508994112



##########
File path: sdk/sdk/src/test/java/org/apache/carbondata/sdk/file/CarbonReaderTest.java
##########
@@ -1260,8 +1260,8 @@ public int compare(Field o1, Field o2) {
       Assert.assertEquals((double) id / 2, row[4]);
       Assert.assertEquals(true, (boolean) row[5]);
       long day = 24L * 3600 * 1000;

Review comment:
       Done

##########
File path: sdk/sdk/src/test/java/org/apache/carbondata/sdk/file/CarbonIUDTest.java
##########
@@ -72,6 +74,48 @@ public void testDelete() throws Exception {
     FileUtils.deleteDirectory(new File(path));
   }
 
+  @Test
+  public void testUpdateOnDateType() throws Exception {
+    String path = "./testWriteFiles";
+    CarbonProperties.getInstance()
+            .addProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT,
+                    CarbonCommonConstants.CARBON_TIMESTAMP_DEFAULT_FORMAT)
+            .addProperty(CarbonCommonConstants.CARBON_DATE_FORMAT,
+                    CarbonCommonConstants.CARBON_DATE_DEFAULT_FORMAT);
+    FileUtils.deleteDirectory(new File(path));
+    Field[] fields = new Field[3];
+    fields[0] = new Field("intField", DataTypes.INT);
+    fields[1] = new Field("dateField", DataTypes.DATE);
+    fields[2] = new Field("timeField", DataTypes.TIMESTAMP);
+    CarbonWriter writer = CarbonWriter.builder()
+            .outputPath(path)
+            .withCsvInput(new Schema(fields))
+            .writtenBy("IUDTest")
+            .build();
+    for (int i = 0; i < 10; i++) {
+      String[] row2 = new String[]{
+              String.valueOf(i % 10000),
+              "2019-03-02",
+              "2019-02-12 03:03:34",
+      };
+      writer.write(row2);
+    }
+    writer.close();
+    CarbonIUD.getInstance().update(path, "intField", "0", "intField", "20").commit();
+    CarbonReader reader =
+            CarbonReader.builder(path).projection(new String[] { "intField", "dateField", "timeField" })
+                    .build();
+    int i = 0;
+    while (reader.hasNext()) {
+      Object[] row = (Object[]) reader.readNextRow();
+      assert ((int) row[0] != 0);

Review comment:
       Done

##########
File path: sdk/sdk/src/main/java/org/apache/carbondata/sdk/file/CarbonReader.java
##########
@@ -109,7 +116,39 @@ public boolean hasNext() throws IOException, InterruptedException {
    */
   public T readNextRow() throws IOException, InterruptedException {
     validateReader();
-    return currentReader.getCurrentValue();
+    return formatDateAndTimeStamp((Object[]) currentReader.getCurrentValue());
+  }
+
+  public T formatDateAndTimeStamp(Object[] row) {
+    List<ProjectionDimension> dimensions = ((AbstractRecordReader) currentReader)
+            .getQueryModel().getProjectionDimensions();
+    String carbonDateFormat = CarbonProperties.getInstance()
+            .getProperty(CarbonCommonConstants.CARBON_DATE_FORMAT);
+    if (carbonDateFormat == null) {
+      carbonDateFormat = CarbonCommonConstants.CARBON_DATE_DEFAULT_FORMAT;
+    }
+    SimpleDateFormat dateFormat = new SimpleDateFormat(carbonDateFormat);
+    String carbonTimeStampFormat = CarbonProperties.getInstance()
+            .getProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT);
+    if (carbonTimeStampFormat  == null) {
+      carbonTimeStampFormat  = CarbonCommonConstants.CARBON_TIMESTAMP_DEFAULT_FORMAT;
+    }
+    SimpleDateFormat timeStampFormat = new SimpleDateFormat(carbonTimeStampFormat);
+    for (ProjectionDimension dimension : dimensions) {
+      ColumnSchema columnSchema = dimension.getDimension().getColumnSchema();
+      if (columnSchema == null) {
+        continue;
+      }
+      DataType dataType = columnSchema.getDataType();
+      if (dataType == DataTypes.DATE) {
+        row[dimension.getOrdinal()] = dateFormat
+                .format(new Date(24L * 3600 * 1000 * (int)row[dimension.getOrdinal()]));

Review comment:
       Done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org