Repository: edgedb/edgedb

Issue:
edgedb error: InternalServerError: cardinality of property '__tname__' of object type 'default::ActivenessSnapshot' is unknown
<!-- Please search existing issues to avoid creating duplicates. -->

<!--
For the EdgeDB Version: run `edgedb query 'select sys::get_version_as_str()'` from your project directory (or run `select sys::get_version_as_str();` in the EdgeDB interactive shell).
For the EdgeDB CLI Version: Run `edgedb --version` from anywhere
-->

- EdgeDB Version: `2.7+81de4dd`
- EdgeDB CLI Version: `EdgeDB CLI 2.2.6+b869667`
- OS Version: Ubuntu 20.04

Steps to Reproduce:

1. Run query 
```
with
snapshots := (
    select ActivenessSnapshot 
    filter 
    .<activeness_history[is Alert].severity in array_unpack(<array<int16>>$severity_levels) and
    .at < <datetime>$horizon
)
group snapshots using alert_id := .<activeness_history[is Alert].id by alert_id
```

<!-- If the issue is about a query error, please also provide your schema -->

Schema:
```
module default {
    type ActivenessSnapshot {
        required property number -> int32;
        required property at -> datetime;
        property new_activeness -> bool;
    }

    type Alert {
        required property active -> bool;
        required property title -> str;
        required property description -> str;
        required property issue_detail -> str;
        required property recommendation -> str;
        required property recommendation_detail -> str;
        property comment -> str;
        property tag -> str;
        required property severity -> int16;
        required property hash -> str;
        required property extra -> json;
        multi link activeness_history -> ActivenessSnapshot {
            constraint exclusive;
            on source delete delete target;
        };
        required property last_seen -> datetime;
        required property cdate -> datetime {
            default := datetime_of_transaction();
        };
        required property mdate -> datetime {
            default := datetime_of_transaction();
        };
    }
}
```

The error I get:

```
edgedb error: InternalServerError: cardinality of property '__tname__' of object type 'default::ActivenessSnapshot' is unknown
  Hint: This is most likely a bug in EdgeDB. Please consider opening an issue ticket at https://github.com/edgedb/edgedb/issues/new?template=bug_report.md
  Server traceback:
      Traceback (most recent call last):
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler_pool/worker_proc.py", line 55, in worker
          res = meth(*args)
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler_pool/worker.py", line 160, in compile
          units, cstate = COMPILER.compile(
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 2277, in compile
          unit_group = self._compile(ctx=ctx, source=source)
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1798, in _compile
          return self._try_compile(ctx=ctx, source=source)
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1862, in _try_compile
          comp, capabilities = self._compile_dispatch_ql(
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1772, in _compile_dispatch_ql
          query = self._compile_ql_query(ctx, ql, script_info=script_info)
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 716, in _compile_ql_query
          out_type_data, out_type_id = sertypes.TypeSerializer.describe(
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/sertypes.py", line 582, in describe
          type_id = builder._describe_type(
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/sertypes.py", line 314, in _describe_type
          subtype_id = self._describe_set(
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/sertypes.py", line 169, in _describe_set
          type_id = self._describe_type(t, view_shapes, view_shapes_metadata,
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/server/compiler/sertypes.py", line 297, in _describe_type
          if ptr.singular(self.schema):
        File "/home/vhh/.local/share/edgedb/portable/2.6/lib/python3.10/site-packages/edb/schema/pointers.py", line 748, in singular
          raise AssertionError(f'cardinality of {vn} is unknown')
      AssertionError: cardinality of property '__tname__' of object type 'default::ActivenessSnapshot' is unknown
edgedb> 

```


Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/viewgen.py b/edb/edgeql/compiler/viewgen.py
--- a/edb/edgeql/compiler/viewgen.py
+++ b/edb/edgeql/compiler/viewgen.py
@@ -2091,6 +2091,9 @@ def _inline_type_computable(
                 ctx=scopectx
             )
 
+        ctx.env.schema = ptr.set_field_value(
+            ctx.env.schema, 'cardinality', qltypes.SchemaCardinality.One)
+
     view_shape = ctx.env.view_shapes[stype]
     view_shape_ptrs = {p for p, _ in view_shape}
     if ptr not in view_shape_ptrs:


Test patch:
diff --git a/tests/test_edgeql_group.py b/tests/test_edgeql_group.py
--- a/tests/test_edgeql_group.py
+++ b/tests/test_edgeql_group.py
@@ -125,6 +125,32 @@ async def test_edgeql_group_simple_03(self):
             ])
         )
 
+    async def test_edgeql_group_simple_04(self):
+        await self.assert_query_result(
+            r'''
+            WITH snapshots := cards::Card
+            GROUP snapshots {} BY .element;
+            ''',
+            tb.bag([
+                {
+                    "elements": tb.bag([{}, {}]),
+                    "key": {"element": "Water"}
+                },
+                {
+                    "elements": tb.bag([{}, {}]),
+                    "key": {"element": "Fire"}
+                },
+                {
+                    "elements": tb.bag([{}, {}]),
+                    "key": {"element": "Earth"}
+                },
+                {
+                    "elements": tb.bag([{}, {}, {}]),
+                    "key": {"element": "Air"}
+                }
+            ])
+        )
+
     async def test_edgeql_group_simple_no_id_output_01(self):
         # the implicitly injected id was making it into the output
         # in native mode at one point