Skip to content

Commit

Permalink
Updates FnDateAddDay
Browse files Browse the repository at this point in the history
  • Loading branch information
rchowell committed Nov 26, 2024
1 parent 3dd23e7 commit 7223aae
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 73 deletions.
22 changes: 22 additions & 0 deletions partiql-spi/src/main/java/org/partiql/spi/value/Datum.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ default OffsetTime getOffsetTime() {
throw new UnsupportedOperationException("Cannot call 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.
*/
@NotNull
default LocalDateTime getLocalDateTime() {
throw new UnsupportedOperationException();
}

/**
* @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.
*/
@NotNull
default OffsetDateTime getOffsetDateTime() {
throw new UnsupportedOperationException();
}

/**
* @return a {@link Period} for the INTERVALYM type.
*
Expand Down
20 changes: 20 additions & 0 deletions partiql-spi/src/main/java/org/partiql/spi/value/DatumNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ public OffsetTime getOffsetTime() {
}
}

@NotNull
@Override
public LocalDateTime getLocalDateTime() {
if (_type.getKind() == PType.Kind.TIMESTAMP) {
throw new NullPointerException();
} else {
throw new UnsupportedOperationException();
}
}

@NotNull
@Override
public OffsetDateTime getOffsetDateTime() {
if (_type.getKind() == PType.Kind.TIMESTAMPZ) {
throw new NullPointerException();
} else {
throw new UnsupportedOperationException();
}
}

@NotNull
@Override
public Duration getDuration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ public LocalDate getLocalDate() {
public LocalTime getLocalTime() {
return value.toLocalTime();
}

@NotNull
@Override
public LocalDateTime getLocalDateTime() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ public LocalDate getLocalDate() {
public OffsetTime getOffsetTime() {
return value.toOffsetTime();
}

@NotNull
@Override
public OffsetDateTime getOffsetDateTime() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.partiql.spi.function.Function
import org.partiql.spi.function.Parameter
import org.partiql.spi.value.Datum
import org.partiql.types.PType
import java.time.LocalDateTime

internal val Fn_BETWEEN__INT8_INT8_INT8__BOOL = Function.static(

Expand Down Expand Up @@ -189,9 +190,9 @@ internal val Fn_BETWEEN__DATE_DATE_DATE__BOOL = Function.static(
),

) { args ->
val value = args[0].date
val lower = args[1].date
val upper = args[2].date
val value = args[0].localDate
val lower = args[1].localDate
val upper = args[2].localDate
Datum.bool(value in lower..upper)
}

Expand All @@ -206,9 +207,9 @@ internal val Fn_BETWEEN__TIME_TIME_TIME__BOOL = Function.static(
),

) { args ->
val value = args[0].time
val lower = args[1].time
val upper = args[2].time
val value = args[0].localTime
val lower = args[1].localTime
val upper = args[2].localTime
Datum.bool(value in lower..upper)
}

Expand All @@ -223,8 +224,8 @@ internal val Fn_BETWEEN__TIMESTAMP_TIMESTAMP_TIMESTAMP__BOOL = Function.static(
),

) { args ->
val value = args[0].timestamp
val lower = args[1].timestamp
val upper = args[2].timestamp
val value = LocalDateTime.of(args[0].localDate, args[0].localTime)
val lower = LocalDateTime.of(args[1].localDate, args[1].localTime)
val upper = LocalDateTime.of(args[2].localDate, args[2].localTime)
Datum.bool(value in lower..upper)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package org.partiql.spi.function.builtins

import org.partiql.errors.DataException
import org.partiql.errors.TypeCheckException
import org.partiql.spi.function.Function
import org.partiql.spi.function.Parameter
import org.partiql.spi.value.Datum
Expand All @@ -20,11 +18,9 @@ internal val Fn_DATE_ADD_DAY__INT32_DATE__DATE = Function.static(
),

) { args ->
val interval = args[0].int
val datetime = args[1].date
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.date(datetimeValue.plusDays(intervalValue))
val days = args[0].int
val date = args[1].localDate
Datum.date(date.plusDays(days.toLong()))
}

internal val Fn_DATE_ADD_DAY__INT64_DATE__DATE = Function.static(
Expand All @@ -37,32 +33,9 @@ internal val Fn_DATE_ADD_DAY__INT64_DATE__DATE = Function.static(
),

) { args ->
val interval = args[0].long
val datetime = args[1].date
val datetimeValue = datetime
val intervalValue = interval
Datum.date(datetimeValue.plusDays(intervalValue))
}

internal val Fn_DATE_ADD_DAY__INT_DATE__DATE = Function.static(

name = "date_add_day",
returns = PType.date(),
parameters = arrayOf(
@Suppress("DEPRECATION") Parameter("interval", PType.numeric()),
Parameter("datetime", PType.date()),
),

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].date
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
} catch (e: DataException) {
throw TypeCheckException()
}
Datum.date(datetimeValue.plusDays(intervalValue))
val days = args[0].long
val date = args[1].localDate
Datum.date(date.plusDays(days))
}

internal val Fn_DATE_ADD_DAY__INT32_TIMESTAMP__TIMESTAMP = Function.static(
Expand All @@ -75,11 +48,9 @@ internal val Fn_DATE_ADD_DAY__INT32_TIMESTAMP__TIMESTAMP = Function.static(
),

) { args ->
val interval = args[0].int
val datetime = args[1].timestamp
val datetimeValue = datetime
val intervalValue = interval.toLong()
Datum.timestamp(datetimeValue.plusDays(intervalValue))
val days = args[0].int
val timestamp = args[1].localDateTime
Datum.timestamp(timestamp.plusDays(days.toLong()), 6)
}

internal val Fn_DATE_ADD_DAY__INT64_TIMESTAMP__TIMESTAMP = Function.static(
Expand All @@ -92,30 +63,7 @@ internal val Fn_DATE_ADD_DAY__INT64_TIMESTAMP__TIMESTAMP = Function.static(
),

) { args ->
val interval = args[0].long
val datetime = args[1].timestamp
val datetimeValue = datetime
val intervalValue = interval
Datum.timestamp(datetimeValue.plusDays(intervalValue))
}

internal val Fn_DATE_ADD_DAY__INT_TIMESTAMP__TIMESTAMP = Function.static(

name = "date_add_day",
returns = PType.timestamp(6),
parameters = arrayOf(
@Suppress("DEPRECATION") Parameter("interval", PType.numeric()),
Parameter("datetime", PType.timestamp(6)),
),

) { args ->
val interval = args[0].bigInteger
val datetime = args[1].timestamp
val datetimeValue = datetime
val intervalValue = try {
interval.toLong()
} catch (e: DataException) {
throw TypeCheckException()
}
Datum.timestamp(datetimeValue.plusDays(intervalValue))
val days = args[0].long
val timestamp = args[1].localDateTime
Datum.timestamp(timestamp.plusDays(days), 6)
}

0 comments on commit 7223aae

Please sign in to comment.