paul-rogers commented on a change in pull request #1114: Drill-6104: Added Logfile Reader URL: https://github.com/apache/drill/pull/1114#discussion_r200864202
########## File path: exec/java-exec/src/main/java/org/apache/drill/exec/store/log/LogRecordReader.java ########## @@ -0,0 +1,623 @@ +/* + * 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.log; + +import com.google.common.base.Charsets; +import org.apache.drill.common.exceptions.UserException; +import org.apache.drill.common.expression.SchemaPath; +import org.apache.drill.common.types.TypeProtos.MinorType; +import org.apache.drill.common.types.Types; +import org.apache.drill.exec.exception.SchemaChangeException; +import org.apache.drill.exec.ops.FragmentContext; +import org.apache.drill.exec.ops.OperatorContext; +import org.apache.drill.exec.physical.impl.OutputMutator; +import org.apache.drill.exec.record.MaterializedField; +import org.apache.drill.exec.store.AbstractRecordReader; +import org.apache.drill.exec.store.dfs.DrillFileSystem; +import org.apache.drill.exec.store.dfs.easy.FileWork; +import org.apache.drill.exec.vector.NullableDateVector; +import org.apache.drill.exec.vector.NullableTimeVector; +import org.apache.drill.exec.vector.NullableTimeStampVector; +import org.apache.drill.exec.vector.NullableIntVector; +import org.apache.drill.exec.vector.NullableVarCharVector; +import org.apache.drill.exec.vector.NullableFloat8Vector; +import org.apache.drill.exec.vector.BaseValueVector; +import org.apache.hadoop.fs.Path; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +public class LogRecordReader extends AbstractRecordReader { + + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LogRecordReader.class); + + private abstract static class ColumnDefn { + public final String name; + public final int index; + public final String format; + + public ColumnDefn(String name, int index) { + this.name = name; + this.index = index; + this.format = ""; + } + + public ColumnDefn(String name, int index, String format) { + this.name = name; + this.index = index; + this.format = format; + } + + public abstract void define(OutputMutator outputMutator) throws SchemaChangeException; + + public abstract void load(int rowIndex, String value); + + @Override + //For testing + public String toString() { + return "Name: " + name + ", Index: " + index; + } + } + + private static class VarCharDefn extends ColumnDefn { + + private NullableVarCharVector.Mutator mutator; + + public VarCharDefn(String name, int index) { + super(name, index); + } + + @Override + public void define(OutputMutator outputMutator) throws SchemaChangeException { + MaterializedField field = MaterializedField.create(name, + Types.optional(MinorType.VARCHAR)); + mutator = outputMutator.addField(field, NullableVarCharVector.class).getMutator(); + } + + @Override + public void load(int rowIndex, String value) { + mutator.set(rowIndex, value.getBytes()); + } + } + + private static class IntDefn extends ColumnDefn { + + private NullableIntVector.Mutator mutator; + + public IntDefn(String name, int index) { + super(name, index); + } + + @Override + public void define(OutputMutator outputMutator) throws SchemaChangeException { + MaterializedField field = MaterializedField.create(name, + Types.optional(MinorType.INT)); + mutator = outputMutator.addField(field, NullableIntVector.class).getMutator(); + } + + @Override + public void load(int rowIndex, String value) { + try { + mutator.set(rowIndex, Integer.parseInt(value)); + } catch (NumberFormatException e) { + throw UserException + .dataReadError(e) + .addContext("Failed to parse an INT field") + .addContext("Column", name) + .addContext("Position", index) + .addContext("Value", value) + .build(logger); + } + } + } + + private static class DoubleDefn extends ColumnDefn { + + private NullableFloat8Vector.Mutator mutator; + + public DoubleDefn(String name, int index) { + super(name, index); + } + + @Override + public void define(OutputMutator outputMutator) throws SchemaChangeException { + MaterializedField field = MaterializedField.create(name, + Types.optional(MinorType.FLOAT8)); + mutator = outputMutator.addField(field, NullableFloat8Vector.class).getMutator(); + } + + @Override + public void load(int rowIndex, String value) { + try { + mutator.set(rowIndex, Double.parseDouble(value)); + } catch (NumberFormatException e) { + throw UserException + .dataReadError(e) + .addContext("Failed to parse an FLOAT field") + .addContext("Column", name) + .addContext("Position", index) + .addContext("Value", value) + .build(logger); + } + } + } + + private static class DateDefn extends ColumnDefn { + + private NullableDateVector.Mutator mutator; + private SimpleDateFormat df; + + public DateDefn(String name, int index, String dateFormat) { + super(name, index, dateFormat); + df = getValidDateObject(dateFormat); + } + + private SimpleDateFormat getValidDateObject(String d) { + SimpleDateFormat tempDateFormat; + if (d != null && !d.isEmpty()) { + tempDateFormat = new SimpleDateFormat(d); + } else { + throw UserException.parseError() + .message("Invalid date format. The date formatting string was empty.") + .build(logger); + } + return tempDateFormat; + } + + @Override + public void define(OutputMutator outputMutator) throws SchemaChangeException { + MaterializedField field = MaterializedField.create(name, + Types.optional(MinorType.DATE)); + mutator = outputMutator.addField(field, NullableDateVector.class).getMutator(); + } + + @Override + public void load(int rowIndex, String value) { + try { + Date d = df.parse(value); + long milliseconds = d.getTime(); + mutator.set(rowIndex, milliseconds); + } catch (NumberFormatException e) { + throw UserException + .dataReadError(e) + .addContext("Failed to parse an DATE field") + .addContext("Column", name) + .addContext("Position", index) + .addContext("Value", value) + .build(logger); + } catch (ParseException e) { + throw UserException + .dataReadError(e) + .addContext("Date Format String does not match field value.") + .addContext("Column", name) + .addContext("Position", index) + .addContext("Format String", format) + .addContext("Value", value) + .build(logger); + } + } + } + + private static class TimeDefn extends ColumnDefn { + + private NullableTimeVector.Mutator mutator; + private SimpleDateFormat df; + + public TimeDefn(String name, int index, String dateFormat) { + super(name, index, dateFormat); + df = getValidDateObject(dateFormat); + } + + private SimpleDateFormat getValidDateObject(String d) { + SimpleDateFormat tempDateFormat; + if (d != null && !d.isEmpty()) { + tempDateFormat = new SimpleDateFormat(d); Review comment: Does the combination of date and time actually work to handle a timestamp? The timestamp is in which timezone? Do we need code to convert the timestamp from its ISO+TZ format to UTC (which drill kinda-sorta-not-really uses internally)? Or, to be more accurate, should we convert the ISO UTC time into the local time zone which Drill uses in place of UTC in its dates? (Yes, this is VERY broken and confusing.) At the very least, should we use a timestamp type in place of separate date and time types so that there is at least some chance that the combined timestamp can be converted correctly across timezones (and converted correctly by JDBC)? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
