-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL: Resolve attributes recursively for improved subquery support #69765
Changes from 7 commits
aba2f63
39e4cf1
015acb3
3b755be
f739e39
932c01c
fa49615
1cb642d
50ab4d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
package org.elasticsearch.xpack.ql.expression; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.ql.QlIllegalArgumentException; | ||
import org.elasticsearch.xpack.ql.tree.Source; | ||
import org.elasticsearch.xpack.ql.type.DataTypes; | ||
|
||
|
@@ -17,6 +18,8 @@ | |
import java.util.Set; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.elasticsearch.xpack.ql.TestUtils.fieldAttribute; | ||
import static org.elasticsearch.xpack.ql.TestUtils.of; | ||
import static org.hamcrest.Matchers.arrayContaining; | ||
import static org.hamcrest.Matchers.arrayWithSize; | ||
import static org.hamcrest.Matchers.contains; | ||
|
@@ -61,6 +64,64 @@ public void testAttributeMapWithSameAliasesCanResolveAttributes() { | |
assertTrue(newAttributeMap.get(param2.toAttribute()) == param2.child()); | ||
} | ||
|
||
public void testResolve() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see more tests for dealing with: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✓ Done. |
||
AttributeMap.Builder<Object> builder = AttributeMap.builder(); | ||
Attribute one = a("one"); | ||
Attribute two = fieldAttribute("two", DataTypes.INTEGER); | ||
Attribute three = fieldAttribute("three", DataTypes.INTEGER); | ||
Alias threeAlias = new Alias(Source.EMPTY, "three_alias", three); | ||
Alias threeAliasAlias = new Alias(Source.EMPTY, "three_alias_alias", threeAlias); | ||
builder.put(one, of("one")); | ||
builder.put(two, "two"); | ||
builder.put(three, of("three")); | ||
builder.put(threeAlias.toAttribute(), threeAlias.child()); | ||
builder.put(threeAliasAlias.toAttribute(), threeAliasAlias.child()); | ||
AttributeMap<Object> map = builder.build(); | ||
|
||
assertEquals(of("one"), map.resolve(one)); | ||
assertEquals("two", map.resolve(two)); | ||
assertEquals(of("three"), map.resolve(three)); | ||
assertEquals(of("three"), map.resolve(threeAlias)); | ||
assertEquals(of("three"), map.resolve(threeAliasAlias)); | ||
assertEquals(of("three"), map.resolve(threeAliasAlias, threeAlias)); | ||
Attribute four = a("four"); | ||
assertEquals("not found", map.resolve(four, "not found")); | ||
assertNull(map.resolve(four)); | ||
assertEquals(four, map.resolve(four, four)); | ||
} | ||
|
||
public void testResolveOneHopCycle() { | ||
AttributeMap.Builder<Object> builder = AttributeMap.builder(); | ||
Attribute a = fieldAttribute("a", DataTypes.INTEGER); | ||
Attribute b = fieldAttribute("b", DataTypes.INTEGER); | ||
builder.put(a, a); | ||
builder.put(b, a); | ||
AttributeMap<Object> map = builder.build(); | ||
|
||
assertEquals(a, map.resolve(a, "default")); | ||
assertEquals(a, map.resolve(b, "default")); | ||
assertEquals("default", map.resolve("non-existing-key", "default")); | ||
} | ||
|
||
public void testResolveMultiHopCycle() { | ||
AttributeMap.Builder<Object> builder = AttributeMap.builder(); | ||
Attribute a = fieldAttribute("a", DataTypes.INTEGER); | ||
Attribute b = fieldAttribute("b", DataTypes.INTEGER); | ||
Attribute c = fieldAttribute("c", DataTypes.INTEGER); | ||
Attribute d = fieldAttribute("d", DataTypes.INTEGER); | ||
builder.put(a, b); | ||
builder.put(b, c); | ||
builder.put(c, d); | ||
builder.put(d, a); | ||
AttributeMap<Object> map = builder.build(); | ||
|
||
// note: multi hop cycles should not happen, unless we have a | ||
// bug in the code that populates the AttributeMaps | ||
expectThrows(QlIllegalArgumentException.class, () -> { | ||
assertEquals(a, map.resolve(a, c)); | ||
}); | ||
} | ||
|
||
private Alias createIntParameterAlias(int index, int value) { | ||
Source source = new Source(1, index * 5, "?"); | ||
Literal literal = new Literal(source, value, DataTypes.INTEGER); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,8 +222,8 @@ public LogicalPlan apply(LogicalPlan plan) { | |
AttributeMap.Builder<Expression> builder = AttributeMap.builder(); | ||
// collect aliases | ||
plan.forEachExpressionUp(Alias.class, a -> builder.put(a.toAttribute(), a.child())); | ||
final Map<Attribute, Expression> collectRefs = builder.build(); | ||
java.util.function.Function<ReferenceAttribute, Expression> replaceReference = r -> collectRefs.getOrDefault(r, r); | ||
final AttributeMap<Expression> collectRefs = builder.build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
java.util.function.Function<ReferenceAttribute, Expression> replaceReference = r -> collectRefs.resolve(r, r); | ||
|
||
plan = plan.transformUp(p -> { | ||
// non attribute defining plans get their references removed | ||
|
@@ -300,7 +300,7 @@ static class PruneOrderByNestedFields extends OptimizerRule<Project> { | |
private void findNested(Expression exp, AttributeMap<Function> functions, Consumer<FieldAttribute> onFind) { | ||
exp.forEachUp(e -> { | ||
if (e instanceof ReferenceAttribute) { | ||
Function f = functions.get(e); | ||
Function f = functions.resolve(e); | ||
if (f != null) { | ||
findNested(f, functions, onFind); | ||
} | ||
|
@@ -578,7 +578,7 @@ private List<NamedExpression> combineProjections(List<? extends NamedExpression> | |
// replace any matching attribute with a lower alias (if there's a match) | ||
// but clean-up non-top aliases at the end | ||
for (NamedExpression ne : upper) { | ||
NamedExpression replacedExp = (NamedExpression) ne.transformUp(Attribute.class, a -> aliases.getOrDefault(a, a)); | ||
NamedExpression replacedExp = (NamedExpression) ne.transformUp(Attribute.class, a -> aliases.resolve(a, a)); | ||
replaced.add((NamedExpression) CleanAliases.trimNonTopLevelAliases(replacedExp)); | ||
} | ||
return replaced; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please break the assignment in two pieces.