arina-ielchiieva commented on a change in pull request #1778: Drill-7233: Format Plugin for HDF5 URL: https://github.com/apache/drill/pull/1778#discussion_r349050374
########## File path: contrib/format-hdf5/src/main/java/org/apache/drill/exec/store/hdf5/HDF5Utils.java ########## @@ -0,0 +1,259 @@ +/* + * 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.hdf5; + +import ch.systemsx.cisd.hdf5.HDF5DataClass; +import ch.systemsx.cisd.hdf5.HDF5EnumerationValue; +import ch.systemsx.cisd.hdf5.IHDF5Reader; +import ch.systemsx.cisd.hdf5.HDF5DataSetInformation; +import ch.systemsx.cisd.hdf5.HDF5DataTypeInformation; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.drill.common.types.TypeProtos.MinorType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.BitSet; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class HDF5Utils { + private static final Logger logger = LoggerFactory.getLogger(HDF5Utils.class); + + private static final boolean caseSensitive = false; + + private static final Pattern PATH_PATTERN = Pattern.compile("/*.*/(.+?)$"); + + /** + * This function returns and HDF5Attribute object for use when Drill maps the attributes. + * + * @param pathName The path to retrieve attributes from + * @param key The key for the specific attribute you are retrieving + * @param reader The IHDF5 reader object for the file you are querying + * @return HDF5Attribute The attribute from the path with the key that was requested. + */ + static HDF5Attribute getAttribute(String pathName, final String key, IHDF5Reader reader) { + if (pathName.equals("")) { + pathName = "/"; + } + + if (!reader.exists(pathName)) { + return null; + } + + if (key.equals("dimensions")) { + final HDF5DataSetInformation datasetInfo = reader.object().getDataSetInformation(pathName); + final long[] dimensions = datasetInfo.getDimensions(); + ArrayUtils.reverse(dimensions); + return new HDF5Attribute(MinorType.LIST, "dimensions", dimensions); + } + + if (key.equals("dataType")) { + final HDF5DataSetInformation datasetInfo = reader.object().getDataSetInformation(pathName); + return new HDF5Attribute(getDataType(datasetInfo), "DataType", datasetInfo.getTypeInformation().getDataClass()); + } + + if (!reader.object().hasAttribute(pathName, key)) { + return null; + } + + HDF5DataTypeInformation attributeInfo = reader.object().getAttributeInformation(pathName, key); + Class<?> type = attributeInfo.tryGetJavaType(); + if (type.isAssignableFrom(long[].class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.BIGINT, key, reader.int64().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.BIGINT, key, reader.uint64().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(int[].class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.INT, key, reader.int32().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.INT, key, reader.uint32().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(short[].class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.INT, key, reader.int16().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.INT, key, reader.uint16().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(byte[].class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.INT, key, reader.int8().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.INT, key, reader.uint8().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(double[].class)) { + return new HDF5Attribute(MinorType.FLOAT8, key, reader.float64().getAttr(pathName, key)); + } else if (type.isAssignableFrom(float[].class)) { + return new HDF5Attribute(MinorType.FLOAT8, key, reader.float32().getAttr(pathName, key)); + } else if (type.isAssignableFrom(String[].class)) { + return new HDF5Attribute(MinorType.VARCHAR, key, reader.string().getAttr(pathName, key)); + } else if (type.isAssignableFrom(long.class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.BIGINT, key, reader.int64().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.BIGINT, key, reader.uint64().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(int.class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.INT, key, reader.int32().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.INT, key, reader.uint32().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(short.class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.INT, key, reader.int16().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.INT, key, reader.uint16().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(byte.class)) { + if (attributeInfo.isSigned()) { + return new HDF5Attribute(MinorType.INT, key, reader.int8().getAttr(pathName, key)); + } else { + return new HDF5Attribute(MinorType.INT, key, reader.uint8().getAttr(pathName, key)); + } + } else if (type.isAssignableFrom(double.class)) { + return new HDF5Attribute(MinorType.FLOAT8, key, reader.float64().getAttr(pathName, key)); + } else if (type.isAssignableFrom(float.class)) { + return new HDF5Attribute(MinorType.FLOAT4, key, reader.float32().getAttr(pathName, key)); + } else if (type.isAssignableFrom(String.class)) { + return new HDF5Attribute(MinorType.VARCHAR, key, reader.string().getAttr(pathName, key)); + } else if (type.isAssignableFrom(boolean.class)) { + return new HDF5Attribute(MinorType.BIT, key, reader.bool().getAttr(pathName, key)); + } else if (type.isAssignableFrom(HDF5EnumerationValue.class)) { + // Convert HDF5 Enum to String + return new HDF5Attribute(MinorType.GENERIC_OBJECT, key, reader.enumeration().getAttr(pathName, key)); + } else if (type.isAssignableFrom(BitSet.class)) { + return new HDF5Attribute(MinorType.BIT, key, reader.bool().getAttr(pathName, key)); + } + + logger.info("Reading attributes of type " + attributeInfo + " not yet implemented."); Review comment: Loggers have `{}`, please do not use concatenation. ---------------------------------------------------------------- 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
