Skip to content

Commit f27464f

Browse files
committed
reject CATEGORIZE with INLINEJOIN
1 parent b2ac6b9 commit f27464f

File tree

2 files changed

+49
-0
lines changed
  • x-pack/plugin/esql/src

2 files changed

+49
-0
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/grouping/Categorize.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.elasticsearch.license.XPackLicenseState;
1717
import org.elasticsearch.xpack.esql.LicenseAware;
1818
import org.elasticsearch.xpack.esql.SupportsObservabilityTier;
19+
import org.elasticsearch.xpack.esql.common.Failures;
1920
import org.elasticsearch.xpack.esql.core.InvalidArgumentException;
21+
import org.elasticsearch.xpack.esql.core.expression.Alias;
2022
import org.elasticsearch.xpack.esql.core.expression.Expression;
2123
import org.elasticsearch.xpack.esql.core.expression.MapExpression;
2224
import org.elasticsearch.xpack.esql.core.expression.Nullability;
@@ -33,6 +35,9 @@
3335
import org.elasticsearch.xpack.esql.expression.function.Options;
3436
import org.elasticsearch.xpack.esql.expression.function.Param;
3537
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
38+
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
39+
import org.elasticsearch.xpack.esql.plan.logical.InlineStats;
40+
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
3641
import org.elasticsearch.xpack.ml.MachineLearning;
3742

3843
import java.io.IOException;
@@ -41,11 +46,13 @@
4146
import java.util.Locale;
4247
import java.util.Map;
4348
import java.util.TreeMap;
49+
import java.util.function.BiConsumer;
4450

4551
import static java.util.Map.entry;
4652
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
4753
import static org.elasticsearch.compute.aggregation.blockhash.BlockHash.CategorizeDef.OutputFormat.REGEX;
4854
import static org.elasticsearch.xpack.esql.SupportsObservabilityTier.ObservabilityTier.COMPLETE;
55+
import static org.elasticsearch.xpack.esql.common.Failure.fail;
4956
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT;
5057
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
5158
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
@@ -248,4 +255,26 @@ public String toString() {
248255
public boolean licenseCheck(XPackLicenseState state) {
249256
return MachineLearning.CATEGORIZE_TEXT_AGG_FEATURE.check(state);
250257
}
258+
259+
@Override
260+
public BiConsumer<LogicalPlan, Failures> postAnalysisPlanVerification() {
261+
return (p, failures) -> {
262+
super.postAnalysisPlanVerification().accept(p, failures);
263+
264+
if (p instanceof InlineStats inlineStats && inlineStats.child() instanceof Aggregate aggregate) {
265+
aggregate.groupings().forEach(grp -> {
266+
if (grp instanceof Alias alias && alias.child() instanceof Categorize categorize) {
267+
failures.add(
268+
fail(
269+
categorize,
270+
"CATEGORIZE [{}] is not yet supported with INLINESTATS [{}]",
271+
categorize.sourceText(),
272+
inlineStats.sourceText()
273+
)
274+
);
275+
}
276+
});
277+
}
278+
};
279+
}
251280
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/VerifierTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,26 @@ public void testCategorizeOptionSimilarityThreshold() {
20632063
);
20642064
}
20652065

2066+
public void testCategorizeWithInlineStats() {
2067+
assertEquals(
2068+
"1:37: CATEGORIZE [CATEGORIZE(last_name, { \"similarity_threshold\": 1 })] is not yet supported with "
2069+
+ "INLINESTATS [INLINESTATS COUNT(*) BY CATEGORIZE(last_name, { \"similarity_threshold\": 1 })]",
2070+
error("FROM test | INLINESTATS COUNT(*) BY CATEGORIZE(last_name, { \"similarity_threshold\": 1 })")
2071+
);
2072+
2073+
assertEquals("""
2074+
3:35: CATEGORIZE [CATEGORIZE(gender)] is not yet supported with \
2075+
INLINESTATS [INLINESTATS SUM(salary) BY c3 = CATEGORIZE(gender)]
2076+
line 2:91: CATEGORIZE grouping function [CATEGORIZE(first_name)] can only be in the first grouping expression
2077+
line 2:32: CATEGORIZE [CATEGORIZE(last_name, { "similarity_threshold": 1 })] is not yet supported with \
2078+
INLINESTATS [INLINESTATS COUNT(*) BY c1 = CATEGORIZE(last_name, { "similarity_threshold": 1 }), \
2079+
c2 = CATEGORIZE(first_name)]""", error("""
2080+
FROM test
2081+
| INLINESTATS COUNT(*) BY c1 = CATEGORIZE(last_name, { "similarity_threshold": 1 }), c2 = CATEGORIZE(first_name)
2082+
| INLINESTATS SUM(salary) BY c3 = CATEGORIZE(gender)
2083+
"""));
2084+
}
2085+
20662086
public void testChangePoint() {
20672087
assumeTrue("change_point must be enabled", EsqlCapabilities.Cap.CHANGE_POINT.isEnabled());
20682088
var airports = AnalyzerTestUtils.analyzer(loadMapping("mapping-airports.json", "airports"));

0 commit comments

Comments
 (0)