Source:
<code>public AbstractExpression visitInputRef(RexInputRef inputRef) { int exprInputIndx = inputRef.getIndex(); int inputIdx = exprInputIndx; RelDataType inputType = inputRef.getType(); final boolean isFromInnerTable = isFromInnerTable(exprInputIndx); final boolean resolvingNonJoinRef = m_numOuterFieldsForJoin < 0; final boolean resolvingOuter = ! isFromInnerTable && ! m_isInnerTable && m_numOuterFieldsForJoin > 0; <START> final boolean resolvingInner = isFromInnerTable && m_isInnerTable && m_numOuterFieldsForJoin > 0; <END> String columnName = null; String tableName = null; final int tableIndex = isFromInnerTable ? 1 : 0; if (resolvingNonJoinRef || resolvingOuter || resolvingInner) { if (resolvingInner) { exprInputIndx -= m_numOuterFieldsForJoin; } if (resolvingOuter || resolvingInner) { Preconditions.checkState(exprInputIndx < m_program.getProjectList().size()); final RexLocalRef inputLocalRef = m_program.getProjectList().get(exprInputIndx); inputIdx = inputLocalRef.getIndex(); inputType = inputLocalRef.getType(); RexNode expr = m_program.getExprList().get(inputIdx); if (expr.isA(SqlKind.CAST)) { expr = ((RexCall) expr).getOperands().get(0); } final AbstractExpression voltExpr = expr.accept(new RefExpressionConvertingVisitor( m_catTableName, m_catColumns, m_program, -1, false)); voltExpr.findAllTupleValueSubexpressions() .forEach(tve -> tve.setTableIndex(tableIndex)); return voltExpr; } if (m_catColumns != null && inputIdx < m_catColumns.size()) { columnName = m_catColumns.get(inputIdx).getTypeName(); } tableName = m_catTableName; } return visitInputRef(tableIndex, inputIdx, inputType, tableName, columnName); }</code><technical_language>logic is a bit convoluted, find a cleaner way? Basically, need distinguish a stand-alone, outer tables convert Calcite's indexes TVE's ones</technical_language>

Target:
public AbstractExpression visitInputRef(RexInputRef inputRef) { int exprInputIndx = inputRef.getIndex(); int inputIdx = exprInputIndx; RelDataType inputType = inputRef.getType(); final boolean isFromInnerTable = isFromInnerTable(exprInputIndx); final boolean resolvingNonJoinRef = m_numOuterFieldsForJoin < 0; final boolean resolvingOuter = ! isFromInnerTable && ! m_isInnerTable && m_numOuterFieldsForJoin > 0; final boolean resolvingInner = isFromInnerTable && m_isInnerTable && m_numOuterFieldsForJoin > 0; String columnName = null; String tableName = null; final int tableIndex = isFromInnerTable ? 1 : 0; if (resolvingNonJoinRef || resolvingOuter || resolvingInner) { if (resolvingInner) { exprInputIndx -= m_numOuterFieldsForJoin; } if (resolvingOuter || resolvingInner) { Preconditions.checkState(exprInputIndx < m_program.getProjectList().size()); final RexLocalRef inputLocalRef = m_program.getProjectList().get(exprInputIndx); inputIdx = inputLocalRef.getIndex(); RexNode expr = m_program.getExprList().get(inputIdx); if (expr.isA(SqlKind.CAST)) { expr = ((RexCall) expr).getOperands().get(0); } final AbstractExpression voltExpr = expr.accept(new RefExpressionConvertingVisitor( m_catTableName, m_catColumns, m_program, -1, false)); voltExpr.findAllTupleValueSubexpressions() .forEach(tve -> tve.setTableIndex(tableIndex)); return voltExpr; } if (m_catColumns != null && inputIdx < m_catColumns.size()) { columnName = m_catColumns.get(inputIdx).getTypeName(); } tableName = m_catTableName; } return visitInputRef(tableIndex, inputIdx, inputType, tableName, columnName); }