-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We model Count as a UnaryExpression for now, since as far as we can tell there is no way to set the "path" field on an AggregateExpression via CQL. PiperOrigin-RevId: 643470653
- Loading branch information
1 parent
68ac365
commit 0001a7f
Showing
7 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024 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 interpreter | ||
|
||
import ( | ||
"github.com/google/cql/model" | ||
"github.com/google/cql/result" | ||
) | ||
|
||
// AGGREGATE FUNCTIONS - https://cql.hl7.org/09-b-cqlreference.html#aggregate-functions | ||
|
||
// Count(argument List<T>) Integer | ||
// https://cql.hl7.org/09-b-cqlreference.html#count | ||
func (i *interpreter) evalCount(m model.IUnaryExpression, operand result.Value) (result.Value, error) { | ||
if result.IsNull(operand) { | ||
return result.New(0) | ||
} | ||
l, err := result.ToSlice(operand) | ||
if err != nil { | ||
return result.Value{}, err | ||
} | ||
count := 0 | ||
for _, elem := range l { | ||
if !result.IsNull(elem) { | ||
count++ | ||
} | ||
} | ||
return result.New(count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2024 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 enginetests | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/cql/interpreter" | ||
"github.com/google/cql/model" | ||
"github.com/google/cql/parser" | ||
"github.com/google/cql/result" | ||
"github.com/google/cql/types" | ||
"github.com/google/go-cmp/cmp" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
) | ||
|
||
func TestCount(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cql string | ||
wantModel model.IExpression | ||
wantResult result.Value | ||
}{ | ||
{ | ||
name: "Count({1, 2, 3})", | ||
cql: "Count({1, 2, 3})", | ||
wantModel: &model.Count{ | ||
UnaryExpression: &model.UnaryExpression{ | ||
Operand: model.NewList([]string{"1", "2", "3"}, types.Integer), | ||
Expression: model.ResultType(types.Integer), | ||
}, | ||
}, | ||
wantResult: newOrFatal(t, 3), | ||
}, | ||
{ | ||
name: "Count with null input", | ||
cql: "Count(null as List<Integer>)", | ||
wantResult: newOrFatal(t, 0), | ||
}, | ||
{ | ||
name: "Count({1, 2, null})", | ||
cql: "Count({1, 2, null})", | ||
wantResult: newOrFatal(t, 2), | ||
}, | ||
{ | ||
name: "Count with empty list", | ||
cql: "Count({})", | ||
wantResult: newOrFatal(t, 0), | ||
}, | ||
{ | ||
name: "Count with all null list", | ||
cql: "Count({null, null})", | ||
wantResult: newOrFatal(t, 0), | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
p := newFHIRParser(t) | ||
parsedLibs, err := p.Libraries(context.Background(), wrapInLib(t, tc.cql), parser.Config{}) | ||
if err != nil { | ||
t.Fatalf("Parse returned unexpected error: %v", err) | ||
} | ||
if diff := cmp.Diff(tc.wantModel, getTESTRESULTModel(t, parsedLibs)); tc.wantModel != nil && diff != "" { | ||
t.Errorf("Parse diff (-want +got):\n%s", diff) | ||
} | ||
|
||
results, err := interpreter.Eval(context.Background(), parsedLibs, defaultInterpreterConfig(t, p)) | ||
if err != nil { | ||
t.Fatalf("Eval returned unexpected error: %v", err) | ||
} | ||
if diff := cmp.Diff(tc.wantResult, getTESTRESULT(t, results), protocmp.Transform()); diff != "" { | ||
t.Errorf("Eval diff (-want +got)\n%v", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters