yashmayya commented on code in PR #16570: URL: https://github.com/apache/pinot/pull/16570#discussion_r2275153599
########## pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/operator/UnionAllOperatorTest.java: ########## @@ -0,0 +1,135 @@ +/** + * 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.pinot.query.runtime.operator; + +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.pinot.common.utils.DataSchema; +import org.apache.pinot.query.routing.VirtualServerAddress; +import org.apache.pinot.query.runtime.blocks.ErrorMseBlock; +import org.apache.pinot.query.runtime.blocks.MseBlock; +import org.apache.pinot.query.runtime.blocks.SuccessMseBlock; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + + +public class UnionAllOperatorTest { + private AutoCloseable _mocks; + + @Mock + private MultiStageOperator _leftOperator; + + @Mock + private MultiStageOperator _rightOperator; + + @Mock + private VirtualServerAddress _serverAddress; + + @BeforeMethod + public void setUp() { + _mocks = MockitoAnnotations.openMocks(this); + Mockito.when(_serverAddress.toString()).thenReturn(new VirtualServerAddress("mock", 80, 0).toString()); + } + + @AfterMethod + public void tearDown() + throws Exception { + _mocks.close(); + } + + @Test + public void testUnionOperator() { + DataSchema schema = new DataSchema(new String[]{"int_col", "string_col"}, new DataSchema.ColumnDataType[]{ + DataSchema.ColumnDataType.INT, DataSchema.ColumnDataType.STRING + }); + Mockito.when(_leftOperator.nextBlock()) + .thenReturn(OperatorTestUtil.block(schema, new Object[]{1, "AA"}, new Object[]{2, "BB"})) + .thenReturn(SuccessMseBlock.INSTANCE); + Mockito.when(_rightOperator.nextBlock()).thenReturn( + OperatorTestUtil.block(schema, new Object[]{3, "aa"}, new Object[]{4, "bb"}, new Object[]{5, "cc"})) + .thenReturn(SuccessMseBlock.INSTANCE); + + UnionAllOperator unionAllOperator = + new UnionAllOperator(OperatorTestUtil.getTracingContext(), ImmutableList.of(_leftOperator, _rightOperator), + schema); + List<Object[]> resultRows = new ArrayList<>(); + MseBlock result = unionAllOperator.nextBlock(); + while (result.isData()) { + resultRows.addAll(((MseBlock.Data) result).asRowHeap().getRows()); + result = unionAllOperator.nextBlock(); + } + // Note that UNION ALL does not guarantee the order of rows, and our implementation adds rows from the right child + // first + List<Object[]> expectedRows = + Arrays.asList(new Object[]{3, "aa"}, new Object[]{4, "bb"}, new Object[]{5, "cc"}, new Object[]{1, "AA"}, + new Object[]{2, "BB"}); + Assert.assertEquals(resultRows.size(), expectedRows.size()); + for (int i = 0; i < resultRows.size(); i++) { + Assert.assertEquals(resultRows.get(i), expectedRows.get(i)); + } + } + + @Test + public void testErrorBlockRightChild() { + DataSchema schema = new DataSchema(new String[]{"int_col", "string_col"}, new DataSchema.ColumnDataType[]{ + DataSchema.ColumnDataType.INT, DataSchema.ColumnDataType.STRING + }); + Mockito.when(_leftOperator.nextBlock()) + .thenReturn(OperatorTestUtil.block(schema, new Object[]{1, "AA"}, new Object[]{2, "BB"})) + .thenReturn(SuccessMseBlock.INSTANCE); + Mockito.when(_rightOperator.nextBlock()) + .thenReturn(ErrorMseBlock.fromException(new RuntimeException("Error in right operator"))); + + UnionAllOperator unionAllOperator = + new UnionAllOperator(OperatorTestUtil.getTracingContext(), ImmutableList.of(_leftOperator, _rightOperator), + schema); + MseBlock result = unionAllOperator.nextBlock(); + Assert.assertTrue(result.isError()); + } + + @Test + public void testErrorBlockLeftChild() { + DataSchema schema = new DataSchema(new String[]{"int_col", "string_col"}, new DataSchema.ColumnDataType[]{ + DataSchema.ColumnDataType.INT, DataSchema.ColumnDataType.STRING + }); + Mockito.when(_leftOperator.nextBlock()) + .thenReturn(ErrorMseBlock.fromException(new RuntimeException("Error in left operator"))); + Mockito.when(_rightOperator.nextBlock()) + .thenReturn(OperatorTestUtil.block(schema, new Object[]{3, "aa"}, new Object[]{4, "bb"})) + .thenReturn(SuccessMseBlock.INSTANCE); + + UnionAllOperator unionAllOperator = + new UnionAllOperator(OperatorTestUtil.getTracingContext(), ImmutableList.of(_leftOperator, _rightOperator), + schema); + MseBlock result = unionAllOperator.nextBlock(); + // The first block will be the regular data block from the right operator + Assert.assertFalse(result.isError()); + Assert.assertTrue(result.isData()); + // The second block should be the error block from the left operator + result = unionAllOperator.nextBlock(); + Assert.assertTrue(result.isError()); Review Comment: Good point, I've updated all these tests to instead call `nextBlock` in a loop until we get an EoS block and then verify that it is an error EoS block. ########## pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/operator/IntersectOperatorTest.java: ########## @@ -115,4 +116,40 @@ public void testDedup() { Assert.assertEquals(resultRows.get(i), expectedRows.get(i)); } } + + @Test + public void testErrorBlockRightChild() { + DataSchema schema = new DataSchema(new String[]{"int_col", "string_col"}, new DataSchema.ColumnDataType[]{ + DataSchema.ColumnDataType.INT, DataSchema.ColumnDataType.STRING + }); + Mockito.when(_leftOperator.nextBlock()) + .thenReturn(OperatorTestUtil.block(schema, new Object[]{1, "AA"}, new Object[]{2, "BB"})) + .thenReturn(SuccessMseBlock.INSTANCE); + Mockito.when(_rightOperator.nextBlock()) + .thenReturn(ErrorMseBlock.fromException(new RuntimeException("Error in right operator"))); Review Comment: There's already tests in this class using the mocking pattern. I'll refactor all of them to use `BlockListMultiStageOperator` in a follow-up PR. ########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/UnionAllOperator.java: ########## @@ -0,0 +1,70 @@ +/** + * 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.pinot.query.runtime.operator; + +import java.util.List; +import org.apache.pinot.common.utils.DataSchema; +import org.apache.pinot.query.runtime.blocks.MseBlock; +import org.apache.pinot.query.runtime.plan.OpChainExecutionContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Union operator for UNION ALL queries. + */ +public class UnionAllOperator extends SetOperator { + private static final Logger LOGGER = LoggerFactory.getLogger(UnionAllOperator.class); + private static final String EXPLAIN_NAME = "UNION_ALL"; + + public UnionAllOperator(OpChainExecutionContext opChainExecutionContext, List<MultiStageOperator> inputOperators, + DataSchema dataSchema) { + super(opChainExecutionContext, inputOperators, dataSchema); + } + + @Override + protected Logger logger() { + return LOGGER; + } + + @Override + public Type getOperatorType() { + return Type.UNION; + } + + @Override + public String toExplainString() { + return EXPLAIN_NAME; + } + + @Override + protected MseBlock processRightOperator() { + return _rightChildOperator.nextBlock(); + } + + @Override + protected MseBlock processLeftOperator() { + return _leftChildOperator.nextBlock(); + } + + @Override + protected boolean handleRowMatched(Object[] row) { + throw new UnsupportedOperationException("UNION ALL operator does not support row matching"); Review Comment: Yup strongly agree with this sentiment, since it's a clear violation of the Interface Segregation Principle. I'll create a follow-up refactor PR doing exactly what you suggested (naming is going to be the only tricky concern 🙂). -- 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]
