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

Adds unary, logical, binary arithmetic, and concat operators #1325

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.partiql.plugin.internal.fn.scalar

import org.partiql.value.ClobValue
import org.partiql.value.DecimalValue
import org.partiql.value.Float32Value
import org.partiql.value.Float64Value
import org.partiql.value.Int16Value
import org.partiql.value.Int32Value
import org.partiql.value.Int64Value
import org.partiql.value.Int8Value
import org.partiql.value.IntValue
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.StringValue
import org.partiql.value.SymbolValue
import org.partiql.value.check
import org.partiql.value.clobValue
import org.partiql.value.decimalValue
import org.partiql.value.float32Value
import org.partiql.value.float64Value
import org.partiql.value.int16Value
import org.partiql.value.int32Value
import org.partiql.value.int64Value
import org.partiql.value.int8Value
import org.partiql.value.intValue
import org.partiql.value.stringValue
import org.partiql.value.symbolValue
import java.math.BigDecimal
import java.math.BigInteger

@OptIn(PartiQLValueExperimental::class)
@JvmName("binaryOpInt8ByteToInt")
internal inline fun binaryOpInt8(lhs: PartiQLValue, rhs: PartiQLValue, op: (Byte, Byte) -> Int): Int8Value {
val lhsValue = lhs.check<Int8Value>().value!!
val rhsValue = rhs.check<Int8Value>().value!!
return int8Value((op(lhsValue, rhsValue)).toByte())
}

