paul-rogers commented on a change in pull request #1778: Drill-7233: Format Plugin for HDF5 URL: https://github.com/apache/drill/pull/1778#discussion_r342887834
########## File path: contrib/format-hdf5/src/main/java/org/apache/drill/exec/store/hdf5/HDF5Utils.java ########## @@ -0,0 +1,253 @@ +/* + * 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.drill.common.types.TypeProtos.MinorType; +import java.io.IOException; +import java.util.BitSet; +import java.util.List; +import java.util.Map; + +public class HDF5Utils { + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HDF5Utils.class); + + private static final boolean caseSensitive = false; + + protected static void reorder(final long[] array) { + long a; + final int max = array.length - 1; + for (int i = (max - 1) / 2; i >= 0; --i) { + final int j = max - i; + a = array[i]; + array[i] = array[j]; + array[j] = a; + } + } + + protected static void reorder(final int[] array) { + int a; + final int max = array.length - 1; + for (int i = (max - 1) / 2; i >= 0; --i) { + final int j = max - i; + a = array[i]; + array[i] = array[j]; + array[j] = a; + } + } + + public static boolean exists(String pathName, IHDF5Reader reader) { + if (pathName.equals("")) { + pathName = "/"; + } + + return reader.exists(pathName); + } + + /** + * This function returns and HDF5Attribute object for use when Drill maps the attributes. + * + * @param pathName + * @param key + * @param reader + * @return + * @throws IOException + */ + public static HDF5Attribute getAttribute(String pathName, final String key, IHDF5Reader reader) throws IOException { + 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(); + reorder(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.enums().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."); + return null; + } + + public static MinorType getDataType(final HDF5DataSetInformation datasetInfo) { + + HDF5DataTypeInformation typeInfo = datasetInfo.getTypeInformation(); + Class<?> type = typeInfo.tryGetJavaType(); + if (type == null) { + logger.info("Datasets of type " + typeInfo + " not implemented."); + //Fall back to string + return MinorType.VARCHAR; + } else if (type.isAssignableFrom(long.class)) { + return MinorType.BIGINT; + } else if (type.isAssignableFrom(short.class)) { + return MinorType.SMALLINT; + } else if (type.isAssignableFrom(byte.class)) { + return MinorType.TINYINT; + } else if (type.isAssignableFrom(int.class)) { + return MinorType.INT; + } else if (type.isAssignableFrom(double.class) || type.isAssignableFrom(float.class)) { + return MinorType.FLOAT8; + } else if (type.isAssignableFrom(String.class)) { + return MinorType.VARCHAR; + } else if (type.isAssignableFrom(java.util.Date.class)) { + return MinorType.TIMESTAMP; + } else if (type.isAssignableFrom(byte.class) || type.isAssignableFrom(boolean.class) || type.isAssignableFrom(BitSet.class)) { + return MinorType.BIT; + } else if (type.isAssignableFrom(Map.class)) { + return MinorType.MAP; + } else if (type.isAssignableFrom(Enum.class)) { + return MinorType.GENERIC_OBJECT; Review comment: Is the intent here to map from an Enum coming from HDF5? Here, is Enum a class defined in HDF5? If so, you could map the enum to either a String (using its name) or an integer (using its ordinal) (assuming that the this `Enum` has those concepts.) ---------------------------------------------------------------- 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 With regards, Apache Git Services