mihaibudiu commented on code in PR #3597:
URL: https://github.com/apache/calcite/pull/3597#discussion_r1447886735


##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.

Review Comment:
   get replaced 



##########
core/src/test/java/org/apache/calcite/test/JdbcTest.java:
##########
@@ -2973,27 +2973,29 @@ void testInnerJoinValues(String format) {
     switch (format) {
     case "text":
       expected = "EnumerableAggregate(group=[{0, 3}])\n"
-          + "  EnumerableNestedLoopJoin(condition=[=(CAST($1):INTEGER NOT 
NULL, $2)], joinType=[inner])\n"
-          + "    EnumerableTableScan(table=[[SALES, EMPS]])\n"
-          + "    EnumerableCalc(expr#0..1=[{inputs}], expr#2=['SameName'], 
expr#3=[=($t1, $t2)], proj#0..1=[{exprs}], $condition=[$t3])\n"
-          + "      EnumerableValues(tuples=[[{ 10, 'SameName' }]])\n";
+          + "  EnumerableCalc(expr#0..1=[{inputs}], expr#2=[10], 
expr#3=['SameName'], expr#4=[CAST($t1):INTEGER NOT NULL], expr#5=[=($t4, $t2)], 
proj#0..3=[{exprs}], $condition=[$t5])\n"
+          + "    EnumerableTableScan(table=[[SALES, EMPS]])\n\n";
       extra = "";
       break;
     case "dot":
       expected = "PLAN=digraph {\n"
-          + "\"EnumerableNestedLoop\\nJoin\\ncondition = =(CAST($\\n1):INTEGER 
NOT NULL,\\n $2)"
-          + "\\njoinType = inner\\n\" -> \"EnumerableAggregate\\ngroup = {0, 
3}\\n\" "
-          + "[label=\"0\"]\n"
-          + "\"EnumerableTableScan\\ntable = [SALES, EMPS\\n]\\n\" -> "
-          + "\"EnumerableNestedLoop\\nJoin\\ncondition = =(CAST($\\n1):INTEGER 
NOT NULL,\\n $2)"
-          + "\\njoinType = inner\\n\" [label=\"0\"]\n"
-          + "\"EnumerableCalc\\nexpr#0..1 = {inputs}\\nexpr#2 = 
'SameName'\\nexpr#3 = =($t1, $t2)"
-          + "\\nproj#0..1 = {exprs}\\n$condition = $t3\" -> "
-          + "\"EnumerableNestedLoop\\nJoin\\ncondition = =(CAST($\\n1):INTEGER 
NOT NULL,\\n $2)"
-          + "\\njoinType = inner\\n\" [label=\"1\"]\n"
-          + "\"EnumerableValues\\ntuples = [{ 10, 'Sam\\neName' }]\\n\" -> "
-          + "\"EnumerableCalc\\nexpr#0..1 = {inputs}\\nexpr#2 = 
'SameName'\\nexpr#3 = =($t1, $t2)"
-          + "\\nproj#0..1 = {exprs}\\n$condition = $t3\" [label=\"0\"]\n"
+          + "\"EnumerableCalc\\n"
+          + "expr#0..1 = {inputs}\\n"
+          + "expr#2 = 10\\n"
+          + "expr#3 = 'SameName'\\n"
+          + "expr#4 = CAST($t1):I\\n"
+          + "NTEGER NOT NULL\\n"
+          + "...\" -> \"EnumerableAggregate\\n"
+          + "group = {0, 3}\\n"
+          + "\" [label=\"0\"]\n"
+          + "\"EnumerableTableScan\\n"
+          + "table = [SALES, EMPS\\n]\\n"
+          + "\" -> \"EnumerableCalc\\n"
+          + "expr#0..1 = {inputs}\\n"
+          + "expr#2 = 10\\n"
+          + "expr#3 = 'SameName'\\n"
+          + "expr#4 = CAST($t1):I\\nNTEGER NOT NULL\\n"

