Skip to content

Commit a019af7

Browse files
committed
SQL: Refactor Literals serialization method (#40058)
Since other classes besides intervals can be serialized as part of the Cursor, the getNamedWritables method should be moved from Intervals to a more generic class Literals. Relates to #39973
1 parent 0b50a67 commit a019af7

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Intervals.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
package org.elasticsearch.xpack.sql.expression.literal;
88

99
import org.elasticsearch.common.Strings;
10-
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
11-
import org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry;
1210
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
1311
import org.elasticsearch.xpack.sql.expression.Foldables;
1412
import org.elasticsearch.xpack.sql.expression.Literal;
@@ -22,7 +20,6 @@
2220
import java.time.Period;
2321
import java.time.temporal.TemporalAmount;
2422
import java.util.ArrayList;
25-
import java.util.Collection;
2623
import java.util.LinkedHashMap;
2724
import java.util.List;
2825
import java.util.Map;
@@ -412,12 +409,4 @@ public static TemporalAmount parseInterval(Source source, String value, DataType
412409
return PARSERS.get(intervalType).parse(source, value);
413410
}
414411

415-
public static Collection<? extends Entry> getNamedWriteables() {
416-
List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
417-
418-
entries.add(new Entry(IntervalDayTime.class, IntervalDayTime.NAME, IntervalDayTime::new));
419-
entries.add(new Entry(IntervalYearMonth.class, IntervalYearMonth.NAME, IntervalYearMonth::new));
420-
421-
return entries;
422-
}
423-
}
412+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.literal;
8+
9+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
10+
11+
import java.util.ArrayList;
12+
import java.util.Collection;
13+
import java.util.List;
14+
15+
/**
16+
* Utility class for common literal-related functions
17+
*/
18+
public final class Literals {
19+
20+
private Literals() {
21+
22+
}
23+
24+
/**
25+
* All custom types that are not serializable by default can be be serialized as a part of Cursor (i.e as constant in ConstantProcessor)
26+
* should implement NamedWriteables interface and register their de-serialization methods here.
27+
*/
28+
public static Collection<? extends NamedWriteableRegistry.Entry> getNamedWriteables() {
29+
List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
30+
31+
entries.add(new NamedWriteableRegistry.Entry(IntervalDayTime.class, IntervalDayTime.NAME, IntervalDayTime::new));
32+
entries.add(new NamedWriteableRegistry.Entry(IntervalYearMonth.class, IntervalYearMonth.NAME, IntervalYearMonth::new));
33+
34+
return entries;
35+
}
36+
}

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.elasticsearch.xpack.sql.execution.search.extractor.BucketExtractors;
2020
import org.elasticsearch.xpack.sql.execution.search.extractor.HitExtractors;
2121
import org.elasticsearch.xpack.sql.expression.function.scalar.Processors;
22-
import org.elasticsearch.xpack.sql.expression.literal.Intervals;
22+
import org.elasticsearch.xpack.sql.expression.literal.Literals;
2323
import org.elasticsearch.xpack.sql.plugin.TextFormatterCursor;
2424

2525
import java.io.ByteArrayOutputStream;
@@ -57,7 +57,7 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
5757
entries.addAll(BucketExtractors.getNamedWriteables());
5858

5959
// and custom types
60-
entries.addAll(Intervals.getNamedWriteables());
60+
entries.addAll(Literals.getNamedWriteables());
6161

6262
return entries;
6363
}
@@ -102,4 +102,4 @@ public static Cursor decodeFromString(String info) {
102102
throw new SqlIllegalArgumentException("Unexpected failure decoding cursor", ex);
103103
}
104104
}
105-
}
105+
}

0 commit comments

Comments
 (0)