Skip to content

Commit

Permalink
Merge branch 'master' into renovate/androidx.test-runner-1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecKazakova committed Sep 5, 2024
2 parents 59ac172 + 10f15cb commit ca045b7
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
INTEGER,
BIG_INT,
REAL,
PostgreSqlType.NUMERIC,
TEXT,
BLOB,
TIMESTAMP_TIMEZONE,
Expand All @@ -113,6 +114,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
PostgreSqlType.INTEGER,
BIG_INT,
REAL,
PostgreSqlType.NUMERIC,
TIMESTAMP_TIMEZONE,
TIMESTAMP,
)
Expand All @@ -124,19 +126,19 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
if (isArrayType(exprType)) {
exprType
} else {
encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB, nullability = { exprListNullability ->
encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, PostgreSqlType.NUMERIC, TEXT, BLOB, nullability = { exprListNullability ->
exprListNullability.all { it }
})
}
}
"max" -> encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
"min" -> encapsulatingTypePreferringKotlin(exprList, BLOB, TEXT, SMALL_INT, INTEGER, PostgreSqlType.INTEGER, BIG_INT, REAL, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
"max" -> encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, PostgreSqlType.NUMERIC, TEXT, BLOB, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
"min" -> encapsulatingTypePreferringKotlin(exprList, BLOB, TEXT, SMALL_INT, INTEGER, PostgreSqlType.INTEGER, BIG_INT, REAL, PostgreSqlType.NUMERIC, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
"sum" -> {
val type = resolvedType(exprList.single())
if (type.dialectType == REAL) {
IntermediateType(REAL).asNullable()
} else {
IntermediateType(INTEGER).asNullable()
when (type.dialectType) {
REAL -> IntermediateType(REAL).asNullable()
PostgreSqlType.NUMERIC -> IntermediateType(PostgreSqlType.NUMERIC).asNullable()
else -> IntermediateType(INTEGER).asNullable()
}
}
"to_hex", "quote_literal", "quote_ident", "md5" -> IntermediateType(TEXT)
Expand Down Expand Up @@ -176,14 +178,14 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
"json_build_object", "jsonb_build_object",
-> IntermediateType(TEXT)
"array_agg" -> {
val typeForAgg = encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
val typeForAgg = encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, PostgreSqlType.NUMERIC, TEXT, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
arrayIntermediateType(typeForAgg)
}
"string_agg" -> IntermediateType(TEXT)
"json_array_length", "jsonb_array_length" -> IntermediateType(INTEGER)
"jsonb_path_exists", "jsonb_path_match", "jsonb_path_exists_tz", "jsonb_path_match_tz" -> IntermediateType(BOOLEAN)
"currval", "lastval", "nextval", "setval" -> IntermediateType(BIG_INT)
"generate_series" -> encapsulatingType(exprList, INTEGER, BIG_INT, REAL, TIMESTAMP_TIMEZONE, TIMESTAMP)
"generate_series" -> encapsulatingType(exprList, INTEGER, BIG_INT, REAL, PostgreSqlType.NUMERIC, TIMESTAMP_TIMEZONE, TIMESTAMP)
"regexp_count", "regexp_instr" -> IntermediateType(INTEGER)
"regexp_like" -> IntermediateType(BOOLEAN)
"regexp_replace", "regexp_substr" -> IntermediateType(TEXT)
Expand All @@ -193,7 +195,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
"websearch_to_tsquery" -> IntermediateType(TEXT)
"rank", "dense_rank", "row_number" -> IntermediateType(INTEGER)
"ntile" -> IntermediateType(INTEGER).asNullable()
"lag", "lead", "first_value", "last_value", "nth_value" -> encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
"lag", "lead", "first_value", "last_value", "nth_value" -> encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, PostgreSqlType.NUMERIC, TEXT, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
else -> null
}

Expand Down Expand Up @@ -265,6 +267,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
INTEGER,
BIG_INT,
REAL,
PostgreSqlType.NUMERIC,
TEXT,
BLOB,
BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.cash.sqldelight.dialects.sqlite_3_25

import app.cash.sqldelight.dialect.api.MigrationSquasher
import app.cash.sqldelight.dialect.api.TypeResolver
import app.cash.sqldelight.dialects.sqlite_3_24.SqliteDialect as Sqlite324Dialect
import app.cash.sqldelight.dialects.sqlite_3_25.grammar.SqliteParserUtil

Expand All @@ -11,6 +12,10 @@ open class SqliteDialect : Sqlite324Dialect() {
SqliteParserUtil.overrideSqlParser()
}

override fun typeResolver(parentResolver: TypeResolver): TypeResolver {
return SqliteTypeResolver(parentResolver)
}

override fun migrationSquasher(parentSquasher: MigrationSquasher): MigrationSquasher {
return SqliteMigrationSquasher(super.migrationSquasher(parentSquasher))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package app.cash.sqldelight.dialects.sqlite_3_25

import app.cash.sqldelight.dialect.api.IntermediateType
import app.cash.sqldelight.dialect.api.PrimitiveType.INTEGER
import app.cash.sqldelight.dialect.api.PrimitiveType.REAL
import app.cash.sqldelight.dialect.api.PrimitiveType.TEXT
import app.cash.sqldelight.dialect.api.TypeResolver
import app.cash.sqldelight.dialect.api.encapsulatingTypePreferringKotlin
import app.cash.sqldelight.dialects.sqlite_3_24.SqliteTypeResolver as Sqlite324TypeResolver
import app.cash.sqldelight.dialects.sqlite_3_25.grammar.psi.SqliteExtensionExpr
import com.alecstrong.sql.psi.core.psi.SqlExpr
import com.alecstrong.sql.psi.core.psi.SqlFunctionExpr

open class SqliteTypeResolver(private val parentResolver: TypeResolver) : Sqlite324TypeResolver(parentResolver) {

override fun resolvedType(expr: SqlExpr): IntermediateType = when (expr) {
is SqliteExtensionExpr -> {
functionType(expr.windowFunctionExpr)!! // currently this is the only sqlite extension expr in 3_25
}
else -> super.resolvedType(expr)
}

override fun functionType(functionExpr: SqlFunctionExpr): IntermediateType? {
return functionExpr.sqliteFunctionType() ?: parentResolver.functionType(functionExpr)
}

private fun SqlFunctionExpr.sqliteFunctionType() = when (functionName.text.lowercase()) {
"dense_rank", "ntile", "rank", "row_number" -> IntermediateType(INTEGER)
"cume_dist", "percent_rank" -> IntermediateType(REAL)
"lag", "lead", "first_value", "last_value", "nth_value", "group_concat" -> encapsulatingTypePreferringKotlin(exprList, INTEGER, REAL, TEXT).asNullable()
else -> null
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins

import app.cash.sqldelight.dialects.sqlite_3_25.grammar.psi.SqliteWindowFunctionExpr
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.alecstrong.sql.psi.core.psi.SqlExpr
import com.alecstrong.sql.psi.core.psi.SqlFunctionExpr
import com.alecstrong.sql.psi.core.psi.SqlFunctionName
import com.intellij.lang.ASTNode

internal abstract class SqliteWindowFunctionMixin(
node: ASTNode,
) : SqlCompositeElementImpl(node),
SqliteWindowFunctionExpr,
SqlFunctionExpr {
override fun getExprList(): List<SqlExpr> {
return children.filterIsInstance<SqlFunctionExpr>()
}

override fun getFunctionName(): SqlFunctionName {
return exprList.first().children.filterIsInstance<SqlFunctionName>().single()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"static com.alecstrong.sql.psi.core.psi.SqlTypes.WINDOW"
]
}
overrides ::= alter_table_rules | result_column | select_stmt
overrides ::= alter_table_rules | extension_expr | select_stmt

alter_table_rules ::= (
{alter_table_add_column}
Expand All @@ -47,18 +47,12 @@ alter_table_rules ::= (
implements = "com.alecstrong.sql.psi.core.psi.SqlAlterTableRules"
override = true
}
result_column ::= ( MULTIPLY
| {table_name} DOT MULTIPLY
| (window_function_invocation | <<expr '-1'>>) [ [ AS ] {column_alias} ] ) {
mixin = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins.ResultColumnMixin"
implements = "com.alecstrong.sql.psi.core.psi.SqlResultColumn"
override = true
}

select_stmt ::= SELECT [ DISTINCT | ALL ] {result_column} ( COMMA {result_column} ) * [ FROM {join_clause} ] [ WHERE <<expr '-1'>> ] [{group_by}] [ HAVING <<expr '-1'>> ] [ WINDOW window_name AS window_defn ( COMMA window_name AS window_defn ) * ] | VALUES {values_expression} ( COMMA {values_expression} ) * {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlSelectStmtImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlSelectStmt"
override = true
pin = 1
pin = 2
}

alter_table_rename_column ::= RENAME [ COLUMN ] {column_name} TO alter_table_column_alias {
Expand All @@ -76,9 +70,15 @@ alter_table_column_alias ::= id | string {
]
}

window_function_invocation ::=
window_func LP [ MULTIPLY | ( <<expr '-1'>> ( COMMA <<expr '-1'>> ) * ) ] RP [ 'FILTER' LP WHERE <<expr '-1'>> RP] 'OVER' ( window_defn | window_name ) {
pin = 6
extension_expr ::= window_function_expr {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlExtensionExprImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlExtensionExpr"
override = true
}

window_function_expr ::= {function_expr} [ 'FILTER' LP WHERE <<expr '-1'>> RP] 'OVER' ( window_defn | window_name ) {
mixin = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins.SqliteWindowFunctionMixin"
implements = "com.alecstrong.sql.psi.core.psi.SqlFunctionExpr"
}

window_defn ::= LP [ base_window_name ]
Expand All @@ -87,6 +87,7 @@ window_defn ::= LP [ base_window_name ]
[ frame_spec ]
RP {
mixin = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins.SqliteWindowDefinitionMixin"
pin = 1
}

frame_spec ::= ( 'RANGE' | 'ROWS' | 'GROUPS' )
Expand All @@ -109,6 +110,5 @@ frame_spec ::= ( 'RANGE' | 'ROWS' | 'GROUPS' )
pin = 1
}

window_func ::= id
window_name ::= id
base_window_name ::= id
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE numbers(
value INTEGER NOT NULL
);

SELECT value
FROM (
SELECT
value,
CASE
WHEN ((row_number() OVER(ORDER BY value ASC) - 1) % :limit) = 0 THEN 1
WHEN value = :anchor THEN 1
ELSE 0
END page_boundary
FROM numbers
ORDER BY value ASC
)
WHERE page_boundary = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package app.cash.sqldelight.dialects.sqlite_3_35
import app.cash.sqldelight.dialect.api.QueryWithResults
import app.cash.sqldelight.dialect.api.ReturningQueryable
import app.cash.sqldelight.dialect.api.TypeResolver
import app.cash.sqldelight.dialects.sqlite_3_24.SqliteTypeResolver as Sqlite324TypeResolver
import app.cash.sqldelight.dialects.sqlite_3_25.SqliteTypeResolver as Sqlite325TypeResolver
import app.cash.sqldelight.dialects.sqlite_3_35.grammar.psi.SqliteDeleteStmtLimited
import app.cash.sqldelight.dialects.sqlite_3_35.grammar.psi.SqliteInsertStmt
import app.cash.sqldelight.dialects.sqlite_3_35.grammar.psi.SqliteUpdateStmtLimited
import com.alecstrong.sql.psi.core.psi.SqlStmt

class SqliteTypeResolver(private val parentResolver: TypeResolver) : Sqlite324TypeResolver(parentResolver) {
class SqliteTypeResolver(private val parentResolver: TypeResolver) : Sqlite325TypeResolver(parentResolver) {
override fun queryWithResults(sqlStmt: SqlStmt): QueryWithResults? {
sqlStmt.insertStmt?.let { insert ->
check(insert is SqliteInsertStmt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
extends="com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl"
psiClassPrefix = "Sqlite"

parserImports=[]
parserImports=[
"static app.cash.sqldelight.dialects.sqlite_3_25.grammar.SqliteParser.window_function_expr_real"
"static app.cash.sqldelight.dialects.sqlite_3_25.grammar.SqliteParserUtil.windowFunctionExprExt"
]
}

overrides ::= extension_expr

extension_expr ::= json_expression {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlExtensionExprImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlExtensionExpr"
extension_expr ::= sqlite_3_25_window_function_expr | json_expression {
extends = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.psi.impl.SqliteExtensionExprImpl"
implements = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.psi.SqliteExtensionExpr"
override = true
}

Expand All @@ -22,3 +26,5 @@ json_expression ::= {column_expr} json_binary_operator <<expr '-1'>> {
pin = 2
}
json_binary_operator ::= '->' | '->>'

private sqlite_3_25_window_function_expr ::= <<windowFunctionExprExt <<window_function_expr_real>>>>
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ stately-concurrency = { module = "co.touchlab:stately-concurrency", version = "2

testhelp = { module = "co.touchlab:testhelp", version = "0.6.12" }
burst = { module = "com.squareup.burst:burst-junit4", version = "1.2.0" }
testParameterInjector = { module = "com.google.testparameterinjector:test-parameter-injector", version = "1.16" }
testParameterInjector = { module = "com.google.testparameterinjector:test-parameter-injector", version = "1.17" }

r2dbc = { module = "io.r2dbc:r2dbc-spi", version = "1.0.0.RELEASE" }
testContainers-mysql = { module = "org.testcontainers:mysql", version.ref = "testContainers" }
Expand Down
Loading

0 comments on commit ca045b7

Please sign in to comment.