Skip to content

Commit 7409ce4

Browse files
committed
Address comments Pt3
1 parent 30f4fde commit 7409ce4

File tree

8 files changed

+16
-31
lines changed

8 files changed

+16
-31
lines changed

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDateUtils.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.time.ZonedDateTime;
1313
import java.time.format.DateTimeFormatter;
1414
import java.time.format.DateTimeFormatterBuilder;
15-
import java.time.format.DateTimeParseException;
1615
import java.util.Locale;
1716
import java.util.function.Function;
1817

@@ -43,25 +42,8 @@ final class JdbcDateUtils {
4342
.appendOffsetId()
4443
.toFormatter(Locale.ROOT);
4544

46-
static final DateTimeFormatter ISO_WITH_MINUTES = new DateTimeFormatterBuilder()
47-
.parseCaseInsensitive()
48-
.append(ISO_LOCAL_DATE)
49-
.appendLiteral('T')
50-
.appendValue(HOUR_OF_DAY, 2)
51-
.appendLiteral(':')
52-
.appendValue(MINUTE_OF_HOUR, 2)
53-
.appendOffsetId()
54-
.toFormatter(Locale.ROOT);
55-
5645
static long asMillisSinceEpoch(String date) {
57-
ZonedDateTime zdt;
58-
try {
59-
zdt = ISO_WITH_MILLIS.parse(date, ZonedDateTime::from);
60-
} catch (DateTimeParseException e) {
61-
// Case of a Group By with casting as DATE
62-
zdt = ISO_WITH_MINUTES.parse(date, ZonedDateTime::from);
63-
}
64-
return zdt.toInstant().toEpochMilli();
46+
return ISO_WITH_MILLIS.parse(date, ZonedDateTime::from).toInstant().toEpochMilli();
6547
}
6648

6749
static Date asDate(String date) {

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public Object extract(Bucket bucket) {
9696
return object;
9797
} else if (object instanceof Long) {
9898
object = DateUtils.asDateTime(((Long) object).longValue(), zoneId);
99+
} else if (object instanceof String) { // CAST(<value> AS DATE) is used
100+
object = DateUtils.asDateOnly(object.toString());
99101
} else {
100102
throw new SqlIllegalArgumentException("Invalid date key returned: {}", object);
101103
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import static java.util.Collections.emptyList;
2323
import static java.util.Collections.emptyMap;
2424
import static org.elasticsearch.xpack.sql.type.DataType.BOOLEAN;
25-
import static org.elasticsearch.xpack.sql.type.DataType.DATE;
26-
import static org.elasticsearch.xpack.sql.type.DataType.DATETIME;
2725

2826
public final class Expressions {
2927

@@ -174,11 +172,11 @@ public static TypeResolution typeMustBeString(Expression e, String operationName
174172
}
175173

176174
public static TypeResolution typeMustBeDate(Expression e, String operationName, ParamOrdinal paramOrd) {
177-
return typeMustBe(e, dt -> dt == DATETIME || dt == DATE, operationName, paramOrd, "datetime, date");
175+
return typeMustBe(e, DataType::isDate, operationName, paramOrd, "datetime, date");
178176
}
179177

180178
public static TypeResolution typeMustBeNumericOrDate(Expression e, String operationName, ParamOrdinal paramOrd) {
181-
return typeMustBe(e, dt -> dt.isNumeric() || dt == DATETIME, operationName, paramOrd, "numeric", "datetime", "date");
179+
return typeMustBe(e, dt -> dt.isNumeric() || dt.isDate(), operationName, paramOrd, "numeric", "datetime", "date");
182180
}
183181

184182
public static TypeResolution typeMustBe(Expression e,

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private static Object asDateTime(Object dateTime, boolean lenient) {
357357
return ((JodaCompatibleZonedDateTime) dateTime).getZonedDateTime();
358358
}
359359
if (dateTime instanceof ZonedDateTime) {
360-
return (ZonedDateTime) dateTime;
360+
return dateTime;
361361
}
362362
if (false == lenient) {
363363
if (dateTime instanceof Number) {

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/script/ScriptWeaver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ default ScriptTemplate scriptWithScalar(ScalarFunctionAttribute scalar) {
7979

8080
default ScriptTemplate scriptWithAggregate(AggregateFunctionAttribute aggregate) {
8181
String template = "{}";
82-
if (aggregate.dataType() == DataType.DATETIME) {
82+
if (aggregate.dataType().isDate()) {
8383
template = "{sql}.asDateTime({})";
8484
}
8585
return new ScriptTemplate(processScript(template),
@@ -89,7 +89,7 @@ default ScriptTemplate scriptWithAggregate(AggregateFunctionAttribute aggregate)
8989

9090
default ScriptTemplate scriptWithGrouping(GroupingFunctionAttribute grouping) {
9191
String template = "{}";
92-
if (grouping.dataType() == DataType.DATETIME) {
92+
if (grouping.dataType().isDate()) {
9393
template = "{sql}.asDateTime({})";
9494
}
9595
return new ScriptTemplate(processScript(template),

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import org.elasticsearch.xpack.sql.rule.Rule;
6262
import org.elasticsearch.xpack.sql.rule.RuleExecutor;
6363
import org.elasticsearch.xpack.sql.session.EmptyExecutable;
64-
import org.elasticsearch.xpack.sql.type.DataType;
6564
import org.elasticsearch.xpack.sql.util.Check;
6665
import org.elasticsearch.xpack.sql.util.DateUtils;
6766

@@ -284,7 +283,7 @@ protected PhysicalPlan rule(AggregateExec a) {
284283
if (matchingGroup != null) {
285284
if (exp instanceof Attribute || exp instanceof ScalarFunction || exp instanceof GroupingFunction) {
286285
Processor action = null;
287-
ZoneId zi = DataType.DATETIME == exp.dataType() ? DateUtils.UTC : null;
286+
ZoneId zi = exp.dataType().isDate() ? DateUtils.UTC : null;
288287
/*
289288
* special handling of dates since aggs return the typed Date object which needs
290289
* extraction instead of handling this in the scroller, the folder handles this
@@ -335,7 +334,7 @@ protected PhysicalPlan rule(AggregateExec a) {
335334
// check if the field is a date - if so mark it as such to interpret the long as a date
336335
// UTC is used since that's what the server uses and there's no conversion applied
337336
// (like for date histograms)
338-
ZoneId zi = DataType.DATETIME == child.dataType() ? DateUtils.UTC : null;
337+
ZoneId zi = child.dataType().isDate() ? DateUtils.UTC : null;
339338
queryC = queryC.addColumn(new GroupByRef(matchingGroup.id(), null, zi));
340339
}
341340
// handle histogram
@@ -359,7 +358,7 @@ else if (child instanceof GroupingFunction) {
359358
matchingGroup = groupingContext.groupFor(ne);
360359
Check.notNull(matchingGroup, "Cannot find group [{}]", Expressions.name(ne));
361360

362-
ZoneId zi = DataType.DATETIME == ne.dataType() ? DateUtils.UTC : null;
361+
ZoneId zi = ne.dataType().isDate() ? DateUtils.UTC : null;
363362
queryC = queryC.addColumn(new GroupByRef(matchingGroup.id(), null, zi));
364363
}
365364
}

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final CompositeValuesSourceBuilder<?> asValueSource() {
3939
builder.valueType(ValueType.DOUBLE);
4040
} else if (script.outputType().isString()) {
4141
builder.valueType(ValueType.STRING);
42-
} else if (script.outputType() == DataType.DATETIME) {
42+
} else if (script.outputType().isDate()) {
4343
builder.valueType(ValueType.DATE);
4444
} else if (script.outputType() == DataType.BOOLEAN) {
4545
builder.valueType(ValueType.BOOLEAN);

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public boolean isString() {
215215
public boolean isPrimitive() {
216216
return this != OBJECT && this != NESTED;
217217
}
218+
219+
public boolean isDate() {
220+
return this == DATE || this == DATETIME;
221+
}
218222

219223
public static DataType fromOdbcType(String odbcType) {
220224
return odbcToEs.get(odbcType);

0 commit comments

Comments
 (0)