-
Notifications
You must be signed in to change notification settings - Fork 25.7k
QL: Make canonical form take into account children #71266
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -97,6 +97,14 @@ public static Nullability nullable(List<? extends Expression> exps) { | |
| return Nullability.and(exps.stream().map(Expression::nullable).toArray(Nullability[]::new)); | ||
| } | ||
|
|
||
| public static List<Expression> canonicalize(List<? extends Expression> exps) { | ||
| List<Expression> canonical = new ArrayList<>(exps.size()); | ||
| for (Expression exp : exps) { | ||
| canonical.add(exp.canonical()); | ||
| } | ||
| return canonical; | ||
|
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. Possible to use more Java 8 syntax? |
||
| } | ||
|
|
||
| public static boolean foldable(List<? extends Expression> exps) { | ||
| for (Expression exp : exps) { | ||
| if (exp.foldable() == false) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,7 +68,12 @@ public Object fold() { | |
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value, dataType); | ||
| return Objects.hash(dataType, value); | ||
|
||
| } | ||
|
|
||
| @Override | ||
| protected Expression canonicalize() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,9 @@ | |
| */ | ||
| package org.elasticsearch.xpack.ql.expression.predicate; | ||
|
|
||
| import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction; | ||
| import org.elasticsearch.xpack.ql.expression.Expression; | ||
|
|
||
| public interface Negatable<T extends ScalarFunction> { | ||
| public interface Negatable<T extends Expression> { | ||
|
||
|
|
||
| T negate(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
|
|
||
| import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isBoolean; | ||
|
|
||
| public class Not extends UnaryScalarFunction { | ||
| public class Not extends UnaryScalarFunction implements Negatable<Expression> { | ||
|
|
||
| public Not(Source source, Expression child) { | ||
| super(source, child); | ||
|
|
@@ -60,15 +60,23 @@ public String processScript(String script) { | |
|
|
||
| @Override | ||
| protected Expression canonicalize() { | ||
| Expression canonicalChild = field().canonical(); | ||
| if (canonicalChild instanceof Negatable) { | ||
| return ((Negatable) canonicalChild).negate(); | ||
| if (field() instanceof Negatable) { | ||
| return ((Negatable) field()).negate().canonical(); | ||
|
||
| } | ||
| return this; | ||
| return super.canonicalize(); | ||
| } | ||
|
|
||
| @Override | ||
| public Expression negate() { | ||
| return field(); | ||
| } | ||
|
|
||
| @Override | ||
| public DataType dataType() { | ||
| return DataTypes.BOOLEAN; | ||
| } | ||
|
|
||
| static Expression negate(Expression exp) { | ||
| return exp instanceof Negatable ? ((Negatable) exp).negate() : new Not(exp.source(), exp); | ||
| } | ||
| } | ||
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.
If it's possible, for the future, maybe we should move this handling inside the
replaceChildren()