|
| 1 | +/* |
| 2 | + * SubscriptValue.java |
| 3 | + * |
| 4 | + * This source file is part of the FoundationDB open source project |
| 5 | + * |
| 6 | + * Copyright 2015-2025 Apple Inc. and the FoundationDB project authors |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.apple.foundationdb.record.query.plan.cascades.values; |
| 22 | + |
| 23 | +import com.apple.foundationdb.record.EvaluationContext; |
| 24 | +import com.apple.foundationdb.record.ObjectPlanHash; |
| 25 | +import com.apple.foundationdb.record.PlanDeserializer; |
| 26 | +import com.apple.foundationdb.record.PlanHashable; |
| 27 | +import com.apple.foundationdb.record.PlanSerializationContext; |
| 28 | +import com.apple.foundationdb.record.planprotos.PSubscriptValue; |
| 29 | +import com.apple.foundationdb.record.planprotos.PValue; |
| 30 | +import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase; |
| 31 | +import com.apple.foundationdb.record.query.plan.cascades.BuiltInFunction; |
| 32 | +import com.apple.foundationdb.record.query.plan.cascades.SemanticException; |
| 33 | +import com.apple.foundationdb.record.query.plan.cascades.typing.Type; |
| 34 | +import com.apple.foundationdb.record.query.plan.cascades.typing.Typed; |
| 35 | +import com.apple.foundationdb.record.query.plan.explain.ExplainTokensWithPrecedence; |
| 36 | +import com.google.auto.service.AutoService; |
| 37 | +import com.google.common.base.Verify; |
| 38 | +import com.google.common.collect.Iterables; |
| 39 | +import com.google.protobuf.Message; |
| 40 | + |
| 41 | +import javax.annotation.Nonnull; |
| 42 | +import javax.annotation.Nullable; |
| 43 | +import java.util.List; |
| 44 | +import java.util.function.Supplier; |
| 45 | + |
| 46 | +public class SubscriptValue extends AbstractValue { |
| 47 | + private static final ObjectPlanHash BASE_HASH = new ObjectPlanHash("Subscript-Value"); |
| 48 | + |
| 49 | + @Nonnull |
| 50 | + private final Value indexValue; |
| 51 | + |
| 52 | + @Nonnull |
| 53 | + private final Value sourceValue; |
| 54 | + |
| 55 | + @Nonnull |
| 56 | + private final Type type; |
| 57 | + |
| 58 | + public SubscriptValue(@Nonnull final Value indexValue, |
| 59 | + @Nonnull final Value sourceValue) { |
| 60 | + this.indexValue = indexValue; |
| 61 | + this.sourceValue = sourceValue; |
| 62 | + this.type = Verify.verifyNotNull(((Type.Array)sourceValue.getResultType()).getElementType()).nullable(); |
| 63 | + } |
| 64 | + |
| 65 | + @Nonnull |
| 66 | + @Override |
| 67 | + protected Iterable<? extends Value> computeChildren() { |
| 68 | + return List.of(indexValue, sourceValue); |
| 69 | + } |
| 70 | + |
| 71 | + @Nonnull |
| 72 | + @Override |
| 73 | + public ExplainTokensWithPrecedence explain(@Nonnull final Iterable<Supplier<ExplainTokensWithPrecedence>> explainSuppliers) { |
| 74 | + final var indexValueExplain = Iterables.get(explainSuppliers, 0).get(); |
| 75 | + final var sourceValueExplain = Iterables.get(explainSuppliers, 1).get(); |
| 76 | + return ExplainTokensWithPrecedence.of(sourceValueExplain.getExplainTokens() |
| 77 | + .addOpeningSquareBracket().addOptionalWhitespace() |
| 78 | + .addNested(indexValueExplain.getExplainTokens()) |
| 79 | + .addClosingAngledBracket()); |
| 80 | + } |
| 81 | + |
| 82 | + @Nullable |
| 83 | + @Override |
| 84 | + public <M extends Message> Object eval(@Nullable final FDBRecordStoreBase<M> store, @Nonnull final EvaluationContext context) { |
| 85 | + final var index = indexValue.eval(store, context); |
| 86 | + if (index == null) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + final var source = sourceValue.eval(store, context); |
| 90 | + if (source == null) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + // index is 1-based as defined in SQL standard (Foundation, Section: 4.10.2): |
| 94 | + // > An array is a collection A in which each element is associated with exactly one ordinal position in A. |
| 95 | + // If n is the cardinality of A, then the ordinal position p of an element is an integer in the range 1 (one) |
| 96 | + // ≤ p ≤ n. |
| 97 | + final var sourceAsList = (List<?>)source; |
| 98 | + final var adjustedIndex = (int)index - 1; |
| 99 | + if (adjustedIndex < 0 || adjustedIndex >= sourceAsList.size()) { |
| 100 | + // this does not raise out-of-bound error. |
| 101 | + return null; |
| 102 | + } |
| 103 | + return sourceAsList.get(adjustedIndex); |
| 104 | + } |
| 105 | + |
| 106 | + @Nonnull |
| 107 | + @Override |
| 108 | + public Type getResultType() { |
| 109 | + return type; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public int hashCodeWithoutChildren() { |
| 114 | + return PlanHashable.objectsPlanHash(PlanHashable.CURRENT_FOR_CONTINUATION, BASE_HASH); |
| 115 | + } |
| 116 | + |
| 117 | + @Nonnull |
| 118 | + @Override |
| 119 | + public PValue toValueProto(@Nonnull final PlanSerializationContext serializationContext) { |
| 120 | + return PValue.newBuilder() |
| 121 | + .setSubscriptValue(toProto(serializationContext)) |
| 122 | + .build(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int planHash(@Nonnull final PlanHashMode hashMode) { |
| 127 | + return PlanHashable.objectsPlanHash(hashMode, BASE_HASH); |
| 128 | + } |
| 129 | + |
| 130 | + @Nonnull |
| 131 | + @Override |
| 132 | + public PSubscriptValue toProto(@Nonnull final PlanSerializationContext serializationContext) { |
| 133 | + return PSubscriptValue.newBuilder() |
| 134 | + .setIndex(indexValue.toValueProto(serializationContext)) |
| 135 | + .setSource(sourceValue.toValueProto(serializationContext)) |
| 136 | + .build(); |
| 137 | + } |
| 138 | + |
| 139 | + @Nonnull |
| 140 | + public static SubscriptValue fromProto(@Nonnull final PlanSerializationContext serializationContext, |
| 141 | + @Nonnull final PSubscriptValue subscriptValueProto) { |
| 142 | + return new SubscriptValue(Value.fromValueProto(serializationContext, subscriptValueProto.getIndex()), |
| 143 | + Value.fromValueProto(serializationContext, subscriptValueProto.getSource())); |
| 144 | + } |
| 145 | + |
| 146 | + @Nonnull |
| 147 | + @Override |
| 148 | + @SuppressWarnings("PMD.CompareObjectsWithEquals") |
| 149 | + public Value withChildren(final Iterable<? extends Value> newChildren) { |
| 150 | + Verify.verify(Iterables.size(newChildren) == 2); |
| 151 | + final var newIndexValue = Iterables.get(newChildren, 0); |
| 152 | + final var newSourceValue = Iterables.get(newChildren, 1); |
| 153 | + if (indexValue == newIndexValue && newSourceValue == sourceValue) { |
| 154 | + return this; |
| 155 | + } |
| 156 | + return new SubscriptValue(newIndexValue, newSourceValue); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Deserializer. |
| 161 | + */ |
| 162 | + @AutoService(PlanDeserializer.class) |
| 163 | + public static class Deserializer implements PlanDeserializer<PSubscriptValue, SubscriptValue> { |
| 164 | + @Nonnull |
| 165 | + @Override |
| 166 | + public Class<PSubscriptValue> getProtoMessageClass() { |
| 167 | + return PSubscriptValue.class; |
| 168 | + } |
| 169 | + |
| 170 | + @Nonnull |
| 171 | + @Override |
| 172 | + public SubscriptValue fromProto(@Nonnull final PlanSerializationContext serializationContext, |
| 173 | + @Nonnull final PSubscriptValue subscriptValueProto) { |
| 174 | + return SubscriptValue.fromProto(serializationContext, subscriptValueProto); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * The {@code subscript} function. |
| 180 | + */ |
| 181 | + @AutoService(BuiltInFunction.class) |
| 182 | + public static class SubscriptValueFn extends BuiltInFunction<Value> { |
| 183 | + public SubscriptValueFn() { |
| 184 | + super("subscript", List.of(Type.primitiveType(Type.TypeCode.INT), Type.any()), Type.any(), SubscriptValue.SubscriptValueFn::encapsulate); |
| 185 | + } |
| 186 | + |
| 187 | + @SuppressWarnings({"PMD.UnusedFormalParameter", "PMD.UnusedPrivateMethod"}) // false positive, method is used |
| 188 | + private static Value encapsulate(@Nonnull BuiltInFunction<Value> ignored, |
| 189 | + @Nonnull final List<? extends Typed> arguments) { |
| 190 | + Verify.verify(arguments.size() == 2); |
| 191 | + var indexValue = (Value)arguments.get(0); |
| 192 | + final var indexMaxType = Type.maximumType(indexValue.getResultType(), Type.primitiveType(Type.TypeCode.INT)); |
| 193 | + SemanticException.check(indexMaxType != null, SemanticException.ErrorCode.INCOMPATIBLE_TYPE); |
| 194 | + indexValue = PromoteValue.inject(indexValue, indexMaxType); |
| 195 | + |
| 196 | + var sourceValue = (Value)arguments.get(1); |
| 197 | + Verify.verify(sourceValue.getResultType().isArray()); |
| 198 | + |
| 199 | + return new SubscriptValue(indexValue, sourceValue); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments