package io.mycat.calcite.rules;

import io.mycat.calcite.MycatConvention;
import io.mycat.calcite.MycatConverterRule;
import io.mycat.calcite.MycatRules;
import io.mycat.calcite.physical.MycatMinus;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Minus;
import org.apache.calcite.tools.RelBuilderFactory;

import java.util.function.Predicate;

public class MycatMinusRule extends MycatConverterRule {
    /**
     * Creates a MycatMinusRule.
     */
    public MycatMinusRule(MycatConvention out,
                          RelBuilderFactory relBuilderFactory) {
        super(Minus.class, (Predicate<RelNode>) r -> true, MycatRules.convention,
                out, relBuilderFactory, "MycatMinusRule");
    }

    public RelNode convert(RelNode rel) {
        final Minus minus = (Minus) rel;
        final RelTraitSet traitSet =
                minus.getTraitSet().replace(out);
        return MycatMinus.create(traitSet,
                convertList(minus.getInputs(), out), minus.all);
    }
}
