Skip to content

Commit

Permalink
Removes getBigInteger for NUMERIC
Browse files Browse the repository at this point in the history
Updates BitwiseAndTest to only use integers

Removes deprecated notices for Datum#getBytes and Datum#getByte

Adds InvalidOperationException to Datum

Updates signatures
  • Loading branch information
johnedquinn committed Jan 10, 2025
1 parent cb0276c commit 801bcbb
Show file tree
Hide file tree
Showing 39 changed files with 384 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal object ValueUtility {
PType.SMALLINT -> this.short.toInt()
PType.INTEGER -> this.int
PType.BIGINT -> this.long.toInt()
PType.NUMERIC -> this.bigInteger.toInt()
PType.NUMERIC -> this.bigDecimal.toInt()
else -> throw TypeCheckException()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import org.partiql.spi.types.PType.VARCHAR
import org.partiql.spi.types.PType.VARIANT
import org.partiql.spi.value.Datum
import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
import java.time.LocalDate
import java.time.LocalTime
Expand Down Expand Up @@ -164,7 +163,7 @@ internal object CastTable {
register(
BOOL,
NUMERIC
) { x, _ -> Datum.numeric(if (x.boolean) BigInteger.ONE else BigInteger.ZERO) }
) { x, _ -> Datum.numeric(if (x.boolean) BigDecimal.ONE else BigDecimal.ZERO) }
register(
BOOL,
DECIMAL
Expand All @@ -191,7 +190,7 @@ internal object CastTable {
register(TINYINT, BIGINT) { x, _ -> Datum.bigint(x.byte.toLong()) }
register(TINYINT, NUMERIC) { x, _ ->
Datum.numeric(
x.byte.toInt().toBigInteger()
x.byte.toInt().toBigDecimal()
)
}
register(TINYINT, DECIMAL) { x, t ->
Expand Down Expand Up @@ -220,7 +219,7 @@ internal object CastTable {
register(SMALLINT, BIGINT) { x, _ -> Datum.bigint(x.short.toLong()) }
register(SMALLINT, NUMERIC) { x, _ ->
Datum.numeric(
x.short.toInt().toBigInteger()
x.short.toInt().toBigDecimal()
)
}
register(SMALLINT, DECIMAL) { x, t ->
Expand All @@ -247,7 +246,7 @@ internal object CastTable {
register(INTEGER, SMALLINT) { x, _ -> datumSmallInt(x.int) }
register(INTEGER, INTEGER) { x, _ -> x }
register(INTEGER, BIGINT) { x, _ -> Datum.bigint(x.int.toLong()) }
register(INTEGER, NUMERIC) { x, _ -> Datum.numeric(x.int.toBigInteger()) }
register(INTEGER, NUMERIC) { x, _ -> Datum.numeric(x.int.toBigDecimal()) }
register(INTEGER, DECIMAL) { x, t ->
Datum.decimal(
x.int.toBigDecimal(),
Expand All @@ -272,7 +271,7 @@ internal object CastTable {
register(BIGINT, SMALLINT) { x, _ -> datumSmallInt(x.long) }
register(BIGINT, INTEGER) { x, _ -> datumInt(x.long) }
register(BIGINT, BIGINT) { x, _ -> x }
register(BIGINT, NUMERIC) { x, _ -> Datum.numeric(x.long.toBigInteger()) }
register(BIGINT, NUMERIC) { x, _ -> Datum.numeric(x.long.toBigDecimal()) }
register(BIGINT, DECIMAL) { x, t ->
Datum.decimal(
x.long.toBigDecimal(),
Expand All @@ -292,28 +291,28 @@ internal object CastTable {
* CAST(<int arbitrary> AS <target>)
*/
private fun registerIntArbitrary() {
register(NUMERIC, BOOL) { x, _ -> Datum.bool(x.bigInteger != BigInteger.ZERO) }
register(NUMERIC, TINYINT) { x, _ -> datumTinyInt(x.bigInteger) }
register(NUMERIC, SMALLINT) { x, _ -> datumSmallInt(x.bigInteger) }
register(NUMERIC, INTEGER) { x, _ -> datumInt(x.bigInteger) }
register(NUMERIC, BIGINT) { x, _ -> datumBigInt(x.bigInteger) }
register(NUMERIC, BOOL) { x, _ -> Datum.bool(x.bigDecimal != BigDecimal.ZERO) }
register(NUMERIC, TINYINT) { x, _ -> datumTinyInt(x.bigDecimal) }
register(NUMERIC, SMALLINT) { x, _ -> datumSmallInt(x.bigDecimal) }
register(NUMERIC, INTEGER) { x, _ -> datumInt(x.bigDecimal) }
register(NUMERIC, BIGINT) { x, _ -> datumBigInt(x.bigDecimal) }
register(NUMERIC, NUMERIC) { x, _ -> x }
register(NUMERIC, DECIMAL) { x, t ->
Datum.decimal(
x.bigInteger.toBigDecimal(),
x.bigDecimal,
t.precision,
t.scale
)
}
register(NUMERIC, REAL) { x, _ -> datumReal(x.bigInteger) }
register(NUMERIC, REAL) { x, _ -> datumReal(x.bigDecimal) }
register(
NUMERIC,
DOUBLE
) { x, _ -> datumDoublePrecision(x.bigInteger) }
register(NUMERIC, STRING) { x, _ -> Datum.string(x.bigInteger.toString()) }
register(NUMERIC, VARCHAR) { x, t -> Datum.varchar(x.bigInteger.toString(), t.length) }
register(NUMERIC, CHAR) { x, t -> Datum.character(x.bigInteger.toString(), t.length) }
register(NUMERIC, CLOB) { x, t -> Datum.clob(x.bigInteger.toString().toByteArray(), t.length) }
) { x, _ -> datumDoublePrecision(x.bigDecimal) }
register(NUMERIC, STRING) { x, _ -> Datum.string(x.bigDecimal.toString()) }
register(NUMERIC, VARCHAR) { x, t -> Datum.varchar(x.bigDecimal.toString(), t.length) }
register(NUMERIC, CHAR) { x, t -> Datum.character(x.bigDecimal.toString(), t.length) }
register(NUMERIC, CLOB) { x, t -> Datum.clob(x.bigDecimal.toString().toByteArray(), t.length) }
}

/**
Expand Down Expand Up @@ -352,7 +351,7 @@ internal object CastTable {
register(REAL, BIGINT) { x, _ -> datumBigInt(x.float) }
register(REAL, NUMERIC) { x, _ ->
Datum.numeric(
x.float.toInt().toBigInteger()
x.float.toInt().toBigDecimal()
)
}
register(REAL, DECIMAL) { x, t ->
Expand Down Expand Up @@ -507,7 +506,7 @@ internal object CastTable {
return Datum.nullValue()
}
return when (ion.type) {
ElementType.INT -> Datum.numeric(ion.bigIntegerValue)
ElementType.INT -> Datum.numeric(ion.bigIntegerValue.toBigDecimal())
ElementType.FLOAT -> Datum.doublePrecision(ion.doubleValue)
ElementType.DECIMAL -> Datum.decimal(ion.decimalValue)
else -> throw TypeCheckException()
Expand Down Expand Up @@ -573,15 +572,6 @@ internal object CastTable {
return Datum.integer(int)
}

private fun datumInt(value: BigInteger): Datum {
val int = try {
value.intValueExact()
} catch (e: ArithmeticException) {
throw DataException("Overflow when casting $value to INT")
}
return Datum.integer(int)
}

private fun datumInt(value: Float): Datum {
if (value > Int.MAX_VALUE || value < Int.MIN_VALUE) {
throw DataException("Overflow when casting $value to INT")
Expand Down Expand Up @@ -624,15 +614,6 @@ internal object CastTable {
return Datum.tinyint(value.toByte())
}

private fun datumTinyInt(value: BigInteger): Datum {
val byte = try {
value.byteValueExact()
} catch (e: ArithmeticException) {
throw DataException("Overflow when casting $value to TINYINT")
}
return Datum.tinyint(byte)
}

private fun datumTinyInt(value: Float): Datum {
if (value > Byte.MAX_VALUE || value < Byte.MIN_VALUE) {
throw DataException("Overflow when casting $value to TINYINT")
Expand Down Expand Up @@ -661,14 +642,7 @@ internal object CastTable {
}
return Datum.smallint(short)
}
private fun datumSmallInt(value: BigInteger): Datum {
val short = try {
value.shortValueExact()
} catch (e: ArithmeticException) {
throw DataException("Overflow when casting $value to SMALLINT")
}
return Datum.smallint(short)
}

private fun datumSmallInt(value: Float): Datum {
if (value > Short.MAX_VALUE || value < Short.MIN_VALUE) {
throw DataException("Overflow when casting $value to SMALLINT")
Expand All @@ -693,15 +667,11 @@ internal object CastTable {
}

private fun datumIntArbitrary(value: BigDecimal): Datum {
return Datum.numeric(value.setScale(0, RoundingMode.HALF_EVEN).toBigInteger())
return Datum.numeric(value.setScale(0, RoundingMode.HALF_EVEN))
}

private fun datumIntArbitrary(value: Double): Datum {
return Datum.numeric(value.toBigDecimal().setScale(0, RoundingMode.DOWN).toBigInteger())
}

private fun datumBigInt(value: BigInteger): Datum {
return Datum.bigint(value.longValueExact())
return Datum.numeric(value.toBigDecimal().setScale(0, RoundingMode.DOWN))
}

private fun datumBigInt(value: BigDecimal): Datum {
Expand All @@ -726,10 +696,6 @@ internal object CastTable {
return Datum.doublePrecision(value.toDouble())
}

private fun datumDoublePrecision(value: BigInteger): Datum {
return Datum.doublePrecision(value.toDouble())
}

private fun datumReal(value: Double): Datum {
if (value > Float.MAX_VALUE || value < Float.MIN_VALUE) {
throw DataException("Overflow when casting $value to REAL")
Expand All @@ -744,8 +710,4 @@ internal object CastTable {
}
return Datum.real(float)
}

private fun datumReal(value: BigInteger): Datum {
return Datum.real(value.toFloat())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.partiql.spi.errors.CardinalityViolation
import org.partiql.spi.errors.DataException
import org.partiql.spi.errors.TypeCheckException
import org.partiql.spi.value.Datum
import org.partiql.spi.value.InvalidOperationException

internal class ExprPermissive(private var expr: ExprValue) :
ExprValue {
Expand All @@ -17,7 +18,7 @@ internal class ExprPermissive(private var expr: ExprValue) :
Datum.missing()
} catch (e: CardinalityViolation) {
Datum.missing()
} catch (e: UnsupportedOperationException) {
} catch (e: InvalidOperationException) {
Datum.missing()
} catch (e: DataException) {
Datum.missing()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class OpBitwiseAndTest : PartiQLTyperTestBase() {
val arg0 = args.first()
val arg1 = args[1]
val output = when {
arg0 !in allIntPType && arg1 !in allIntPType -> PType.numeric(38, 0)
arg0 in allIntPType && arg1 !in allIntPType -> PType.numeric(38, 0)
arg0 !in allIntPType && arg1 in allIntPType -> PType.numeric(38, 0)
arg0 !in allIntPType && arg1 !in allIntPType -> PType.bigint()
arg0 in allIntPType && arg1 !in allIntPType -> PType.bigint()
arg0 !in allIntPType && arg1 in allIntPType -> PType.bigint()
arg0 == arg1 -> arg1
castTablePType(arg1, arg0) == CastType.COERCION -> arg0
castTablePType(arg0, arg1) == CastType.COERCION -> arg1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ val allBooleanPType = setOf(
PType.bool()
)

val allIntPType = setOf(PType.tinyint(), PType.smallint(), PType.integer(), PType.bigint(), PType.numeric(38, 0))
val allIntPType = setOf(PType.tinyint(), PType.smallint(), PType.integer(), PType.bigint())

val allNumberPType = allIntPType + setOf(
PType.decimal(38, 19),
Expand Down
9 changes: 7 additions & 2 deletions partiql-spi/api/partiql-spi.api
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ public abstract interface class org/partiql/spi/value/Datum : java/lang/Iterable
public static fun doublePrecision (D)Lorg/partiql/spi/value/Datum;
public fun get (Ljava/lang/String;)Lorg/partiql/spi/value/Datum;
public fun getBigDecimal ()Ljava/math/BigDecimal;
public fun getBigInteger ()Ljava/math/BigInteger;
public fun getBoolean ()Z
public fun getByte ()B
public fun getBytes ()[B
Expand Down Expand Up @@ -597,7 +596,8 @@ public abstract interface class org/partiql/spi/value/Datum : java/lang/Iterable
public static fun missing (Lorg/partiql/spi/types/PType;)Lorg/partiql/spi/value/Datum;
public static fun nullValue ()Lorg/partiql/spi/value/Datum;
public static fun nullValue (Lorg/partiql/spi/types/PType;)Lorg/partiql/spi/value/Datum;
public static fun numeric (Ljava/math/BigInteger;)Lorg/partiql/spi/value/Datum;
public static fun numeric (Ljava/math/BigDecimal;)Lorg/partiql/spi/value/Datum;
public static fun numeric (Ljava/math/BigDecimal;II)Lorg/partiql/spi/value/Datum;
public fun pack (Ljava/nio/charset/Charset;)[B
public static fun real (F)Lorg/partiql/spi/value/Datum;
public static fun smallint (S)Lorg/partiql/spi/value/Datum;
Expand Down Expand Up @@ -642,3 +642,8 @@ public abstract interface class org/partiql/spi/value/Field {
public static fun of (Ljava/lang/String;Lorg/partiql/spi/value/Datum;)Lorg/partiql/spi/value/Field;
}

public final class org/partiql/spi/value/InvalidOperationException : java/lang/RuntimeException {
public fun <init> (Lorg/partiql/spi/types/PType;Ljava/lang/String;)V
public fun getMessage ()Ljava/lang/String;
}

Loading

0 comments on commit 801bcbb

Please sign in to comment.