-
Notifications
You must be signed in to change notification settings - Fork 8
/
convert.go
593 lines (543 loc) · 22 KB
/
convert.go
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright 2023 Google LLC
//
// 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 convert is responsible for all things related to implicit conversions. This includes
// inserting necessary implicit conversions into the model at parse time and finding the exact match
// overload at run time in the interpreter.
package convert
import (
"errors"
"fmt"
"math"
"strings"
"github.com/google/cql/internal/modelinfo"
"github.com/google/cql/model"
"github.com/google/cql/types"
)
// ErrAmbiguousMatch is returned when two or more overloads were matched with the same score.
var ErrAmbiguousMatch = errors.New("ambiguous match")
// ErrNoMatch is returned when no overloads were matched.
var ErrNoMatch = errors.New("no matching overloads")
// Overload holds the the declared operands and the result returned if those operands are matched by
// an invocation.
type Overload[F any] struct {
Operands []types.IType
// Result is what is returned by OverloadMatch.
Result F
}
// MatchedOverload returns the result of the OverloadMatch function.
type MatchedOverload[F any] struct {
// Result is the result of the overload that was matched.
Result F
// WrappedOperands are the operands wrapped in all necessary system operators and function refs to
// convert them to the matched overload.
WrappedOperands []model.IExpression
}
// OverloadMatch returns MatchedOverload on a match, and an error if there is no match or if the
// match is ambiguous. OverloadMatch returns the least converting match by summing the conversion
// score of each of the arguments. Ambiguous is returned if the least converting match has the same
// conversion score. Example of an ambiguous match:
//
// Declared: Foo(Long), Foo(Decimal)
// Invocation: Foo(Integer)
//
// Name is the function name and is only used for error messages.
// https://cql.hl7.org/03-developersguide.html#function-resolution
// https://cql.hl7.org/03-developersguide.html#conversion-precedence
func OverloadMatch[F any](invoked []model.IExpression, overloads []Overload[F], modelinfo *modelinfo.ModelInfos, name string) (MatchedOverload[F], error) {
if len(overloads) == 0 {
return MatchedOverload[F]{}, fmt.Errorf("could not resolve %v(%v): %w", name, OperandsToString(invoked), ErrNoMatch)
}
// To create concreteOverloads we go through the overloads, find any that have generics and
// replace the generics with concrete types.
concreteOverloads := make([]Overload[F], 0, len(overloads))
for _, overload := range overloads {
if isGeneric(overload.Operands) {
concreteOverload, matched, err := convertGeneric(invoked, overload, modelinfo)
if err != nil {
return MatchedOverload[F]{}, fmt.Errorf("%v(%v): %w", name, OperandsToString(invoked), err)
}
if matched {
concreteOverloads = append(concreteOverloads, concreteOverload)
}
} else {
concreteOverloads = append(concreteOverloads, overload)
}
}
ambiguous := false
minScore := math.MaxInt
matched := MatchedOverload[F]{WrappedOperands: make([]model.IExpression, len(invoked))}
for _, overload := range concreteOverloads {
res, err := operandsImplicitConverter(OperandsToTypes(invoked), overload.Operands, invoked, modelinfo)
if err != nil {
return MatchedOverload[F]{}, fmt.Errorf("%v(%v): %w", name, OperandsToString(invoked), err)
}
if res.Matched && res.Score == minScore {
// The least converting match is now ambiguous
ambiguous = true
continue
}
if res.Matched && res.Score < minScore {
// A new least converting match
ambiguous = false
minScore = res.Score
matched.Result = overload.Result
// Beware of the shallow copy
matched.WrappedOperands = res.WrappedOperands
}
}
if ambiguous {
return matched, fmt.Errorf("%v(%v) %w", name, OperandsToString(invoked), ErrAmbiguousMatch)
}
if minScore != math.MaxInt {
// Matched with conversion to a single overloaded function.
return matched, nil
}
return MatchedOverload[F]{}, fmt.Errorf("could not resolve %v(%v): %w", name, OperandsToString(invoked), ErrNoMatch)
}
type convertedOperands struct {
// Matched is true if the invoked types can be converted to the declared types.
Matched bool
// Score is the least converting score of the conversion based on the CQL conversion precedence.
// The score of each operand are added together. Multiple conversions on the same operand are not
// taken into account. https://cql.hl7.org/03-developersguide.html#conversion-precedence
Score int
// WrappedOperands are the operands wrapped in all necessary system operators and function refs to
// convert them.
WrappedOperands []model.IExpression
}
// operandsImplicitConverter may be called with nil opsToWrap if the caller only cares about the
// score.
func operandsImplicitConverter(invokedTypes []types.IType, declaredTypes []types.IType, opsToWrap []model.IExpression, tHelper *modelinfo.ModelInfos) (convertedOperands, error) {
if len(invokedTypes) != len(declaredTypes) {
return convertedOperands{Matched: false}, nil
}
if opsToWrap == nil {
// A slice of nil used as placeholder model.IExpressions.
opsToWrap = make([]model.IExpression, len(invokedTypes))
}
results := convertedOperands{Matched: true, Score: 0, WrappedOperands: make([]model.IExpression, len(invokedTypes))}
for i := range invokedTypes {
result, err := OperandImplicitConverter(invokedTypes[i], declaredTypes[i], opsToWrap[i], tHelper)
if err != nil {
return convertedOperands{}, err
}
if !result.Matched {
return convertedOperands{Matched: false}, nil
}
results.Score += result.Score
results.WrappedOperands[i] = result.WrappedOperand
}
return results, nil
}
// ConvertedOperand is the result of OperandImplicitConverter.
type ConvertedOperand struct {
// Matched is true if the invoked type can be converted to the declared type.
Matched bool
// Score is the least converting score of the conversion based on the CQL conversion precedence.
// The score does not take into account multiple conversions.
// https://cql.hl7.org/03-developersguide.html#conversion-precedence
Score int
// WrappedOperand is the operand wrapped in all necessary system operators and function refs to
// convert it.
WrappedOperand model.IExpression
}
// OperandImplicitConverter wraps the operand in any system operators or FHIRHelper function
// references needed to convert the operand from invokedType to declaredType. For example, if going
// from Integer --> Decimal the operand will be wrapped in ToDecimal(operand).
// operandImplicitConverter may apply multiple conversions, and always returns the least converting
// path.
//
// OperandImplicitConverter may be called with a nil opToWrap if the caller only cares about the
// score.
//
// Implementation note, invokedType may not be the same as opToWrap.GetResultType().The two
// diverge on some recursive calls. opToWrap.GetResultType() should not be used in the implementation
// of operandImplicitConverter.
func OperandImplicitConverter(invokedType types.IType, declaredType types.IType, opToWrap model.IExpression, mi *modelinfo.ModelInfos) (ConvertedOperand, error) {
// As we try different conversion paths minConverted will keep track of the lowest scoring
// conversion.
minConverted := ConvertedOperand{Score: math.MaxInt}
if invokedType == types.Unset {
return ConvertedOperand{Matched: false, Score: 0, WrappedOperand: opToWrap}, fmt.Errorf("internal error - invokedType is %v", invokedType)
}
if declaredType == types.Unset {
return ConvertedOperand{Matched: false, Score: 0, WrappedOperand: opToWrap}, fmt.Errorf("internal error - declaredType is %v", declaredType)
}
// EXACT MATCH
if invokedType.Equal(declaredType) {
return ConvertedOperand{Matched: true, Score: 0, WrappedOperand: opToWrap}, nil
}
// SUBTYPE
isSub, err := mi.IsSubType(invokedType, declaredType)
if err != nil {
return ConvertedOperand{}, err
}
if isSub {
// No wrapper is needed, the interpreter will handle subtypes.
minConverted = ConvertedOperand{Matched: true, Score: 1, WrappedOperand: opToWrap}
}
// All types can be converted from invoked --> Any --> declared. However that leads to incorrect
// conversions such as String --> Any --> Integer, so BaseTypes does not return Any.
baseTypes, err := mi.BaseTypes(invokedType)
if err != nil {
return ConvertedOperand{}, err
}
for _, baseType := range baseTypes {
r, err := OperandImplicitConverter(baseType, declaredType, opToWrap, mi)
if err != nil {
return ConvertedOperand{}, err
}
if r.Matched {
// Increment score by one since we applied a subtype before recursively calling
// OperandImplicitConverter.
r.Score++
if r.Score < minConverted.Score {
minConverted = r
}
}
}
// COMPATIBLE/NULL
// Ex Any --> Decimal As(operand, Decimal)
if invokedType.Equal(types.Any) {
// This is not described well in the CQL spec but, you can pass null literals to any function.
// Null has type Any.
wrapped := &model.As{
UnaryExpression: &model.UnaryExpression{
Operand: opToWrap,
Expression: model.ResultType(declaredType),
},
AsTypeSpecifier: declaredType,
Strict: false,
}
if 2 < minConverted.Score {
minConverted = ConvertedOperand{Matched: true, Score: 2, WrappedOperand: wrapped}
}
}
// CAST - invokedType is a Choice type
// Ex Choice<Integer> --> Decimal ToDecimal(As(operand, Integer))
if invokedChoice, ok := invokedType.(*types.Choice); ok {
for _, choiceType := range invokedChoice.ChoiceTypes {
choiceWrapped := &model.As{
UnaryExpression: &model.UnaryExpression{
Operand: opToWrap,
Expression: model.ResultType(choiceType),
},
AsTypeSpecifier: choiceType,
Strict: false,
}
r, err := OperandImplicitConverter(choiceType, declaredType, choiceWrapped, mi)
if err != nil {
return ConvertedOperand{}, err
}
if r.Matched {
r.Score += 3
if r.Score < minConverted.Score {
minConverted = r
}
}
}
}
// CAST - declaredType is a Choice type
// Ex Integer --> Choice<Decimal> As(ToDecimal(operand), Choice<Decimal>)
if declaredChoice, ok := declaredType.(*types.Choice); ok {
for _, choiceType := range declaredChoice.ChoiceTypes {
r, err := OperandImplicitConverter(invokedType, choiceType, opToWrap, mi)
if err != nil {
return ConvertedOperand{}, err
}
if r.Matched {
wrapped := &model.As{
UnaryExpression: &model.UnaryExpression{
Operand: r.WrappedOperand,
Expression: model.ResultType(declaredType),
},
AsTypeSpecifier: declaredType,
Strict: false,
}
if 3 < minConverted.Score {
minConverted = ConvertedOperand{Matched: true, Score: 3, WrappedOperand: wrapped}
}
}
}
}
// IMPLICIT CONVERSION
res, err := mi.IsImplicitlyConvertible(invokedType, declaredType)
if err != nil {
return ConvertedOperand{}, err
}
_, invokedIsSystem := invokedType.(types.System)
if res.IsConvertible && invokedIsSystem {
// Ex Integer --> Decimal ToDecimal(operand)
wrapped, err := wrapSystemImplicitConversion(res.Library, res.Function, opToWrap)
if err != nil {
return ConvertedOperand{}, err
}
score := implicitConversionScore(declaredType)
if score < minConverted.Score {
minConverted = ConvertedOperand{Matched: true, Score: score, WrappedOperand: wrapped}
}
}
if res.IsConvertible {
// IMPLICIT CONVERSION TO CLASS TYPE
// EX FHIR.date --> System.Date FHIRHelpers.ToDate(operand)
wrapped := &model.FunctionRef{
LibraryName: res.Library,
Name: res.Function,
Operands: []model.IExpression{opToWrap},
Expression: model.ResultType(res.OutputType),
}
score := implicitConversionScore(declaredType)
if score < minConverted.Score {
minConverted = ConvertedOperand{Matched: true, Score: score, WrappedOperand: wrapped}
}
}
// IMPLICIT CONVERSION TO CLASS TYPE - Intervals and Lists
switch i := invokedType.(type) {
// Ex Interval<Integer> --> Interval<Decimal> Interval[ToDecimal(operand.Low), ToDecimal(operand.High), operand.lowClosed, operand.highClosed]
case *types.Interval:
d, ok := declaredType.(*types.Interval)
if !ok {
break
}
low := &model.Property{Source: opToWrap, Path: "low", Expression: model.ResultType(i.PointType)}
high := &model.Property{Source: opToWrap, Path: "high", Expression: model.ResultType(i.PointType)}
rLow, err := OperandImplicitConverter(i.PointType, d.PointType, low, mi)
if err != nil {
return ConvertedOperand{}, err
}
if !rLow.Matched {
break
}
rHigh, err := OperandImplicitConverter(i.PointType, d.PointType, high, mi)
if err != nil {
return ConvertedOperand{}, err
}
if !rHigh.Matched {
break
}
wrapped := &model.Interval{
Low: rLow.WrappedOperand,
High: rHigh.WrappedOperand,
// Since operand could be any CQL expression that resolves to an interval we use the lowClosed
// and highClosed properties to forwards the bounds of the interval.
LowClosedExpression: &model.Property{Source: opToWrap, Path: "lowClosed", Expression: model.ResultType(types.Boolean)},
HighClosedExpression: &model.Property{Source: opToWrap, Path: "highClosed", Expression: model.ResultType(types.Boolean)},
Expression: model.ResultType(d),
}
if 5 < minConverted.Score {
minConverted = ConvertedOperand{Matched: true, Score: 5, WrappedOperand: wrapped}
}
// Ex List<Integer> --> List<Decimal> [operand] X return ToDecimal(X)
case *types.List:
d, ok := declaredType.(*types.List)
if !ok {
break
}
ref := &model.AliasRef{Name: "X", Expression: model.ResultType(i.ElementType)}
r, err := OperandImplicitConverter(i.ElementType, d.ElementType, ref, mi)
if err != nil {
return ConvertedOperand{}, err
}
if !r.Matched {
break
}
wrapped := &model.Query{
Source: []*model.AliasedSource{&model.AliasedSource{
Alias: "X",
Source: opToWrap,
Expression: model.ResultType(i),
}},
Return: &model.ReturnClause{
Expression: r.WrappedOperand,
Distinct: false,
Element: &model.Element{ResultType: d.ElementType}},
Expression: model.ResultType(declaredType),
}
if 5 < minConverted.Score {
minConverted = ConvertedOperand{Matched: true, Score: 5, WrappedOperand: wrapped}
}
}
if minConverted.Matched {
return minConverted, nil
}
return ConvertedOperand{Matched: false}, nil
}
func wrapSystemImplicitConversion(library string, function string, operand model.IExpression) (model.IExpression, error) {
if library != "SYSTEM" {
return nil, fmt.Errorf("internal error - could not find wrapper for %v %v", library, function)
}
switch function {
case "ToDecimal":
return &model.ToDecimal{UnaryExpression: &model.UnaryExpression{Operand: operand, Expression: model.ResultType(types.Decimal)}}, nil
case "ToLong":
return &model.ToLong{UnaryExpression: &model.UnaryExpression{Operand: operand, Expression: model.ResultType(types.Long)}}, nil
case "ToDateTime":
return &model.ToDateTime{UnaryExpression: &model.UnaryExpression{Operand: operand, Expression: model.ResultType(types.DateTime)}}, nil
case "ToQuantity":
return &model.ToQuantity{UnaryExpression: &model.UnaryExpression{Operand: operand, Expression: model.ResultType(types.Quantity)}}, nil
case "ToConcept":
return &model.ToConcept{UnaryExpression: &model.UnaryExpression{Operand: operand, Expression: model.ResultType(types.Concept)}}, nil
}
return nil, fmt.Errorf("internal error - could not find wrapper for %v %v", library, function)
}
// OperandsToString returns a print friendly representation of the operands.
func OperandsToString(operands []model.IExpression) string {
var stringOperands strings.Builder
for i, operand := range operands {
if i > 0 {
stringOperands.WriteString(", ")
}
if operand == nil || operand.GetResultType() == nil {
stringOperands.WriteString("nil")
} else {
stringOperands.WriteString(operand.GetResultType().String())
}
}
return stringOperands.String()
}
// OperandsToTypes returns the types of the ResultType of the operands.
func OperandsToTypes(operands []model.IExpression) []types.IType {
var types []types.IType
for _, operand := range operands {
types = append(types, operand.GetResultType())
}
return types
}
func implicitConversionScore(t types.IType) int {
switch t {
case types.String, types.Integer, types.Long, types.Decimal, types.Boolean, types.Date, types.DateTime, types.Time:
// Simple types.
return 4
default:
// Everything else is a class type.
return 5
}
}
// Generic types are used to define generic overloads, the overloads with T in
// https://cql.hl7.org/09-b-cqlreference.html. The only place generics are used is to define system
// operators in the Parser. The ResultType in model should never be a Generic type. The interpreter
// should never deal with generic types.
type Generic string
const (
// GenericType represents a generic CQL type, shown as T in the CQL reference. Never nest a
// GenericType in a real type (ex List<GenericType>). Instead use the GenericList below.
GenericType Generic = "GenericType"
// GenericInterval represents a generic interval type, shown as Interval<T> in the CQL reference.
GenericInterval Generic = "GenericInterval"
// GenericList represents a generic list type, shown as List<T> in the CQL reference.
GenericList Generic = "GenericList"
)
// Equal is a strict equal. X.Equal(Y) is true when X and Y are the exact same types.
func (s Generic) Equal(a types.IType) bool {
aBase, ok := a.(Generic)
if !ok {
return false
}
return s == aBase
}
// String returns the model info based name for the type, and implements fmt.Stringer for easy
// printing.
func (s Generic) String() string {
return fmt.Sprintf("Generic.%v", string(s))
}
// ModelInfoName should never be called for Generics.
func (s Generic) ModelInfoName() (string, error) {
return "", errors.New("Generic type does not have a model info name")
}
// MarshalJSON should never be called for Generics.
func (s Generic) MarshalJSON() ([]byte, error) {
return nil, errors.New("Generics should not be marshalled")
}
// convertGeneric takes the invoked operands ex (Integer, String, Decimal), a generic overload (T,
// String, T) and returns the least converting concrete overload that still satisfies the the
// generic constraints, in this case (Decimal, String, Decimal). If there is no concrete
// instantiation of this generic overload that will work false is returned.
func convertGeneric[F any](invoked []model.IExpression, genericDeclared Overload[F], mi *modelinfo.ModelInfos) (Overload[F], bool, error) {
if len(invoked) != len(genericDeclared.Operands) {
// There is no concrete instantiation of this generic overload that will work, so return false.
return Overload[F]{}, false, nil
}
// genericInvokedTypes holds the types of the invoked operands that need to match with a generic.
// For example for invoked (Integer, String, Decimal), and genericDeclared (T, String, T) then
// genericInvokedTypes is (Integer, Decimal). We use inferMixedType to find the least converting T
// for genericInvokedTypes.
genericInvokedTypes := make([]types.IType, 0)
for i := range invoked {
switch genericDeclared.Operands[i] {
case GenericType:
genericInvokedTypes = append(genericInvokedTypes, invoked[i].GetResultType())
case GenericInterval:
if interval, ok := invoked[i].GetResultType().(*types.Interval); ok {
genericInvokedTypes = append(genericInvokedTypes, interval.PointType)
} else {
// If we are matching Interval<T> and invoked[i] is not an interval, we will either need to
// apply interval promotion or check that there's an implicit conversion to Interval<T>.
// For now, only the interval promotion case is supported, so we add invoked[i] as is to
// find the least converting path to the T in Interval<T>.
// TODO(b/333923412): add in full support for the cases in which there's an implicit
// conversion to an Interval<T>.
genericInvokedTypes = append(genericInvokedTypes, invoked[i].GetResultType())
}
case GenericList:
if list, ok := invoked[i].GetResultType().(*types.List); ok {
genericInvokedTypes = append(genericInvokedTypes, list.ElementType)
} else {
genericInvokedTypes = append(genericInvokedTypes, invoked[i].GetResultType())
}
}
}
// TODO(b/301606416): We should convert the generic overload into all least converting concrete
// overloads, but if there is a tie inferMixedType just randomly picks one.
inferred, err := inferMixedType(genericInvokedTypes, nil, mi)
if err != nil {
return Overload[F]{}, false, err
}
if inferred.PuntedToChoice {
// There is no concrete instantiation of this generic overload that will work, so return false.
return Overload[F]{}, false, nil
}
// concreteOverload is the genericDeclared overload with T replaced by the inferred.UniformType.
concreteOverload := make([]types.IType, len(genericDeclared.Operands))
for i := range genericDeclared.Operands {
switch genericDeclared.Operands[i] {
case GenericType:
concreteOverload[i] = inferred.UniformType
case GenericInterval:
if _, ok := inferred.UniformType.(*types.Interval); !ok {
// Since we sometimes use the PointType above we need to rewrap in an Interval.
concreteOverload[i] = &types.Interval{PointType: inferred.UniformType}
} else {
concreteOverload[i] = inferred.UniformType
}
case GenericList:
if _, ok := inferred.UniformType.(*types.List); !ok {
concreteOverload[i] = &types.List{ElementType: inferred.UniformType}
} else {
concreteOverload[i] = inferred.UniformType
}
default:
concreteOverload[i] = genericDeclared.Operands[i]
}
}
genericDeclared.Operands = concreteOverload
return genericDeclared, true, nil
}
func isGeneric(operands []types.IType) bool {
for _, operand := range operands {
if operand.Equal(GenericType) || operand.Equal(GenericInterval) || operand.Equal(GenericList) {
return true
}
}
return false
}