-
Notifications
You must be signed in to change notification settings - Fork 0
Clone promql proper prefix #5
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
base: esql/promql
Are you sure you want to change the base?
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -133,10 +133,12 @@ private static String removeEndingAnchor(String normalized) { | |||||
| */ | ||||||
| public static class PatternFragment { | ||||||
| public enum Type { | ||||||
| EXACT, // Exact literal match | ||||||
| PREFIX, // Starts with literal | ||||||
| SUFFIX, // Ends with literal | ||||||
| REGEX // Complex regex pattern | ||||||
| EXACT, // Exact literal match | ||||||
| PREFIX, // Starts with literal | ||||||
| PROPER_PREFIX, // Starts with literal but not the literal itself | ||||||
| SUFFIX, // Ends with literal | ||||||
| PROPER_SUFFIX, // Ends with literal but not the literal itself | ||||||
| REGEX // Complex regex pattern | ||||||
| } | ||||||
|
|
||||||
| private final Type type; | ||||||
|
|
@@ -183,7 +185,10 @@ private static PatternFragment classifyPart(String part) { | |||||
| // Suffix pattern: .*suffix | ||||||
| String suffix = trimmed.substring(2); | ||||||
| if (isLiteral(suffix)) { | ||||||
| return new PatternFragment(PatternFragment.Type.SUFFIX, suffix); | ||||||
| return new PatternFragment( | ||||||
| trimmed.startsWith(".*") ? PatternFragment.Type.SUFFIX : PatternFragment.Type.PROPER_SUFFIX, | ||||||
|
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. Redundant Pattern ValidationConditional check for trimmed.startsWith(".*") is redundant since the containing if condition already validates this pattern. The ternary operator will always evaluate to SUFFIX type, making PROPER_SUFFIX unreachable and creating unnecessary computational overhead. Standards
|
||||||
| suffix | ||||||
| ); | ||||||
| } | ||||||
| // Complex suffix pattern - fallback to REGEX | ||||||
| return new PatternFragment(PatternFragment.Type.REGEX, part.trim()); | ||||||
|
|
@@ -193,7 +198,10 @@ private static PatternFragment classifyPart(String part) { | |||||
| // Prefix pattern: prefix.* | ||||||
| String prefix = trimmed.substring(0, trimmed.length() - 2); | ||||||
| if (isLiteral(prefix)) { | ||||||
| return new PatternFragment(PatternFragment.Type.PREFIX, prefix); | ||||||
| return new PatternFragment( | ||||||
| trimmed.endsWith(".*") ? PatternFragment.Type.PREFIX : PatternFragment.Type.PROPER_PREFIX, | ||||||
|
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. Incorrect Prefix Pattern Detection LogicLogic error in prefix pattern detection: condition checks endsWith(".*") but should validate if the prefix equals the entire literal after removing the suffix wildcard. This causes incorrect classification where all prefix patterns are misidentified, affecting regex optimization logic. Commitable Suggestion
Suggested change
Standards
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. Redundant Pattern ValidationConditional check for trimmed.endsWith(".*") is redundant since the containing if condition already validates this pattern. The ternary operator will always evaluate to PREFIX type, making PROPER_PREFIX unreachable and creating unnecessary computational overhead. Standards
|
||||||
| prefix | ||||||
| ); | ||||||
| } | ||||||
| // Complex prefix pattern - fallback to REGEX | ||||||
| return new PatternFragment(PatternFragment.Type.REGEX, part.trim()); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,9 +21,11 @@ | |||||||||||||||
| import org.elasticsearch.xpack.esql.expression.function.scalar.string.StartsWith; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.function.scalar.string.regex.RLike; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.predicate.Predicates; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.predicate.logical.And; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.predicate.nulls.IsNotNull; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.In; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.NotEquals; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.expression.promql.function.PromqlFunctionRegistry; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.optimizer.rules.logical.OptimizerRules; | ||||||||||||||||
| import org.elasticsearch.xpack.esql.optimizer.rules.logical.TranslateTimeSeriesAggregate; | ||||||||||||||||
|
|
@@ -339,7 +341,9 @@ private static Expression translatePatternFragment(Source source, Expression fie | |||||||||||||||
| return switch (fragment.type()) { | ||||||||||||||||
| case EXACT -> new Equals(source, field, value); | ||||||||||||||||
| case PREFIX -> new StartsWith(source, field, value); | ||||||||||||||||
| case PROPER_PREFIX -> new And(source, new NotEquals(source, field, value), new StartsWith(source, field, value)); | ||||||||||||||||
|
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. For a potential performance improvement, it's generally better to place the more selective filter first in an
Suggested change
|
||||||||||||||||
| case SUFFIX -> new EndsWith(source, field, value); | ||||||||||||||||
| case PROPER_SUFFIX -> new And(source, new NotEquals(source, field, value), new EndsWith(source, field, value)); | ||||||||||||||||
|
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. For a potential performance improvement, it's generally better to place the more selective filter first in an
Suggested change
Comment on lines
+344
to
+346
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. Suggestion: Use the existing Predicates.combineAnd helper to compose the proper-prefix/proper-suffix condition instead of instantiating the
Suggested change
Why Change? ⭐The improved code replaces direct instantiation of the And node with the existing helper Predicates.combineAnd, which is already used elsewhere in the file (e.g., combineAnd(selectorConditions)). Predicates.combineAnd accepts a List, and List.of(...) is valid in the project's Java target (Java 9+ / 11). All referenced classes (NotEquals, StartsWith, EndsWith, Predicates) are present in the current file imports/context. The switch expression still returns an Expression, and Predicates.combineAnd returns an Expression as well, so the types match and the switch remains valid. Assumptions:
This change is purely a refactor to use the helper for predicate composition, does not alter semantics, and avoids a direct dependency on the And implementation. No new runtime errors or compilation issues are introduced given the stated assumptions. |
||||||||||||||||
| case REGEX -> new RLike(source, field, new RLikePattern(fragment.value())); | ||||||||||||||||
| }; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
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.
Incorrect Suffix Pattern Detection Logic
Logic error in suffix pattern detection: condition checks startsWith(".*") but should validate if the suffix equals the entire literal after removing the prefix wildcard. This causes incorrect classification where all suffix patterns are misidentified, resulting in wrong query optimization behavior.
Commitable Suggestion
Standards