Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public String toExplainString() {

@Override
protected boolean handleRowMatched(Object[] row) {
return _rightRowSet.remove(new Record(row));
return _rightRowSet.setCount(new Record(row), 0) != 0;
}
}
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public String toExplainString() {

@Override
protected boolean handleRowMatched(Object[] row) {
return _rightRowSet.add(new Record(row));
Record record = new Record(row);
if (_rightRowSet.contains(record)) {
return false;
} else {
_rightRowSet.add(record);
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/
package org.apache.pinot.query.runtime.operator;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import org.apache.pinot.common.datablock.DataBlock;
import org.apache.pinot.common.datatable.StatMap;
Expand All @@ -44,7 +44,7 @@
* UnionOperator: The right child operator is consumed in a blocking manner.
*/
public abstract class SetOperator extends MultiStageOperator {
protected final Set<Record> _rightRowSet;
protected final Multiset<Record> _rightRowSet;

private final List<MultiStageOperator> _upstreamOperators;
private final MultiStageOperator _leftChildOperator;
Expand All @@ -65,7 +65,7 @@ public SetOperator(OpChainExecutionContext opChainExecutionContext, List<MultiSt
_upstreamOperators = upstreamOperators;
_leftChildOperator = getChildOperators().get(0);
_rightChildOperator = getChildOperators().get(1);
_rightRowSet = new HashSet<>();
_rightRowSet = HashMultiset.create();
_isRightSetBuilt = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
import org.apache.pinot.query.runtime.operator.AggregateOperator;
import org.apache.pinot.query.runtime.operator.FilterOperator;
import org.apache.pinot.query.runtime.operator.HashJoinOperator;
import org.apache.pinot.query.runtime.operator.IntersectAllOperator;
import org.apache.pinot.query.runtime.operator.IntersectOperator;
import org.apache.pinot.query.runtime.operator.LeafStageTransferableBlockOperator;
import org.apache.pinot.query.runtime.operator.LiteralValueOperator;
import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
import org.apache.pinot.query.runtime.operator.MailboxSendOperator;
import org.apache.pinot.query.runtime.operator.MinusAllOperator;
import org.apache.pinot.query.runtime.operator.MinusOperator;
import org.apache.pinot.query.runtime.operator.MultiStageOperator;
import org.apache.pinot.query.runtime.operator.OpChain;
Expand Down Expand Up @@ -125,9 +127,13 @@ public MultiStageOperator visitSetOp(SetOpNode setOpNode, OpChainExecutionContex
case UNION:
return new UnionOperator(context, inputs, setOpNode.getInputs().get(0).getDataSchema());
case INTERSECT:
return new IntersectOperator(context, inputs, setOpNode.getInputs().get(0).getDataSchema());
return setOpNode.isAll()
? new IntersectAllOperator(context, inputs, setOpNode.getInputs().get(0).getDataSchema())
: new IntersectOperator(context, inputs, setOpNode.getInputs().get(0).getDataSchema());
case MINUS:
return new MinusOperator(context, inputs, setOpNode.getInputs().get(0).getDataSchema());
return setOpNode.isAll()
? new MinusAllOperator(context, inputs, setOpNode.getInputs().get(0).getDataSchema())
: new MinusOperator(context, inputs, setOpNode.getInputs().get(0).getDataSchema());
default:
throw new IllegalStateException();
}
Expand Down
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));
Comment on lines +68 to +73
Copy link
Contributor

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:

_leftOperator = new BlockListOperator.builder()
     .andThen(block1)
     .andThen(block2)
     .endWith(eosBlock(stats)) // or errorBlock(errors)

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but has the advantage of not returning null as soon as we call other method

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.

it would mean there is a single place to apply changes in future refactors

I didn't fully follow this part though.

It is not something I think we should do in this PR, but I want just to open that discussion for the future.

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.

Copy link
Contributor

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:

  • In the code that use mocks, we need to iterate over all the tests to verify that we add an answer for that new method. We need to iterate over these tests manually because the compiler won't help us here. We can run the tests and see which ones fail, but in case we have tests that try different mocks in the same method (something that is also an anti-pattern, but we have several cases like that) we would need to run tests several times.
  • In the code that use a test operator, we just need to implement that method once (in that class) and ideally the compiler help us here.


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));
}
}
}
Loading