Review Comment:
   do you know what is happening here?
   What is this \\n inside an INTEGER?



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.
+   */
+  protected abstract static class PruneSingleValueRule
+      extends RelRule<PruneSingleValueRule.Config>
+      implements SubstitutionRule {
+    protected PruneSingleValueRule(PruneSingleValueRule.Config config) {
+      super(config);
+    }
+
+    protected BiFunction<RexNode, List<RexNode>, List<RexNode>>
+        getRexTransformer(RexBuilder rexBuilder,
+        JoinRelType joinRelType) {
+      switch (joinRelType) {
+      case LEFT:
+      case RIGHT:
+        return (condition, rexLiterals) -> rexLiterals.stream().map(lit ->
+            rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
+                lit, 
rexBuilder.makeNullLiteral(lit.getType()))).collect(Collectors.toList());
+      default:
+        return (condition, rexLiterals) -> new ArrayList<>(rexLiterals);
+      }
+    }
+
+    /**
+     * onMatch function contains common optimization logic for all the
+     * SingleValueOptimization rules. It simplifies the rel node tree by
+     * removing a Join node and creating a required filter as applicable.
+     * In case of the LEFT/RIGHT joins, a case expression which produce NULL

Review Comment:
   I still think that showing an example plan before and after could very much 
help understand what is going on. You can just take the plans from the XML file.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.
+   */
+  protected abstract static class PruneSingleValueRule
+      extends RelRule<PruneSingleValueRule.Config>
+      implements SubstitutionRule {
+    protected PruneSingleValueRule(PruneSingleValueRule.Config config) {
+      super(config);
+    }
+
+    protected BiFunction<RexNode, List<RexNode>, List<RexNode>>
+        getRexTransformer(RexBuilder rexBuilder,
+        JoinRelType joinRelType) {
+      switch (joinRelType) {
+      case LEFT:
+      case RIGHT:
+        return (condition, rexLiterals) -> rexLiterals.stream().map(lit ->
+            rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
+                lit, 
rexBuilder.makeNullLiteral(lit.getType()))).collect(Collectors.toList());
+      default:
+        return (condition, rexLiterals) -> new ArrayList<>(rexLiterals);
+      }
+    }
+
+    /**
+     * onMatch function contains common optimization logic for all the
+     * SingleValueOptimization rules. It simplifies the rel node tree by
+     * removing a Join node and creating a required filter as applicable.
+     * In case of the LEFT/RIGHT joins, a case expression which produce NULL
+     * values for non-matching rows will be created as part of a Project node.
+     *
+     * @param call A RelOptRuleCall object
+     * @param values A constant relation node which produces a single row.
+     * @param project A project node which has dynamic constants (can be null).
+     * @param join A join node which will get removed.
+     * @param other A node on the other side of the join (apart from Values)
+     * @param isLeft Whether a Values node is on the Left side or the right 
side of the Join.
+     */
+    protected void onMatch(RelOptRuleCall call, Values values,
+        @Nullable Project project, Join join,
+        RelNode other, boolean isLeft) {
+      Predicate<Join> predicate = eligibilityPredicate(isLeft);
+      List<RexNode> rexNodes;
+      if (project != null) {
+        ImmutableBitSet bitSet = ImmutableBitSet.range(0, 
values.getRowType().getFieldCount());
+        RexShuttle shuttle =
+            new ReplaceExprWithConstValue(bitSet,
+                    new ArrayList<>(values.getTuples().get(0)),
+                0);
+
+        rexNodes = project.getProjects().stream()
+            .map(shuttle::apply)
+            .collect(Collectors.toList());
+      } else {
+        rexNodes = new ArrayList<>(values.tuples.get(0));

Review Comment:
   Maybe you can factor out this as a helper function.
   You previously used getTuples(), here you just used tuples.
   This could be called getSingle(Values) and could also assert that values has 
a single tuple.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.

Review Comment:
   I wouldn't say that this function "removes" the join. This function returns 
the replacement for the join. The removal is done somewhere else. Although it 
is a standard use of the return result, I would still document that this 
returns "null" when the join cannot be optimized.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.
+   */
+  protected abstract static class PruneSingleValueRule
+      extends RelRule<PruneSingleValueRule.Config>
+      implements SubstitutionRule {
+    protected PruneSingleValueRule(PruneSingleValueRule.Config config) {
+      super(config);
+    }
+
+    protected BiFunction<RexNode, List<RexNode>, List<RexNode>>
+        getRexTransformer(RexBuilder rexBuilder,
+        JoinRelType joinRelType) {
+      switch (joinRelType) {
+      case LEFT:
+      case RIGHT:
+        return (condition, rexLiterals) -> rexLiterals.stream().map(lit ->
+            rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
+                lit, 
rexBuilder.makeNullLiteral(lit.getType()))).collect(Collectors.toList());
+      default:
+        return (condition, rexLiterals) -> new ArrayList<>(rexLiterals);

Review Comment:
   do you have to copy the rexLiterals here?



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.

Review Comment:
   I don't find it useful in the comment to describe something which is obvious 
from the adjacent code: the fact that it implements this interface. The comment 
can describe perhaps why you implement this interface.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {

Review Comment:
   why call this argument "condition"?
   



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.
+   */
+  protected abstract static class PruneSingleValueRule
+      extends RelRule<PruneSingleValueRule.Config>
+      implements SubstitutionRule {
+    protected PruneSingleValueRule(PruneSingleValueRule.Config config) {
+      super(config);
+    }
+
+    protected BiFunction<RexNode, List<RexNode>, List<RexNode>>
+        getRexTransformer(RexBuilder rexBuilder,
+        JoinRelType joinRelType) {
+      switch (joinRelType) {
+      case LEFT:
+      case RIGHT:
+        return (condition, rexLiterals) -> rexLiterals.stream().map(lit ->
+            rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
+                lit, 
rexBuilder.makeNullLiteral(lit.getType()))).collect(Collectors.toList());
+      default:
+        return (condition, rexLiterals) -> new ArrayList<>(rexLiterals);
+      }
+    }
+
+    /**
+     * onMatch function contains common optimization logic for all the
+     * SingleValueOptimization rules. It simplifies the rel node tree by
+     * removing a Join node and creating a required filter as applicable.
+     * In case of the LEFT/RIGHT joins, a case expression which produce NULL
+     * values for non-matching rows will be created as part of a Project node.
+     *
+     * @param call A RelOptRuleCall object
+     * @param values A constant relation node which produces a single row.
+     * @param project A project node which has dynamic constants (can be null).
+     * @param join A join node which will get removed.
+     * @param other A node on the other side of the join (apart from Values)
+     * @param isLeft Whether a Values node is on the Left side or the right 
side of the Join.

Review Comment:
   no need to capitalize Left.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.

Review Comment:
   technically this shuttle works even if the fieldValues are not constants.
   So perhaps you can say that and say that and say that in practice the 
fieldValues are constant.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.
+   */
+  protected abstract static class PruneSingleValueRule
+      extends RelRule<PruneSingleValueRule.Config>
+      implements SubstitutionRule {
+    protected PruneSingleValueRule(PruneSingleValueRule.Config config) {
+      super(config);
+    }
+
+    protected BiFunction<RexNode, List<RexNode>, List<RexNode>>
+        getRexTransformer(RexBuilder rexBuilder,
+        JoinRelType joinRelType) {
+      switch (joinRelType) {
+      case LEFT:
+      case RIGHT:
+        return (condition, rexLiterals) -> rexLiterals.stream().map(lit ->
+            rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
+                lit, 
rexBuilder.makeNullLiteral(lit.getType()))).collect(Collectors.toList());
+      default:
+        return (condition, rexLiterals) -> new ArrayList<>(rexLiterals);
+      }
+    }
+
+    /**
+     * onMatch function contains common optimization logic for all the
+     * SingleValueOptimization rules. It simplifies the rel node tree by
+     * removing a Join node and creating a required filter as applicable.
+     * In case of the LEFT/RIGHT joins, a case expression which produce NULL
+     * values for non-matching rows will be created as part of a Project node.
+     *
+     * @param call A RelOptRuleCall object
+     * @param values A constant relation node which produces a single row.
+     * @param project A project node which has dynamic constants (can be null).
+     * @param join A join node which will get removed.
+     * @param other A node on the other side of the join (apart from Values)
+     * @param isLeft Whether a Values node is on the Left side or the right 
side of the Join.
+     */
+    protected void onMatch(RelOptRuleCall call, Values values,
+        @Nullable Project project, Join join,
+        RelNode other, boolean isLeft) {
+      Predicate<Join> predicate = eligibilityPredicate(isLeft);
+      List<RexNode> rexNodes;
+      if (project != null) {
+        ImmutableBitSet bitSet = ImmutableBitSet.range(0, 
values.getRowType().getFieldCount());
+        RexShuttle shuttle =
+            new ReplaceExprWithConstValue(bitSet,
+                    new ArrayList<>(values.getTuples().get(0)),
+                0);
+
+        rexNodes = project.getProjects().stream()
+            .map(shuttle::apply)
+            .collect(Collectors.toList());
+      } else {
+        rexNodes = new ArrayList<>(values.tuples.get(0));
+      }
+      RelBuilder relBuilder = call.builder();
+      RelNode transformed =
+          new SingleValuesRelTransformer(join, rexNodes, other,
+              predicate, isLeft, getRexTransformer(relBuilder.getRexBuilder(),
+              join.getJoinType())).transform(relBuilder);
+      if (transformed != null) {
+        call.transformTo(transformed);
+      }
+    }
+
+    static Predicate<Join> eligibilityPredicate(boolean isLeft) {
+      if (isLeft) {
+        return jn -> jn.getJoinType() == JoinRelType.LEFT

Review Comment:
   This may be clearer if you factor out the common case outside the 'if'.
   One other thing that is confusing is that this is actually the 
"nonEligibilityPredicate".
   Perhaps it would be clearer if you actually complement all definitions and 
uses of this predicate.
   Then you can call the previous field "canTransform".



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValuesOptimizationRules.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify joins which have one of their input as 
constant relations
+ * {@link Values} that produce a single row.
+ *
+ * <p>Conventionally, the way to represent a single row constant relational 
expression is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValuesOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValuesOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValuesOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValuesOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a Join rel node tree with single constant 
row {@link Values}
+   * on either side of the Join to a simplified tree without a Join rel node.
+   */
+  private static class SingleValuesRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    /**
+     * A transformer object which transforms a Join rel node tree with 
constant relation
+     * node as one of its input to a rel node tree without a Join.
+     *
+     * @param join Join which is eligible for removal.
+     * @param rexNodes List of the expressions that are part of Project
+     * @param otherNode RelNode which is other side of the Join (apart from 
Values node)
+     * @param nonTransformable Predicate to check if the given Join is 
transformable or not.
+     * @param isValuesLeftChild TRUE if Values is left child of join, FALSE 
otherwise.
+     * @param litTransformer A transformer function supplied by the caller.
+     *                       This function is specific to Join Type.
+     *                       LEFT/ RIGHT => has logic to produce null values 
for unmatched rows.
+     *                       INNER => produce the rexLiterals specified in the 
Values node.
+     */
+    protected SingleValuesRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    /**
+     * A transform function which removes the joins when eligibility criteria 
is met.
+     *
+     * @param relBuilder Relation Builder supplied by the planner framework.
+     * @return Simplified relNode tree by removing Join.
+     */
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+
+    /**
+     * A RexShuttle replacer which replaces an inputRefs with corresponding
+     * constant values.
+     *
+     * @param bitSet A bitmap to track indices of the inputRef that gets 
replaced.
+     * @param values Constant values that are used to replace inputRefs.
+     * @param offset offset to be applied for the inputRef index to get the 
constant values.
+     */
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.
+   */
+  protected abstract static class PruneSingleValueRule
+      extends RelRule<PruneSingleValueRule.Config>
+      implements SubstitutionRule {
+    protected PruneSingleValueRule(PruneSingleValueRule.Config config) {
+      super(config);
+    }
+
+    protected BiFunction<RexNode, List<RexNode>, List<RexNode>>
+        getRexTransformer(RexBuilder rexBuilder,
+        JoinRelType joinRelType) {
+      switch (joinRelType) {
+      case LEFT:
+      case RIGHT:
+        return (condition, rexLiterals) -> rexLiterals.stream().map(lit ->
+            rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
+                lit, 
rexBuilder.makeNullLiteral(lit.getType()))).collect(Collectors.toList());
+      default:
+        return (condition, rexLiterals) -> new ArrayList<>(rexLiterals);
+      }
+    }
+
+    /**
+     * onMatch function contains common optimization logic for all the
+     * SingleValueOptimization rules. It simplifies the rel node tree by
+     * removing a Join node and creating a required filter as applicable.
+     * In case of the LEFT/RIGHT joins, a case expression which produce NULL
+     * values for non-matching rows will be created as part of a Project node.
+     *
+     * @param call A RelOptRuleCall object
+     * @param values A constant relation node which produces a single row.
+     * @param project A project node which has dynamic constants (can be null).
+     * @param join A join node which will get removed.
+     * @param other A node on the other side of the join (apart from Values)
+     * @param isLeft Whether a Values node is on the Left side or the right 
side of the Join.
+     */
+    protected void onMatch(RelOptRuleCall call, Values values,
+        @Nullable Project project, Join join,
+        RelNode other, boolean isLeft) {
+      Predicate<Join> predicate = eligibilityPredicate(isLeft);
+      List<RexNode> rexNodes;
+      if (project != null) {
+        ImmutableBitSet bitSet = ImmutableBitSet.range(0, 
values.getRowType().getFieldCount());
+        RexShuttle shuttle =
+            new ReplaceExprWithConstValue(bitSet,
+                    new ArrayList<>(values.getTuples().get(0)),
+                0);
+
+        rexNodes = project.getProjects().stream()
+            .map(shuttle::apply)
+            .collect(Collectors.toList());
+      } else {
+        rexNodes = new ArrayList<>(values.tuples.get(0));
+      }
+      RelBuilder relBuilder = call.builder();
+      RelNode transformed =

Review Comment:
   I find that it's more convenient for debugging to have one statement per 
line.
   So I would make a separate statement with the new and one with 'transform'.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValueOptimizationRules.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify sections of query plan which are known to
+ * produce single row.
+ *
+ * <p>Conventionally, the way to represent a single row relational expression 
is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValueOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValueOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValueOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValueOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValueOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a single value nodes on either side of the 
join.
+   * This transformer contains the common code for all the SingleValueJoin 
rules.
+   */
+  private static class SingleValueRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    protected SingleValueRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    public @Nullable RelNode transform(RelBuilder relBuilder) {

Review Comment:
   Can you please add a comment in the code containing the second paragraph in 
my comment? I had to run in the debugger to understand why this happens.



##########
core/src/main/java/org/apache/calcite/rel/rules/SingleValueOptimizationRules.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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.RelOptRule;
+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.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Collection of rules which simplify sections of query plan which are known to
+ * produce single row.
+ *
+ * <p>Conventionally, the way to represent a single row relational expression 
is
+ * with a {@link Values} that has one tuple.
+ *
+ * @see LogicalValues#createOneRow
+ */
+public abstract class SingleValueOptimizationRules {
+
+  public static final RelOptRule JOIN_LEFT_INSTANCE =
+      SingleValueOptimizationRules.JoinLeftSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_INSTANCE =
+      SingleValueOptimizationRules.JoinRightSingleRuleConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_LEFT_PROJECT_INSTANCE =
+      
SingleValueOptimizationRules.JoinLeftSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  public static final RelOptRule JOIN_RIGHT_PROJECT_INSTANCE =
+      
SingleValueOptimizationRules.JoinRightSingleValueRuleWithExprConfig.DEFAULT.toRule();
+
+  /**
+   * Transformer class to transform a single value nodes on either side of the 
join.
+   * This transformer contains the common code for all the SingleValueJoin 
rules.
+   */
+  private static class SingleValueRelTransformer {
+
+    private final Join join;
+    private final RelNode relNode;
+    private final Predicate<Join> cannotTransform;
+    private final BiFunction<RexNode, List<RexNode>, List<RexNode>> 
litTransformer;
+    private final boolean valuesAsLeftChild;
+    private final List<RexNode> literals;
+
+    protected SingleValueRelTransformer(
+        Join join, List<RexNode> rexNodes, RelNode otherNode,
+        Predicate<Join> nonTransformable, boolean isValuesLeftChild,
+        BiFunction<RexNode, List<RexNode>, List<RexNode>> litTransformer) {
+      this.relNode = otherNode;
+      this.join = join;
+      this.cannotTransform = nonTransformable;
+      this.litTransformer = litTransformer;
+      this.valuesAsLeftChild = isValuesLeftChild;
+      this.literals = rexNodes;
+    }
+
+    public @Nullable RelNode transform(RelBuilder relBuilder) {
+      if (cannotTransform.test(join)) {
+        return null;
+      }
+      int end = valuesAsLeftChild
+          ? join.getLeft().getRowType().getFieldCount()
+          : join.getRowType().getFieldCount();
+
+      int start = valuesAsLeftChild
+          ? 0
+          : join.getLeft().getRowType().getFieldCount();
+      ImmutableBitSet bitSet = ImmutableBitSet.range(start, end);
+      RexNode trueNode = relBuilder.getRexBuilder().makeLiteral(true);
+      final RexNode filterCondition =
+          new ReplaceExprWithConstValue(bitSet,
+              literals,
+              (valuesAsLeftChild ? 0 : -1) * 
join.getLeft().getRowType().getFieldCount())
+              .go(join.getCondition());
+
+      RexNode fixedCondition =
+          valuesAsLeftChild
+              ? RexUtil.shift(filterCondition,
+              -1 * join.getLeft().getRowType().getFieldCount())
+              : filterCondition;
+
+      List<RexNode> rexLiterals = litTransformer.apply(fixedCondition, 
literals);
+      relBuilder.push(relNode)
+          .filter(join.getJoinType().isOuterJoin() ? trueNode : 
fixedCondition);
+
+      List<RexNode> rexNodes = relNode
+          .getRowType()
+          .getFieldList()
+          .stream()
+          .map(fld -> relBuilder.field(fld.getIndex()))
+          .collect(Collectors.toList());
+
+      List<RexNode> projects = new ArrayList<>();
+      projects.addAll(valuesAsLeftChild ? rexLiterals : rexNodes);
+      projects.addAll(valuesAsLeftChild ? rexNodes : rexLiterals);
+      return relBuilder.project(projects).build();
+    }
+  }
+
+  /**
+   * A rex shuttle to replace field refs with constants from a {@link Values} 
row.
+   */
+  private static class ReplaceExprWithConstValue extends RexShuttle {
+
+    private final ImmutableBitSet bitSet;
+    private final List<RexNode> fieldValues;
+    private final int offset;
+    ReplaceExprWithConstValue(ImmutableBitSet bitSet, List<RexNode> values, 
int offset) {
+      this.bitSet = bitSet;
+      this.fieldValues = values;
+      this.offset = offset;
+    }
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (bitSet.get(inputRef.getIndex())) {
+        return this.fieldValues.get(inputRef.getIndex() + offset);
+      }
+      return super.visitInputRef(inputRef);
+    }
+
+    public RexNode go(RexNode condition) {
+      return condition.accept(this);
+    }
+  }
+
+  /**
+   * Abstract prune single value rule that implements SubstitutionRule 
interface.
+   */
+  protected abstract static class PruneSingleValueRule
+      extends RelRule<PruneSingleValueRule.Config>
+      implements SubstitutionRule {
+    protected PruneSingleValueRule(PruneSingleValueRule.Config config) {
+      super(config);
+    }
+
+    protected BiFunction<RexNode, List<RexNode>, List<RexNode>>
+        getRexTransformer(RexBuilder rexBuilder,
+        JoinRelType joinRelType) {
+      switch (joinRelType) {
+      case LEFT:
+      case RIGHT:
+        return (condition, rexLiterals) -> rexLiterals.stream().map(lit ->
+            rexBuilder.makeCall(SqlStdOperatorTable.CASE, condition,
+                lit, 
rexBuilder.makeNullLiteral(lit.getType()))).collect(Collectors.toList());
+      default:
+        return (condition, rexLiterals) -> new ArrayList<>(rexLiterals);
+      }
+    }
+
+    protected void onMatch(RelOptRuleCall call, Values values,
+        @Nullable Project project, Join join,
+        RelNode other, boolean isLeft) {
+      Predicate<Join> predicate = elibilityPredicate(isLeft);
+      List<RexNode> rexNodes;
+      if (project != null) {
+        ImmutableBitSet bitSet = ImmutableBitSet.range(0, 
values.getRowType().getFieldCount());
+        RexShuttle shuttle =
+            new ReplaceExprWithConstValue(bitSet,
+                    new ArrayList<>(values.getTuples().get(0)),
+                0);
+
+        rexNodes = project.getProjects().stream()
+            .map(shuttle::apply)
+            .collect(Collectors.toList());
+      } else {
+        rexNodes = new ArrayList<>(values.tuples.get(0));
+      }
+      RelBuilder relBuilder = call.builder();
+      RelNode transformed =
+          new SingleValueRelTransformer(join, rexNodes, other,
+              predicate, isLeft, getRexTransformer(relBuilder.getRexBuilder(),
+              join.getJoinType())).transform(relBuilder);
+      if (transformed != null) {
+        call.transformTo(transformed);
+      }
+    }
+
+    static Predicate<Join> elibilityPredicate(boolean isLeft) {
+      if (isLeft) {
+        return jn -> jn.getJoinType() == JoinRelType.LEFT
+            || jn.getJoinType() == JoinRelType.FULL
+            || jn.getJoinType() == JoinRelType.ANTI;
+      } else {
+        return jn -> jn.getJoinType() == JoinRelType.RIGHT
+            || jn.getJoinType() == JoinRelType.FULL
+            || jn.getJoinType() == JoinRelType.ANTI;
+      }
+    }
+
+    @Override public boolean autoPruneOld() {
+      return true;
+    }
+
+    /** Rule configuration. */
+    protected interface Config extends RelRule.Config {
+      @Override PruneSingleValueRule toRule();
+    }
+  }
+
+  /** Configuration for a rule that simplifies join node with constant row on 
its right input. */
+  @Value.Immutable
+  interface JoinRightSingleRuleConfig extends PruneSingleValueRule.Config {
+    JoinRightSingleRuleConfig DEFAULT = ImmutableJoinRightSingleRuleConfig.of()
+        .withOperandSupplier(b0 ->
+            b0.operand(Join.class).inputs(
+                b1 -> b1.operand(RelNode.class).anyInputs(),
+                b2 -> 
b2.operand(Values.class).predicate(Values::isSingleValue).noInputs()))
+        .withDescription("PruneJoinSingleValue(right)");
+
+    @Override default SingleValueOptimizationRules.PruneSingleValueRule 
toRule() {
+      return new SingleValueOptimizationRules.PruneSingleValueRule(this) {
+        @Override public void onMatch(RelOptRuleCall call) {
+          final Join join = call.rel(0);
+          final Values values = call.rel(2);
+          final RelNode other = call.rel(1);
+          onMatch(call, values, null, join, other, false);
+        }
+      };
+    }
+  }
+
+  /** Configuration for a rule that simplifies join node with constant row on 
its left input. */
+  @Value.Immutable
+  interface JoinLeftSingleRuleConfig extends PruneSingleValueRule.Config {
+    JoinLeftSingleRuleConfig DEFAULT = ImmutableJoinLeftSingleRuleConfig.of()
+        .withOperandSupplier(b0 ->
+            b0.operand(Join.class).inputs(
+                b1 -> 
b1.operand(Values.class).predicate(Values::isSingleValue).noInputs(),
+                b2 -> b2.operand(RelNode.class).anyInputs()))
+        .withDescription("PruneJoinSingleValueRule(left)");
+
+    @Override default SingleValueOptimizationRules.PruneSingleValueRule 
toRule() {
+      return new SingleValueOptimizationRules.PruneSingleValueRule(this) {
+        @Override public void onMatch(RelOptRuleCall call) {
+          final Join join = call.rel(0);
+          final Values values = call.rel(1);
+          final RelNode other = call.rel(2);
+          onMatch(call, values, null, join, other, true);
+        }
+      };
+    }
+  }
+
+  /** Configuration for a rule that simplifies join node with a project on a 
constant row

Review Comment:
   please add this as a comment in the code too.
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to