[ 
https://issues.apache.org/jira/browse/CALCITE-6440?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17860999#comment-17860999
 ] 

Caican Cai edited comment on CALCITE-6440 at 6/30/24 2:48 PM:
--------------------------------------------------------------

[~julianhyde] Hi

I encountered a little difficulty. I used the following form to match the form 
of order by empty
{code:java}
Config DEFAULT = ImmutableSortRemoveConstantKeysRule.Config.of()
    .withOperandSupplier(b -> b.operand(Sort.class).oneInput(b1 ->
        b1.operand(Project.class).anyInputs()));{code}
Through this matching, I can achieve the above optimization rules

However, I cannot verify
{code:java}
However, I cannot verify RelOptUtil.verifyTypeEquivalence(rel0, rel, 
rel0);{code}
Because expectedRowType and actualRowType are not the same

I think I can solve this problem by writing a new rule to match two layers of 
Project
{code:java}
Config DEFAULT = ImmutableSortRemoveConstantKeysRule2.Config.of()
    .withOperandSupplier(b -> b.operand(Project.class).oneInput(
        b1 -> b1.operand(Project.class)
    .anyInputs())); {code}
Do you have any good suggestions?

 


was (Author: JIRAUSER302115):
[~julianhyde] Hi

I encountered a little difficulty. I used the following form to match the form 
of order by empty
{code:java}
Config DEFAULT = ImmutableSortRemoveConstantKeysRule.Config.of()
    .withOperandSupplier(b -> b.operand(Sort.class).oneInput(b1 ->
        b1.operand(Project.class).anyInputs()));{code}
Through this matching, I can achieve the above optimization rules

However, I cannot verify
{code:java}
However, I cannot verify RelOptUtil.verifyTypeEquivalence(rel0, rel, 
rel0);{code}
Because expectedRowType and actualRowType are not the same

I think I can solve this problem by writing a new rule to match two layers of 
Project
{code:java}
/*
 * 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.RelOptPredicateList;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelRule;
import org.apache.calcite.rel.RelCollationTraitDef;
import org.apache.calcite.rel.RelCollations;
import org.apache.calcite.rel.RelFieldCollation;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Project;
import org.apache.calcite.rel.core.Sort;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.calcite.rex.RexBuilder;

import org.immutables.value.Value;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Planner rule that removes keys from a
 * a {@link org.apache.calcite.rel.core.Sort} if those keys are known to be
 * constant, or removes the entire Sort if all keys are constant.
 *
 * <p>Requires {@link RelCollationTraitDef}.
 */
@Value.Enclosing
public class SortRemoveConstantKeysRule2
    extends RelRule<SortRemoveConstantKeysRule2.Config>
    implements SubstitutionRule {

  /** Creates a SortRemoveConstantKeysRule. */
  protected SortRemoveConstantKeysRule2(Config config) {
    super(config);
  }

  @Override public void onMatch(RelOptRuleCall call) {
    // Get the first Project
    Project project1 = call.rel(0);
    Project project2 = call.rel(1);

    final RelNode input = project2.getInput();
    final int size = input.getRowType().getFieldList().size();
    // Check if the input of the first Project is also a Project
    if (input.getRowType().getFieldList().get(size-1).getValue().isNullable()) {
      // Replace the second Project with its input
      Project newProject1 = (Project) project1.copy(project1.getTraitSet(), 
ImmutableList.of(input));
      // Transform the rule
      call.transformTo(newProject1);
    }
  }

  /** Rule configuration. */
  @Value.Immutable
  public interface Config extends RelRule.Config {
    Config DEFAULT = ImmutableSortRemoveConstantKeysRule2.Config.of()
        .withOperandSupplier(b -> b.operand(Project.class).oneInput(
            b1 -> b1.operand(Project.class)
        .anyInputs()));

    @Override default SortRemoveConstantKeysRule2 toRule() {
      return new SortRemoveConstantKeysRule2(this);
    }
  }
}
{code}
Do you have any good suggestions?

 

> SortRemoveConstantKeysRule should remove NULL literal sort keys (e.g. ORDER 
> BY NULL)
> ------------------------------------------------------------------------------------
>
>                 Key: CALCITE-6440
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6440
>             Project: Calcite
>          Issue Type: New Feature
>          Components: core
>    Affects Versions: 1.37.0
>            Reporter: Caican Cai
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 1.38.0
>
>
> For the following SQL:
> {code:java}
> select * from task order by null; {code}
> We could rewrite it to:
> {code:java}
>  select * from task;{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to