illiabarbashov-sketch commented on code in PR #6456: URL: https://github.com/apache/hive/pull/6456#discussion_r3380089558
########## ql/src/test/org/apache/hadoop/hive/ql/optimizer/metainfo/query/TestJoinOperationMetadataResolver.java: ########## @@ -0,0 +1,455 @@ +/* + * 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.hadoop.hive.ql.optimizer.metainfo.query; + +import org.apache.hadoop.hive.ql.exec.*; +import org.apache.hadoop.hive.ql.plan.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link JoinOperationMetadataResolver}. + * <p> + * All Hive operator/plan objects are mocked so the test has no Hadoop/Hive + * runtime dependency. + */ +class TestJoinOperationMetadataResolver { + + /** + * Tracks the intermediate "schema-parent" operator created by rsWithColumnKey/rsWithColumnKeys + * so that attachTso() can wire the TSO into the correct position in the chain. + */ + private final Map<ReduceSinkOperator, Operator<OperatorDesc>> schemaParents = new HashMap<>(); + private JoinOperationMetadataResolver resolver; + + @BeforeEach + void setUp() { + resolver = new JoinOperationMetadataResolver(); + schemaParents.clear(); + } + + @Test + void resolveJoinMetadata_nullParents_leavesArraysNull() { + JoinOperator join = mock(JoinOperator.class); + when(join.getParentOperators()).thenReturn(null); + + resolver.resolveJoinMetadata(join); + + assertNull(resolver.getKeyNames(), "keyNames should remain null when parent list is null"); + assertNull(resolver.getTableAliases(), "tableAliases should remain null when parent list is null"); + } + + @Test + void resolveJoinMetadata_emptyParents_leavesArraysNull() { + JoinOperator join = mock(JoinOperator.class); + when(join.getParentOperators()).thenReturn(Collections.emptyList()); + + resolver.resolveJoinMetadata(join); + + assertNull(resolver.getKeyNames()); + assertNull(resolver.getTableAliases()); + } + + @Test + void resolveJoinMetadata_nullParentSlot_producesUnknown() { + JoinOperator join = mock(JoinOperator.class); + when(join.getParentOperators()).thenReturn(Collections.singletonList(null)); + + resolver.resolveJoinMetadata(join); + + assertEquals("unknown", resolver.getKeyNames()[0]); + assertEquals("unknown", resolver.getTableAliases()[0]); + } + + @Test + void resolveTableAlias_prefersTsoAlias_overTableName() { + RowSchema schema = mock(RowSchema.class); + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "a", "orders"); + + JoinOperator join = joinOp(rs); + resolver.resolveJoinMetadata(join); + + assertEquals("a", resolver.getTableAliases()[0], "Should prefer the TSO alias over the physical table name"); + } + + @Test + void resolveTableAlias_fallsBackToTableName_whenAliasBlank() { + RowSchema schema = mock(RowSchema.class); + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "", "orders"); + + JoinOperator join = joinOp(rs); + resolver.resolveJoinMetadata(join); + + assertEquals("orders", resolver.getTableAliases()[0]); + } + + @Test + void resolveTableAlias_unknownWhenNoTsoFound() { + // Parent is not a ReduceSinkOperator (no TS reachable) + Operator<?> oddParent = mock(Operator.class); + when(oddParent.getParentOperators()).thenReturn(Collections.emptyList()); + + JoinOperator join = joinOp(oddParent); + resolver.resolveJoinMetadata(join); + + assertEquals("unknown", resolver.getTableAliases()[0]); + } + + @Test + void resolveKeyName_columnRef_resolvesToSchemaAlias() { + ColumnInfo ci = mock(ColumnInfo.class); + when(ci.getAlias()).thenReturn("key"); + + RowSchema schema = mock(RowSchema.class); + when(schema.getColumnInfo("_col0")).thenReturn(ci); + + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + + // attach a TSO so table alias is set too + attachTso(rs, "a", "t"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("key", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_columnRef_fallsBackToInternalName_whenNoSchemaAlias() { + ColumnInfo ci = mock(ColumnInfo.class); + when(ci.getAlias()).thenReturn(""); // empty alias + + RowSchema schema = mock(RowSchema.class); + when(schema.getColumnInfo("_col0")).thenReturn(ci); + + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "a", "t"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("_col0", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_columnRef_fallsBackToInternalName_whenColumnInfoNull() { + RowSchema schema = mock(RowSchema.class); + when(schema.getColumnInfo("_col0")).thenReturn(null); + + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "a", "t"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("_col0", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_columnRef_unknownWhenInternalNameNull() { + ExprNodeColumnDesc keyExpr = mock(ExprNodeColumnDesc.class); + when(keyExpr.getColumn()).thenReturn(null); + + ReduceSinkDesc rsConf = mock(ReduceSinkDesc.class); + when(rsConf.getKeyCols()).thenReturn(Collections.singletonList(keyExpr)); + + ReduceSinkOperator rs = mock(ReduceSinkOperator.class); + when(rs.getConf()).thenReturn(rsConf); + when(rs.getParentOperators()).thenReturn(Collections.emptyList()); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("unknown", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_exprKey_usesExprString() { + ReduceSinkOperator rs = rsWithExprKey("upper(val)"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("upper(val)", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_exprKey_unknownWhenExprStringNull() { + ExprNodeDesc keyExpr = mock(ExprNodeDesc.class); + when(keyExpr.getExprString()).thenReturn(null); + + ReduceSinkDesc rsConf = mock(ReduceSinkDesc.class); + when(rsConf.getKeyCols()).thenReturn(Collections.singletonList(keyExpr)); + + ReduceSinkOperator rs = mock(ReduceSinkOperator.class); + when(rs.getConf()).thenReturn(rsConf); + when(rs.getParentOperators()).thenReturn(Collections.emptyList()); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("unknown", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_unknownWhenParentNotReduceSink() { + Operator<?> filterOp = mock(Operator.class); Review Comment: added ########## ql/src/test/org/apache/hadoop/hive/ql/optimizer/metainfo/query/TestJoinOperationMetadataResolver.java: ########## @@ -0,0 +1,455 @@ +/* + * 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.hadoop.hive.ql.optimizer.metainfo.query; + +import org.apache.hadoop.hive.ql.exec.*; +import org.apache.hadoop.hive.ql.plan.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link JoinOperationMetadataResolver}. + * <p> + * All Hive operator/plan objects are mocked so the test has no Hadoop/Hive + * runtime dependency. + */ +class TestJoinOperationMetadataResolver { + + /** + * Tracks the intermediate "schema-parent" operator created by rsWithColumnKey/rsWithColumnKeys + * so that attachTso() can wire the TSO into the correct position in the chain. + */ + private final Map<ReduceSinkOperator, Operator<OperatorDesc>> schemaParents = new HashMap<>(); + private JoinOperationMetadataResolver resolver; + + @BeforeEach + void setUp() { + resolver = new JoinOperationMetadataResolver(); + schemaParents.clear(); + } + + @Test + void resolveJoinMetadata_nullParents_leavesArraysNull() { + JoinOperator join = mock(JoinOperator.class); + when(join.getParentOperators()).thenReturn(null); + + resolver.resolveJoinMetadata(join); + + assertNull(resolver.getKeyNames(), "keyNames should remain null when parent list is null"); + assertNull(resolver.getTableAliases(), "tableAliases should remain null when parent list is null"); + } + + @Test + void resolveJoinMetadata_emptyParents_leavesArraysNull() { + JoinOperator join = mock(JoinOperator.class); + when(join.getParentOperators()).thenReturn(Collections.emptyList()); + + resolver.resolveJoinMetadata(join); + + assertNull(resolver.getKeyNames()); + assertNull(resolver.getTableAliases()); + } + + @Test + void resolveJoinMetadata_nullParentSlot_producesUnknown() { + JoinOperator join = mock(JoinOperator.class); + when(join.getParentOperators()).thenReturn(Collections.singletonList(null)); + + resolver.resolveJoinMetadata(join); + + assertEquals("unknown", resolver.getKeyNames()[0]); + assertEquals("unknown", resolver.getTableAliases()[0]); + } + + @Test + void resolveTableAlias_prefersTsoAlias_overTableName() { + RowSchema schema = mock(RowSchema.class); + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "a", "orders"); + + JoinOperator join = joinOp(rs); + resolver.resolveJoinMetadata(join); + + assertEquals("a", resolver.getTableAliases()[0], "Should prefer the TSO alias over the physical table name"); + } + + @Test + void resolveTableAlias_fallsBackToTableName_whenAliasBlank() { + RowSchema schema = mock(RowSchema.class); + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "", "orders"); + + JoinOperator join = joinOp(rs); + resolver.resolveJoinMetadata(join); + + assertEquals("orders", resolver.getTableAliases()[0]); + } + + @Test + void resolveTableAlias_unknownWhenNoTsoFound() { + // Parent is not a ReduceSinkOperator (no TS reachable) + Operator<?> oddParent = mock(Operator.class); + when(oddParent.getParentOperators()).thenReturn(Collections.emptyList()); + + JoinOperator join = joinOp(oddParent); + resolver.resolveJoinMetadata(join); + + assertEquals("unknown", resolver.getTableAliases()[0]); + } + + @Test + void resolveKeyName_columnRef_resolvesToSchemaAlias() { + ColumnInfo ci = mock(ColumnInfo.class); + when(ci.getAlias()).thenReturn("key"); + + RowSchema schema = mock(RowSchema.class); + when(schema.getColumnInfo("_col0")).thenReturn(ci); + + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + + // attach a TSO so table alias is set too + attachTso(rs, "a", "t"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("key", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_columnRef_fallsBackToInternalName_whenNoSchemaAlias() { + ColumnInfo ci = mock(ColumnInfo.class); + when(ci.getAlias()).thenReturn(""); // empty alias + + RowSchema schema = mock(RowSchema.class); + when(schema.getColumnInfo("_col0")).thenReturn(ci); + + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "a", "t"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("_col0", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_columnRef_fallsBackToInternalName_whenColumnInfoNull() { + RowSchema schema = mock(RowSchema.class); + when(schema.getColumnInfo("_col0")).thenReturn(null); + + ReduceSinkOperator rs = rsWithColumnKey("_col0", schema); + attachTso(rs, "a", "t"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("_col0", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_columnRef_unknownWhenInternalNameNull() { + ExprNodeColumnDesc keyExpr = mock(ExprNodeColumnDesc.class); + when(keyExpr.getColumn()).thenReturn(null); + + ReduceSinkDesc rsConf = mock(ReduceSinkDesc.class); + when(rsConf.getKeyCols()).thenReturn(Collections.singletonList(keyExpr)); + + ReduceSinkOperator rs = mock(ReduceSinkOperator.class); + when(rs.getConf()).thenReturn(rsConf); + when(rs.getParentOperators()).thenReturn(Collections.emptyList()); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("unknown", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_exprKey_usesExprString() { + ReduceSinkOperator rs = rsWithExprKey("upper(val)"); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("upper(val)", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_exprKey_unknownWhenExprStringNull() { + ExprNodeDesc keyExpr = mock(ExprNodeDesc.class); + when(keyExpr.getExprString()).thenReturn(null); + + ReduceSinkDesc rsConf = mock(ReduceSinkDesc.class); + when(rsConf.getKeyCols()).thenReturn(Collections.singletonList(keyExpr)); + + ReduceSinkOperator rs = mock(ReduceSinkOperator.class); + when(rs.getConf()).thenReturn(rsConf); + when(rs.getParentOperators()).thenReturn(Collections.emptyList()); + + resolver.resolveJoinMetadata(joinOp(rs)); + + assertEquals("unknown", resolver.getKeyNames()[0]); + } + + @Test + void resolveKeyName_unknownWhenParentNotReduceSink() { + Operator<?> filterOp = mock(Operator.class); Review Comment: removed -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
