michaelmior commented on a change in pull request #1302: [CALCITE-3176] File 
adapter for parsing JSON files
URL: https://github.com/apache/calcite/pull/1302#discussion_r305566941
 
 

 ##########
 File path: 
example/csv/src/main/java/org/apache/calcite/adapter/csv/JsonTable.java
 ##########
 @@ -16,46 +16,91 @@
  */
 package org.apache.calcite.adapter.csv;
 
-import org.apache.calcite.DataContext;
-import org.apache.calcite.linq4j.AbstractEnumerable;
-import org.apache.calcite.linq4j.Enumerable;
-import org.apache.calcite.linq4j.Enumerator;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
-import org.apache.calcite.schema.ScannableTable;
+import org.apache.calcite.schema.Statistic;
+import org.apache.calcite.schema.Statistics;
 import org.apache.calcite.schema.impl.AbstractTable;
-import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.Source;
 
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * Table based on a JSON file.
  */
-public class JsonTable extends AbstractTable implements ScannableTable {
-  private final Source source;
+public class JsonTable extends AbstractTable {
+  private final ObjectMapper objectMapper = new ObjectMapper();
+  protected final List<Object> list;
+
+  private LinkedHashMap<String, Object> jsonFieldMap = new LinkedHashMap<>(1);
 
-  /** Creates a JsonTable. */
   public JsonTable(Source source) {
-    this.source = source;
-  }
+    Object jsonObj = null;
+    try {
+      objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, 
true)
+          .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
+          .configure(JsonParser.Feature.ALLOW_COMMENTS, true);
+      if (source.file().exists() && source.file().length() > 0) {
+        if ("file".equals(source.protocol())) {
+          //noinspection unchecked
+          jsonObj = objectMapper.readValue(source.file(), Object.class);
+        } else {
+          //noinspection unchecked
+          jsonObj = objectMapper.readValue(source.url(), Object.class);
+        }
+      }
+    } catch (MismatchedInputException e) {
+      if (!e.getMessage().contains("No content")) {
+        throw new RuntimeException(e);
+      }
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
 
-  public String toString() {
-    return "JsonTable";
+    if (jsonObj == null) {
+      list = new ArrayList<>();
+      jsonFieldMap.put("EmptyFileHasNoColumns", Boolean.TRUE);
 
 Review comment:
   Thanks for pointing that out. I didn't realize that logic was already in 
there. This looks good to me!

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to