ihuzenko commented on a change in pull request #1374: [CALCITE-3138] Restructuring ROW type fields is not supported URL: https://github.com/apache/calcite/pull/1374#discussion_r314242038
########## File path: core/src/main/java/org/apache/calcite/sql2rel/RelStructuredTypeFlattener.java ########## @@ -296,42 +273,42 @@ protected int getNewForOldInput(int oldOrdinal) { } /** - * Maps the ordinal of a field pre-flattening to the ordinal of the - * corresponding field post-flattening, and also returns its type. + * Finds type and new ordinal relative to new inputs by oldOrdinal and + * innerOrdinal indexes. * - * @param oldOrdinal Pre-flattening ordinal - * @param existingOffset offset already calculated the target column inside the oldOrdinal column. - * For unnested column, it should be 0. - * @return Post-flattening ordinal and type + * @param oldOrdinal ordinal of the field relative to old inputs + * @param innerOrdinal when oldOrdinal points to struct and target field + * is inner field of struct, this argument should contain + * calculated field's ordinal within struct after flattening. + * Otherwise when oldOrdinal points to primitive field, this + * argument should be zero. + * @return flat type with new ordinal relative to new inputs */ - private Ord<RelDataType> getNewFieldForOldInput(int oldOrdinal, int existingOffset) { + private Ord<RelDataType> getNewFieldForOldInput(int oldOrdinal, int innerOrdinal) { assert currentRel != null; - int newOrdinal = 0; - - // determine which input rel oldOrdinal references, and adjust - // oldOrdinal to be relative to that input rel - RelNode oldInput = null; - RelNode newInput = null; - for (RelNode oldInput1 : currentRel.getInputs()) { - newInput = getNewForOldRel(oldInput1); - RelDataType oldInputType = oldInput1.getRowType(); - int n = oldInputType.getFieldCount(); - if (oldOrdinal < n) { - oldInput = oldInput1; - break; - } - newOrdinal += newInput.getRowType().getFieldCount(); - oldOrdinal -= n; - } - assert oldInput != null; - assert newInput != null; - - RelDataType oldInputType = oldInput.getRowType(); - final int newOffset = calculateFlattenedOffset(oldInputType, oldOrdinal) + existingOffset; - newOrdinal += newOffset; - final RelDataTypeField field = - newInput.getRowType().getFieldList().get(newOffset); - return Ord.of(newOrdinal, field.getType()); + // sum of predecessors post flatten sizes points to new ordinal + // of flat field or first field of flattened struct + final int postFlatteningOrdinal = currentRel.getInputs().stream() + .flatMap(node -> node.getRowType().getFieldList().stream()) Review comment: The code is only called when it's exactly was input reference to field returned by previous node. I don't think that problems may be introduced here, because it's basically a little bit more readable version of the previous method. ---------------------------------------------------------------- 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