-
Notifications
You must be signed in to change notification settings - Fork 496
/
ExprUtil.kt
280 lines (252 loc) · 9.78 KB
/
ExprUtil.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (C) 2017 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.sqldelight.core.lang.util
import app.cash.sqldelight.core.compiler.SqlDelightCompiler.allocateName
import app.cash.sqldelight.core.compiler.model.PragmaWithResults
import app.cash.sqldelight.core.compiler.model.SelectQueryable
import app.cash.sqldelight.core.lang.types.typeResolver
import app.cash.sqldelight.dialect.api.IntermediateType
import app.cash.sqldelight.dialect.api.PrimitiveType
import app.cash.sqldelight.dialect.api.PrimitiveType.ARGUMENT
import app.cash.sqldelight.dialect.api.PrimitiveType.BLOB
import app.cash.sqldelight.dialect.api.PrimitiveType.INTEGER
import app.cash.sqldelight.dialect.api.PrimitiveType.NULL
import app.cash.sqldelight.dialect.api.PrimitiveType.REAL
import app.cash.sqldelight.dialect.api.PrimitiveType.TEXT
import app.cash.sqldelight.dialect.api.QueryWithResults
import app.cash.sqldelight.dialect.api.TypeResolver
import app.cash.sqldelight.dialect.api.encapsulatingType
import com.alecstrong.sql.psi.core.psi.SqlBetweenExpr
import com.alecstrong.sql.psi.core.psi.SqlBinaryAddExpr
import com.alecstrong.sql.psi.core.psi.SqlBinaryExpr
import com.alecstrong.sql.psi.core.psi.SqlBinaryLikeExpr
import com.alecstrong.sql.psi.core.psi.SqlBinaryMultExpr
import com.alecstrong.sql.psi.core.psi.SqlBinaryPipeExpr
import com.alecstrong.sql.psi.core.psi.SqlBindExpr
import com.alecstrong.sql.psi.core.psi.SqlCaseExpr
import com.alecstrong.sql.psi.core.psi.SqlCastExpr
import com.alecstrong.sql.psi.core.psi.SqlCollateExpr
import com.alecstrong.sql.psi.core.psi.SqlColumnExpr
import com.alecstrong.sql.psi.core.psi.SqlExistsExpr
import com.alecstrong.sql.psi.core.psi.SqlExpr
import com.alecstrong.sql.psi.core.psi.SqlFunctionExpr
import com.alecstrong.sql.psi.core.psi.SqlInExpr
import com.alecstrong.sql.psi.core.psi.SqlIsExpr
import com.alecstrong.sql.psi.core.psi.SqlLiteralExpr
import com.alecstrong.sql.psi.core.psi.SqlNullExpr
import com.alecstrong.sql.psi.core.psi.SqlOtherExpr
import com.alecstrong.sql.psi.core.psi.SqlParenExpr
import com.alecstrong.sql.psi.core.psi.SqlRaiseExpr
import com.alecstrong.sql.psi.core.psi.SqlSetterExpression
import com.alecstrong.sql.psi.core.psi.SqlStmt
import com.alecstrong.sql.psi.core.psi.SqlTypeName
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.alecstrong.sql.psi.core.psi.SqlUnaryExpr
import com.intellij.psi.PsiElement
import com.intellij.psi.tree.TokenSet
internal val SqlExpr.name: String get() = when (this) {
is SqlCastExpr -> expr.name
is SqlParenExpr -> expr?.name ?: "value"
is SqlFunctionExpr -> functionName.text
is SqlColumnExpr -> allocateName(columnName)
else -> "expr"
}
internal object AnsiSqlTypeResolver : TypeResolver {
override fun resolvedType(expr: SqlExpr): IntermediateType {
return expr.ansiType()
}
override fun functionType(functionExpr: SqlFunctionExpr): IntermediateType? {
return functionExpr.typeReturned()
}
override fun definitionType(typeName: SqlTypeName) =
throw UnsupportedOperationException("ANSI SQL is not supported for being used as a dialect.")
override fun argumentType(
parent: PsiElement,
argument: SqlExpr,
): IntermediateType {
return when (parent) {
is SqlExpr -> parent.argumentType(argument)
is SqlSetterExpression -> parent.argumentType()
else -> throw IllegalStateException("Cannot infer argument type for $parent")
}
}
private fun SqlFunctionExpr.typeReturned() = when (functionName.text.lowercase()) {
"round" -> {
// Single arg round function returns an int. Otherwise real.
if (exprList.size == 1) {
IntermediateType(INTEGER).nullableIf(exprList[0].type().javaType.isNullable)
} else {
IntermediateType(REAL).nullableIf(exprList.any { it.type().javaType.isNullable })
}
}
/**
* sum's output is always nullable because it returns NULL for an input that's empty or only contains NULLs.
*
* https://www.sqlite.org/lang_aggfunc.html#sumunc
* >>> The result of sum() is an integer value if all non-NULL inputs are integers. If any input to sum() is neither
* >>> an integer or a NULL then sum() returns a floating point value which might be an approximation to the true sum.
*
*/
"sum" -> {
val type = exprList[0].type()
if (type.dialectType == INTEGER && !type.javaType.isNullable) {
type.asNullable()
} else {
IntermediateType(REAL).asNullable()
}
}
"lower", "ltrim", "replace", "rtrim", "substr", "trim", "upper", "group_concat" -> {
IntermediateType(TEXT).nullableIf(exprList[0].type().javaType.isNullable)
}
"date", "time", "char", "hex", "quote", "soundex", "typeof" -> {
IntermediateType(TEXT)
}
"random", "count" -> {
IntermediateType(INTEGER)
}
"instr", "length" -> {
IntermediateType(INTEGER).nullableIf(exprList.any { it.type().javaType.isNullable })
}
"avg" -> IntermediateType(REAL).asNullable()
"abs" -> encapsulatingType(exprList, INTEGER, REAL)
"iif" -> exprList[1].type()
"coalesce", "ifnull" -> encapsulatingType(exprList, INTEGER, REAL, TEXT, BLOB)
"nullif" -> exprList[0].type().asNullable()
"max" -> encapsulatingType(exprList, INTEGER, REAL, TEXT, BLOB).asNullable()
"min" -> encapsulatingType(exprList, BLOB, TEXT, INTEGER, REAL).asNullable()
else -> null
}
override fun simplifyType(intermediateType: IntermediateType): IntermediateType {
with(intermediateType) {
if (javaType != dialectType.javaType &&
javaType.copy(nullable = false, annotations = emptyList()) == dialectType.javaType
) {
// We don't need an adapter for only annotations.
return copy(simplified = true)
}
}
return intermediateType
}
override fun queryWithResults(sqlStmt: SqlStmt): QueryWithResults? {
sqlStmt.compoundSelectStmt?.let { return SelectQueryable(it) }
sqlStmt.pragmaStmt?.let {
if (it.pragmaValue == null) return PragmaWithResults(it)
}
return null
}
}
internal fun TypeResolver.argumentType(bindArg: SqlBindExpr): IntermediateType {
return bindArg.inferredType().copy(bindArg = bindArg)
}
private fun SqlExpr.type(): IntermediateType {
return typeResolver.resolvedType(this)
}
/**
* If javaType is true, this will return a possible more descriptive type for column expressions.
*
* Order of operations:
* expr ::= ( raise_expr
* | case_expr
* | exists_expr
* | in_expr
* | between_expr
* | is_expr
* | null_expr
* | like_expr
* | collate_expr
* | cast_expr
* | paren_expr
* | function_expr
* | binary_expr
* | unary_expr
* | bind_expr
* | literal_expr
* | column_expr )
*/
private fun SqlExpr.ansiType(): IntermediateType = when (this) {
is SqlRaiseExpr -> IntermediateType(NULL)
is SqlCaseExpr -> childOfType(SqlTypes.THEN)!!.nextSiblingOfType<SqlExpr>().type()
is SqlExistsExpr -> {
val isExists = childOfType(SqlTypes.EXISTS) != null
if (isExists) {
IntermediateType(PrimitiveType.BOOLEAN)
} else {
compoundSelectStmt.queryExposed().single().columns.single().element.type()
}
}
is SqlInExpr -> IntermediateType(PrimitiveType.BOOLEAN)
is SqlBetweenExpr -> IntermediateType(PrimitiveType.BOOLEAN)
is SqlIsExpr -> IntermediateType(PrimitiveType.BOOLEAN)
is SqlNullExpr -> IntermediateType(PrimitiveType.BOOLEAN)
is SqlBinaryLikeExpr -> IntermediateType(PrimitiveType.BOOLEAN)
is SqlCollateExpr -> expr.type()
is SqlCastExpr -> typeName.type().nullableIf(expr.type().javaType.isNullable)
is SqlParenExpr -> expr?.type() ?: IntermediateType(NULL)
is SqlFunctionExpr -> typeResolver.functionType(this) ?: IntermediateType(NULL)
is SqlBinaryExpr -> {
if (childOfType(
TokenSet.create(
SqlTypes.EQ, SqlTypes.EQ2, SqlTypes.NEQ,
SqlTypes.NEQ2, SqlTypes.AND, SqlTypes.OR, SqlTypes.GT, SqlTypes.GTE,
SqlTypes.LT, SqlTypes.LTE,
),
) != null
) {
IntermediateType(PrimitiveType.BOOLEAN)
} else {
typeResolver.encapsulatingType(
exprList = getExprList(),
nullableIfAny = (
this is SqlBinaryAddExpr || this is SqlBinaryMultExpr ||
this is SqlBinaryPipeExpr
),
INTEGER,
REAL,
TEXT,
BLOB,
)
}
}
is SqlUnaryExpr -> expr.type()
is SqlBindExpr -> IntermediateType(ARGUMENT)
is SqlLiteralExpr -> when {
(literalValue.stringLiteral != null) -> IntermediateType(TEXT)
(literalValue.blobLiteral != null) -> IntermediateType(BLOB)
(literalValue.numericLiteral != null) -> {
if (literalValue.text.contains('.')) {
IntermediateType(REAL)
} else {
IntermediateType(INTEGER)
}
}
(
literalValue.childOfType(
TokenSet.create(
SqlTypes.CURRENT_TIMESTAMP,
SqlTypes.CURRENT_TIME,
SqlTypes.CURRENT_DATE,
),
) != null
) -> IntermediateType(TEXT)
(literalValue.childOfType(SqlTypes.NULL) != null) -> IntermediateType(NULL)
else -> IntermediateType(BLOB).asNullable()
}
is SqlColumnExpr -> columnName.type()
is SqlOtherExpr -> {
extensionExpr.type()
}
else -> throw IllegalStateException("Unknown expression type $this")
}