walterddr commented on code in PR #9832: URL: https://github.com/apache/pinot/pull/9832#discussion_r1036173291
########## pinot-query-planner/src/test/java/org/apache/calcite/rel/rules/PinotSortExchangeCopyRuleTest.java: ########## @@ -0,0 +1,222 @@ +/** + * 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.calcite.rel.rules; + +import com.google.common.collect.ImmutableList; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelTraitSet; +import org.apache.calcite.rel.RelCollation; +import org.apache.calcite.rel.RelCollations; +import org.apache.calcite.rel.RelDistributions; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Sort; +import org.apache.calcite.rel.core.SortExchange; +import org.apache.calcite.rel.logical.LogicalSort; +import org.apache.calcite.rel.logical.LogicalSortExchange; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.pinot.query.type.TypeFactory; +import org.apache.pinot.query.type.TypeSystem; +import org.mockito.ArgumentCaptor; +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 PinotSortExchangeCopyRuleTest { + + public static final TypeFactory TYPE_FACTORY = new TypeFactory(new TypeSystem()); + private static final RexBuilder REX_BUILDER = new RexBuilder(TYPE_FACTORY); + + private AutoCloseable _mocks; + + @Mock + private RelOptRuleCall _call; + @Mock + private RelNode _input; + @Mock + private RelOptCluster _cluster; + @Mock + private RelMetadataQuery _query; + + @BeforeMethod + public void setUp() { + _mocks = MockitoAnnotations.openMocks(this); + RelTraitSet traits = RelTraitSet.createEmpty(); + Mockito.when(_input.getTraitSet()).thenReturn(traits); + Mockito.when(_input.getCluster()).thenReturn(_cluster); + Mockito.when(_call.getMetadataQuery()).thenReturn(_query); + Mockito.when(_query.getMaxRowCount(Mockito.any())).thenReturn(null); + } + + @AfterMethod + public void tearDown() + throws Exception { + _mocks.close(); + } + + @Test + public void shouldMatchLimitNoOffsetNoSort() { + // Given: + SortExchange exchange = LogicalSortExchange.create(_input, RelDistributions.SINGLETON, RelCollations.EMPTY); + Sort sort = LogicalSort.create(exchange, RelCollations.EMPTY, null, literal(1)); + Mockito.when(_call.rel(0)).thenReturn(sort); + Mockito.when(_call.rel(1)).thenReturn(exchange); + + // When: + PinotSortExchangeCopyRule.SORT_EXCHANGE_COPY.onMatch(_call); + + // Then: + ArgumentCaptor<RelNode> sortCopyCapture = ArgumentCaptor.forClass(LogicalSort.class); + Mockito.verify(_call, Mockito.times(1)).transformTo(sortCopyCapture.capture(), Mockito.anyMap()); + + RelNode sortCopy = sortCopyCapture.getValue(); + Assert.assertTrue(sortCopy instanceof LogicalSort); + Assert.assertTrue(((LogicalSort) sortCopy).getInput() instanceof LogicalSortExchange); + Assert.assertTrue(((LogicalSort) sortCopy).getInput().getInput(0) instanceof LogicalSort); + + LogicalSort innerSort = (LogicalSort) ((LogicalSort) sortCopy).getInput().getInput(0); + Assert.assertEquals(innerSort.getCollation().getKeys().size(), 0); + Assert.assertNull((innerSort).offset); + Assert.assertEquals((innerSort).fetch, literal(1)); + } + + @Test + public void shouldMatchNoSortAndPushDownLimitPlusOffset() { + // Given: + SortExchange exchange = LogicalSortExchange.create(_input, RelDistributions.SINGLETON, RelCollations.EMPTY); + Sort sort = LogicalSort.create(exchange, RelCollations.EMPTY, literal(2), literal(1)); + Mockito.when(_call.rel(0)).thenReturn(sort); + Mockito.when(_call.rel(1)).thenReturn(exchange); + + // When: + PinotSortExchangeCopyRule.SORT_EXCHANGE_COPY.onMatch(_call); + + // Then: + ArgumentCaptor<RelNode> sortCopyCapture = ArgumentCaptor.forClass(LogicalSort.class); + Mockito.verify(_call, Mockito.times(1)).transformTo(sortCopyCapture.capture(), Mockito.anyMap()); + + RelNode sortCopy = sortCopyCapture.getValue(); + Assert.assertTrue(sortCopy instanceof LogicalSort); + Assert.assertTrue(((LogicalSort) sortCopy).getInput() instanceof LogicalSortExchange); + Assert.assertTrue(((LogicalSort) sortCopy).getInput().getInput(0) instanceof LogicalSort); + + LogicalSort innerSort = (LogicalSort) ((LogicalSort) sortCopy).getInput().getInput(0); + Assert.assertEquals(innerSort.getCollation().getKeys().size(), 0); + Assert.assertNull((innerSort).offset); + Assert.assertEquals((innerSort).fetch, literal(3)); + } + + @Test + public void shouldMatchSortOnly() { + // Given: + RelCollation collation = RelCollations.of(1); + SortExchange exchange = LogicalSortExchange.create(_input, RelDistributions.SINGLETON, collation); + Sort sort = LogicalSort.create(exchange, collation, null, null); + Mockito.when(_call.rel(0)).thenReturn(sort); + Mockito.when(_call.rel(1)).thenReturn(exchange); + + // When: + PinotSortExchangeCopyRule.SORT_EXCHANGE_COPY.onMatch(_call); + + // Then: + ArgumentCaptor<RelNode> sortCopyCapture = ArgumentCaptor.forClass(LogicalSort.class); + Mockito.verify(_call, Mockito.times(1)).transformTo(sortCopyCapture.capture(), Mockito.anyMap()); + + RelNode sortCopy = sortCopyCapture.getValue(); + Assert.assertTrue(sortCopy instanceof LogicalSort); + Assert.assertTrue(((LogicalSort) sortCopy).getInput() instanceof LogicalSortExchange); + Assert.assertTrue(((LogicalSort) sortCopy).getInput().getInput(0) instanceof LogicalSort); + + LogicalSort innerSort = (LogicalSort) ((LogicalSort) sortCopy).getInput().getInput(0); + Assert.assertEquals(innerSort.getCollation(), collation); + Assert.assertNull((innerSort).offset); + Assert.assertNull((innerSort).fetch); + } + + @Test + public void shouldMatchLimitOffsetAndSort() { Review Comment: missing `shouldMatchLimitNoOffsetSort`? -- 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: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org