vvysotskyi commented on a change in pull request #1951: DRILL-7454: Convert Avro to EVF URL: https://github.com/apache/drill/pull/1951#discussion_r363088696
########## File path: exec/java-exec/src/test/java/org/apache/drill/exec/store/avro/AvroDataGenerator.java ########## @@ -0,0 +1,819 @@ +/* + * 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.avro; + +import org.apache.avro.LogicalType; +import org.apache.avro.LogicalTypes; +import org.apache.avro.Schema; +import org.apache.avro.Schema.Type; +import org.apache.avro.SchemaBuilder; +import org.apache.avro.file.DataFileWriter; +import org.apache.avro.generic.GenericArray; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericDatumWriter; +import org.apache.avro.generic.GenericRecord; +import org.apache.drill.exec.util.JsonStringArrayList; +import org.apache.drill.exec.util.JsonStringHashMap; +import org.apache.drill.exec.util.Text; +import org.apache.drill.test.BaseDirTestWatcher; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Paths; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +/** + * Utilities for generating Avro test data. + */ +public class AvroDataGenerator { + + public static final int RECORD_COUNT = 50; + public static int ARRAY_SIZE = 4; + + private final BaseDirTestWatcher dirTestWatcher; + + public AvroDataGenerator(BaseDirTestWatcher dirTestWatcher) { + this.dirTestWatcher = dirTestWatcher; + } + + /** + * Class to write records to an Avro file while simultaneously + * constructing a corresponding list of records in the format taken in + * by the Drill test builder to describe expected results. + */ + public static class AvroTestRecordWriter implements Closeable { + + private final List<Map<String, Object>> expectedRecords; + private final Schema schema; + private final DataFileWriter<GenericData.Record> writer; + private final String filePath; + private final String fileName; + + private GenericData.Record currentAvroRecord; + private Map<String, Object> currentExpectedRecord; + + public AvroTestRecordWriter(Schema schema, File file) { + writer = new DataFileWriter<>(new GenericDatumWriter<>(schema)); + try { + writer.create(schema, file); + } catch (IOException e) { + throw new RuntimeException("Error creating file in Avro test setup.", e); + } + this.schema = schema; + currentExpectedRecord = new TreeMap<>(); + expectedRecords = new ArrayList<>(); + filePath = file.getAbsolutePath(); + fileName = file.getName(); + } + + public void startRecord() { + currentAvroRecord = new GenericData.Record(schema); + currentExpectedRecord = new TreeMap<>(); + } + + public void put(String key, Object value) { + currentAvroRecord.put(key, value); + // convert binary values into byte[], the format they will be given + // in the Drill result set in the test framework + currentExpectedRecord.put("`" + key + "`", convertAvroValToDrill(value, true)); + } + + // TODO - fix this the test wrapper to prevent the need for this hack + // to make the root behave differently than nested fields for String vs. Text + private Object convertAvroValToDrill(Object value, boolean root) { + if (value instanceof ByteBuffer) { + ByteBuffer bb = ((ByteBuffer)value); + byte[] drillVal = new byte[((ByteBuffer)value).remaining()]; Review comment: ```suggestion byte[] drillVal = new byte[((ByteBuffer) value).remaining()]; ``` ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
