[ 
https://issues.apache.org/jira/browse/DRILL-7362?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16921605#comment-16921605
 ] 

ASF GitHub Bot commented on DRILL-7362:
---------------------------------------

vvysotskyi commented on pull request #1849: DRILL-7362: COUNT(*) on JSON with 
outer list results in JsonParse error
URL: https://github.com/apache/drill/pull/1849#discussion_r320351497
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/reader/BaseJsonReader.java
 ##########
 @@ -0,0 +1,161 @@
+/*
+ * 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.store.easy.json.reader;
+
+import com.fasterxml.jackson.core.JsonToken;
+import io.netty.buffer.DrillBuf;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.vector.complex.writer.BaseWriter.ComplexWriter;
+
+import java.io.IOException;
+
+/**
+ * Basic reader implementation for json documents.
+ */
+public abstract class BaseJsonReader extends BaseJsonProcessor {
+
+  private static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(BaseJsonReader.class);
+
+  /**
+   * Describes whether or not this reader can unwrap a single root array record
+   * and treat it like a set of distinct records.
+   */
+  private final boolean skipOuterList;
+
+  /**
+   * Whether the reader is currently in a situation where we are unwrapping an
+   * outer list.
+   */
+  private boolean inOuterList;
+
+  public BaseJsonReader(DrillBuf workBuf, boolean enableNanInf, boolean 
enableEscapeAnyChar, boolean skipOuterList) {
+    super(workBuf, enableNanInf, enableEscapeAnyChar);
+    this.skipOuterList = skipOuterList;
+  }
+
+  @Override
+  public ReadState write(ComplexWriter writer) throws IOException {
+
+    ReadState readState = null;
+    try {
+      JsonToken t = lastSeenJsonToken;
+      if (t == null || t == JsonToken.END_OBJECT) {
+        t = parser.nextToken();
+      }
+      while (!parser.hasCurrentToken() && !parser.isClosed()) {
+        t = parser.nextToken();
+      }
+      lastSeenJsonToken = null;
+
+      if (parser.isClosed()) {
+        return ReadState.END_OF_STREAM;
+      }
+
+      readState = writeToVector(writer, t);
+
+      switch (readState) {
+        case END_OF_STREAM:
+          break;
+        case WRITE_SUCCEED:
+          break;
+        default:
+          throw getExceptionWithContext(UserException.dataReadError(), 
null).message(
+            "Failure while reading JSON. (Got an invalid read state %s )", 
readState.toString())
+            .build(logger);
+      }
+    } catch (com.fasterxml.jackson.core.JsonParseException ex) {
+      if (ignoreJSONParseError()) {
+        if (processJSONException() == 
JsonExceptionProcessingState.END_OF_STREAM) {
+          return ReadState.JSON_RECORD_PARSE_EOF_ERROR;
+        } else {
+          return ReadState.JSON_RECORD_PARSE_ERROR;
+        }
+      } else {
+        throw ex;
+      }
+    }
+    return readState;
+  }
+
+
+  private ReadState writeToVector(ComplexWriter writer, JsonToken t)
+    throws IOException {
+
+    switch (t) {
+      case START_OBJECT:
+        writeDocument(writer, t);
+        break;
+      case START_ARRAY:
+        if (inOuterList) {
+          throw createDocumentTopLevelException();
+        }
+
+        if (skipOuterList) {
+          t = parser.nextToken();
+          if (t == JsonToken.START_OBJECT) {
+            inOuterList = true;
+            writeDocument(writer, t);
+          } else {
+            throw createDocumentTopLevelException();
+          }
+
+        } else {
+          writeDocument(writer, t);
+        }
+        break;
+      case END_ARRAY:
+
+        if (inOuterList) {
+          confirmLast();
+          return ReadState.END_OF_STREAM;
+        } else {
+          throw getExceptionWithContext(UserException.dataReadError(), 
null).message(
+            "Failure while parsing JSON.  Ran across unexpected %s.", 
JsonToken.END_ARRAY).build(logger);
+        }
+
+      case NOT_AVAILABLE:
+        return ReadState.END_OF_STREAM;
+      default:
+        throw getExceptionWithContext(UserException.dataReadError(), null)
+          .message(
+            "Failure while parsing JSON.  Found token of [%s].  Drill 
currently only supports parsing "
+              + "json strings that contain either lists or maps.  The root 
object cannot be a scalar.",
+            t).build(logger);
+    }
+
+    return ReadState.WRITE_SUCCEED;
+  }
+
+  protected abstract void writeDocument(ComplexWriter writer, JsonToken t) 
throws IOException;
 
 Review comment:
   could you please add JavaDocs to these methods?
 
----------------------------------------------------------------
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:
us...@infra.apache.org


> COUNT(*) on JSON with outer list results in JsonParse error
> -----------------------------------------------------------
>
>                 Key: DRILL-7362
>                 URL: https://issues.apache.org/jira/browse/DRILL-7362
>             Project: Apache Drill
>          Issue Type: Bug
>    Affects Versions: 1.16.0
>            Reporter: Oleg Zinoviev
>            Assignee: Oleg Zinoviev
>            Priority: Major
>             Fix For: 1.17.0
>
>
> Count from a JSON file with a outer array results in JsonParseException: 
> Cannot read from the middle of a record. Current token was START_ARRAY.
> P.S. A simple select from such file works normally.



--
This message was sent by Atlassian Jira
(v8.3.2#803003)

Reply via email to