sanel commented on a change in pull request #2085:
URL: https://github.com/apache/drill/pull/2085#discussion_r434064308



##########
File path: 
contrib/storage-http/src/main/java/org/apache/drill/exec/store/http/HttpCSVBatchReader.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.http;
+
+import com.univocity.parsers.csv.CsvParser;
+import com.univocity.parsers.csv.CsvParserSettings;
+import okhttp3.HttpUrl;
+import org.apache.drill.common.exceptions.ChildErrorContext;
+import org.apache.drill.common.exceptions.CustomErrorContext;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.common.types.TypeProtos;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.physical.impl.scan.framework.SchemaNegotiator;
+import org.apache.drill.exec.physical.resultSet.ResultSetLoader;
+import org.apache.drill.exec.physical.resultSet.RowSetLoader;
+import org.apache.drill.exec.record.metadata.SchemaBuilder;
+import org.apache.drill.exec.record.metadata.TupleMetadata;
+import org.apache.drill.exec.store.http.util.SimpleHttp;
+import org.apache.drill.exec.vector.accessor.ScalarWriter;
+import org.apache.drill.shaded.guava.com.google.common.base.Strings;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HttpCSVBatchReader extends HttpBatchReader {
+  private final HttpSubScan subScan;
+  private final CsvParserSettings csvSettings;
+  private CsvParser csvReader;
+  private List<StringColumnWriter> columnWriters;
+  private String[] firstRow;
+  private SchemaBuilder builder;
+  private RowSetLoader rowWriter;
+
+
+  public HttpCSVBatchReader(HttpSubScan subScan) {
+    super(subScan);
+    this.subScan = subScan;
+
+    this.csvSettings = new CsvParserSettings();
+    csvSettings.setLineSeparatorDetectionEnabled(true);
+  }
+
+  @Override
+  public boolean open(SchemaNegotiator negotiator) {
+
+    // Result set loader setup
+    String tempDirPath = 
negotiator.drillConfig().getString(ExecConstants.DRILL_TMP_DIR);
+
+    HttpUrl url = buildUrl();
+
+    CustomErrorContext errorContext = new 
ChildErrorContext(negotiator.parentErrorContext()) {
+      @Override
+      public void addContext(UserException.Builder builder) {
+        super.addContext(builder);
+        builder.addContext("URL", url.toString());
+      }
+    };
+    negotiator.setErrorContext(errorContext);
+
+    // Http client setup
+    SimpleHttp http = new SimpleHttp(subScan, url, new File(tempDirPath), 
proxySettings(negotiator.drillConfig(), url), errorContext);
+
+    // CSV loader setup
+    InputStream inStream = http.getInputStream();
+
+    this.csvReader = new CsvParser(csvSettings);
+    csvReader.beginParsing(inStream);
+
+    // Build the Schema
+    builder = new SchemaBuilder();
+    TupleMetadata drillSchema = buildSchema();
+    negotiator.tableSchema(drillSchema, true);
+    ResultSetLoader resultLoader = negotiator.build();
+
+    // Create ScalarWriters
+    rowWriter = resultLoader.writer();
+    populateWriterArray();
+
+    return true;
+  }
+
+  @Override
+  public boolean next() {
+    while (!rowWriter.isFull()) {
+      if (!processRow()) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  private TupleMetadata buildSchema() {
+
+    this.firstRow = csvReader.parseNext();
+
+    // Case for empty dataset
+    if (firstRow == null) {
+      return builder.buildSchema();
+    }
+
+    // Parse the first row
+    for (String value : firstRow) {
+      builder.addNullable(value, TypeProtos.MinorType.VARCHAR);
+    }
+
+    return builder.buildSchema();
+  }
+
+  private void populateWriterArray() {
+    columnWriters = new ArrayList<>();
+
+    // Case for empty result set
+    if (firstRow == null || firstRow.length == 0) {
+      return;
+    }

Review comment:
       Can `columnWriters = new ArrayList<>();` go here (after `firstRow` 
checks)?




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


Reply via email to