Skip to content

Commit

Permalink
refactoring, addressing comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
hatyo committed Nov 7, 2024
1 parent ddaae8a commit 03e4c8d
Show file tree
Hide file tree
Showing 62 changed files with 333 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,17 @@ public class Bindings {
/**
* Bindings slots used internally by plan operators.
*/
public enum BindingType {
public enum BindingKind {
IN("__in_"),
RANK("__rank_"),
CORRELATION("__corr_"),
CONSTANT("__const_"),
TABLE_QUEUE("__tq_");
CONSTANT("__const_");

public static final String PREFIX = "__";
private final String value;


BindingType(String value) {
BindingKind(String value) {
this.value = value;
}

Expand Down Expand Up @@ -88,16 +87,14 @@ public PBindingKind toProto(@Nonnull final PlanSerializationContext serializatio
return PBindingKind.CONSTANT;
case CORRELATION:
return PBindingKind.CORRELATION;
case TABLE_QUEUE:
return PBindingKind.TABLE_QUEUE; // TEMP_TABLE
default:
throw new RecordCoreException("unknown binding mapping. did you forget to map it?");
}
}

@Nonnull
@SuppressWarnings("unused")
public static BindingType fromProto(@Nonnull final PlanSerializationContext serializationContext, @Nonnull final PBindingKind bindingKindProto) {
public static BindingKind fromProto(@Nonnull final PlanSerializationContext serializationContext, @Nonnull final PBindingKind bindingKindProto) {
switch (bindingKindProto) {
case IN:
return IN;
Expand All @@ -107,8 +104,6 @@ public static BindingType fromProto(@Nonnull final PlanSerializationContext seri
return CONSTANT;
case CORRELATION:
return CORRELATION;
case TABLE_QUEUE:
return TABLE_QUEUE;
default:
throw new RecordCoreException("unknown binding mapping. did you forget to map it?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Object getBinding(@Nonnull String name) {
*
* @return the value bound to the given parameter
*/
public Object getBinding(@Nonnull Bindings.BindingType type, @Nonnull CorrelationIdentifier alias) {
public Object getBinding(@Nonnull Bindings.BindingKind type, @Nonnull CorrelationIdentifier alias) {
return bindings.get(type.bindingName(alias.getId()));
}

Expand All @@ -146,7 +146,7 @@ public Object getBinding(@Nonnull Bindings.BindingType type, @Nonnull Correlatio
@SuppressWarnings("unchecked")
@Nullable
public Object dereferenceConstant(@Nonnull final CorrelationIdentifier alias, @Nonnull final String constantId) {
final var constantsMap = (Map<String, ?>)bindings.get(Bindings.BindingType.CONSTANT.bindingName(alias.getId()));
final var constantsMap = (Map<String, ?>)bindings.get(Bindings.BindingKind.CONSTANT.bindingName(alias.getId()));
if (constantsMap == null) {
throw new RecordCoreException("could not find constant in the evaluation context")
.addLogInfo(LogMessageKeys.KEY, "'" + alias.getId() + "' - '" + constantId + "'");
Expand Down Expand Up @@ -209,7 +209,7 @@ public EvaluationContext withBinding(@Nonnull String bindingName, @Nullable Obje
*
* @return a new <code>EvaluationContext</code> with the new binding
*/
public EvaluationContext withBinding(final Bindings.BindingType type, @Nonnull CorrelationIdentifier alias, @Nullable Object value) {
public EvaluationContext withBinding(final Bindings.BindingKind type, @Nonnull CorrelationIdentifier alias, @Nullable Object value) {
return childBuilder().setBinding(type.bindingName(alias.getId()), value).build(typeRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public EvaluationContextBuilder setBinding(@Nonnull String name, @Nullable Objec
* @return this <code>EvaluationContextBuilder</code>
*/
public EvaluationContextBuilder setBinding(@Nonnull CorrelationIdentifier alias, @Nullable Object value) {
return setBinding(Bindings.BindingType.CORRELATION.bindingName(alias.getId()), value);
return setBinding(Bindings.BindingKind.CORRELATION.bindingName(alias.getId()), value);
}

public EvaluationContextBuilder setConstant(@Nonnull CorrelationIdentifier alias, @Nullable Object value) {
return setBinding(Bindings.BindingType.CONSTANT.bindingName(alias.getId()), value);
return setBinding(Bindings.BindingKind.CONSTANT.bindingName(alias.getId()), value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ private void finalizeGroup(Object nextGroup) {
}

private void accumulate(@Nullable Object currentObject) {
EvaluationContext nestedContext = context.withBinding(Bindings.BindingType.CORRELATION, alias, currentObject);
EvaluationContext nestedContext = context.withBinding(Bindings.BindingKind.CORRELATION, alias, currentObject);
final Object partial = aggregateValue.evalToPartial(store, nestedContext);
accumulator.accumulate(partial);
}

private Object evalGroupingKey(@Nullable final Object currentObject) {
final EvaluationContext nestedContext = context.withBinding(Bindings.BindingType.CORRELATION, alias, currentObject);
final EvaluationContext nestedContext = context.withBinding(Bindings.BindingKind.CORRELATION, alias, currentObject);
return Objects.requireNonNull(groupingKeyValue).eval(store, nestedContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public <M extends Message> boolean shouldIndexThisRecord(@Nonnull FDBRecordStore
CorrelationIdentifier objectQuantifier = Quantifier.current();
QueryPredicate queryPredicate = getQueryPredicate(store.getRecordMetaData(), savedRecord.getRecordType(), objectQuantifier);

String bindingName = Bindings.BindingType.CORRELATION.bindingName(objectQuantifier.getId());
String bindingName = Bindings.BindingKind.CORRELATION.bindingName(objectQuantifier.getId());
Bindings bindings = Bindings.newBuilder().set(bindingName, QueryResult.ofComputed(savedRecord.getRecord())).build();

return Boolean.TRUE.equals(queryPredicate.eval(store, EvaluationContext.forBindings(bindings)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1150,19 +1150,19 @@ public abstract static class ParameterComparisonBase implements ComparisonWithPa
@Nonnull
protected final String parameter;
@Nullable
protected final Bindings.BindingType bindingType;
protected final Bindings.BindingKind bindingKind;
@Nonnull
protected final ParameterRelationshipGraph parameterRelationshipGraph;
@Nonnull
protected final Supplier<Integer> hashCodeSupplier;

protected ParameterComparisonBase(@Nonnull Type type, @Nonnull String parameter,
@Nullable Bindings.BindingType bindingType,
@Nullable Bindings.BindingKind bindingKind,
@Nonnull ParameterRelationshipGraph parameterRelationshipGraph) {
checkInternalBinding(parameter, bindingType);
checkInternalBinding(parameter, bindingKind);
this.type = type;
this.parameter = parameter;
this.bindingType = bindingType;
this.bindingKind = bindingKind;
if (type.isUnary()) {
throw new RecordCoreException("Unary comparison type " + type + " cannot be bound to a parameter");
}
Expand All @@ -1183,15 +1183,15 @@ public Type getType() {
}

public boolean isCorrelation() {
return bindingType == Bindings.BindingType.CORRELATION;
return bindingKind == Bindings.BindingKind.CORRELATION;
}

@Override
public boolean isCorrelatedTo(@Nonnull final CorrelationIdentifier alias) {
if (!isCorrelation()) {
return false;
}
return Bindings.BindingType.CORRELATION.identifier(getParameter()).equals(alias.getId());
return Bindings.BindingKind.CORRELATION.identifier(getParameter()).equals(alias.getId());
}

@Nullable
Expand All @@ -1212,7 +1212,7 @@ public Object getComparand(@Nullable FDBRecordStoreBase<?> store, @Nullable Eval
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public Comparison translateCorrelations(@Nonnull final TranslationMap translationMap) {
if (isCorrelation()) {
final var alias = CorrelationIdentifier.of(Bindings.BindingType.CORRELATION.identifier(parameter));
final var alias = CorrelationIdentifier.of(Bindings.BindingKind.CORRELATION.identifier(parameter));
final var quantifiedObjectValue = QuantifiedObjectValue.of(alias,
com.apple.foundationdb.record.query.plan.cascades.typing.Type.any());

Expand Down Expand Up @@ -1309,7 +1309,7 @@ public CorrelationIdentifier getAlias() {
if (!isCorrelation()) {
throw new IllegalStateException("caller should check for type of binding before calling this method");
}
return CorrelationIdentifier.of(Bindings.BindingType.CORRELATION.identifier(parameter));
return CorrelationIdentifier.of(Bindings.BindingKind.CORRELATION.identifier(parameter));
}

@Override
Expand Down Expand Up @@ -1359,10 +1359,10 @@ public int queryHash(@Nonnull final QueryHashKind hashKind) {
}

@Nonnull
private static String checkInternalBinding(@Nonnull String parameter, @Nullable Bindings.BindingType bindingType) {
if (bindingType == null && Bindings.BindingType.isInternal(parameter)) {
private static String checkInternalBinding(@Nonnull String parameter, @Nullable Bindings.BindingKind bindingKind) {
if (bindingKind == null && Bindings.BindingKind.isInternal(parameter)) {
throw new RecordCoreException(
"Parameter is internal, parameters cannot start with \"" + Bindings.BindingType.PREFIX + "\"");
"Parameter is internal, parameters cannot start with \"" + Bindings.BindingKind.PREFIX + "\"");
}
return parameter;
}
Expand All @@ -1373,17 +1373,17 @@ private static String checkInternalBinding(@Nonnull String parameter, @Nullable
*/
public static class ParameterComparison extends ParameterComparisonBase {
protected ParameterComparison(@Nonnull Type type, @Nonnull String parameter,
@Nullable Bindings.BindingType bindingType,
@Nullable Bindings.BindingKind bindingKind,
@Nonnull ParameterRelationshipGraph parameterRelationshipGraph) {
super(type, parameter, bindingType, parameterRelationshipGraph);
super(type, parameter, bindingKind, parameterRelationshipGraph);
}

public ParameterComparison(@Nonnull Type type, @Nonnull String parameter) {
this(type, parameter, null, ParameterRelationshipGraph.unbound());
}

public ParameterComparison(@Nonnull Type type, @Nonnull String parameter, @Nullable Bindings.BindingType bindingType) {
this(type, parameter, bindingType, ParameterRelationshipGraph.unbound());
public ParameterComparison(@Nonnull Type type, @Nonnull String parameter, @Nullable Bindings.BindingKind bindingKind) {
this(type, parameter, bindingKind, ParameterRelationshipGraph.unbound());
}

@Nonnull
Expand All @@ -1392,23 +1392,23 @@ public Comparison withType(@Nonnull final Type newType) {
if (type == newType) {
return this;
}
return new ParameterComparison(newType, parameter, bindingType, parameterRelationshipGraph);
return new ParameterComparison(newType, parameter, bindingKind, parameterRelationshipGraph);
}

@Nonnull
@Override
protected ParameterComparisonBase withTranslatedCorrelation(@Nonnull CorrelationIdentifier translatedAlias) {
return new ParameterComparison(type,
Bindings.BindingType.CORRELATION.bindingName(translatedAlias.getId()),
Bindings.BindingType.CORRELATION,
Bindings.BindingKind.CORRELATION.bindingName(translatedAlias.getId()),
Bindings.BindingKind.CORRELATION,
parameterRelationshipGraph);
}

@Nonnull
@Override
public Comparison withParameterRelationshipMap(@Nonnull final ParameterRelationshipGraph parameterRelationshipGraph) {
Verify.verify(this.parameterRelationshipGraph.isUnbound());
return new ParameterComparison(type, parameter, bindingType, parameterRelationshipGraph);
return new ParameterComparison(type, parameter, bindingKind, parameterRelationshipGraph);
}

@Nonnull
Expand All @@ -1418,8 +1418,8 @@ public PParameterComparison toProto(@Nonnull final PlanSerializationContext seri
.setType(type.toProto(serializationContext))
.setParameter(parameter);

if (bindingType != null) {
builder.setInternal(bindingType.toProto(serializationContext));
if (bindingKind != null) {
builder.setInternal(bindingKind.toProto(serializationContext));
}
return builder.build();
}
Expand All @@ -1433,15 +1433,15 @@ public PComparison toComparisonProto(@Nonnull final PlanSerializationContext ser
@Nonnull
public static ParameterComparison fromProto(@Nonnull final PlanSerializationContext serializationContext,
@Nonnull final PParameterComparison parameterComparisonProto) {
final Bindings.BindingType bindingType;
final Bindings.BindingKind bindingKind;
if (parameterComparisonProto.hasInternal()) {
bindingType = Bindings.BindingType.fromProto(serializationContext, Objects.requireNonNull(parameterComparisonProto.getInternal()));
bindingKind = Bindings.BindingKind.fromProto(serializationContext, Objects.requireNonNull(parameterComparisonProto.getInternal()));
} else {
bindingType = null;
bindingKind = null;
}
return new ParameterComparison(Type.fromProto(serializationContext, Objects.requireNonNull(parameterComparisonProto.getType())),
Objects.requireNonNull(parameterComparisonProto.getParameter()),
bindingType);
bindingKind);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ private static final class ConversionParameterComparison extends Comparisons.Par
private final Function<Object, Object> conversion;

protected ConversionParameterComparison(@Nonnull Comparisons.Type type, @Nonnull String parameter,
@Nullable Bindings.BindingType bindingType,
@Nullable Bindings.BindingKind bindingKind,
@Nonnull ParameterRelationshipGraph parameterRelationshipGraph,
@Nonnull QueryableKeyExpression keyExpression) {
super(type, parameter, bindingType, parameterRelationshipGraph);
super(type, parameter, bindingKind, parameterRelationshipGraph);
this.keyExpression = keyExpression;
this.conversion = Objects.requireNonNull(keyExpression.getComparandConversionFunction());
}
Expand Down Expand Up @@ -446,7 +446,7 @@ public Comparisons.Comparison withType(@Nonnull final Comparisons.Type newType)
if (type == newType) {
return this;
}
return new ConversionParameterComparison(newType, parameter, bindingType, parameterRelationshipGraph, keyExpression);
return new ConversionParameterComparison(newType, parameter, bindingKind, parameterRelationshipGraph, keyExpression);
}

@Override
Expand Down Expand Up @@ -490,8 +490,8 @@ public int queryHash(@Nonnull final QueryHashKind hashKind) {
@Override
protected Comparisons.ParameterComparisonBase withTranslatedCorrelation(@Nonnull CorrelationIdentifier translatedAlias) {
return new ConversionParameterComparison(type,
Bindings.BindingType.CORRELATION.bindingName(translatedAlias.getId()),
Bindings.BindingType.CORRELATION,
Bindings.BindingKind.CORRELATION.bindingName(translatedAlias.getId()),
Bindings.BindingKind.CORRELATION,
parameterRelationshipGraph,
keyExpression);
}
Expand All @@ -500,7 +500,7 @@ protected Comparisons.ParameterComparisonBase withTranslatedCorrelation(@Nonnull
@Override
public Comparisons.Comparison withParameterRelationshipMap(@Nonnull final ParameterRelationshipGraph parameterRelationshipGraph) {
Verify.verify(this.parameterRelationshipGraph.isUnbound());
return new ConversionParameterComparison(type, parameter, bindingType, parameterRelationshipGraph, keyExpression);
return new ConversionParameterComparison(type, parameter, bindingKind, parameterRelationshipGraph, keyExpression);
}

@Nonnull
Expand All @@ -510,8 +510,8 @@ public PConversionParameterComparison toProto(@Nonnull final PlanSerializationCo
.setType(type.toProto(serializationContext))
.setParameter(parameter)
.setConversion(keyExpression.toKeyExpression());
if (bindingType != null) {
builder.setInternal(bindingType.toProto(serializationContext));
if (bindingKind != null) {
builder.setInternal(bindingKind.toProto(serializationContext));
}
return builder.build();
}
Expand All @@ -525,17 +525,17 @@ public PComparison toComparisonProto(@Nonnull final PlanSerializationContext ser
@Nonnull
public static ConversionParameterComparison fromProto(@Nonnull final PlanSerializationContext serializationContext,
@Nonnull final PConversionParameterComparison conversionParameterComparisonProto) {
final Bindings.BindingType bindingType;
final Bindings.BindingKind bindingKind;
if (conversionParameterComparisonProto.hasInternal()) {
bindingType = Bindings.BindingType.fromProto(serializationContext, Objects.requireNonNull(conversionParameterComparisonProto.getInternal()));
bindingKind = Bindings.BindingKind.fromProto(serializationContext, Objects.requireNonNull(conversionParameterComparisonProto.getInternal()));
} else {
bindingType = null;
bindingKind = null;
}
final QueryableKeyExpression keyExpression = (QueryableKeyExpression)
KeyExpression.fromProto(conversionParameterComparisonProto.getConversion());
return new ConversionParameterComparison(Comparisons.Type.fromProto(serializationContext, Objects.requireNonNull(conversionParameterComparisonProto.getType())),
Objects.requireNonNull(conversionParameterComparisonProto.getParameter()),
bindingType, ParameterRelationshipGraph.unbound(),
bindingKind, ParameterRelationshipGraph.unbound(),
keyExpression);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ private String getIndexEntryBindingName() {

@Nonnull
private String computeIndexEntryBindingName() {
return Bindings.BindingType.CORRELATION.bindingName(indexEntryAlias.getId());
return Bindings.BindingKind.CORRELATION.bindingName(indexEntryAlias.getId());
}

@Override
Expand Down
Loading

0 comments on commit 03e4c8d

Please sign in to comment.