-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implement INTERSECT ALL, EXCEPT ALL set operations for the multi-stage query engine #13151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...y-runtime/src/main/java/org/apache/pinot/query/runtime/operator/IntersectAllOperator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /** | ||
| * 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 javax.annotation.Nullable; | ||
| import org.apache.pinot.common.utils.DataSchema; | ||
| import org.apache.pinot.core.data.table.Record; | ||
| import org.apache.pinot.query.runtime.plan.OpChainExecutionContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
||
| /** | ||
| * INTERSECT ALL operator. | ||
| */ | ||
| public class IntersectAllOperator extends SetOperator { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(IntersectAllOperator.class); | ||
| private static final String EXPLAIN_NAME = "INTERSECT_ALL"; | ||
|
|
||
| public IntersectAllOperator(OpChainExecutionContext opChainExecutionContext, | ||
| List<MultiStageOperator> upstreamOperators, | ||
| DataSchema dataSchema) { | ||
| super(opChainExecutionContext, upstreamOperators, dataSchema); | ||
| } | ||
|
|
||
| @Override | ||
| public Type getOperatorType() { | ||
| return Type.INTERSECT; | ||
| } | ||
|
|
||
| @Override | ||
| protected Logger logger() { | ||
| return LOGGER; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String toExplainString() { | ||
| return EXPLAIN_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean handleRowMatched(Object[] row) { | ||
| return _rightRowSet.remove(new Record(row)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MinusAllOperator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * 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 javax.annotation.Nullable; | ||
| import org.apache.pinot.common.utils.DataSchema; | ||
| import org.apache.pinot.core.data.table.Record; | ||
| import org.apache.pinot.query.runtime.plan.OpChainExecutionContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
||
| /** | ||
| * EXCEPT ALL operator. | ||
| */ | ||
| public class MinusAllOperator extends SetOperator { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(MinusAllOperator.class); | ||
| private static final String EXPLAIN_NAME = "MINUS_ALL"; | ||
|
|
||
| public MinusAllOperator(OpChainExecutionContext opChainExecutionContext, List<MultiStageOperator> upstreamOperators, | ||
| DataSchema dataSchema) { | ||
| super(opChainExecutionContext, upstreamOperators, dataSchema); | ||
| } | ||
|
|
||
| @Override | ||
| protected Logger logger() { | ||
| return LOGGER; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getOperatorType() { | ||
| return Type.MINUS; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String toExplainString() { | ||
| return EXPLAIN_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean handleRowMatched(Object[] row) { | ||
| return !_rightRowSet.remove(new Record(row)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...ntime/src/test/java/org/apache/pinot/query/runtime/operator/IntersectAllOperatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /** | ||
| * 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.Arrays; | ||
| import java.util.List; | ||
| import org.apache.pinot.common.datablock.DataBlock; | ||
| import org.apache.pinot.common.utils.DataSchema; | ||
| import org.apache.pinot.query.routing.VirtualServerAddress; | ||
| import org.apache.pinot.query.runtime.blocks.TransferableBlock; | ||
| import org.apache.pinot.query.runtime.blocks.TransferableBlockTestUtils; | ||
| 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 IntersectAllOperatorTest { | ||
|
|
||
| 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 testIntersectAllOperator() { | ||
| DataSchema schema = new DataSchema(new String[]{"int_col"}, | ||
| new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.INT}); | ||
|
|
||
| Mockito.when(_leftOperator.nextBlock()) | ||
| .thenReturn(OperatorTestUtil.block(schema, new Object[]{1}, new Object[]{2}, new Object[]{3})) | ||
| .thenReturn(TransferableBlockTestUtils.getEndOfStreamTransferableBlock(0)); | ||
| Mockito.when(_rightOperator.nextBlock()).thenReturn( | ||
| OperatorTestUtil.block(schema, new Object[]{1}, new Object[]{2}, new Object[]{4})) | ||
| .thenReturn(TransferableBlockTestUtils.getEndOfStreamTransferableBlock(0)); | ||
|
|
||
| IntersectAllOperator intersectOperator = | ||
| new IntersectAllOperator(OperatorTestUtil.getTracingContext(), ImmutableList.of(_leftOperator, _rightOperator), | ||
| schema); | ||
|
|
||
| TransferableBlock result = intersectOperator.nextBlock(); | ||
| while (result.getType() != DataBlock.Type.ROW) { | ||
| result = intersectOperator.nextBlock(); | ||
| } | ||
| List<Object[]> resultRows = result.getContainer(); | ||
| List<Object[]> expectedRows = Arrays.asList(new Object[]{1}, new Object[]{2}); | ||
| 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 testIntersectAllOperatorWithDups() { | ||
| DataSchema schema = new DataSchema(new String[]{"int_col"}, | ||
| new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.INT}); | ||
|
|
||
| Mockito.when(_leftOperator.nextBlock()) | ||
| .thenReturn(OperatorTestUtil.block(schema, new Object[]{1}, new Object[]{2}, new Object[]{2}, new Object[]{3}, | ||
| new Object[]{3}, new Object[]{3})) | ||
| .thenReturn(TransferableBlockTestUtils.getEndOfStreamTransferableBlock(0)); | ||
| Mockito.when(_rightOperator.nextBlock()).thenReturn( | ||
| OperatorTestUtil.block(schema, new Object[]{2}, new Object[]{3}, new Object[]{3}, new Object[]{4})) | ||
| .thenReturn(TransferableBlockTestUtils.getEndOfStreamTransferableBlock(0)); | ||
|
|
||
| IntersectAllOperator intersectOperator = | ||
| new IntersectAllOperator(OperatorTestUtil.getTracingContext(), ImmutableList.of(_leftOperator, _rightOperator), | ||
| schema); | ||
|
|
||
| TransferableBlock result = intersectOperator.nextBlock(); | ||
| while (result.getType() != DataBlock.Type.ROW) { | ||
| result = intersectOperator.nextBlock(); | ||
| } | ||
| List<Object[]> resultRows = result.getContainer(); | ||
| List<Object[]> expectedRows = Arrays.asList(new Object[]{2}, new Object[]{3}, new Object[]{3}); | ||
| Assert.assertEquals(resultRows.size(), expectedRows.size()); | ||
| for (int i = 0; i < resultRows.size(); i++) { | ||
| Assert.assertEquals(resultRows.get(i), expectedRows.get(i)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we could create an actual operator that receives a list of blocks and returns that? It would be great to do not use mocks and simplify these tests.
It is not something I think we should do in this PR, but I want just to open that discussion for the future.
Something like:
That is not specially better than mocks, but has the advantage of not returning null as soon as we call other method and it would mean there is a single place to apply changes in future refactors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use a spy or partial mock, but if we're able to use the actual operator without much setup overhead, this definitely makes more sense.
I didn't fully follow this part though.
I can make this change in a follow-up PR for both these new tests as well as the existing tests for the other operators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in general against using mocks. In my experience, they are a cheap solution at short term but problematic in the long run. For example, right now we are mocking operators in a lot of different places just to return a well known sequence of blocks. That can be easily done with a new operator instead of using a mock, but in the first time we needed, we just used mocks. Now that kind of mock is being replicated in a lot of different tests. Imagine that tomorrow we add a second method that is sometimes called in operators: