Skip to content

Commit

Permalink
Update Datum from PartiQLValue
Browse files Browse the repository at this point in the history
  • Loading branch information
RCHowell committed Nov 26, 2024
1 parent 7223aae commit 3fa3853
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 109 deletions.
64 changes: 36 additions & 28 deletions partiql-spi/src/main/java/org/partiql/spi/value/Datum.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import org.partiql.value.PartiQL;
import org.partiql.value.PartiQLValue;
import org.partiql.value.PartiQLValueType;
import org.partiql.value.datetime.Date;
import org.partiql.value.datetime.Time;
import org.partiql.value.datetime.Timestamp;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -106,6 +103,7 @@ default boolean getBoolean() {
* <p>
* <b>! ! ! EXPERIMENTAL ! ! !</b> This is an experimental API under development by the PartiQL maintainers.
* </p>
*
* @return the underlying value applicable to the types:
* {@link PartiQLValueType#BINARY},
* {@link PartiQLValueType#BLOB},
Expand All @@ -115,7 +113,7 @@ default boolean getBoolean() {
* will throw this exception upon invocation.
* @throws NullPointerException if this instance also returns true on {@link #isNull()}; callers should check that
* {@link #isNull()} returns false before attempting to invoke this method.
* Please abstain from using this API until given notice otherwise. This may break between iterations without prior notice.
* Please abstain from using this API until given notice otherwise. This may break between iterations without prior notice.
* @deprecated BINARY doesn't exist in SQL or Ion. This is subject to deletion. BLOB and CLOB are typically represented
* in a fashion that can support much larger values -- this may be modified at any time.
*/
Expand All @@ -126,6 +124,7 @@ default byte[] getBytes() {

/**
* <b>! ! ! EXPERIMENTAL ! ! !</b> This is an experimental API under development by the PartiQL maintainers.
*
* @return the underlying value applicable to the types:
* {@link PartiQLValueType#BYTE},
* {@link PartiQLValueType#INT8}
Expand All @@ -134,7 +133,7 @@ default byte[] getBytes() {
* will throw this exception upon invocation.
* @throws NullPointerException if this instance also returns true on {@link #isNull()}; callers should check that
* {@link #isNull()} returns false before attempting to invoke this method.
* Please abstain from using this API until given notice otherwise. This may break between iterations without prior notice.
* Please abstain from using this API until given notice otherwise. This may break between iterations without prior notice.
* @deprecated BYTE is not present in SQL or Ion. This is subject to deletion.
*/
@Deprecated
Expand All @@ -144,7 +143,6 @@ default byte getByte() {

/**
* @return a {@link LocalDate} for DATE, TIMESTAMP, and TIMESTAMPZ types.
*
* @throws UnsupportedOperationException if type not in (DATE, TIMESTAMP, TIMESTAMPZ)
* @throws NullPointerException if isNull() is true; callers should check to avoid NPEs.
*/
Expand All @@ -155,7 +153,6 @@ default LocalDate getLocalDate() {

/**
* @return an {@link OffsetTime} for TIME and TIMESTAMP types.
*
* @throws UnsupportedOperationException if type not in (TIME, TIMESTAMP)
* @throws NullPointerException if isNull() is true; callers should check to avoid NPEs.
*/
Expand All @@ -166,7 +163,6 @@ default LocalTime getLocalTime() {

/**
* @return an {@link OffsetTime} for TIMEZ and TIMESTAMPZ types.
*
* @throws UnsupportedOperationException if type not in (TIMEZ, TIMESTAMPZ)
* @throws NullPointerException if isNull() is true; callers should check to avoid NPEs.
*/
Expand All @@ -177,7 +173,6 @@ default OffsetTime getOffsetTime() {

/**
* @return a {@link LocalDateTime} for TIMESTAMP types.
*
* @throws UnsupportedOperationException if type not TIMESTAMP
* @throws NullPointerException if isNull() is true; callers should check to avoid NPEs.
*/
Expand All @@ -188,7 +183,6 @@ default LocalDateTime getLocalDateTime() {

/**
* @return a {@link OffsetDateTime} for TIMESTAMPZ types.
*
* @throws UnsupportedOperationException if type not TIMESTAMPZ
* @throws NullPointerException if isNull() is true; callers should check to avoid NPEs.
*/
Expand All @@ -199,7 +193,6 @@ default OffsetDateTime getOffsetDateTime() {

/**
* @return a {@link Period} for the INTERVALYM type.
*
* @throws UnsupportedOperationException if type not in [INTERVAL]
* @throws NullPointerException if isNull() is true; callers should check to avoid NPEs.
*/
Expand All @@ -210,7 +203,6 @@ default Period getPeriod() {

/**
* @return a {@link Duration} for the INTERVALDT type.
*
* @throws UnsupportedOperationException if type not in [INTERVALDT]
* @throws NullPointerException if isNull() is true; callers should check to avoid NPEs.
*/
Expand Down Expand Up @@ -433,13 +425,13 @@ default PartiQLValue toPartiQLValue() {
case CLOB:
return this.isNull() ? PartiQL.clobValue(null) : PartiQL.clobValue(this.getBytes());
case DATE:
return this.isNull() ? PartiQL.dateValue(null) : PartiQL.dateValue(this.getDate());
throw new UnsupportedOperationException("Datum date to PartiQLValue is not supported");
case TIMEZ:
case TIME: // TODO
return this.isNull() ? PartiQL.timeValue(null) : PartiQL.timeValue(this.getTime());
case TIME:
throw new UnsupportedOperationException("Datum time/timez to PartiQLValue is not supported");
case TIMESTAMPZ:
case TIMESTAMP:
return this.isNull() ? PartiQL.timestampValue(null) : PartiQL.timestampValue(this.getTimestamp());
throw new UnsupportedOperationException("Datum timestamp/timestampz to PartiQLValue is not supported");
case BAG:
return this.isNull() ? PartiQL.bagValue((Iterable<? extends PartiQLValue>) null) : PartiQL.bagValue(new PQLToPartiQLIterable(this));
case ARRAY:
Expand Down Expand Up @@ -511,17 +503,34 @@ static Datum of(PartiQLValue value) {
case BINARY:
throw new UnsupportedOperationException();
case DATE:
org.partiql.value.DateValue DATEValue = (org.partiql.value.DateValue) value;
return new DatumDate(Objects.requireNonNull(DATEValue.getValue()));
org.partiql.value.datetime.Date DATEValue = ((org.partiql.value.DateValue) value).getValue();
Objects.requireNonNull(DATEValue);
LocalDate date = LocalDate.of(DATEValue.getYear(), DATEValue.getMonth(), DATEValue.getDay());
return new DatumDate(date);
case INTERVAL:
org.partiql.value.IntervalValue INTERVALValue = (org.partiql.value.IntervalValue) value;
return new DatumInterval(Objects.requireNonNull(INTERVALValue.getValue()));
throw new UnsupportedOperationException("INTERVAL not implemented");
case TIMESTAMP:
org.partiql.value.TimestampValue TIMESTAMPValue = (org.partiql.value.TimestampValue) value;
return new DatumTimestamp(Objects.requireNonNull(TIMESTAMPValue.getValue()));
org.partiql.value.datetime.Timestamp TIMESTAMPValue = ((org.partiql.value.TimestampValue) value).getValue();
Objects.requireNonNull(TIMESTAMPValue);
// calculate seconds precision
BigDecimal tsds = TIMESTAMPValue.getDecimalSecond();
int tsprec = tsds.scale();
int tssecs = tsds.intValue();
int tsnano = tsds.remainder(BigDecimal.ONE).movePointRight(tsds.scale()).abs().intValue();
// make datum
LocalDateTime timestamp = LocalDateTime.of(TIMESTAMPValue.getYear(), TIMESTAMPValue.getMonth(), TIMESTAMPValue.getDay(), TIMESTAMPValue.getHour(), TIMESTAMPValue.getMinute(), tssecs, tsnano);
return new DatumTimestamp(timestamp, tsprec);
case TIME:
org.partiql.value.TimeValue TIMEValue = (org.partiql.value.TimeValue) value;
return new DatumTime(Objects.requireNonNull(TIMEValue.getValue()));
org.partiql.value.datetime.Timestamp TIMEValue = ((org.partiql.value.TimestampValue) value).getValue();
Objects.requireNonNull(TIMEValue);
// calculate seconds precision
BigDecimal tds = TIMEValue.getDecimalSecond();
int tprec = tds.scale();
int tsecs = tds.intValue();
int tnano = tds.remainder(BigDecimal.ONE).movePointRight(tds.scale()).abs().intValue();
// make datum
LocalTime time = LocalTime.of(TIMEValue.getHour(), TIMEValue.getMinute(), tsecs, tnano);
return new DatumTime(time, tprec);
case FLOAT32:
org.partiql.value.Float32Value FLOAT32Value = (org.partiql.value.Float32Value) value;
return new DatumFloat(Objects.requireNonNull(FLOAT32Value.getValue()));
Expand Down Expand Up @@ -571,6 +580,7 @@ static Datum nullValue(@NotNull PType type) {
/**
* Returns a typed missing value
* ! EXPERIMENTAL ! This is subject to breaking changes and/or removal without prior notice.
*
* @param type the type of the value
* @return a typed missing value
* @deprecated this may not be required. This is subject to removal.
Expand Down Expand Up @@ -646,7 +656,6 @@ static Datum string(@NotNull String value) {
}

/**
*
* @param value the string to place in the varchar
* @return a varchar value with a default length of 255
*/
Expand All @@ -656,7 +665,6 @@ static Datum varchar(@NotNull String value) {
}

/**
*
* @param value the string to place in the varchar
* @return a varchar value
* TODO: Error or coerce here? Right now coerce, though I think this should likely error.
Expand All @@ -678,7 +686,6 @@ static Datum varchar(@NotNull String value, int length) {
}

/**
*
* @param value the string to place in the char
* @return a char value with a default length of 255
*/
Expand All @@ -688,7 +695,6 @@ static Datum character(@NotNull String value) {
}

/**
*
* @param value the string to place in the char
* @return a char value
*/
Expand Down Expand Up @@ -801,6 +807,7 @@ static Datum struct(@NotNull Iterable<Field> values) {
* {@link java.util.TreeSet} in combination with this {@link Comparator} to implement the before-mentioned
* operations.
* </p>
*
* @return the default comparator for {@link Datum}. The comparator orders null values first.
* @see Datum
* @see java.util.TreeSet
Expand All @@ -821,6 +828,7 @@ static Comparator<Datum> comparator() {
* {@link java.util.TreeSet} in combination with this {@link Comparator} to implement the before-mentioned
* operations.
* </p>
*
* @param nullsFirst if true, nulls are ordered before non-null values, otherwise after.
* @return the default comparator for {@link Datum}.
* @see Datum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal val Fn_DATE_ADD_MONTH__INT32_DATE__DATE = Function.static(

) { args ->
val interval = args[0].int
val datetime = args[1].date
val datetime = args[1].localDate
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.date(datetimeValue.plusMonths(intervalValue))
Expand All @@ -38,7 +38,7 @@ internal val Fn_DATE_ADD_MONTH__INT64_DATE__DATE = Function.static(

) { args ->
val interval = args[0].long
val datetime = args[1].date
val datetime = args[1].localDate
val datetimeValue = datetime
val intervalValue = interval
Datum.date(datetimeValue.plusMonths(intervalValue))
Expand All @@ -55,7 +55,7 @@ internal val Fn_DATE_ADD_MONTH__INT_DATE__DATE = Function.static(

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].date
val datetime = args[1].localDate
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
Expand All @@ -76,7 +76,7 @@ internal val Fn_DATE_ADD_MONTH__INT32_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].int
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.timestamp(datetimeValue.plusMonths(intervalValue))
Expand All @@ -93,7 +93,7 @@ internal val Fn_DATE_ADD_MONTH__INT64_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].long
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = interval
Datum.timestamp(datetimeValue.plusMonths(intervalValue))
Expand All @@ -110,7 +110,7 @@ internal val Fn_DATE_ADD_MONTH__INT_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal val Fn_DATE_ADD_SECOND__INT32_TIME__TIME = Function.static(

) { args ->
val interval = args[0].int
val datetime = args[1].time
val datetime = args[1].localTime
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.time(datetimeValue.plusSeconds(intervalValue))
Expand All @@ -38,7 +38,7 @@ internal val Fn_DATE_ADD_SECOND__INT64_TIME__TIME = Function.static(

) { args ->
val interval = args[0].long
val datetime = args[1].time
val datetime = args[1].localTime
val datetimeValue = datetime
val intervalValue = interval
Datum.time(datetimeValue.plusSeconds(intervalValue))
Expand All @@ -55,7 +55,7 @@ internal val Fn_DATE_ADD_SECOND__INT_TIME__TIME = Function.static(

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].time
val datetime = args[1].localTime
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
Expand All @@ -76,7 +76,7 @@ internal val Fn_DATE_ADD_SECOND__INT32_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].int
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.timestamp(datetimeValue.plusSeconds(intervalValue))
Expand All @@ -93,7 +93,7 @@ internal val Fn_DATE_ADD_SECOND__INT64_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].long
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = interval
Datum.timestamp(datetimeValue.plusSeconds(intervalValue))
Expand All @@ -110,7 +110,7 @@ internal val Fn_DATE_ADD_SECOND__INT_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal val Fn_DATE_ADD_YEAR__INT32_DATE__DATE = Function.static(

) { args ->
val interval = args[0].int
val datetime = args[1].date
val datetime = args[1].localDate
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.date(datetimeValue.plusYears(intervalValue))
Expand All @@ -38,7 +38,7 @@ internal val Fn_DATE_ADD_YEAR__INT64_DATE__DATE = Function.static(

) { args ->
val interval = args[0].long
val datetime = args[1].date
val datetime = args[1].localDate
val datetimeValue = datetime
val intervalValue = interval
Datum.date(datetimeValue.plusYears(intervalValue))
Expand All @@ -55,7 +55,7 @@ internal val Fn_DATE_ADD_YEAR__INT_DATE__DATE = Function.static(

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].date
val datetime = args[1].localDate
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
Expand All @@ -76,7 +76,7 @@ internal val Fn_DATE_ADD_YEAR__INT32_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].int
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.timestamp(datetimeValue.plusYears(intervalValue))
Expand All @@ -93,7 +93,7 @@ internal val Fn_DATE_ADD_YEAR__INT64_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].long
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = interval
Datum.timestamp(datetimeValue.plusYears(intervalValue))
Expand All @@ -110,7 +110,7 @@ internal val Fn_DATE_ADD_YEAR__INT_TIMESTAMP__TIMESTAMP = Function.static(

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].timestamp
val datetime = args[1].localDateTime
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
Expand Down
Loading

0 comments on commit 3fa3853

Please sign in to comment.