From 07ac938010b05bce7eafa012ccd0770af4538b34 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Thu, 14 Mar 2019 11:53:01 -0400 Subject: [PATCH] SQL: Refactor Literals serialization method 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 --- .../sql/expression/literal/Intervals.java | 13 +------ .../sql/expression/literal/Literals.java | 36 +++++++++++++++++++ .../xpack/sql/session/Cursors.java | 6 ++-- 3 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Literals.java diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Intervals.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Intervals.java index d67ec8b2d68be..b06a1fb887433 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Intervals.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Intervals.java @@ -7,8 +7,6 @@ package org.elasticsearch.xpack.sql.expression.literal; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; import org.elasticsearch.xpack.sql.expression.Foldables; import org.elasticsearch.xpack.sql.expression.Literal; @@ -22,7 +20,6 @@ import java.time.Period; import java.time.temporal.TemporalAmount; import java.util.ArrayList; -import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -412,12 +409,4 @@ public static TemporalAmount parseInterval(Source source, String value, DataType return PARSERS.get(intervalType).parse(source, value); } - public static Collection getNamedWriteables() { - List entries = new ArrayList<>(); - - entries.add(new Entry(IntervalDayTime.class, IntervalDayTime.NAME, IntervalDayTime::new)); - entries.add(new Entry(IntervalYearMonth.class, IntervalYearMonth.NAME, IntervalYearMonth::new)); - - return entries; - } -} \ No newline at end of file +} diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Literals.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Literals.java new file mode 100644 index 0000000000000..333ba3f11c0b1 --- /dev/null +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/Literals.java @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.sql.expression.literal; + +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Utility class for common literal-related functions + */ +public final class Literals { + + private Literals() { + + } + + /** + * All custom types that are not serializable by default can be be serialized as a part of Cursor (i.e as constant in ConstantProcessor) + * should implement NamedWriteables interface and register their de-serialization methods here. + */ + public static Collection getNamedWriteables() { + List entries = new ArrayList<>(); + + entries.add(new NamedWriteableRegistry.Entry(IntervalDayTime.class, IntervalDayTime.NAME, IntervalDayTime::new)); + entries.add(new NamedWriteableRegistry.Entry(IntervalYearMonth.class, IntervalYearMonth.NAME, IntervalYearMonth::new)); + + return entries; + } +} diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java index d3495c4f719a7..65f2586f1e1fc 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java @@ -19,7 +19,7 @@ import org.elasticsearch.xpack.sql.execution.search.extractor.BucketExtractors; import org.elasticsearch.xpack.sql.execution.search.extractor.HitExtractors; import org.elasticsearch.xpack.sql.expression.function.scalar.Processors; -import org.elasticsearch.xpack.sql.expression.literal.Intervals; +import org.elasticsearch.xpack.sql.expression.literal.Literals; import org.elasticsearch.xpack.sql.plugin.TextFormatterCursor; import java.io.ByteArrayOutputStream; @@ -57,7 +57,7 @@ public static List getNamedWriteables() { entries.addAll(BucketExtractors.getNamedWriteables()); // and custom types - entries.addAll(Intervals.getNamedWriteables()); + entries.addAll(Literals.getNamedWriteables()); return entries; } @@ -102,4 +102,4 @@ public static Cursor decodeFromString(String info) { throw new SqlIllegalArgumentException("Unexpected failure decoding cursor", ex); } } -} \ No newline at end of file +}