Skip to content

Commit b57f045

Browse files
committed
Propagate resultTypeSpecifier from Query -> ReturnClause -> Tuple
1 parent 7b12482 commit b57f045

4 files changed

+41
-15
lines changed

Cql/Cql.Compiler/Preprocessing/MissingResultTypeSpecifierCorrector.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
using Hl7.Cql.Elm;
10+
using Tuple = Hl7.Cql.Elm.Tuple;
1011

1112
namespace Hl7.Cql.Compiler.Preprocessing;
1213

@@ -18,17 +19,54 @@ internal class MissingResultTypeSpecifierCorrector(ILogger<MissingResultTypeSpec
1819
{
1920
private readonly ElmTreeWalker _walker = ElmTreeWalker.Create((self, node) =>
2021
{
21-
switch (node)
2222
{
23-
case Element { resultTypeSpecifier: null, resultTypeName: {} resultTypeName } element:
23+
// If an Element has a resultTypeName but no resultTypeSpecifier, set resultTypeSpecifier to a NamedTypeSpecifier with the name from resultTypeName
24+
if (node is Element { resultTypeSpecifier: null, resultTypeName: { } resultTypeName } element)
25+
{
2426
logger.LogDebug(
2527
"Setting missing resultTypeSpecifier to resultTypeName on {elementType} to '{resultType}' @ {locator}.\n{expressionKey}",
2628
element.GetType().ToString(),
2729
resultTypeName,
2830
element.locator,
2931
self.ContextStackString);
3032
element.resultTypeSpecifier = new NamedTypeSpecifier { name = resultTypeName };
31-
break;
33+
}
34+
}
35+
36+
{
37+
// If a Query has a ListTypeSpecifier as resultTypeSpecifier but its ReturnClause has no resultTypeSpecifier, set the ReturnClause's resultTypeSpecifier to the elementType of the ListTypeSpecifier
38+
if (node is Query
39+
{
40+
resultTypeSpecifier: ListTypeSpecifier { elementType: { } elementType },
41+
@return: { resultTypeSpecifier: null } returnClause,
42+
})
43+
{
44+
returnClause.resultTypeSpecifier = elementType;
45+
}
46+
}
47+
48+
{
49+
// If a ReturnClause has a TupleTypeSpecifier as resultTypeSpecifier but its Tuple has no resultTypeSpecifier, set the Tuple's resultTypeSpecifier to the ReturnClause's TupleTypeSpecifier
50+
if (node is ReturnClause
51+
{
52+
resultTypeSpecifier: TupleTypeSpecifier tupleTypeSpecifier,
53+
expression: Tuple { resultTypeSpecifier: null } tuple,
54+
})
55+
{
56+
tuple.resultTypeSpecifier = tupleTypeSpecifier;
57+
}
58+
}
59+
60+
{
61+
// If a Tuple has elements with missing resultTypeSpecifier but the Tuple itself has a TupleTypeSpecifier as resultTypeSpecifier, set the missing element resultTypeSpecifiers from the TupleTypeSpecifier
62+
if (node is Tuple { element: { Length: > 0 } elements, resultTypeSpecifier: TupleTypeSpecifier tupleTypeSpecifier }
63+
&& elements.Any(e => e.value.resultTypeSpecifier is null))
64+
{
65+
foreach (var (index, tupleElement) in elements.Select((v, i) => (i, v)))
66+
{
67+
tupleElement.value.resultTypeSpecifier ??= tupleTypeSpecifier.element[index].elementType;
68+
}
69+
}
3270
}
3371

3472
return false; // Continue walking children

Cql/PackagerCLI/Hl7.Cql.Packager.ecqm-content-cms-2025.appsettings.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{
22
"Elm": {
33
"SkipFiles": [
4-
// Tuple element value does not have a resultTypeSpecifier
5-
"CMS2FHIRPCSDepressionScreenAndFollowUp.json",
6-
74
// Exception: cannot convert from 'Hl7.Fhir.Model.Id' to 'Hl7.Fhir.Model.Code<Hl7.Fhir.Model.Account.AccountStatus>'
85
"NHSNHelpers.json",
96

Cql/PackagerCLI/Hl7.Cql.Packager.ecqm-content-qicore-2024.appsettings.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
"CMS506FHIRSafeUseofOpioids.json",
2020
"PCSBMIScreenAndFollowUpFHIR.json",
2121

22-
// Tuple element value does not have a resultTypeSpecifier
23-
"CADBetaBlockerTherapyPriorMIorLVSDFHIR.json",
24-
"PCSDepressionScreenAndFollowUpFHIR.json",
25-
2622
// Multiple sort fields not supported yet
2723
"CMS986FHIRMalnutritionScore.json",
2824
"HospitalHarmAcuteKidneyInjuryFHIR.json",

Cql/PackagerCLI/Hl7.Cql.Packager.ecqm-content-qicore-2025.appsettings.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
{
22
"Elm": {
33
"SkipFiles": [
4-
// Tuple element value does not have a resultTypeSpecifier
5-
"CMS2FHIRPCSDepressionScreenAndFollowUp.json",
6-
"CMS145FHIRCADBetaBlockerTherapyPriorMIorLVSD.json",
7-
"CMS832HHAKIFHIR.json",
8-
94
// Cannot resolve type {http://hl7.org/fhir}DoNotPerformReason} for expression
105
"CMS190VTEProphylaxisICUFHIR.json",
116
"CMS108FHIRVTEProphylaxis.json"

0 commit comments

Comments
 (0)