You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "mihaibudiu (via GitHub)" <gi...@apache.org> on 2025/03/31 19:02:47 UTC
Re: [PR] [CALCITE-6927] Add rule for join condition remove IS NOT DISTINCT FROM [calcite]
mihaibudiu commented on code in PR #4277:
URL: https://github.com/apache/calcite/pull/4277#discussion_r2021616347
##########
core/src/main/java/org/apache/calcite/rel/rules/JoinConditionExpandIsNotDistinctFromRule.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.immutables.value.Value;
+
+/**
+ * Planner rule that replaces {@code IS NOT DISTINCT FROM}
+ * in a {@link Join} condition with logically equivalent operations.
+ *
+ * @see SqlStdOperatorTable#IS_NOT_DISTINCT_FROM
+ * @see RelBuilder#isNotDistinctFrom
+ */
+@Value.Enclosing
+public final class JoinConditionExpandIsNotDistinctFromRule
+ extends RelRule<JoinConditionExpandIsNotDistinctFromRule.Config>
+ implements TransformationRule {
+
+ /** Creates a JoinConditionExpandIsNotDistinctFromRule. */
+ JoinConditionExpandIsNotDistinctFromRule(Config config) {
+ super(config);
+ }
+
+ //~ Methods ----------------------------------------------------------------
+
+ @Override public void onMatch(RelOptRuleCall call) {
+ Join join = call.rel(0);
+ RexNode oldJoinCond = join.getCondition();
+
+ if (RexUtil.findOperatorCall(
+ SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
+ oldJoinCond)
+ == null) {
+ // no longer contains isNotDistinctFromOperator
+ return;
+ }
+
+ RemoveIsNotDistinctFromRexShuttle rewriteShuttle =
+ new RemoveIsNotDistinctFromRexShuttle(
+ join.getCluster().getRexBuilder());
+
+ RelNode newJoin = join.accept(rewriteShuttle);
+ call.transformTo(newJoin);
+ }
+
+ //~ Inner Classes ----------------------------------------------------------
+
+ /** Shuttle that removes 'x IS NOT DISTINCT FROM y' and converts it
+ * to '(COALESCE(x, '') = COALESCE(y, ''))
Review Comment:
'' is not an accurate description of what's happening.
##########
core/src/main/java/org/apache/calcite/rel/rules/JoinConditionExpandIsNotDistinctFromRule.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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 org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.immutables.value.Value;
+
+/**
+ * Planner rule that replaces {@code IS NOT DISTINCT FROM}
+ * in a {@link Join} condition with logically equivalent operations.
+ *
+ * @see SqlStdOperatorTable#IS_NOT_DISTINCT_FROM
+ * @see RelBuilder#isNotDistinctFrom
+ */
+@Value.Enclosing
+public final class JoinConditionExpandIsNotDistinctFromRule
+ extends RelRule<JoinConditionExpandIsNotDistinctFromRule.Config>
+ implements TransformationRule {
+
+ /** Creates a JoinConditionExpandIsNotDistinctFromRule. */
+ JoinConditionExpandIsNotDistinctFromRule(Config config) {
+ super(config);
+ }
+
+ //~ Methods ----------------------------------------------------------------
+
+ @Override public void onMatch(RelOptRuleCall call) {
+ Join join = call.rel(0);
+ RexNode oldJoinCond = join.getCondition();
+
+ if (RexUtil.findOperatorCall(
+ SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
+ oldJoinCond)
+ == null) {
+ // no longer contains isNotDistinctFromOperator
+ return;
+ }
+
+ RemoveIsNotDistinctFromRexShuttle rewriteShuttle =
+ new RemoveIsNotDistinctFromRexShuttle(
+ join.getCluster().getRexBuilder());
+
+ RelNode newJoin = join.accept(rewriteShuttle);
+ call.transformTo(newJoin);
+ }
+
+ //~ Inner Classes ----------------------------------------------------------
+
+ /** Shuttle that removes 'x IS NOT DISTINCT FROM y' and converts it
+ * to '(COALESCE(x, '') = COALESCE(y, ''))
+ * AND ((x IS NULL) = (y IS NULL))'. */
+ private static class RemoveIsNotDistinctFromRexShuttle extends RexShuttle {
+ final RexBuilder rexBuilder;
+
+ RemoveIsNotDistinctFromRexShuttle(
+ RexBuilder rexBuilder) {
+ this.rexBuilder = rexBuilder;
+ }
+
+ @Override public RexNode visitCall(RexCall call) {
+ RexNode newCall = super.visitCall(call);
+
+ if (call.getOperator()
+ == SqlStdOperatorTable.IS_NOT_DISTINCT_FROM) {
+ RexCall tmpCall = (RexCall) newCall;
+
+ RexNode operand0 = tmpCall.operands.get(0);
+ RexNode operand1 = tmpCall.operands.get(1);
+
+ RexNode operand0Zero =
+ rexBuilder.makeZeroLiteral(operand0.getType());
Review Comment:
this only works if the type is a numeric type.
joins work on arbitrary types, including arrays and maps
--
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-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org