|
1 |
| -/* |
2 |
| - * Copyright OpenSearch Contributors |
3 |
| - * SPDX-License-Identifier: Apache-2.0 |
4 |
| - */ |
5 |
| - |
6 |
| -package org.opensearch.sql.common.grok; |
7 |
| - |
8 |
| -import java.time.Instant; |
9 |
| -import java.time.LocalDate; |
10 |
| -import java.time.LocalDateTime; |
11 |
| -import java.time.OffsetDateTime; |
12 |
| -import java.time.ZoneId; |
13 |
| -import java.time.ZoneOffset; |
14 |
| -import java.time.ZonedDateTime; |
15 |
| -import java.time.format.DateTimeFormatter; |
16 |
| -import java.time.temporal.TemporalAccessor; |
17 |
| -import java.util.AbstractMap; |
18 |
| -import java.util.Arrays; |
19 |
| -import java.util.Collection; |
20 |
| -import java.util.List; |
21 |
| -import java.util.Map; |
22 |
| -import java.util.function.Function; |
23 |
| -import java.util.regex.Pattern; |
24 |
| -import java.util.stream.Collectors; |
25 |
| - |
26 |
| -/** |
27 |
| - * Convert String argument to the right type. |
28 |
| - */ |
29 |
| -public class Converter { |
30 |
| - |
31 |
| - public enum Type { |
32 |
| - BYTE(Byte::valueOf), |
33 |
| - BOOLEAN(Boolean::valueOf), |
34 |
| - SHORT(Short::valueOf), |
35 |
| - INT(Integer::valueOf, "integer"), |
36 |
| - LONG(Long::valueOf), |
37 |
| - FLOAT(Float::valueOf), |
38 |
| - DOUBLE(Double::valueOf), |
39 |
| - DATETIME(new DateConverter(), "date"), |
40 |
| - STRING(v -> v, "text"); |
41 |
| - |
42 |
| - public final IConverter<? extends Object> converter; |
43 |
| - public final List<String> aliases; |
44 |
| - |
45 |
| - Type(IConverter<? extends Object> converter, String... aliases) { |
46 |
| - this.converter = converter; |
47 |
| - this.aliases = Arrays.asList(aliases); |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - private static final Pattern SPLITTER = Pattern.compile("[:;]"); |
52 |
| - |
53 |
| - private static final Map<String, Type> TYPES = |
54 |
| - Arrays.stream(Type.values()) |
55 |
| - .collect(Collectors.toMap(t -> t.name().toLowerCase(), t -> t)); |
56 |
| - |
57 |
| - private static final Map<String, Type> TYPE_ALIASES = |
58 |
| - Arrays.stream(Type.values()) |
59 |
| - .flatMap(type -> type.aliases.stream() |
60 |
| - .map(alias -> new AbstractMap.SimpleEntry<>(alias, type))) |
61 |
| - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
62 |
| - |
63 |
| - private static Type getType(String key) { |
64 |
| - key = key.toLowerCase(); |
65 |
| - Type type = TYPES.getOrDefault(key, TYPE_ALIASES.get(key)); |
66 |
| - if (type == null) { |
67 |
| - throw new IllegalArgumentException("Invalid data type :" + key); |
68 |
| - } |
69 |
| - return type; |
70 |
| - } |
71 |
| - |
72 |
| - /** |
73 |
| - * getConverters. |
74 |
| - */ |
75 |
| - public static Map<String, IConverter<? extends Object>> |
76 |
| - getConverters(Collection<String> groupNames, Object... params) { |
77 |
| - return groupNames.stream() |
78 |
| - .filter(Converter::containsDelimiter) |
79 |
| - .collect(Collectors.toMap(Function.identity(), key -> { |
80 |
| - String[] list = splitGrokPattern(key); |
81 |
| - IConverter<? extends Object> converter = getType(list[1]).converter; |
82 |
| - if (list.length == 3) { |
83 |
| - converter = converter.newConverter(list[2], params); |
84 |
| - } |
85 |
| - return converter; |
86 |
| - })); |
87 |
| - } |
88 |
| - |
89 |
| - /** |
90 |
| - * getGroupTypes. |
91 |
| - */ |
92 |
| - public static Map<String, Type> getGroupTypes(Collection<String> groupNames) { |
93 |
| - return groupNames.stream() |
94 |
| - .filter(Converter::containsDelimiter) |
95 |
| - .map(Converter::splitGrokPattern) |
96 |
| - .collect(Collectors.toMap( |
97 |
| - l -> l[0], |
98 |
| - l -> getType(l[1]) |
99 |
| - )); |
100 |
| - } |
101 |
| - |
102 |
| - public static String extractKey(String key) { |
103 |
| - return splitGrokPattern(key)[0]; |
104 |
| - } |
105 |
| - |
106 |
| - private static boolean containsDelimiter(String string) { |
107 |
| - return string.indexOf(':') >= 0 || string.indexOf(';') >= 0; |
108 |
| - } |
109 |
| - |
110 |
| - private static String[] splitGrokPattern(String string) { |
111 |
| - return SPLITTER.split(string, 3); |
112 |
| - } |
113 |
| - |
114 |
| - interface IConverter<T> { |
115 |
| - |
116 |
| - T convert(String value); |
117 |
| - |
118 |
| - default IConverter<T> newConverter(String param, Object... params) { |
119 |
| - return this; |
120 |
| - } |
121 |
| - } |
122 |
| - |
123 |
| - |
124 |
| - static class DateConverter implements IConverter<Instant> { |
125 |
| - |
126 |
| - private final DateTimeFormatter formatter; |
127 |
| - private final ZoneId timeZone; |
128 |
| - |
129 |
| - public DateConverter() { |
130 |
| - this.formatter = DateTimeFormatter.ISO_DATE_TIME; |
131 |
| - this.timeZone = ZoneOffset.UTC; |
132 |
| - } |
133 |
| - |
134 |
| - private DateConverter(DateTimeFormatter formatter, ZoneId timeZone) { |
135 |
| - this.formatter = formatter; |
136 |
| - this.timeZone = timeZone; |
137 |
| - } |
138 |
| - |
139 |
| - @Override |
140 |
| - public Instant convert(String value) { |
141 |
| - TemporalAccessor dt = formatter |
142 |
| - .parseBest(value.trim(), ZonedDateTime::from, LocalDateTime::from, OffsetDateTime::from, |
143 |
| - Instant::from, |
144 |
| - LocalDate::from); |
145 |
| - if (dt instanceof ZonedDateTime) { |
146 |
| - return ((ZonedDateTime) dt).toInstant(); |
147 |
| - } else if (dt instanceof LocalDateTime) { |
148 |
| - return ((LocalDateTime) dt).atZone(timeZone).toInstant(); |
149 |
| - } else if (dt instanceof OffsetDateTime) { |
150 |
| - return ((OffsetDateTime) dt).atZoneSameInstant(timeZone).toInstant(); |
151 |
| - } else if (dt instanceof Instant) { |
152 |
| - return ((Instant) dt); |
153 |
| - } else if (dt instanceof LocalDate) { |
154 |
| - return ((LocalDate) dt).atStartOfDay(timeZone).toInstant(); |
155 |
| - } else { |
156 |
| - return null; |
157 |
| - } |
158 |
| - } |
159 |
| - |
160 |
| - @Override |
161 |
| - public DateConverter newConverter(String param, Object... params) { |
162 |
| - if (!(params.length == 1 && params[0] instanceof ZoneId)) { |
163 |
| - throw new IllegalArgumentException("Invalid parameters"); |
164 |
| - } |
165 |
| - return new DateConverter(DateTimeFormatter.ofPattern(param), (ZoneId) params[0]); |
166 |
| - } |
167 |
| - } |
168 |
| -} |
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.common.grok; |
| 7 | + |
| 8 | +import java.time.Instant; |
| 9 | +import java.time.LocalDate; |
| 10 | +import java.time.LocalDateTime; |
| 11 | +import java.time.OffsetDateTime; |
| 12 | +import java.time.ZoneId; |
| 13 | +import java.time.ZoneOffset; |
| 14 | +import java.time.ZonedDateTime; |
| 15 | +import java.time.format.DateTimeFormatter; |
| 16 | +import java.time.temporal.TemporalAccessor; |
| 17 | +import java.util.AbstractMap; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Function; |
| 23 | +import java.util.regex.Pattern; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +/** |
| 27 | + * Convert String argument to the right type. |
| 28 | + */ |
| 29 | +public class Converter { |
| 30 | + |
| 31 | + public enum Type { |
| 32 | + BYTE(Byte::valueOf), |
| 33 | + BOOLEAN(Boolean::valueOf), |
| 34 | + SHORT(Short::valueOf), |
| 35 | + INT(Integer::valueOf, "integer"), |
| 36 | + LONG(Long::valueOf), |
| 37 | + FLOAT(Float::valueOf), |
| 38 | + DOUBLE(Double::valueOf), |
| 39 | + DATETIME(new DateConverter(), "date"), |
| 40 | + STRING(v -> v, "text"); |
| 41 | + |
| 42 | + public final IConverter<? extends Object> converter; |
| 43 | + public final List<String> aliases; |
| 44 | + |
| 45 | + Type(IConverter<? extends Object> converter, String... aliases) { |
| 46 | + this.converter = converter; |
| 47 | + this.aliases = Arrays.asList(aliases); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static final Pattern SPLITTER = Pattern.compile("[:;]"); |
| 52 | + |
| 53 | + private static final Map<String, Type> TYPES = |
| 54 | + Arrays.stream(Type.values()) |
| 55 | + .collect(Collectors.toMap(t -> t.name().toLowerCase(), t -> t)); |
| 56 | + |
| 57 | + private static final Map<String, Type> TYPE_ALIASES = |
| 58 | + Arrays.stream(Type.values()) |
| 59 | + .flatMap(type -> type.aliases.stream() |
| 60 | + .map(alias -> new AbstractMap.SimpleEntry<>(alias, type))) |
| 61 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 62 | + |
| 63 | + private static Type getType(String key) { |
| 64 | + key = key.toLowerCase(); |
| 65 | + Type type = TYPES.getOrDefault(key, TYPE_ALIASES.get(key)); |
| 66 | + if (type == null) { |
| 67 | + throw new IllegalArgumentException("Invalid data type :" + key); |
| 68 | + } |
| 69 | + return type; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * getConverters. |
| 74 | + */ |
| 75 | + public static Map<String, IConverter<? extends Object>> |
| 76 | + getConverters(Collection<String> groupNames, Object... params) { |
| 77 | + return groupNames.stream() |
| 78 | + .filter(Converter::containsDelimiter) |
| 79 | + .collect(Collectors.toMap(Function.identity(), key -> { |
| 80 | + String[] list = splitGrokPattern(key); |
| 81 | + IConverter<? extends Object> converter = getType(list[1]).converter; |
| 82 | + if (list.length == 3) { |
| 83 | + converter = converter.newConverter(list[2], params); |
| 84 | + } |
| 85 | + return converter; |
| 86 | + })); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * getGroupTypes. |
| 91 | + */ |
| 92 | + public static Map<String, Type> getGroupTypes(Collection<String> groupNames) { |
| 93 | + return groupNames.stream() |
| 94 | + .filter(Converter::containsDelimiter) |
| 95 | + .map(Converter::splitGrokPattern) |
| 96 | + .collect(Collectors.toMap( |
| 97 | + l -> l[0], |
| 98 | + l -> getType(l[1]) |
| 99 | + )); |
| 100 | + } |
| 101 | + |
| 102 | + public static String extractKey(String key) { |
| 103 | + return splitGrokPattern(key)[0]; |
| 104 | + } |
| 105 | + |
| 106 | + private static boolean containsDelimiter(String string) { |
| 107 | + return string.indexOf(':') >= 0 || string.indexOf(';') >= 0; |
| 108 | + } |
| 109 | + |
| 110 | + private static String[] splitGrokPattern(String string) { |
| 111 | + return SPLITTER.split(string, 3); |
| 112 | + } |
| 113 | + |
| 114 | + interface IConverter<T> { |
| 115 | + |
| 116 | + T convert(String value); |
| 117 | + |
| 118 | + default IConverter<T> newConverter(String param, Object... params) { |
| 119 | + return this; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + static class DateConverter implements IConverter<Instant> { |
| 125 | + |
| 126 | + private final DateTimeFormatter formatter; |
| 127 | + private final ZoneId timeZone; |
| 128 | + |
| 129 | + public DateConverter() { |
| 130 | + this.formatter = DateTimeFormatter.ISO_DATE_TIME; |
| 131 | + this.timeZone = ZoneOffset.UTC; |
| 132 | + } |
| 133 | + |
| 134 | + private DateConverter(DateTimeFormatter formatter, ZoneId timeZone) { |
| 135 | + this.formatter = formatter; |
| 136 | + this.timeZone = timeZone; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Instant convert(String value) { |
| 141 | + TemporalAccessor dt = formatter |
| 142 | + .parseBest(value.trim(), ZonedDateTime::from, LocalDateTime::from, OffsetDateTime::from, |
| 143 | + Instant::from, |
| 144 | + LocalDate::from); |
| 145 | + if (dt instanceof ZonedDateTime) { |
| 146 | + return ((ZonedDateTime) dt).toInstant(); |
| 147 | + } else if (dt instanceof LocalDateTime) { |
| 148 | + return ((LocalDateTime) dt).atZone(timeZone).toInstant(); |
| 149 | + } else if (dt instanceof OffsetDateTime) { |
| 150 | + return ((OffsetDateTime) dt).atZoneSameInstant(timeZone).toInstant(); |
| 151 | + } else if (dt instanceof Instant) { |
| 152 | + return ((Instant) dt); |
| 153 | + } else if (dt instanceof LocalDate) { |
| 154 | + return ((LocalDate) dt).atStartOfDay(timeZone).toInstant(); |
| 155 | + } else { |
| 156 | + return null; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public DateConverter newConverter(String param, Object... params) { |
| 162 | + if (!(params.length == 1 && params[0] instanceof ZoneId)) { |
| 163 | + throw new IllegalArgumentException("Invalid parameters"); |
| 164 | + } |
| 165 | + return new DateConverter(DateTimeFormatter.ofPattern(param), (ZoneId) params[0]); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments