|
39 | 39 | import com.apple.foundationdb.record.query.plan.cascades.values.JavaCallFunction;
|
40 | 40 | import com.apple.foundationdb.record.query.plan.cascades.values.LiteralValue;
|
41 | 41 | import com.apple.foundationdb.record.query.plan.cascades.values.NotValue;
|
| 42 | +import com.apple.foundationdb.record.query.plan.cascades.values.ObjectValue; |
42 | 43 | import com.apple.foundationdb.record.query.plan.cascades.values.RecordConstructorValue;
|
43 | 44 | import com.apple.foundationdb.record.query.plan.cascades.values.RelOpValue;
|
44 | 45 | import com.apple.foundationdb.record.query.plan.cascades.values.StreamableAggregateValue;
|
@@ -373,7 +374,7 @@ private List<Expression> lookup(@Nonnull Identifier referenceIdentifier,
|
373 | 374 | continue;
|
374 | 375 | }
|
375 | 376 | }
|
376 |
| - final var nestedFieldMaybe = lookupNestedField(referenceIdentifier, attribute, operator, matchQualifiedOnly); |
| 377 | + final var nestedFieldMaybe = resolveIdentifierInType(referenceIdentifier, attribute, operator, matchQualifiedOnly); |
377 | 378 | if (nestedFieldMaybe.isPresent()) {
|
378 | 379 | matchedAttributes.add(nestedFieldMaybe.get());
|
379 | 380 | checkForPseudoColumns = false;
|
@@ -413,10 +414,10 @@ public Optional<Expression> lookupAlias(@Nonnull Identifier requestedAlias,
|
413 | 414 | }
|
414 | 415 |
|
415 | 416 | @Nonnull
|
416 |
| - public Optional<Expression> lookupNestedField(@Nonnull Identifier requestedIdentifier, |
417 |
| - @Nonnull Expression existingExpression, |
418 |
| - @Nonnull LogicalOperator logicalOperator, |
419 |
| - boolean matchQualifiedOnly) { |
| 417 | + public Optional<Expression> resolveIdentifierInType(@Nonnull Identifier requestedIdentifier, |
| 418 | + @Nonnull Expression existingExpression, |
| 419 | + @Nonnull LogicalOperator logicalOperator, |
| 420 | + boolean matchQualifiedOnly) { |
420 | 421 | if (existingExpression.getName().isEmpty() || requestedIdentifier.fullyQualifiedName().size() <= 1) {
|
421 | 422 | return Optional.empty();
|
422 | 423 | }
|
@@ -467,6 +468,64 @@ public Optional<Expression> lookupNestedField(@Nonnull Identifier requestedIdent
|
467 | 468 | return Optional.of(nestedAttribute);
|
468 | 469 | }
|
469 | 470 |
|
| 471 | + @Nonnull |
| 472 | + public Optional<Expression> resolveIdentifierInType(@Nonnull Identifier requestedIdentifier, |
| 473 | + @Nonnull String param, |
| 474 | + @Nonnull DataType existingDataType) { |
| 475 | + /* |
| 476 | + if (existingExpression.getName().isEmpty() || requestedIdentifier.fullyQualifiedName().size() <= 1) { |
| 477 | + return Optional.empty(); |
| 478 | + } |
| 479 | + final var effectiveExistingExpr = matchQualifiedOnly && logicalOperator.getName().isPresent() ? |
| 480 | + existingExpression.withQualifier(Optional.of(logicalOperator.getName().get())) : |
| 481 | + existingExpression.clearQualifier(); |
| 482 | + var effectiveExprName = effectiveExistingExpr.getName().orElseThrow(); |
| 483 | +
|
| 484 | + if (!requestedIdentifier.prefixedWith(effectiveExprName)) { |
| 485 | + if (existingExpression.getName().isPresent() && |
| 486 | + requestedIdentifier.prefixedWith(existingExpression.getName().get())) { |
| 487 | + effectiveExprName = existingExpression.getName().get(); |
| 488 | + } else { |
| 489 | + return Optional.empty(); |
| 490 | + } |
| 491 | + } |
| 492 | + */ |
| 493 | + // requestedIdentifier = x.latitude |
| 494 | + ObjectValue objectValue = ObjectValue.of(CorrelationIdentifier.of("PARAM"), DataTypeUtils.toRecordLayerType(existingDataType)); |
| 495 | + final var remainingPath = requestedIdentifier.fullyQualifiedName(); |
| 496 | + /* |
| 497 | + if (remainingPath.isEmpty()) { |
| 498 | + return Optional.of(existingExpression.withName(requestedIdentifier)); |
| 499 | + } |
| 500 | +
|
| 501 | + */ |
| 502 | + final ImmutableList.Builder<FieldValue.Accessor> accessors = ImmutableList.builder(); |
| 503 | + DataType currentDataType = existingDataType; |
| 504 | + for (String s : remainingPath) { |
| 505 | + if (currentDataType.getCode() != DataType.Code.STRUCT) { |
| 506 | + return Optional.empty(); |
| 507 | + } |
| 508 | + final var fields = ((DataType.StructType) currentDataType).getFields(); |
| 509 | + var found = false; |
| 510 | + for (int j = 0; j < fields.size(); j++) { |
| 511 | + if (fields.get(j).getName().equals(s)) { |
| 512 | + accessors.add(new FieldValue.Accessor(fields.get(j).getName(), j)); |
| 513 | + currentDataType = fields.get(j).getType(); |
| 514 | + found = true; |
| 515 | + break; |
| 516 | + } |
| 517 | + } |
| 518 | + if (!found) { |
| 519 | + return Optional.empty(); |
| 520 | + } |
| 521 | + } |
| 522 | + // probably need to check if currentDataType = targetDataType |
| 523 | + final var fieldPath = FieldValue.resolveFieldPath(DataTypeUtils.toRecordLayerType(existingDataType), accessors.build()); |
| 524 | + final var attributeExpression = FieldValue.ofFieldsAndFuseIfPossible(objectValue, fieldPath); |
| 525 | + final var nestedAttribute = new Expression(Optional.of(requestedIdentifier), existingDataType, attributeExpression); |
| 526 | + return Optional.of(nestedAttribute); |
| 527 | + } |
| 528 | + |
470 | 529 | @Nonnull
|
471 | 530 | public DataType lookupType(@Nonnull Identifier typeIdentifier, boolean isNullable, boolean isRepeated,
|
472 | 531 | @Nonnull Function<String, Optional<DataType>> dataTypeProvider) {
|
|
0 commit comments