cgivre commented on a change in pull request #2164: URL: https://github.com/apache/drill/pull/2164#discussion_r582486398
########## File path: contrib/format-hdf5/src/main/java/org/apache/drill/exec/store/hdf5/HDF5Utils.java ########## @@ -265,4 +185,126 @@ public static String getNameFromPath(String path) { return ""; } } + + public static Object[] toMatrix(Object[] inputArray) { + return flatten(inputArray).toArray(); + } + + public static boolean[][] toBooleanMatrix(Object[] inputArray) { + Object[] input = flatten(inputArray).toArray(); + int rows = input.length; + int cols = ((boolean[][])input[0]).length; + + boolean[][] result = new boolean[cols][rows]; + + for (int i = 0; i < rows; i++) { + boolean[] row = (boolean[])input[i]; + for (int j = 0; j < cols; j++) { + result[j][i] = row[j]; + } + } + return result; + } + + public static byte[][] toByteMatrix(Object[] inputArray) { + Object[] input = flatten(inputArray).toArray(); + int rows = input.length; + int cols = ((byte[])input[0]).length; + + byte[][] result = new byte[cols][rows]; + + for (int i = 0; i < rows; i++) { + byte[] row = (byte[])input[i]; + for (int j = 0; j < cols; j++) { + result[j][i] = row[j]; + } + } + return result; + } + + public static short[][] toShortMatrix(Object[] inputArray) { + Object[] input = flatten(inputArray).toArray(); + int rows = input.length; + int cols = ((short[])input[0]).length; + + short[][] result = new short[cols][rows]; + + for (int i = 0; i < rows; i++) { + short[] row = (short[])input[i]; + for (int j = 0; j < cols; j++) { + result[j][i] = row[j]; + } + } + return result; + } + + + public static int[][] toIntMatrix(Object[] inputArray) { + Object[] input = flatten(inputArray).toArray(); + int rows = input.length; + int cols = ((int[])input[0]).length; + + int[][] result = new int[cols][rows]; + + for (int i = 0; i < rows; i++) { + int[] row = (int[])input[i]; + for (int j = 0; j < cols; j++) { + result[j][i] = row[j]; + } + } + return result; + } + + public static long[][] toLongMatrix(Object[] inputArray) { + Object[] input = flatten(inputArray).toArray(); + int rows = input.length; + int cols = ((long[])input[0]).length; + + long[][] result = new long[cols][rows]; + + for (int i = 0; i < rows; i++) { + long[] row = (long[])input[i]; + for (int j = 0; j < cols; j++) { + result[j][i] = row[j]; + } + } + return result; + } + + public static float[][] toFloatMatrix(Object[] inputArray) { + Object[] input = flatten(inputArray).toArray(); + int rows = input.length; + int cols = ((float[])input[0]).length; + + float[][] result = new float[cols][rows]; + + for (int i = 0; i < rows; i++) { + float[] row = (float[])input[i]; + for (int j = 0; j < cols; j++) { + result[j][i] = row[j]; + } + } + return result; + } + + public static double[][] toDoubleMatrix(Object[] inputArray) { + Object[] input = flatten(inputArray).toArray(); + int rows = input.length; + int cols = ((double[])input[0]).length; + + double[][] result = new double[cols][rows]; + + for (int i = 0; i < rows; i++) { + double[] row = (double[])input[i]; + for (int j = 0; j < cols; j++) { + result[j][i] = row[j]; + } + } + return result; + } + + public static Stream<Object> flatten(Object[] array) { + return Arrays.stream(array) + .flatMap(o -> o instanceof Object[]? flatten((Object[])o): Stream.of(o)); + } Review comment: Thanks!! The other HDF5 implementation had a `toMatrix()` function which flattened everything greater than 2D. Drill has the possibility of creating indefinitely nested arrays to mirror the HDF5 structure, but it's kind of difficult to get right. ---------------------------------------------------------------- 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