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

Support ANY type in Header functions And Planning of Control Flow Functions. #1256

Merged
merged 6 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions partiql-lang/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ tasks.processTestResources {

tasks.shadowJar {
archiveBaseName.set("shadow")
exclude("**/*.kotlin_metadata")
yliuuuu marked this conversation as resolved.
Show resolved Hide resolved
archiveClassifier.set("")
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class Header {
* For functions, output CREATE FUNCTION statements.
*/
override fun toString(): String = buildString {
functions.groupBy { it.name }.forEach {
(functions + operators + aggregations).groupBy { it.name }.forEach {
appendLine("-- [${it.key}] ---------")
appendLine()
it.value.forEach { fn -> appendLine(fn) }
Expand Down
241 changes: 202 additions & 39 deletions partiql-planner/src/main/kotlin/org/partiql/planner/PartiQLHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ import org.partiql.ast.DatetimeField
import org.partiql.types.function.FunctionParameter
import org.partiql.types.function.FunctionSignature
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.PartiQLValueType.ANY
import org.partiql.value.PartiQLValueType.BOOL
import org.partiql.value.PartiQLValueType.CHAR
import org.partiql.value.PartiQLValueType.DATE
import org.partiql.value.PartiQLValueType.DECIMAL
import org.partiql.value.PartiQLValueType.INT
import org.partiql.value.PartiQLValueType.INT32
import org.partiql.value.PartiQLValueType.INT64
import org.partiql.value.PartiQLValueType.STRING
import org.partiql.value.PartiQLValueType.TIME
import org.partiql.value.PartiQLValueType.TIMESTAMP
import org.partiql.value.PartiQLValueType.*

/**
* A header which uses the PartiQL Lang Kotlin default standard library. All functions exist in a global namespace.
Expand Down Expand Up @@ -115,15 +105,15 @@ object PartiQLHeader : Header() {

// OPERATORS

private fun not(): List<FunctionSignature.Scalar> = listOf(unary("not", BOOL, BOOL))
private fun not(): List<FunctionSignature.Scalar> = listOf(unary("not", BOOL, BOOL), unary("not", ANY, ANY))

private fun pos(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
unary("pos", t, t)
}
} + listOf(unary("pos", ANY, ANY))

private fun neg(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
unary("neg", t, t)
}
} + listOf(unary("pos", ANY, ANY))

private fun eq(): List<FunctionSignature.Scalar> = types.all.map { t ->
FunctionSignature.Scalar(
Expand All @@ -137,55 +127,55 @@ object PartiQLHeader : Header() {

private fun ne(): List<FunctionSignature.Scalar> = types.all.map { t ->
binary("ne", BOOL, t, t)
}
} + listOf(binary("ne", ANY, ANY, ANY))

private fun and(): List<FunctionSignature.Scalar> = listOf(binary("and", BOOL, BOOL, BOOL))
private fun and(): List<FunctionSignature.Scalar> = listOf(binary("and", BOOL, BOOL, BOOL), binary("and", ANY, ANY, ANY))

private fun or(): List<FunctionSignature.Scalar> = listOf(binary("or", BOOL, BOOL, BOOL))
private fun or(): List<FunctionSignature.Scalar> = listOf(binary("or", BOOL, BOOL, BOOL), binary("or", ANY, ANY, ANY))

private fun lt(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("lt", BOOL, t, t)
}
} + listOf(binary("lt", ANY, ANY, ANY))

private fun lte(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("lte", BOOL, t, t)
}
} + listOf(binary("lte", ANY, ANY, ANY))

private fun gt(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("gt", BOOL, t, t)
}
} + listOf(binary("gt", ANY, ANY, ANY))

private fun gte(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("gte", BOOL, t, t)
}
} + listOf(binary("gte", ANY, ANY, ANY))

private fun plus(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("plus", t, t, t)
}
} + listOf(binary("plus", ANY, ANY, ANY))

private fun minus(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("minus", t, t, t)
}
} + listOf(binary("minus", ANY, ANY, ANY))

private fun times(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("times", t, t, t)
}
} + listOf(binary("times", ANY, ANY, ANY))

private fun div(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("divide", t, t, t)
}
} + listOf(binary("divide", ANY, ANY, ANY))

private fun mod(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
binary("modulo", t, t, t)
}
} + listOf(binary("modulo", ANY, ANY, ANY))

private fun concat(): List<FunctionSignature.Scalar> = types.text.map { t ->
binary("concat", t, t, t)
}
} + listOf(binary("concat", ANY, ANY, ANY))

private fun bitwiseAnd(): List<FunctionSignature.Scalar> = types.integer.map { t ->
binary("bitwise_and", t, t, t)
}
} + listOf(binary("bitwise_and", ANY, ANY, ANY))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are slightly different functions. The return type is still BOOL, but the functions are MISSABLE because we could have a mis-typing case.


// BUILT INTS

Expand Down Expand Up @@ -233,6 +223,27 @@ object PartiQLHeader : Header() {
isNullCall = true,
isNullable = false,
),
FunctionSignature.Scalar(
name = "like",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("pattern", ANY),
),
isNullCall = true,
isNullable = false,
),
FunctionSignature.Scalar(
name = "like_escape",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("pattern", ANY),
FunctionParameter("escape", ANY),
),
isNullCall = true,
isNullable = false,
),
)

private fun between(): List<FunctionSignature.Scalar> = types.numeric.map { t ->
Expand Down Expand Up @@ -326,8 +337,29 @@ object PartiQLHeader : Header() {
)
}

// TODO
private fun coalesce(): List<FunctionSignature.Scalar> = emptyList()
// COALESCE(expression, expression, ... )
// Initial implementation of Coalesce.
// As the number of parameter can not be pre-determined, we wrap those into a list
private fun coalesce(): List<FunctionSignature.Scalar> = listOf(
yliuuuu marked this conversation as resolved.
Show resolved Hide resolved
FunctionSignature.Scalar(
name = "coalesce",
returns = ANY,
parameters = listOf(
FunctionParameter("values", LIST)
),
isNullCall = false,
isNullable = true,
),
FunctionSignature.Scalar(
name = "coalesce",
returns = ANY,
parameters = listOf(
FunctionParameter("values", ANY)
),
isNullCall = false,
isNullable = true,
),
)

// NULLIF(x, y)
private fun nullIf(): List<FunctionSignature.Scalar> = types.nullable.map { t ->
Expand All @@ -341,7 +373,18 @@ object PartiQLHeader : Header() {
isNullCall = true,
isNullable = true,
)
}
} + listOf(
FunctionSignature.Scalar(
name = "null_if",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("nullifier", ANY),
),
isNullCall = true,
isNullable = true,
)
)

// SUBSTRING (expression, start[, length]?)
// SUBSTRINGG(expression from start [FOR length]? )
Expand Down Expand Up @@ -369,7 +412,29 @@ object PartiQLHeader : Header() {
isNullable = false,
)
)
}.flatten()
}.flatten() + listOf(
FunctionSignature.Scalar(
name = "substring",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("start", ANY),
),
isNullCall = true,
isNullable = false,
),
FunctionSignature.Scalar(
name = "substring",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("start", ANY),
FunctionParameter("end", ANY),
),
isNullCall = true,
isNullable = false,
)
)

// position (str1, str2)
// position (str1 in str2)
Expand All @@ -384,7 +449,18 @@ object PartiQLHeader : Header() {
isNullCall = true,
isNullable = false,
)
}
} + listOf(
FunctionSignature.Scalar(
name = "position",
returns = ANY,
parameters = listOf(
FunctionParameter("probe", ANY),
FunctionParameter("value", ANY),
),
isNullCall = true,
isNullable = false,
)
)

// trim(str)
private fun trim(): List<FunctionSignature.Scalar> = types.text.map { t ->
Expand All @@ -397,7 +473,17 @@ object PartiQLHeader : Header() {
isNullCall = true,
isNullable = false,
)
}
} + listOf(
FunctionSignature.Scalar(
name = "trim",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
),
isNullCall = true,
isNullable = false,
)
)

// TODO: We need to add a special form function for TRIM(BOTH FROM value)
private fun trimSpecial(): List<FunctionSignature.Scalar> = types.text.map { t ->
Expand Down Expand Up @@ -457,7 +543,62 @@ object PartiQLHeader : Header() {
isNullable = false,
),
)
}.flatten()
}.flatten() + listOf(
// TRIM(chars FROM value)
// TRIM(both chars from value)
FunctionSignature.Scalar(
name = "trim_chars",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("chars", ANY),
),
isNullCall = true,
isNullable = false,
),
// TRIM(LEADING FROM value)
FunctionSignature.Scalar(
name = "trim_leading",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
),
isNullCall = true,
isNullable = false,
),
// TRIM(LEADING chars FROM value)
FunctionSignature.Scalar(
name = "trim_leading_chars",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("chars", ANY),
),
isNullCall = true,
isNullable = false,
),
// TRIM(TRAILING FROM value)
FunctionSignature.Scalar(
name = "trim_trailing",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
),
isNullCall = true,
isNullable = false,
),
// TRIM(TRAILING chars FROM value)
FunctionSignature.Scalar(
name = "trim_trailing_chars",
returns = ANY,
parameters = listOf(
FunctionParameter("value", ANY),
FunctionParameter("chars", ANY),
),
isNullCall = true,
isNullable = false,
),
)

// TODO
private fun overlay(): List<FunctionSignature.Scalar> = emptyList()
Expand All @@ -467,8 +608,8 @@ object PartiQLHeader : Header() {

private fun dateAdd(): List<FunctionSignature.Scalar> {
val operators = mutableListOf<FunctionSignature.Scalar>()
for (type in types.datetime) {
for (field in DatetimeField.values()) {
for (field in DatetimeField.values()) {
for (type in types.datetime) {
if (field == DatetimeField.TIMEZONE_HOUR || field == DatetimeField.TIMEZONE_MINUTE) {
continue
}
Expand All @@ -484,14 +625,25 @@ object PartiQLHeader : Header() {
)
operators.add(signature)
}
val anySignature = FunctionSignature.Scalar(
name = "date_diff_${field.name.lowercase()}",
returns = ANY,
parameters = listOf(
FunctionParameter("datetime1", ANY),
FunctionParameter("datetime2", ANY),
),
isNullCall = true,
isNullable = false,
)
operators.add(anySignature)
}
return operators
}

private fun dateDiff(): List<FunctionSignature.Scalar> {
val operators = mutableListOf<FunctionSignature.Scalar>()
for (type in types.datetime) {
for (field in DatetimeField.values()) {
for (field in DatetimeField.values()) {
for (type in types.datetime) {
if (field == DatetimeField.TIMEZONE_HOUR || field == DatetimeField.TIMEZONE_MINUTE) {
continue
}
Expand All @@ -507,6 +659,17 @@ object PartiQLHeader : Header() {
)
operators.add(signature)
}
val anySignature = FunctionSignature.Scalar(
name = "date_diff_${field.name.lowercase()}",
returns = ANY,
parameters = listOf(
FunctionParameter("datetime1", ANY),
FunctionParameter("datetime2", ANY),
),
isNullCall = true,
isNullable = false,
)
operators.add(anySignature)
}
return operators
}
Expand Down
Loading