@@ -3,40 +3,14 @@ package org.partiql.planner.internal.typer
3
3
import org.partiql.planner.internal.ir.Agg
4
4
import org.partiql.planner.internal.ir.Fn
5
5
import org.partiql.planner.internal.ir.Identifier
6
- import org.partiql.plugin.PartiQLFunctions
6
+ import org.partiql.planner.internal.typer.FnBuiltins.pAggs
7
+ import org.partiql.planner.internal.typer.FnBuiltins.pCasts
8
+ import org.partiql.planner.internal.typer.FnBuiltins.pFns
9
+ import org.partiql.planner.internal.typer.FnBuiltins.pOps
7
10
import org.partiql.spi.connector.ConnectorFunctions
8
- import org.partiql.types.function.FunctionParameter
9
11
import org.partiql.types.function.FunctionSignature
10
12
import org.partiql.value.PartiQLValueExperimental
11
13
import org.partiql.value.PartiQLValueType
12
- import org.partiql.value.PartiQLValueType.ANY
13
- import org.partiql.value.PartiQLValueType.BAG
14
- import org.partiql.value.PartiQLValueType.BINARY
15
- import org.partiql.value.PartiQLValueType.BLOB
16
- import org.partiql.value.PartiQLValueType.BOOL
17
- import org.partiql.value.PartiQLValueType.BYTE
18
- import org.partiql.value.PartiQLValueType.CHAR
19
- import org.partiql.value.PartiQLValueType.CLOB
20
- import org.partiql.value.PartiQLValueType.DATE
21
- import org.partiql.value.PartiQLValueType.DECIMAL
22
- import org.partiql.value.PartiQLValueType.DECIMAL_ARBITRARY
23
- import org.partiql.value.PartiQLValueType.FLOAT32
24
- import org.partiql.value.PartiQLValueType.FLOAT64
25
- import org.partiql.value.PartiQLValueType.INT
26
- import org.partiql.value.PartiQLValueType.INT16
27
- import org.partiql.value.PartiQLValueType.INT32
28
- import org.partiql.value.PartiQLValueType.INT64
29
- import org.partiql.value.PartiQLValueType.INT8
30
- import org.partiql.value.PartiQLValueType.INTERVAL
31
- import org.partiql.value.PartiQLValueType.LIST
32
- import org.partiql.value.PartiQLValueType.MISSING
33
- import org.partiql.value.PartiQLValueType.NULL
34
- import org.partiql.value.PartiQLValueType.SEXP
35
- import org.partiql.value.PartiQLValueType.STRING
36
- import org.partiql.value.PartiQLValueType.STRUCT
37
- import org.partiql.value.PartiQLValueType.SYMBOL
38
- import org.partiql.value.PartiQLValueType.TIME
39
- import org.partiql.value.PartiQLValueType.TIMESTAMP
40
14
41
15
/* *
42
16
* Function signature lookup by name.
@@ -105,7 +79,8 @@ internal class FnRegistry(private val metadata: Collection<ConnectorFunctions>)
105
79
internal fun lookupCoercion (operand : PartiQLValueType , target : PartiQLValueType ): FunctionSignature .Scalar ? {
106
80
val i = operand.ordinal
107
81
val j = target.ordinal
108
- return pCasts.graph[i][j]?.castFn
82
+ val rel = pCasts.graph[i][j] ? : return null
83
+ return if (rel.castType == CastType .COERCION ) rel.castFn else null
109
84
}
110
85
111
86
internal fun isUnsafeCast (specific : String ): Boolean = pCasts.unsafeCastSet.contains(specific)
@@ -117,107 +92,4 @@ internal class FnRegistry(private val metadata: Collection<ConnectorFunctions>)
117
92
is Identifier .Qualified -> throw IllegalArgumentException (" Qualified function identifiers not supported" )
118
93
is Identifier .Symbol -> identifier.symbol.lowercase()
119
94
}
120
-
121
- companion object {
122
-
123
- /* *
124
- * Static PartiQL casts information.
125
- */
126
- @JvmStatic
127
- val pCasts = TypeCasts .partiql()
128
-
129
- /* *
130
- * Static PartiQL function signatures, don't recompute.
131
- */
132
- @JvmStatic
133
- val pFns = (PartiQLFunctions .functions + pCasts.relationships().map { it.castFn }).toFnMap()
134
-
135
- /* *
136
- * Static PartiQL operator signatures, don't recompute.
137
- */
138
- @JvmStatic
139
- val pOps = PartiQLFunctions .operators.toFnMap()
140
-
141
- /* *
142
- * Static PartiQL aggregation signatures, don't recompute.
143
- */
144
- @JvmStatic
145
- val pAggs = PartiQLFunctions .aggregations.toFnMap()
146
-
147
- /* *
148
- * Group all function implementations by their name, sorting by precedence.
149
- */
150
- fun <T : FunctionSignature > List<T>.toFnMap (): FnMap <T > = this
151
- .distinctBy { it.specific }
152
- .sortedWith(fnPrecedence)
153
- .groupBy { it.name }
154
-
155
- // ====================================
156
- // SORTING
157
- // ====================================
158
-
159
- // Function precedence comparator
160
- // 1. Fewest args first
161
- // 2. Parameters are compared left-to-right
162
- @JvmStatic
163
- private val fnPrecedence = Comparator <FunctionSignature > { fn1, fn2 ->
164
- // Compare number of arguments
165
- if (fn1.parameters.size != fn2.parameters.size) {
166
- return @Comparator fn1.parameters.size - fn2.parameters.size
167
- }
168
- // Compare operand type precedence
169
- for (i in fn1.parameters.indices) {
170
- val p1 = fn1.parameters[i]
171
- val p2 = fn2.parameters[i]
172
- val comparison = p1.compareTo(p2)
173
- if (comparison != 0 ) return @Comparator comparison
174
- }
175
- // unreachable?
176
- 0
177
- }
178
-
179
- private fun FunctionParameter.compareTo (other : FunctionParameter ): Int =
180
- comparePrecedence(this .type, other.type)
181
-
182
- private fun comparePrecedence (t1 : PartiQLValueType , t2 : PartiQLValueType ): Int {
183
- if (t1 == t2) return 0
184
- val p1 = precedence[t1]!!
185
- val p2 = precedence[t2]!!
186
- return p1 - p2
187
- }
188
-
189
- // This simply describes some precedence for ordering functions.
190
- // This is not explicitly defined in the PartiQL Specification!!
191
- // This does not imply the ability to CAST; this defines function resolution behavior.
192
- private val precedence: Map <PartiQLValueType , Int > = listOf (
193
- NULL ,
194
- MISSING ,
195
- BOOL ,
196
- INT8 ,
197
- INT16 ,
198
- INT32 ,
199
- INT64 ,
200
- INT ,
201
- DECIMAL ,
202
- FLOAT32 ,
203
- FLOAT64 ,
204
- DECIMAL_ARBITRARY , // Arbitrary precision decimal has a higher precedence than FLOAT
205
- CHAR ,
206
- STRING ,
207
- CLOB ,
208
- SYMBOL ,
209
- BINARY ,
210
- BYTE ,
211
- BLOB ,
212
- DATE ,
213
- TIME ,
214
- TIMESTAMP ,
215
- INTERVAL ,
216
- LIST ,
217
- SEXP ,
218
- BAG ,
219
- STRUCT ,
220
- ANY ,
221
- ).mapIndexed { precedence, type -> type to precedence }.toMap()
222
- }
223
95
}
0 commit comments