Skip to content

Commit 362385d

Browse files
committed
SQL: Register missing processors (#35121)
Add registration (and tests) for missing processors in the serialization chain. Fix #35119 (cherry picked from commit f87a534)
1 parent d8af1a0 commit 362385d

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/Processors.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@
2525
import org.elasticsearch.xpack.sql.expression.gen.processor.ConstantProcessor;
2626
import org.elasticsearch.xpack.sql.expression.gen.processor.HitExtractorProcessor;
2727
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
28+
import org.elasticsearch.xpack.sql.expression.predicate.IsNotNullProcessor;
29+
import org.elasticsearch.xpack.sql.expression.predicate.logical.BinaryLogicProcessor;
30+
import org.elasticsearch.xpack.sql.expression.predicate.logical.NotProcessor;
2831
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.BinaryArithmeticProcessor;
2932
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.UnaryArithmeticProcessor;
3033
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.BinaryComparisonProcessor;
34+
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.InProcessor;
35+
import org.elasticsearch.xpack.sql.expression.predicate.regex.RegexProcessor;
3136

3237
import java.util.ArrayList;
3338
import java.util.List;
@@ -49,13 +54,23 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
4954
entries.add(new Entry(Processor.class, CastProcessor.NAME, CastProcessor::new));
5055
entries.add(new Entry(Processor.class, ChainingProcessor.NAME, ChainingProcessor::new));
5156

52-
// comparators
53-
entries.add(new Entry(Processor.class, BinaryComparisonProcessor.NAME, BinaryComparisonProcessor::new));
57+
// logical
58+
entries.add(new Entry(Processor.class, BinaryLogicProcessor.NAME, BinaryLogicProcessor::new));
59+
entries.add(new Entry(Processor.class, NotProcessor.NAME, NotProcessor::new));
60+
// null
61+
entries.add(new Entry(Processor.class, IsNotNullProcessor.NAME, IsNotNullProcessor::new));
5462

5563
// arithmetic
5664
entries.add(new Entry(Processor.class, BinaryArithmeticProcessor.NAME, BinaryArithmeticProcessor::new));
5765
entries.add(new Entry(Processor.class, UnaryArithmeticProcessor.NAME, UnaryArithmeticProcessor::new));
5866
entries.add(new Entry(Processor.class, BinaryMathProcessor.NAME, BinaryMathProcessor::new));
67+
// comparators
68+
entries.add(new Entry(Processor.class, BinaryComparisonProcessor.NAME, BinaryComparisonProcessor::new));
69+
entries.add(new Entry(Processor.class, InProcessor.NAME, InProcessor::new));
70+
// regex
71+
entries.add(new Entry(Processor.class, RegexProcessor.NAME, RegexProcessor::new));
72+
73+
5974
// datetime
6075
entries.add(new Entry(Processor.class, DateTimeProcessor.NAME, DateTimeProcessor::new));
6176
entries.add(new Entry(Processor.class, NamedDateTimeProcessor.NAME, NamedDateTimeProcessor::new));

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/IsNotNullProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.sql.expression.predicate;
77

8+
import org.elasticsearch.common.io.stream.StreamInput;
89
import org.elasticsearch.common.io.stream.StreamOutput;
910
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
1011

@@ -18,6 +19,8 @@ public class IsNotNullProcessor implements Processor {
1819

1920
private IsNotNullProcessor() {}
2021

22+
public IsNotNullProcessor(StreamInput in) throws IOException {}
23+
2124
@Override
2225
public String getWriteableName() {
2326
return NAME;

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/logical/NotProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.sql.expression.predicate.logical;
77

8+
import org.elasticsearch.common.io.stream.StreamInput;
89
import org.elasticsearch.common.io.stream.StreamOutput;
910
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
1011
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
@@ -19,6 +20,8 @@ public class NotProcessor implements Processor {
1920

2021
private NotProcessor() {}
2122

23+
public NotProcessor(StreamInput in) throws IOException {}
24+
2225
@Override
2326
public String getWriteableName() {
2427
return NAME;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.sql.expression;
8+
9+
import org.elasticsearch.common.io.stream.NamedWriteable;
10+
import org.elasticsearch.test.ESTestCase;
11+
import org.elasticsearch.xpack.sql.expression.function.scalar.Processors;
12+
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
13+
import org.elasticsearch.xpack.sql.tree.NodeSubclassTests;
14+
import org.junit.BeforeClass;
15+
16+
import java.lang.reflect.Field;
17+
import java.util.ArrayList;
18+
import java.util.LinkedHashSet;
19+
import java.util.List;
20+
21+
import static java.util.stream.Collectors.toCollection;
22+
23+
24+
public class ProcessorTests extends ESTestCase {
25+
26+
private static List<Class<? extends Processor>> processors;
27+
28+
@BeforeClass
29+
public static void init() throws Exception {
30+
processors = NodeSubclassTests.subclassesOf(Processor.class);
31+
}
32+
33+
34+
public void testProcessorRegistration() throws Exception {
35+
LinkedHashSet<String> registered = Processors.getNamedWriteables().stream()
36+
.map(e -> e.name)
37+
.collect(toCollection(LinkedHashSet::new));
38+
39+
// discover available processors
40+
int missing = processors.size() - registered.size();
41+
42+
43+
if (missing > 0) {
44+
List<String> notRegistered = new ArrayList<>();
45+
for (Class<? extends Processor> proc : processors) {
46+
String procName = proc.getName();
47+
assertTrue(procName + " does NOT implement NamedWriteable", NamedWriteable.class.isAssignableFrom(proc));
48+
Field name = null;
49+
String value = null;
50+
try {
51+
name = proc.getField("NAME");
52+
} catch (Exception ex) {
53+
fail(procName + " does NOT provide a NAME field\n" + ex);
54+
}
55+
try {
56+
value = name.get(proc).toString();
57+
} catch (Exception ex) {
58+
fail(procName + " does NOT provide a static NAME field\n" + ex);
59+
}
60+
if (!registered.contains(value)) {
61+
notRegistered.add(procName);
62+
}
63+
}
64+
65+
fail(missing + " processor(s) not registered : " + notRegistered);
66+
} else {
67+
assertEquals("Detection failed: discovered more registered processors than classes", 0, missing);
68+
}
69+
}
70+
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/tree/NodeSubclassTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.sql.tree;
77

88
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
9+
910
import org.elasticsearch.common.Strings;
1011
import org.elasticsearch.common.io.PathUtils;
1112
import org.elasticsearch.test.ESTestCase;
@@ -585,7 +586,7 @@ public static <T extends Node<?>> T makeNode(Class<? extends T> nodeClass) throw
585586
/**
586587
* Find all subclasses of a particular class.
587588
*/
588-
private static <T> List<Class<? extends T>> subclassesOf(Class<T> clazz) throws IOException {
589+
public static <T> List<Class<? extends T>> subclassesOf(Class<T> clazz) throws IOException {
589590
@SuppressWarnings("unchecked") // The map is built this way
590591
List<Class<? extends T>> lookup = (List<Class<? extends T>>) subclassCache.get(clazz);
591592
if (lookup != null) {

0 commit comments

Comments
 (0)