ExprDatetimeValue.java

1
/*
2
 * Copyright OpenSearch Contributors
3
 * SPDX-License-Identifier: Apache-2.0
4
 */
5
6
7
package org.opensearch.sql.data.model;
8
9
import com.google.common.base.Objects;
10
import java.time.Instant;
11
import java.time.LocalDate;
12
import java.time.LocalDateTime;
13
import java.time.LocalTime;
14
import java.time.ZoneId;
15
import java.time.ZonedDateTime;
16
import java.time.format.DateTimeFormatter;
17
import java.time.format.DateTimeFormatterBuilder;
18
import java.time.format.DateTimeParseException;
19
import java.time.temporal.ChronoField;
20
import java.time.temporal.ChronoUnit;
21
import lombok.RequiredArgsConstructor;
22
import org.opensearch.sql.data.type.ExprCoreType;
23
import org.opensearch.sql.data.type.ExprType;
24
import org.opensearch.sql.exception.SemanticCheckException;
25
26
@RequiredArgsConstructor
27
public class ExprDatetimeValue extends AbstractExprValue {
28
  private final LocalDateTime datetime;
29
30
  private static final DateTimeFormatter FORMATTER_VARIABLE_NANOS;
31
  private static final int MIN_FRACTION_SECONDS = 0;
32
  private static final int MAX_FRACTION_SECONDS = 9;
33
34
  static {
35
    FORMATTER_VARIABLE_NANOS = new DateTimeFormatterBuilder()
36
        .appendPattern("uuuu-MM-dd HH:mm:ss[xxx]")
37
        .appendFraction(
38
            ChronoField.NANO_OF_SECOND,
39
            MIN_FRACTION_SECONDS,
40
            MAX_FRACTION_SECONDS,
41
            true)
42
        .toFormatter();
43
  }
44
45
  /**
46
   * Constructor with datetime string as input.
47
   */
48
  public ExprDatetimeValue(String datetime) {
49
    try {
50
      this.datetime = LocalDateTime.parse(datetime, FORMATTER_VARIABLE_NANOS);
51
    } catch (DateTimeParseException e) {
52
      throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please "
53
          + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", datetime));
54
    }
55
  }
56
57
  @Override
58
  public LocalDateTime datetimeValue() {
59 1 1. datetimeValue : replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::datetimeValue → KILLED
    return datetime;
60
  }
61
62
  @Override
63
  public LocalDate dateValue() {
64 1 1. dateValue : replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::dateValue → KILLED
    return datetime.toLocalDate();
65
  }
66
67
  @Override
68
  public LocalTime timeValue() {
69 1 1. timeValue : replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::timeValue → KILLED
    return datetime.toLocalTime();
70
  }
71
72
  @Override
73
  public Instant timestampValue() {
74 1 1. timestampValue : replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::timestampValue → KILLED
    return ZonedDateTime.of(datetime, ZoneId.of("UTC")).toInstant();
75
  }
76
77
  @Override
78
  public int compare(ExprValue other) {
79 1 1. compare : replaced int return with 0 for org/opensearch/sql/data/model/ExprDatetimeValue::compare → KILLED
    return datetime.compareTo(other.datetimeValue());
80
  }
81
82
  @Override
83
  public boolean equal(ExprValue other) {
84 2 1. equal : replaced boolean return with true for org/opensearch/sql/data/model/ExprDatetimeValue::equal → SURVIVED
2. equal : replaced boolean return with false for org/opensearch/sql/data/model/ExprDatetimeValue::equal → KILLED
    return datetime.equals(other.datetimeValue());
85
  }
86
87
  @Override
88
  public String value() {
89 1 1. value : replaced return value with "" for org/opensearch/sql/data/model/ExprDatetimeValue::value → KILLED
    return String.format("%s %s", DateTimeFormatter.ISO_DATE.format(datetime),
90 1 1. value : negated conditional → KILLED
        DateTimeFormatter.ISO_TIME.format((datetime.getNano() == 0)
91
            ? datetime.truncatedTo(ChronoUnit.SECONDS) : datetime));
92
  }
93
94
  @Override
95
  public ExprType type() {
96 1 1. type : replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::type → KILLED
    return ExprCoreType.DATETIME;
97
  }
98
99
  @Override
100
  public String toString() {
101 1 1. toString : replaced return value with "" for org/opensearch/sql/data/model/ExprDatetimeValue::toString → KILLED
    return String.format("DATETIME '%s'", value());
102
  }
103
104
  @Override
105
  public int hashCode() {
106 1 1. hashCode : replaced int return with 0 for org/opensearch/sql/data/model/ExprDatetimeValue::hashCode → SURVIVED
    return Objects.hashCode(datetime);
107
  }
108
}

