Skip to content

Commit 58a8f19

Browse files
committed
Addressed PR comments
Signed-off-by: Guian Gumpac <[email protected]>
1 parent 2672a79 commit 58a8f19

File tree

7 files changed

+41
-38
lines changed

7 files changed

+41
-38
lines changed

opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ public static OpenSearchDataType of(ExprType type) {
193193
if (res != null) {
194194
return res;
195195
}
196+
if (OpenSearchDateType.isDateTypeCompatible(type)) {
197+
return OpenSearchDateType.of(type);
198+
}
199+
196200
return new OpenSearchDataType((ExprCoreType) type);
197201
}
198202

opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDateType.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import static org.opensearch.common.time.DateFormatter.splitCombinedPatterns;
99
import static org.opensearch.common.time.DateFormatter.strip8Prefix;
1010
import static org.opensearch.sql.data.type.ExprCoreType.DATE;
11+
import static org.opensearch.sql.data.type.ExprCoreType.TIME;
12+
import static org.opensearch.sql.data.type.ExprCoreType.TIMESTAMP;
1113

1214
import java.util.List;
1315
import java.util.stream.Collectors;
@@ -25,8 +27,6 @@ public class OpenSearchDateType extends OpenSearchDataType {
2527

2628
private static final OpenSearchDateType instance = new OpenSearchDateType();
2729

28-
private static final String FORMAT_DELIMITER = "\\|\\|";
29-
3030
public static final List<FormatNames> SUPPORTED_NAMED_DATETIME_FORMATS = List.of(
3131
FormatNames.ISO8601,
3232
FormatNames.EPOCH_MILLIS,
@@ -129,14 +129,12 @@ private OpenSearchDateType() {
129129
}
130130

131131
private OpenSearchDateType(ExprCoreType exprCoreType) {
132-
super(MappingType.Date);
133-
this.formatString = "";
132+
this();
134133
this.exprCoreType = exprCoreType;
135134
}
136135

137136
private OpenSearchDateType(ExprType exprType) {
138-
super(MappingType.Date);
139-
this.formatString = "";
137+
this();
140138
this.exprCoreType = (ExprCoreType) exprType;
141139
}
142140

@@ -158,7 +156,7 @@ private List<String> getFormatList() {
158156

159157

160158
/**
161-
* Retrieves a list of named formatters defined by OpenSearch.
159+
* Retrieves a list of named OpenSearch formatters given by user mapping.
162160
* @return a list of DateFormatters that can be used to parse a Date/Time/Timestamp.
163161
*/
164162
public List<DateFormatter> getAllNamedFormatters() {
@@ -207,26 +205,26 @@ public List<DateFormatter> getTimeNamedFormatters() {
207205

208206
private ExprCoreType getExprTypeFromFormatString(String formatString) {
209207
if (formatString.isEmpty()) {
210-
// TODO: check the default formatter - and set it here instead of assuming that the default
211-
// is always a timestamp
212-
return ExprCoreType.TIMESTAMP;
208+
// FOLLOW-UP: check the default formatter - and set it here instead
209+
// of assuming that the default is always a timestamp
210+
return TIMESTAMP;
213211
}
214212

215213
List<DateFormatter> namedFormatters = getAllNamedFormatters();
216214

217215
if (namedFormatters.isEmpty()) {
218-
return ExprCoreType.TIMESTAMP;
216+
return TIMESTAMP;
219217
}
220218

221219
if (!getAllCustomFormatters().isEmpty()) {
222-
// TODO: support custom format in <issue#>
223-
return ExprCoreType.TIMESTAMP;
220+
// FOLLOW-UP: support custom format in <issue#>
221+
return TIMESTAMP;
224222
}
225223

226224
// if there is nothing in the dateformatter that accepts a year/month/day, then
227225
// we can assume the type is strictly a Time object
228226
if (namedFormatters.size() == getTimeNamedFormatters().size()) {
229-
return ExprCoreType.TIME;
227+
return TIME;
230228
}
231229

232230
// if there is nothing in the dateformatter that accepts a hour/minute/second, then
@@ -235,7 +233,8 @@ private ExprCoreType getExprTypeFromFormatString(String formatString) {
235233
return DATE;
236234
}
237235

238-
return ExprCoreType.TIMESTAMP;
236+
// According to the user mapping, this field may contain a DATE or a TIME
237+
return TIMESTAMP;
239238
}
240239

241240
/**

opensearch/src/main/java/org/opensearch/sql/opensearch/data/value/OpenSearchExprValueFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ public void extendTypeMapping(Map<String, OpenSearchDataType> typeMapping) {
116116
(c, dt) -> ExprBooleanValue.of(c.booleanValue()))
117117
//Handles the creation of DATE, TIME & DATETIME
118118
.put(OpenSearchDateType.of(TIME),
119-
(c, dt) -> createOpenSearchDateType(c, dt))
119+
this::createOpenSearchDateType)
120120
.put(OpenSearchDateType.of(DATE),
121-
(c, dt) -> createOpenSearchDateType(c, dt))
121+
this::createOpenSearchDateType)
122122
.put(OpenSearchDateType.of(TIMESTAMP),
123-
(c, dt) -> createOpenSearchDateType(c, dt))
123+
this::createOpenSearchDateType)
124124
.put(OpenSearchDateType.of(DATETIME),
125-
(c, dt) -> createOpenSearchDateType(c, dt))
125+
this::createOpenSearchDateType)
126126
.put(OpenSearchDataType.of(OpenSearchDataType.MappingType.Ip),
127127
(c, dt) -> new OpenSearchExprIpValue(c.stringValue()))
128128
.put(OpenSearchDataType.of(OpenSearchDataType.MappingType.GeoPoint),
@@ -202,7 +202,7 @@ private Optional<ExprType> type(String field) {
202202
}
203203

204204
/**
205-
* return the first matching formatter as an Instant to UTF.
205+
* Parses value with the first matching formatter as an Instant to UTF.
206206
*
207207
* @param value - timestamp as string
208208
* @param dateType - field type

opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/aggregation/AggregationQueryBuilder.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,10 @@ public Map<String, OpenSearchDataType> buildTypeMapping(
113113
List<NamedExpression> groupByList) {
114114
ImmutableMap.Builder<String, OpenSearchDataType> builder = new ImmutableMap.Builder<>();
115115
namedAggregatorList.forEach(agg -> {
116-
if (OpenSearchDateType.isDateTypeCompatible(agg.type())) {
117-
builder.put(agg.getName(), OpenSearchDateType.of(agg.type()));
118-
} else {
119-
builder.put(agg.getName(), OpenSearchDataType.of(agg.type()));
120-
}
116+
builder.put(agg.getName(), OpenSearchDataType.of(agg.type()));
121117
});
122118
groupByList.forEach(group -> {
123-
if (OpenSearchDateType.isDateTypeCompatible(group.type())) {
124-
builder.put(group.getNameOrAlias(), OpenSearchDateType.of(group.type()));
125-
} else {
126-
builder.put(group.getNameOrAlias(), OpenSearchDataType.of(group.type()));
127-
}
119+
builder.put(group.getNameOrAlias(), OpenSearchDataType.of(group.type()));
128120
});
129121
return builder.build();
130122
}

opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/core/ExpressionScript.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ public Object visitParse(ParseExpression node, Set<ReferenceExpression> context)
114114
private OpenSearchExprValueFactory buildValueFactory(Set<ReferenceExpression> fields) {
115115
Map<String, OpenSearchDataType> typeEnv = fields.stream().collect(toMap(
116116
ReferenceExpression::getAttr, e -> {
117-
if (OpenSearchDateType.isDateTypeCompatible(e.type())) {
118-
return OpenSearchDateType.of(e.type());
119-
}
120117
return OpenSearchDataType.of(e.type());
121118
}));
122119
return new OpenSearchExprValueFactory(typeEnv);

opensearch/src/test/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataTypeTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ public void of_MappingType(MappingType mappingType, String name, ExprType dataTy
137137
public void of_ExprCoreType(ExprCoreType coreType) {
138138
assumeFalse(coreType == UNKNOWN);
139139
var type = OpenSearchDataType.of(coreType);
140-
assertAll(
141-
() -> assertEquals(coreType.toString(), type.typeName()),
142-
() -> assertEquals(coreType.toString(), type.legacyTypeName()),
143-
() -> assertEquals(coreType, type.getExprType())
144-
);
140+
if (type instanceof OpenSearchDateType) {
141+
assertEquals(coreType, type.getExprType());
142+
} else {
143+
assertEquals(coreType.toString(), type.typeName());
144+
assertEquals(coreType.toString(), type.legacyTypeName());
145+
assertEquals(coreType, type.getExprType());
146+
}
145147
}
146148

147149
@ParameterizedTest(name = "{0}")

opensearch/src/test/java/org/opensearch/sql/opensearch/data/type/OpenSearchDateTypeTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import static org.opensearch.sql.opensearch.data.type.OpenSearchDateType.SUPPORTED_NAMED_DATETIME_FORMATS;
2424
import static org.opensearch.sql.opensearch.data.type.OpenSearchDateType.SUPPORTED_NAMED_DATE_FORMATS;
2525
import static org.opensearch.sql.opensearch.data.type.OpenSearchDateType.SUPPORTED_NAMED_TIME_FORMATS;
26+
import static org.opensearch.sql.opensearch.data.type.OpenSearchDateType.isDateTypeCompatible;
2627

2728
import java.util.EnumSet;
2829
import org.junit.jupiter.api.DisplayNameGeneration;
2930
import org.junit.jupiter.api.DisplayNameGenerator;
3031
import org.junit.jupiter.api.Test;
3132
import org.opensearch.common.time.FormatNames;
33+
import org.opensearch.sql.data.type.ExprType;
3234

3335
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
3436
class OpenSearchDateTypeTest {
@@ -192,4 +194,11 @@ public void checkTimeFormatNames() {
192194
}
193195
);
194196
}
197+
198+
@Test
199+
public void checkIfDateTypeCompatible() {
200+
assertTrue(isDateTypeCompatible(DATE));
201+
assertFalse(isDateTypeCompatible(OpenSearchDataType.of(
202+
OpenSearchDataType.MappingType.Text)));
203+
}
195204
}

0 commit comments

Comments
 (0)