Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Fix ZonedDateTime with nanos serialisation (#68253) #68254

Merged
merged 2 commits into from Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,36 +10,69 @@
import org.elasticsearch.common.io.stream.StreamOutput;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Objects;

public class ConstantProcessor implements Processor {

public static String NAME = "c";

private final Object constant;
private final boolean namedWriteable;
private Object constant;
private final Type type;

enum Type {
NAMED_WRITABLE,
ZONEDDATETIME,
GENERIC
}

public ConstantProcessor(Object value) {
this.constant = value;
this.namedWriteable = value instanceof NamedWriteable;
if (value instanceof NamedWriteable) {
type = Type.NAMED_WRITABLE;
} else if (value instanceof ZonedDateTime) {
type = Type.ZONEDDATETIME;
} else {
type = Type.GENERIC;
}
}

public ConstantProcessor(StreamInput in) throws IOException {
namedWriteable = in.readBoolean();
if (namedWriteable) {
constant = in.readNamedWriteable(ConstantNamedWriteable.class);
} else {
constant = in.readGenericValue();
type = in.readEnum(Type.class);
switch (type) {
case NAMED_WRITABLE:
constant = in.readNamedWriteable(ConstantNamedWriteable.class);
break;
case ZONEDDATETIME:
ZonedDateTime zdt;
ZoneId zoneId = in.readZoneId();
zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(in.readLong()), zoneId);
constant = zdt.withNano(in.readInt());
break;
case GENERIC:
constant = in.readGenericValue();
break;
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(namedWriteable);
if (namedWriteable) {
out.writeNamedWriteable((NamedWriteable) constant);
} else {
out.writeGenericValue(constant);
out.writeEnum(type);
switch (type) {
case NAMED_WRITABLE:
out.writeNamedWriteable((NamedWriteable) constant);
break;
case ZONEDDATETIME:
ZonedDateTime zdt = (ZonedDateTime) constant;
out.writeZoneId(zdt.getZone());
out.writeLong(zdt.toInstant().toEpochMilli());
out.writeInt(zdt.getNano());
break;
case GENERIC:
out.writeGenericValue(constant);
break;
}
}

Expand Down Expand Up @@ -76,4 +109,4 @@ public boolean equals(Object obj) {
public String toString() {
return "^" + constant;
}
}
}
Expand Up @@ -9,10 +9,24 @@
import org.elasticsearch.test.AbstractWireSerializingTestCase;

import java.io.IOException;
import java.time.Clock;
import java.time.Duration;
import java.time.ZonedDateTime;

public class ConstantProcessorTests extends AbstractWireSerializingTestCase<ConstantProcessor> {

public static ConstantProcessor randomConstantProcessor() {
return new ConstantProcessor(randomAlphaOfLength(5));
if (randomBoolean()) {
Clock clock;
if (randomBoolean()) {
clock = Clock.tick(Clock.system(randomZone()), Duration.ofNanos(1));
} else {
clock = Clock.tick(Clock.system(randomZone()), Duration.ofMillis(1));
}
return new ConstantProcessor(ZonedDateTime.now(clock));
} else {
return new ConstantProcessor(randomAlphaOfLength(5));
}
}

@Override
Expand Down
Expand Up @@ -80,7 +80,11 @@ public Object extract(Bucket bucket) {

Object value = agg.getHits().getAt(0).getFields().values().iterator().next().getValue();
if (fieldDataType == DATETIME || fieldDataType == DATE) {
return DateUtils.asDateTimeWithMillis(Long.parseLong(value.toString()), zoneId);
try {
return DateUtils.asDateTimeWithMillis(Long.parseLong(value.toString()), zoneId);
} catch (NumberFormatException e) {
return DateUtils.asDateTimeWithNanos(value.toString()).withZoneSameInstant(zoneId);
}
} else if (fieldDataType == DATETIME_NANOS) {
return DateUtils.asDateTimeWithNanos(value.toString());
} else if (SqlDataTypes.isTimeBased(fieldDataType)) {
Expand Down