Mutations

59

1.1
Location : datetimeValue
Killed by : org.opensearch.sql.data.model.ExprValueUtilsTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.ExprValueUtilsTest]/[test-template:getValue(org.opensearch.sql.data.model.ExprValue, java.util.function.Function, java.lang.Object)]/[test-template-invocation:#13]
replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::datetimeValue → KILLED

64

1.1
Location : dateValue
Killed by : org.opensearch.sql.data.model.DateTimeValueTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.DateTimeValueTest]/[method:datetimeWithVariableNanoPrecision()]
replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::dateValue → KILLED

69

1.1
Location : timeValue
Killed by : org.opensearch.sql.data.model.DateTimeValueTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.DateTimeValueTest]/[method:datetimeWithVariableNanoPrecision()]
replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::timeValue → KILLED

74

1.1
Location : timestampValue
Killed by : org.opensearch.sql.data.model.DateTimeValueTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.DateTimeValueTest]/[method:datetimeValueInterfaceTest()]
replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::timestampValue → KILLED

79

1.1
Location : compare
Killed by : org.opensearch.sql.data.model.ExprValueCompareTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.ExprValueCompareTest]/[method:datetimeValueCompare()]
replaced int return with 0 for org/opensearch/sql/data/model/ExprDatetimeValue::compare → KILLED

84

1.1
Location : equal
Killed by : org.opensearch.sql.data.model.ExprValueUtilsTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.ExprValueUtilsTest]/[method:constructDateAndTimeValue()]
replaced boolean return with false for org/opensearch/sql/data/model/ExprDatetimeValue::equal → KILLED

2.2
Location : equal
Killed by : none
replaced boolean return with true for org/opensearch/sql/data/model/ExprDatetimeValue::equal → SURVIVED

89

1.1
Location : value
Killed by : org.opensearch.sql.data.model.DateTimeValueTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.DateTimeValueTest]/[method:datetimeValueInterfaceTest()]
replaced return value with "" for org/opensearch/sql/data/model/ExprDatetimeValue::value → KILLED

90

1.1
Location : value
Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:microsecond()]
negated conditional → KILLED

96

1.1
Location : type
Killed by : org.opensearch.sql.data.model.ExprValueUtilsTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.ExprValueUtilsTest]/[test-template:getType(org.opensearch.sql.data.model.ExprValue, org.opensearch.sql.data.type.ExprCoreType)]/[test-template-invocation:#13]
replaced return value with null for org/opensearch/sql/data/model/ExprDatetimeValue::type → KILLED

101

1.1
Location : toString
Killed by : org.opensearch.sql.data.model.DateTimeValueTest.[engine:junit-jupiter]/[class:org.opensearch.sql.data.model.DateTimeValueTest]/[method:datetimeValueInterfaceTest()]
replaced return value with "" for org/opensearch/sql/data/model/ExprDatetimeValue::toString → KILLED

106

1.1
Location : hashCode
Killed by : none
replaced int return with 0 for org/opensearch/sql/data/model/ExprDatetimeValue::hashCode → SURVIVED

Active mutators

Tests examined


Report generated by PIT 1.9.0