/**
 * 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.pinot.plugin.inputformat.json;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MappingIterator;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import org.apache.pinot.spi.data.readers.GenericRow;
import org.apache.pinot.spi.data.readers.RecordReader;
import org.apache.pinot.spi.data.readers.RecordReaderConfig;
import org.apache.pinot.spi.utils.JsonUtils;


/**
 * Record reader for JSON file.
 */
public class JSONRecordReader implements RecordReader {
  private File _dataFile;
  private JSONRecordExtractor _recordExtractor;

  private MappingIterator<Map<String, Object>> _iterator;

  public JSONRecordReader() {
  }

  private void init()
      throws IOException {
    try {
      _iterator = JsonUtils.DEFAULT_READER.forType(new TypeReference<Map<String, Object>>() {
      }).readValues(_dataFile);
    } catch (Exception e) {
      if (_iterator != null) {
        _iterator.close();
      }
      throw e;
    }
  }

  @Override
  public void init(File dataFile, Set<String> fieldsToRead, @Nullable RecordReaderConfig recordReaderConfig)
      throws IOException {
    _dataFile = dataFile;
    _recordExtractor = new JSONRecordExtractor();
    _recordExtractor.init(fieldsToRead, null);
    init();
  }

  @Override
  public boolean hasNext() {
    return _iterator.hasNext();
  }

  @Override
  public GenericRow next() {
    return next(new GenericRow());
  }

  @Override
  public GenericRow next(GenericRow reuse) {
    Map<String, Object> record = _iterator.next();
    _recordExtractor.extract(record, reuse);
    return reuse;
  }

  @Override
  public void rewind()
      throws IOException {
    _iterator.close();
    init();
  }

  @Override
  public void close()
      throws IOException {
    _iterator.close();
  }
}

@@ -1011,16 +1011,44 @@
 eption;%0A
+import java.io.InputStream;%0A
 import j
@@ -1061,16 +1061,16 @@
 il.Map;%0A
-
 import j
@@ -1286,16 +1286,76 @@
 Config;%0A
+import org.apache.pinot.spi.data.readers.RecordReaderUtils;%0A
 import o
@@ -1515,16 +1515,52 @@
 taFile;%0A
+  private InputStream _inputStream;%0A
   privat
@@ -1739,24 +1739,96 @@
 Exception %7B%0A
+    _inputStream = RecordReaderUtils.getBufferedInputStream(_dataFile);%0A
     try %7B%0A  
@@ -1940,24 +1940,27 @@
 Values(_
-dataFile
+inputStream
 );%0A    %7D
@@ -2048,16 +2048,88 @@
       %7D%0A
+      if (_inputStream != null) %7B%0A        _inputStream.close();%0A      %7D%0A
       th
@@ -2850,24 +2850,50 @@
 or.close();%0A
+    _inputStream.close();%0A
     init();%0A
@@ -2950,32 +2950,32 @@
 s IOException %7B%0A
-
     _iterator.cl
@@ -2977,18 +2977,44 @@
 or.close();%0A
+    _inputStream.close();%0A
   %7D%0A%7D%0A

Enhance JSONRecordReader to handle GZIP compressed JSON files. (#6321)