|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.plan.metadata; |
| 7 | + |
| 8 | +import com.google.common.collect.ImmutableList; |
| 9 | +import java.lang.invoke.MethodHandle; |
| 10 | +import java.lang.invoke.MethodHandles; |
| 11 | +import java.lang.invoke.MethodType; |
| 12 | +import java.util.List; |
| 13 | +import org.apache.calcite.adapter.enumerable.EnumerableConvention; |
| 14 | +import org.apache.calcite.adapter.enumerable.EnumerableRules; |
| 15 | +import org.apache.calcite.adapter.enumerable.EnumerableValues; |
| 16 | +import org.apache.calcite.adapter.enumerable.EnumerableValuesRule; |
| 17 | +import org.apache.calcite.plan.Contexts; |
| 18 | +import org.apache.calcite.plan.RelOptCluster; |
| 19 | +import org.apache.calcite.plan.RelOptPlanner; |
| 20 | +import org.apache.calcite.plan.RelOptRule; |
| 21 | +import org.apache.calcite.plan.RelRule; |
| 22 | +import org.apache.calcite.plan.RelTraitSet; |
| 23 | +import org.apache.calcite.rel.RelCollations; |
| 24 | +import org.apache.calcite.rel.RelDistributionTraitDef; |
| 25 | +import org.apache.calcite.rel.RelNode; |
| 26 | +import org.apache.calcite.rel.core.Values; |
| 27 | +import org.apache.calcite.rel.metadata.RelMdDistribution; |
| 28 | +import org.apache.calcite.rel.rules.PruneEmptyRules; |
| 29 | +import org.apache.calcite.rel.type.RelDataType; |
| 30 | +import org.apache.calcite.tools.RelBuilder; |
| 31 | +import org.apache.calcite.tools.RelBuilderFactory; |
| 32 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 33 | + |
| 34 | +/** |
| 35 | + * Volcano rules that create empty {@code Values} without deriving their collation trait. |
| 36 | + * |
| 37 | + * <p>Calcite's {@link PruneEmptyRules} build their result with {@code RelBuilder.empty()} through |
| 38 | + * the default values factory, and {@link EnumerableValuesRule} converts with {@link |
| 39 | + * EnumerableValues#create}. Both derive the collation trait with {@code RelMdCollation.values}, |
| 40 | + * which is O(n^2) in the number of output fields for an empty {@code Values} |
| 41 | + * (opensearch-project/sql#5952). {@link #register} replaces them with equivalents that skip that |
| 42 | + * derivation for empty values. Non-empty values are handled exactly as before. |
| 43 | + */ |
| 44 | +public final class EmptyValuesRules { |
| 45 | + |
| 46 | + private static final RelBuilderFactory EMPTY_VALUES_BUILDER = |
| 47 | + RelBuilder.proto(Contexts.of(EmptyValuesFactory.INSTANCE)); |
| 48 | + |
| 49 | + private static final List<RelOptRule> PRUNE_EMPTY_RULES = |
| 50 | + List.of( |
| 51 | + PruneEmptyRules.UNION_INSTANCE, |
| 52 | + PruneEmptyRules.MINUS_INSTANCE, |
| 53 | + PruneEmptyRules.INTERSECT_INSTANCE, |
| 54 | + PruneEmptyRules.EMPTY_TABLE_INSTANCE, |
| 55 | + PruneEmptyRules.PROJECT_INSTANCE, |
| 56 | + PruneEmptyRules.FILTER_INSTANCE, |
| 57 | + PruneEmptyRules.SORT_INSTANCE, |
| 58 | + PruneEmptyRules.SORT_FETCH_ZERO_INSTANCE, |
| 59 | + PruneEmptyRules.AGGREGATE_INSTANCE, |
| 60 | + PruneEmptyRules.WINDOW_INSTANCE, |
| 61 | + PruneEmptyRules.JOIN_LEFT_INSTANCE, |
| 62 | + PruneEmptyRules.JOIN_RIGHT_INSTANCE, |
| 63 | + PruneEmptyRules.CORRELATE_RIGHT_INSTANCE, |
| 64 | + PruneEmptyRules.CORRELATE_LEFT_INSTANCE); |
| 65 | + |
| 66 | + static final RelOptRule ENUMERABLE_VALUES_RULE = |
| 67 | + EnumerableValuesRule.DEFAULT_CONFIG |
| 68 | + .withRuleFactory(EmptyEnumerableValuesRule::new) |
| 69 | + .toRule(EmptyEnumerableValuesRule.class); |
| 70 | + |
| 71 | + private EmptyValuesRules() {} |
| 72 | + |
| 73 | + /** Replaces Calcite's empty-pruning and values conversion rules that the planner has. */ |
| 74 | + public static void register(RelOptPlanner planner) { |
| 75 | + for (RelOptRule rule : PRUNE_EMPTY_RULES) { |
| 76 | + if (planner.removeRule(rule)) { |
| 77 | + planner.addRule(withEmptyValuesBuilder(rule)); |
| 78 | + } |
| 79 | + } |
| 80 | + if (planner.removeRule(EnumerableRules.ENUMERABLE_VALUES_RULE)) { |
| 81 | + planner.addRule(ENUMERABLE_VALUES_RULE); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static RelOptRule withEmptyValuesBuilder(RelOptRule rule) { |
| 86 | + return ((RelRule<?>) rule).config.withRelBuilderFactory(EMPTY_VALUES_BUILDER).toRule(); |
| 87 | + } |
| 88 | + |
| 89 | + /** Converts an empty {@code Values} to {@link EnumerableValues} without deriving collations. */ |
| 90 | + public static class EmptyEnumerableValuesRule extends EnumerableValuesRule { |
| 91 | + // EnumerableValues has only a private constructor besides create(), which derives collations. |
| 92 | + private static final @Nullable MethodHandle CONSTRUCTOR = findConstructor(); |
| 93 | + |
| 94 | + protected EmptyEnumerableValuesRule(Config config) { |
| 95 | + super(config); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public @Nullable RelNode convert(RelNode rel) { |
| 100 | + Values values = (Values) rel; |
| 101 | + if (!values.getTuples().isEmpty() || CONSTRUCTOR == null) { |
| 102 | + return super.convert(rel); |
| 103 | + } |
| 104 | + // Same traits as EnumerableValues.create, minus the O(n^2) collation derivation. |
| 105 | + RelTraitSet traits = |
| 106 | + values |
| 107 | + .getCluster() |
| 108 | + .traitSetOf(EnumerableConvention.INSTANCE) |
| 109 | + .replace(RelCollations.EMPTY) |
| 110 | + .replaceIf( |
| 111 | + RelDistributionTraitDef.INSTANCE, |
| 112 | + () -> RelMdDistribution.values(values.getRowType(), values.getTuples())); |
| 113 | + try { |
| 114 | + return (RelNode) |
| 115 | + CONSTRUCTOR.invoke( |
| 116 | + values.getCluster(), values.getRowType(), values.getTuples(), traits); |
| 117 | + } catch (Throwable e) { |
| 118 | + return super.convert(rel); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static @Nullable MethodHandle findConstructor() { |
| 123 | + try { |
| 124 | + return MethodHandles.privateLookupIn(EnumerableValues.class, MethodHandles.lookup()) |
| 125 | + .findConstructor( |
| 126 | + EnumerableValues.class, |
| 127 | + MethodType.methodType( |
| 128 | + void.class, |
| 129 | + RelOptCluster.class, |
| 130 | + RelDataType.class, |
| 131 | + ImmutableList.class, |
| 132 | + RelTraitSet.class)); |
| 133 | + } catch (ReflectiveOperationException | RuntimeException e) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments