Skip to content

Commit

Permalink
Method analyzeLookupCopyFields simplified.
Browse files Browse the repository at this point in the history
Signed-off-by: Lukasz Soszynski <[email protected]>
  • Loading branch information
lukasz-soszynski-eliatra committed Jul 9, 2024
1 parent c0fcf57 commit 6f5a678
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions core/src/main/java/org/opensearch/sql/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -544,7 +545,7 @@ public LogicalPlan visitLookup(Lookup node, AnalysisContext queryContext) {
node.getIndexName(),
matchFieldMap,
appendOnly,
analyzeLookupCopyFields(node.getCopyFieldList(), queryContext, table));
analyzeLookupCopyFields(node.getCopyFieldList(), queryContext, table, appendOnly));
}

private ImmutableMap<ReferenceExpression, ReferenceExpression> analyzeLookupMatchFields(
Expand Down Expand Up @@ -573,50 +574,52 @@ private ImmutableMap<ReferenceExpression, ReferenceExpression> analyzeLookupMatc
}

private ImmutableMap<ReferenceExpression, ReferenceExpression> analyzeLookupCopyFields(
List<Map> inputMap, AnalysisContext context, Table table) {

TypeEnvironment curEnv = context.peek();
java.util.Map<String, ExprType> fieldTypes = table.getFieldTypes();
List<Map> inputMap, AnalysisContext context, Table table, Boolean appendOnly) {

if (inputMap.isEmpty()) {
fieldTypes.forEach((k, v) -> curEnv.define(new Symbol(Namespace.FIELD_NAME, k), v));
return ImmutableMap.<ReferenceExpression, ReferenceExpression>builder().build();
}

TypeEnvironment curEnv = context.peek();
Set<String> queryTableFieldNames = curEnv.lookupAllFields(Namespace.FIELD_NAME).keySet();
java.util.Map<String, ExprType> fieldTypes = table.getFieldTypes();

ImmutableMap.Builder<ReferenceExpression, ReferenceExpression> copyMapBuilder =
new ImmutableMap.Builder<>();
for (Map resultMap : inputMap) {
if (resultMap.getOrigin() instanceof Field && resultMap.getTarget() instanceof Field) {
String fieldName = ((Field) resultMap.getOrigin()).getField().toString();
ExprType ex = fieldTypes.get(fieldName);

if (ex == null) {
throw new SemanticCheckException(String.format("no such field %s", fieldName));
}

ReferenceExpression origin = new ReferenceExpression(fieldName, ex);

if (resultMap.getTarget().equals(resultMap.getOrigin())) {

curEnv.define(origin);
copyMapBuilder.put(origin, origin);
} else {
ReferenceExpression target =
new ReferenceExpression(((Field) resultMap.getTarget()).getField().toString(), ex);
curEnv.define(target);
copyMapBuilder.put(origin, target);
}
} else {
if (!(resultMap.getOrigin() instanceof Field && resultMap.getTarget() instanceof Field)) {
throw new SemanticCheckException(
String.format(
"the origin and target expected to be field, but is %s/%s",
resultMap.getOrigin(), resultMap.getTarget()));
}
}

String originFieldNameInLookupTable = ((Field) resultMap.getOrigin()).getField().toString();
String targetFieldNameInQueryTable = ((Field) resultMap.getTarget()).getField().toString();
ExprType ex = fieldTypes.get(originFieldNameInLookupTable);

if (ex == null) {
throw new SemanticCheckException(
String.format("no such field %s", originFieldNameInLookupTable));
}

ReferenceExpression origin = new ReferenceExpression(originFieldNameInLookupTable, ex);
ReferenceExpression target =
new ReferenceExpression(((Field) resultMap.getTarget()).getField().toString(), ex);

if (shouldAppendField(appendOnly, targetFieldNameInQueryTable, queryTableFieldNames)) {
curEnv.define(target.equals(origin) ? origin : target);
}
copyMapBuilder.put(origin, target);
}
return copyMapBuilder.build();
}

private static boolean shouldAppendField(
Boolean appendOnly, String k, Set<String> queryTableFieldNames) {
return !appendOnly || !queryTableFieldNames.contains(k);
}

/** Logical head is identical to {@link LogicalLimit}. */
public LogicalPlan visitHead(Head node, AnalysisContext context) {
LogicalPlan child = node.getChild().get(0).accept(this, context);
Expand Down

0 comments on commit 6f5a678

Please sign in to comment.