61yao commented on code in PR #9832: URL: https://github.com/apache/pinot/pull/9832#discussion_r1029892679
########## pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/IntExprRexVisitor.java: ########## @@ -0,0 +1,126 @@ +/** + * 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.planner.logical; + +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexCorrelVariable; +import org.apache.calcite.rex.RexDynamicParam; +import org.apache.calcite.rex.RexFieldAccess; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexOver; +import org.apache.calcite.rex.RexPatternFieldRef; +import org.apache.calcite.rex.RexRangeRef; +import org.apache.calcite.rex.RexSubQuery; +import org.apache.calcite.rex.RexTableInputRef; +import org.apache.calcite.rex.RexVisitor; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; + + +/** + * {@code IntExprRexVisitor} will visit a {@link RexNode} that + * contains only integer literals and uses the {@link SqlStdOperatorTable#PLUS} + * operator and return the Integer that the expression represents. + */ +public class IntExprRexVisitor implements RexVisitor<Integer> { + + public static final IntExprRexVisitor INSTANCE = new IntExprRexVisitor(); + + private IntExprRexVisitor() { + } + + public Integer visit(RexNode in) { + if (in == null) { + return null; + } + + return in.accept(this); + } + + @Override + public Integer visitInputRef(RexInputRef inputRef) { + throw new UnsupportedOperationException(); + } + + @Override + public Integer visitLocalRef(RexLocalRef localRef) { + throw new UnsupportedOperationException(); + } + + @Override + public Integer visitLiteral(RexLiteral literal) { + return literal.getValueAs(Integer.class); + } + + @Override + public Integer visitCall(RexCall call) { + SqlOperator operator = call.getOperator(); + if (!(operator == SqlStdOperatorTable.PLUS)) { Review Comment: Can you explain why only plus is supported? ########## pinot-query-planner/src/main/java/org/apache/calcite/rel/rules/PinotSortExchangeCopyRule.java: ########## @@ -0,0 +1,117 @@ +/** + * 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.base.Preconditions; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelCollation; +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.RelMdUtil; +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.planner.logical.IntExprRexVisitor; +import org.apache.pinot.query.type.TypeFactory; +import org.apache.pinot.query.type.TypeSystem; + + +public class PinotSortExchangeCopyRule extends RelRule<RelRule.Config> { + + public static final PinotSortExchangeCopyRule SORT_EXCHANGE_COPY = + PinotSortExchangeCopyRule.Config.DEFAULT.toRule(); + private static final TypeFactory TYPE_FACTORY = new TypeFactory(new TypeSystem()); + + /** + * Creates a PinotSortExchangeCopyRule. + */ + protected PinotSortExchangeCopyRule(Config config) { + super(config); + } + + @Override + public void onMatch(RelOptRuleCall call) { + final Sort sort = call.rel(0); + final SortExchange exchange = call.rel(1); + final RelMetadataQuery metadataQuery = call.getMetadataQuery(); + + if (RelMdUtil.checkInputForCollationAndLimit( + metadataQuery, + exchange.getInput(), + sort.getCollation(), + sort.offset, + sort.fetch)) { + // Don't rewrite anything if the input is already sorted Review Comment: Add comment saying don't rewrite anything if the input is already sorted and the fetch number greater than row count? ########## pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/IntExprRexVisitor.java: ########## @@ -0,0 +1,126 @@ +/** + * 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.planner.logical; + +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexCorrelVariable; +import org.apache.calcite.rex.RexDynamicParam; +import org.apache.calcite.rex.RexFieldAccess; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexOver; +import org.apache.calcite.rex.RexPatternFieldRef; +import org.apache.calcite.rex.RexRangeRef; +import org.apache.calcite.rex.RexSubQuery; +import org.apache.calcite.rex.RexTableInputRef; +import org.apache.calcite.rex.RexVisitor; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; + + +/** + * {@code IntExprRexVisitor} will visit a {@link RexNode} that + * contains only integer literals and uses the {@link SqlStdOperatorTable#PLUS} + * operator and return the Integer that the expression represents. + */ +public class IntExprRexVisitor implements RexVisitor<Integer> { + + public static final IntExprRexVisitor INSTANCE = new IntExprRexVisitor(); + + private IntExprRexVisitor() { + } + + public Integer visit(RexNode in) { + if (in == null) { + return null; + } Review Comment: Should this just return 0 or we can say do Precheck(in !=null)? I feel it is not good to return null here. ########## pinot-query-planner/src/main/java/org/apache/calcite/rel/rules/PinotSortExchangeCopyRule.java: ########## @@ -0,0 +1,117 @@ +/** + * 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.base.Preconditions; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelCollation; +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.RelMdUtil; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.pinot.query.type.TypeFactory; +import org.apache.pinot.query.type.TypeSystem; + + +public class PinotSortExchangeCopyRule extends RelRule<RelRule.Config> { + + public static final PinotSortExchangeCopyRule SORT_EXCHANGE_COPY = + PinotSortExchangeCopyRule.Config.DEFAULT.toRule(); + public static final TypeFactory TYPE_FACTORY = new TypeFactory(new TypeSystem()); + + /** + * Creates a PinotSortExchangeCopyRule. + */ + protected PinotSortExchangeCopyRule(Config config) { + super(config); + } + + @Override + public void onMatch(RelOptRuleCall call) { + final Sort sort = call.rel(0); + final SortExchange exchange = call.rel(1); + final RelMetadataQuery metadataQuery = call.getMetadataQuery(); + + if (RelMdUtil.checkInputForCollationAndLimit( Review Comment: Thanks a lot for doing this! ########## pinot-query-planner/src/main/java/org/apache/calcite/rel/rules/PinotSortExchangeCopyRule.java: ########## @@ -0,0 +1,117 @@ +/** + * 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.base.Preconditions; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelCollation; +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.RelMdUtil; +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.planner.logical.IntExprRexVisitor; +import org.apache.pinot.query.type.TypeFactory; +import org.apache.pinot.query.type.TypeSystem; + + +public class PinotSortExchangeCopyRule extends RelRule<RelRule.Config> { + + public static final PinotSortExchangeCopyRule SORT_EXCHANGE_COPY = + PinotSortExchangeCopyRule.Config.DEFAULT.toRule(); + private static final TypeFactory TYPE_FACTORY = new TypeFactory(new TypeSystem()); + + /** + * Creates a PinotSortExchangeCopyRule. + */ + protected PinotSortExchangeCopyRule(Config config) { + super(config); + } + + @Override + public void onMatch(RelOptRuleCall call) { + final Sort sort = call.rel(0); + final SortExchange exchange = call.rel(1); + final RelMetadataQuery metadataQuery = call.getMetadataQuery(); + + if (RelMdUtil.checkInputForCollationAndLimit( + metadataQuery, + exchange.getInput(), + sort.getCollation(), + sort.offset, + sort.fetch)) { + // Don't rewrite anything if the input is already sorted + return; + } + + RelCollation collation = sort.getCollation(); + Preconditions.checkArgument( + collation.equals(exchange.getCollation()), + "Expected collation on exchange and sort to be the same" + ); + + // for now, we push down the sort with the offset + limit as the Review Comment: Can we put a comment block saying something like /***customized code for pinot***/.xxxxx/**********/ -- 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