Skip to content

Commit

Permalink
SQL: Refactor Literals serialization method
Browse files Browse the repository at this point in the history
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 elastic#39973
  • Loading branch information
imotov committed Mar 14, 2019
1 parent d803676 commit 07ac938
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -412,12 +409,4 @@ public static TemporalAmount parseInterval(Source source, String value, DataType
return PARSERS.get(intervalType).parse(source, value);
}

public static Collection<? extends Entry> getNamedWriteables() {
List<NamedWriteableRegistry.Entry> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends NamedWriteableRegistry.Entry> getNamedWriteables() {
List<NamedWriteableRegistry.Entry> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,7 +57,7 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
entries.addAll(BucketExtractors.getNamedWriteables());

// and custom types
entries.addAll(Intervals.getNamedWriteables());
entries.addAll(Literals.getNamedWriteables());

return entries;
}
Expand Down Expand Up @@ -102,4 +102,4 @@ public static Cursor decodeFromString(String info) {
throw new SqlIllegalArgumentException("Unexpected failure decoding cursor", ex);
}
}
}
}

0 comments on commit 07ac938

Please sign in to comment.