Skip to content

Commit

Permalink
JetBrains#623 Refactor window function definition
Browse files Browse the repository at this point in the history
Window function definitions moved from top-level to ISqlExpressionBuilder
to eliminate their irrelevant appearance in code completion.
  • Loading branch information
Dmitry Levin committed Jul 7, 2023
1 parent 9d37596 commit 17b658c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,68 +126,6 @@ inline fun <reified T : Any> ExpressionWithColumnType<*>.jsonExtract(vararg path
return JsonExtract(this, path = path, toScalar, this.columnType, columnType)
}

// Window Functions

/** Returns the number of the current row within its partition, counting from 1. */
fun rowNumber(): RowNumber = RowNumber()

/** Returns the rank of the current row, with gaps; that is, the row_number of the first row in its peer group. */
fun rank(): Rank = Rank()

/** Returns the rank of the current row, without gaps; this function effectively counts peer groups. */
fun denseRank(): DenseRank = DenseRank()

/**
* Returns the relative rank of the current row, that is (rank - 1) / (total partition rows - 1).
* The value thus ranges from 0 to 1 inclusive.
*/
fun percentRank(): PercentRank = PercentRank()

/**
* Returns the cumulative distribution, that is (number of partition rows preceding or peers with current row) /
* (total partition rows). The value thus ranges from 1/N to 1.
*/
fun cumeDist(): CumeDist = CumeDist()

/** Returns an integer ranging from 1 to the [numBuckets], dividing the partition as equally as possible. */
fun ntile(numBuckets: ExpressionWithColumnType<Int>): Ntile = Ntile(numBuckets)

/**
* Returns value evaluated at the row that is [offset] rows before the current row within the partition;
* if there is no such row, instead returns [defaultValue].
* Both [offset] and [defaultValue] are evaluated with respect to the current row.
*/
fun <T> ExpressionWithColumnType<T>.lag(
offset: ExpressionWithColumnType<Int> = intLiteral(1),
defaultValue: ExpressionWithColumnType<T>? = null
): Lag<T> = Lag(this, offset, defaultValue)

/**
* Returns value evaluated at the row that is [offset] rows after the current row within the partition;
* if there is no such row, instead returns [defaultValue].
* Both [offset] and [defaultValue] are evaluated with respect to the current row.
*/
fun <T> ExpressionWithColumnType<T>.lead(
offset: ExpressionWithColumnType<Int> = intLiteral(1),
defaultValue: ExpressionWithColumnType<T>? = null
): Lead<T> = Lead(this, offset, defaultValue)

/**
* Returns value evaluated at the row that is the first row of the window frame.
*/
fun <T> ExpressionWithColumnType<T>.firstValue(): FirstValue<T> = FirstValue(this)

/**
* Returns value evaluated at the row that is the last row of the window frame.
*/
fun <T> ExpressionWithColumnType<T>.lastValue(): LastValue<T> = LastValue(this)

/**
* Returns value evaluated at the row that is the [n]'th row of the window frame
* (counting from 1); null if no such row.
*/
fun <T> ExpressionWithColumnType<T>.nthValue(n: ExpressionWithColumnType<Int>): NthValue<T> = NthValue(this, n)

// Sequence Manipulation Functions

/** Advances this sequence and returns the new value. */
Expand Down Expand Up @@ -588,6 +526,68 @@ interface ISqlExpressionBuilder {
caseSensitive: Boolean = true
): RegexpOp<T> = RegexpOp(this, pattern, caseSensitive)

// Window Functions

/** Returns the number of the current row within its partition, counting from 1. */
fun rowNumber(): RowNumber = RowNumber()

/** Returns the rank of the current row, with gaps; that is, the row_number of the first row in its peer group. */
fun rank(): Rank = Rank()

/** Returns the rank of the current row, without gaps; this function effectively counts peer groups. */
fun denseRank(): DenseRank = DenseRank()

/**
* Returns the relative rank of the current row, that is (rank - 1) / (total partition rows - 1).
* The value thus ranges from 0 to 1 inclusive.
*/
fun percentRank(): PercentRank = PercentRank()

/**
* Returns the cumulative distribution, that is (number of partition rows preceding or peers with current row) /
* (total partition rows). The value thus ranges from 1/N to 1.
*/
fun cumeDist(): CumeDist = CumeDist()

/** Returns an integer ranging from 1 to the [numBuckets], dividing the partition as equally as possible. */
fun ntile(numBuckets: ExpressionWithColumnType<Int>): Ntile = Ntile(numBuckets)

/**
* Returns value evaluated at the row that is [offset] rows before the current row within the partition;
* if there is no such row, instead returns [defaultValue].
* Both [offset] and [defaultValue] are evaluated with respect to the current row.
*/
fun <T> ExpressionWithColumnType<T>.lag(
offset: ExpressionWithColumnType<Int> = intLiteral(1),
defaultValue: ExpressionWithColumnType<T>? = null
): Lag<T> = Lag(this, offset, defaultValue)

/**
* Returns value evaluated at the row that is [offset] rows after the current row within the partition;
* if there is no such row, instead returns [defaultValue].
* Both [offset] and [defaultValue] are evaluated with respect to the current row.
*/
fun <T> ExpressionWithColumnType<T>.lead(
offset: ExpressionWithColumnType<Int> = intLiteral(1),
defaultValue: ExpressionWithColumnType<T>? = null
): Lead<T> = Lead(this, offset, defaultValue)

/**
* Returns value evaluated at the row that is the first row of the window frame.
*/
fun <T> ExpressionWithColumnType<T>.firstValue(): FirstValue<T> = FirstValue(this)

/**
* Returns value evaluated at the row that is the last row of the window frame.
*/
fun <T> ExpressionWithColumnType<T>.lastValue(): LastValue<T> = LastValue(this)

/**
* Returns value evaluated at the row that is the [n]'th row of the window frame
* (counting from 1); null if no such row.
*/
fun <T> ExpressionWithColumnType<T>.nthValue(n: ExpressionWithColumnType<Int>): NthValue<T> = NthValue(this, n)

// JSON Conditions

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package org.jetbrains.exposed.sql.tests.shared.functions

import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.cumeDist
import org.jetbrains.exposed.sql.SqlExpressionBuilder.denseRank
import org.jetbrains.exposed.sql.SqlExpressionBuilder.firstValue
import org.jetbrains.exposed.sql.SqlExpressionBuilder.lag
import org.jetbrains.exposed.sql.SqlExpressionBuilder.lastValue
import org.jetbrains.exposed.sql.SqlExpressionBuilder.lead
import org.jetbrains.exposed.sql.SqlExpressionBuilder.minus
import org.jetbrains.exposed.sql.SqlExpressionBuilder.nthValue
import org.jetbrains.exposed.sql.SqlExpressionBuilder.ntile
import org.jetbrains.exposed.sql.SqlExpressionBuilder.percentRank
import org.jetbrains.exposed.sql.SqlExpressionBuilder.plus
import org.jetbrains.exposed.sql.SqlExpressionBuilder.rank
import org.jetbrains.exposed.sql.SqlExpressionBuilder.rowNumber
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB.*
import org.jetbrains.exposed.sql.tests.TestDB.Companion.allH2TestDB
Expand Down

0 comments on commit 17b658c

Please sign in to comment.