cgivre commented on a change in pull request #2386:
URL: https://github.com/apache/drill/pull/2386#discussion_r756112189



##########
File path: 
contrib/format-sas/src/main/java/org/apache/drill/exec/store/sas/SasBatchReader.java
##########
@@ -0,0 +1,454 @@
+/*
+ * 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.sas;
+
+import com.epam.parso.Column;
+import com.epam.parso.SasFileProperties;
+import com.epam.parso.SasFileReader;
+import com.epam.parso.impl.SasFileReaderImpl;
+import org.apache.drill.common.AutoCloseables;
+import org.apache.drill.common.exceptions.CustomErrorContext;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.common.types.TypeProtos.DataMode;
+import org.apache.drill.common.types.TypeProtos.MinorType;
+import org.apache.drill.exec.physical.impl.scan.file.FileScanFramework;
+import org.apache.drill.exec.physical.impl.scan.framework.ManagedReader;
+import org.apache.drill.exec.physical.resultSet.ResultSetLoader;
+import org.apache.drill.exec.physical.resultSet.RowSetLoader;
+import org.apache.drill.exec.record.MaterializedField;
+import org.apache.drill.exec.record.metadata.ColumnMetadata;
+import org.apache.drill.exec.record.metadata.MetadataUtils;
+import org.apache.drill.exec.record.metadata.SchemaBuilder;
+import org.apache.drill.exec.record.metadata.TupleMetadata;
+import org.apache.drill.exec.vector.accessor.ScalarWriter;
+import org.apache.hadoop.mapred.FileSplit;
+import org.apache.parquet.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+public class SasBatchReader implements 
ManagedReader<FileScanFramework.FileSchemaNegotiator> {
+  private static final Logger logger = 
LoggerFactory.getLogger(SasBatchReader.class);
+  private final int maxRecords;
+  private final List<SasColumnWriter> writerList;
+  private FileSplit split;
+  private InputStream fsStream;
+  private SasFileReader sasFileReader;
+  private CustomErrorContext errorContext;
+  private RowSetLoader rowWriter;
+  private Object[] firstRow;
+
+
+  private String compressionMethod;
+  private String fileLabel;
+  private String fileType;
+  private String osName;
+  private String osType;
+  private String sasRelease;
+  private String sessionEncoding;
+  private String serverType;
+  private LocalDate dateCreated;
+  private LocalDate dateModified;
+
+  private enum IMPLICIT_STRING_COLUMN {
+    COMPRESSION_METHOD("_compression_method"),
+    ENCODING("_encoding"),
+    FILE_LABEL("_file_label"),
+    FILE_TYPE("_file_type"),
+    OS_NAME("_os_name"),
+    OS_TYPE("_os_type"),
+    SAS_RELEASE("_sas_release"),
+    SESSION_ENCODING("_session_encoding");
+
+    private final String fieldName;
+
+    IMPLICIT_STRING_COLUMN(String fieldName) {
+      this.fieldName = fieldName;
+    }
+
+    public String getFieldName() {
+      return fieldName;
+    }
+  }
+
+  private enum IMPLICIT_DATE_COLUMN {
+    CREATED_DATE("_date_created"),
+    MODIFIED_DATE("_date_modified");
+
+    private final String fieldName;
+
+    IMPLICIT_DATE_COLUMN(String fieldName) {
+      this.fieldName = fieldName;
+    }
+
+    public String getFieldName() {
+      return fieldName;
+    }
+  }
+
+  public static class SasReaderConfig {
+    protected final SasFormatPlugin plugin;
+    public SasReaderConfig(SasFormatPlugin plugin) {
+      this.plugin = plugin;
+    }
+  }
+
+  public SasBatchReader(int maxRecords) {
+    this.maxRecords = maxRecords;
+    writerList = new ArrayList<>();
+  }
+
+  @Override
+  public boolean open(FileScanFramework.FileSchemaNegotiator negotiator) {
+    split = negotiator.split();
+    errorContext = negotiator.parentErrorContext();
+    openFile(negotiator);
+
+    TupleMetadata schema;
+    if (negotiator.hasProvidedSchema()) {
+      schema = negotiator.providedSchema();
+    } else {
+      schema = buildSchema();
+    }
+    schema = addImplicitColumnsToSchema(schema);
+    negotiator.tableSchema(schema, true);
+
+    ResultSetLoader loader = negotiator.build();
+    rowWriter = loader.writer();
+    buildWriterList(schema);
+
+    return true;
+  }
+
+  private void openFile(FileScanFramework.FileSchemaNegotiator negotiator) {
+    try {
+      fsStream = 
negotiator.fileSystem().openPossiblyCompressedStream(split.getPath());
+      sasFileReader = new SasFileReaderImpl(fsStream);
+      firstRow = sasFileReader.readNext();
+    } catch (IOException e) {
+      throw UserException
+        .dataReadError(e)
+        .message("Unable to open SAS File %s", split.getPath())
+        .addContext(e.getMessage())
+        .addContext(errorContext)
+        .build(logger);
+    }
+  }
+
+  private TupleMetadata buildSchema() {
+    SchemaBuilder builder = new SchemaBuilder();
+    List<Column> columns = sasFileReader.getColumns();
+    int counter = 0;
+    for (Column column : columns) {
+      String fieldName = column.getName();
+      try {
+        MinorType type = getType(firstRow[counter].getClass().getSimpleName());
+        if (type == MinorType.BIGINT && !column.getFormat().isEmpty()) {
+          logger.debug("Found possible time");
+          type = MinorType.TIME;
+        }
+        builder.addNullable(fieldName, type);
+      } catch (Exception e) {
+        System.out.println("Error with column type: " + 
firstRow[counter].getClass().getSimpleName());

Review comment:
       Replaced with `UserException`.




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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to