@OptIn(PartiQLValueExperimental::class)
@JvmName("binaryOpInt8ByteToByte")
internal inline fun binaryOpInt8(lhs: PartiQLValue, rhs: PartiQLValue, op: (Byte, Byte) -> Byte): Int8Value {
val lhsValue = lhs.check<Int8Value>().value!!
val rhsValue = rhs.check<Int8Value>().value!!
return int8Value((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
@JvmName("binaryOpInt16ShortToInt")
internal inline fun binaryOpInt16(lhs: PartiQLValue, rhs: PartiQLValue, op: (Short, Short) -> Int): Int16Value {
val lhsValue = lhs.check<Int16Value>().value!!
val rhsValue = rhs.check<Int16Value>().value!!
return int16Value((op(lhsValue, rhsValue)).toShort())
}

@OptIn(PartiQLValueExperimental::class)
@JvmName("binaryOpInt16ShortToShort")
internal inline fun binaryOpInt16(lhs: PartiQLValue, rhs: PartiQLValue, op: (Short, Short) -> Short): Int16Value {
val lhsValue = lhs.check<Int16Value>().value!!
val rhsValue = rhs.check<Int16Value>().value!!
return int16Value((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpInt32(lhs: PartiQLValue, rhs: PartiQLValue, op: (Int, Int) -> Int): Int32Value {
val lhsValue = lhs.check<Int32Value>().value!!
val rhsValue = rhs.check<Int32Value>().value!!
return int32Value((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpInt64(lhs: PartiQLValue, rhs: PartiQLValue, op: (Long, Long) -> Long): Int64Value {
val lhsValue = lhs.check<Int64Value>().value!!
val rhsValue = rhs.check<Int64Value>().value!!
return int64Value((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpInt(lhs: PartiQLValue, rhs: PartiQLValue, op: (BigInteger, BigInteger) -> BigInteger): IntValue {
val lhsValue = lhs.check<IntValue>().value!!
val rhsValue = rhs.check<IntValue>().value!!
return intValue((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpDecimal(lhs: PartiQLValue, rhs: PartiQLValue, op: (BigDecimal, BigDecimal) -> BigDecimal): DecimalValue {
val lhsValue = lhs.check<DecimalValue>().value!!
val rhsValue = rhs.check<DecimalValue>().value!!
return decimalValue((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpFloat32(lhs: PartiQLValue, rhs: PartiQLValue, op: (Float, Float) -> Float): Float32Value {
val lhsValue = lhs.check<Float32Value>().value!!
val rhsValue = rhs.check<Float32Value>().value!!
return float32Value((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpFloat64(lhs: PartiQLValue, rhs: PartiQLValue, op: (Double, Double) -> Double): Float64Value {
val lhsValue = lhs.check<Float64Value>().value!!
val rhsValue = rhs.check<Float64Value>().value!!
return float64Value((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpString(lhs: PartiQLValue, rhs: PartiQLValue, op: (String, String) -> String): StringValue {
val lhsValue = lhs.check<StringValue>().value!!
val rhsValue = rhs.check<StringValue>().value!!
return stringValue((op(lhsValue, rhsValue)))
}

// TODO: We are still debating on whether symbol is a value. It looks like it may not be, and therefore, this
// will be removed.
@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpSymbol(lhs: PartiQLValue, rhs: PartiQLValue, op: (String, String) -> String): SymbolValue {
val lhsValue = lhs.check<SymbolValue>().value!!
val rhsValue = rhs.check<SymbolValue>().value!!
return symbolValue((op(lhsValue, rhsValue)))
}

@OptIn(PartiQLValueExperimental::class)
internal inline fun binaryOpClob(lhs: PartiQLValue, rhs: PartiQLValue, op: (ByteArray, ByteArray) -> ByteArray): ClobValue {
val lhsValue = lhs.check<ClobValue>().value!!
val rhsValue = rhs.check<ClobValue>().value!!
return clobValue((op(lhsValue, rhsValue)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import org.partiql.spi.function.PartiQLFunction
import org.partiql.spi.function.PartiQLFunctionExperimental
import org.partiql.types.function.FunctionParameter
import org.partiql.types.function.FunctionSignature
import org.partiql.value.BoolValue
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.PartiQLValueType.BOOL
import org.partiql.value.PartiQLValueType.MISSING
import org.partiql.value.boolValue
import org.partiql.value.check

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
internal object Fn_AND__BOOL_BOOL__BOOL : PartiQLFunction.Scalar {
Expand All @@ -27,7 +30,14 @@ internal object Fn_AND__BOOL_BOOL__BOOL : PartiQLFunction.Scalar {
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function and not implemented")
val lhs = args[0].check<BoolValue>().value
val rhs = args[1].check<BoolValue>().value
val toReturn = when {
lhs == false || rhs == false -> false
lhs == null || rhs == null -> null
else -> true
}
return boolValue(toReturn)
}
}

Expand All @@ -46,7 +56,11 @@ internal object Fn_AND__MISSING_BOOL__BOOL : PartiQLFunction.Scalar {
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function and not implemented")
val rhs = args[1].check<BoolValue>().value
return when (rhs) {
false -> boolValue(false)
else -> boolValue(null)
}
}
}

Expand All @@ -65,7 +79,11 @@ internal object Fn_AND__BOOL_MISSING__BOOL : PartiQLFunction.Scalar {
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function and not implemented")
val lhs = args[0].check<BoolValue>().value
return when (lhs) {
false -> boolValue(false)
else -> boolValue(null)
}
}
}

Expand All @@ -84,6 +102,6 @@ internal object Fn_AND__MISSING_MISSING__BOOL : PartiQLFunction.Scalar {
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function and not implemented")
return boolValue(null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import org.partiql.spi.function.PartiQLFunction
import org.partiql.spi.function.PartiQLFunctionExperimental
import org.partiql.types.function.FunctionParameter
import org.partiql.types.function.FunctionSignature
import org.partiql.value.Int16Value
import org.partiql.value.Int32Value
import org.partiql.value.Int64Value
import org.partiql.value.Int8Value
import org.partiql.value.IntValue
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.PartiQLValueType.INT
import org.partiql.value.PartiQLValueType.INT16
import org.partiql.value.PartiQLValueType.INT32
import org.partiql.value.PartiQLValueType.INT64
import org.partiql.value.PartiQLValueType.INT8
import java.math.BigInteger
import kotlin.experimental.and

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
internal object Fn_BITWISE_AND__INT8_INT8__INT8 : PartiQLFunction.Scalar {
Expand All @@ -29,9 +36,7 @@ internal object Fn_BITWISE_AND__INT8_INT8__INT8 : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function bitwise_and not implemented")
}
override fun invoke(args: Array<PartiQLValue>): Int8Value = binaryOpInt8(args[0], args[1], Byte::and)
}

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
Expand All @@ -48,9 +53,7 @@ internal object Fn_BITWISE_AND__INT16_INT16__INT16 : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function bitwise_and not implemented")
}
override fun invoke(args: Array<PartiQLValue>): Int16Value = binaryOpInt16(args[0], args[1], Short::and)
}

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
Expand All @@ -67,9 +70,7 @@ internal object Fn_BITWISE_AND__INT32_INT32__INT32 : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function bitwise_and not implemented")
}
override fun invoke(args: Array<PartiQLValue>): Int32Value = binaryOpInt32(args[0], args[1], Int::and)
}

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
Expand All @@ -86,9 +87,7 @@ internal object Fn_BITWISE_AND__INT64_INT64__INT64 : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function bitwise_and not implemented")
}
override fun invoke(args: Array<PartiQLValue>): Int64Value = binaryOpInt64(args[0], args[1], Long::and)
}

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
Expand All @@ -105,7 +104,5 @@ internal object Fn_BITWISE_AND__INT_INT__INT : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function bitwise_and not implemented")
}
override fun invoke(args: Array<PartiQLValue>): IntValue = binaryOpInt(args[0], args[1], BigInteger::and)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import org.partiql.spi.function.PartiQLFunction
import org.partiql.spi.function.PartiQLFunctionExperimental
import org.partiql.types.function.FunctionParameter
import org.partiql.types.function.FunctionSignature
import org.partiql.value.ClobValue
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.PartiQLValueType.CLOB
import org.partiql.value.PartiQLValueType.STRING
import org.partiql.value.PartiQLValueType.SYMBOL
import org.partiql.value.StringValue
import org.partiql.value.SymbolValue

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
internal object Fn_CONCAT__STRING_STRING__STRING : PartiQLFunction.Scalar {
Expand All @@ -27,9 +30,7 @@ internal object Fn_CONCAT__STRING_STRING__STRING : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function concat not implemented")
}
override fun invoke(args: Array<PartiQLValue>): StringValue = binaryOpString(args[0], args[1], String::plus)
}

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
Expand All @@ -46,9 +47,9 @@ internal object Fn_CONCAT__SYMBOL_SYMBOL__SYMBOL : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function concat not implemented")
}
// TODO: We are still debating on whether symbol is a value. It looks like it may not be, and therefore, this
// will be removed.
override fun invoke(args: Array<PartiQLValue>): SymbolValue = binaryOpSymbol(args[0], args[1], String::plus)
}

@OptIn(PartiQLValueExperimental::class, PartiQLFunctionExperimental::class)
Expand All @@ -65,7 +66,5 @@ internal object Fn_CONCAT__CLOB_CLOB__CLOB : PartiQLFunction.Scalar {
isNullable = false,
)

override fun invoke(args: Array<PartiQLValue>): PartiQLValue {
TODO("Function concat not implemented")
}
override fun invoke(args: Array<PartiQLValue>): ClobValue = binaryOpClob(args[0], args[1], ByteArray::plus)
}
Loading
Loading