You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2019/07/19 15:31:25 UTC

[GitHub] [calcite] michaelmior commented on a change in pull request #1302: [CALCITE-3176] File adapter for parsing JSON files

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_r305410450
 
 

 ##########
 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:
   I haven't tested this, but is it not possible to actually have a table with no columns?

----------------------------------------------------------------
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


With regards,
Apache Git Services