Repository: edgedb/edgedb

Issue:
cardinality is unknown
<!-- Please search existing issues to avoid creating duplicates. -->
- OS Version: ubuntu 22.04

```
$ edgedb
EdgeDB 3.0-dev.7018+de3aa47 (repl 2.1.0-dev.865+40c556b)
Type \help for help, \quit to quit.

edgedb> select <json>{ a := 1 } union <json>{ a := 2 };

edgedb error: InternalServerError: cardinality 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/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/server/compiler_pool/worker_proc.py", line 55, in worker
          res = meth(*args)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/server/compiler_pool/worker.py", line 160, in compile
          units, cstate = COMPILER.compile(
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 2543, in compile
          unit_group = self._compile(ctx=ctx, source=source)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 2053, in _compile
          return self._try_compile(ctx=ctx, source=source)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 2117, in _try_compile
          comp, capabilities = self._compile_dispatch_ql(
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 2027, in _compile_dispatch_ql
          query = self._compile_ql_query(ctx, ql, script_info=script_info)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 687, in _compile_ql_query
          ir = qlcompiler.compile_ast_to_ir(
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/edgeql/compiler/__init__.py", line 179, in wrapper
          return func(*args, **kwargs)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/edgeql/compiler/__init__.py", line 237, in compile_ast_to_ir
          ir_set = dispatch_mod.compile(tree, ctx=ctx)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 127, in compile_SelectQuery
          stmt.result = compile_result_clause(
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 1288, in compile_result_clause
          expr = dispatch.compile(result_expr, ctx=ectx)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/edgeql/compiler/expr.py", line 136, in compile_BinOp
          folded = try_fold_binop(op_node.expr, ctx=ctx)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/edgeql/compiler/expr.py", line 278, in try_fold_binop
          const = ireval.evaluate(opcall, schema=ctx.env.schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 150, in evaluate_OperatorCall
          return _evaluate_union(opcall, schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 192, in _evaluate_union
          val = evaluate(arg.expr, schema=schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 125, in evaluate_Set
          return evaluate(ir_set.expr, schema=schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 92, in evaluate_SelectStmt
          return evaluate(ir_stmt.result, schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 125, in evaluate_Set
          return evaluate(ir_set.expr, schema=schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 107, in evaluate_TypeCast
          schema_type_to_python_type(from_type, schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 305, in schema_type_to_python_type
          return object_type_to_python_type(stype, schema)
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/ir/staeval.py", line 378, in object_type_to_python_type
          is_multi = ptr_card.is_multi()
        File "/home/fmoor/.local/share/edgedb/portable/3.0-dev.7018/lib/python3.10/site-packages/edb/edgeql/qltypes.py", line 92, in is_multi
          raise ValueError('cardinality is unknown')
      ValueError: cardinality is unknown
```


Discussion:
This works though
```
edgedb> with a := <json>{ x := 1 },
....... b := <json>{ x := 2 },
....... select a union b;
{Json("{\"x\": 1}"), Json("{\"x\": 2}")}
```
I swear the only thing that having the static evaluator in the pipeline accomplishes is introducing bugs

Resolving patch:
diff --git a/edb/ir/staeval.py b/edb/ir/staeval.py
--- a/edb/ir/staeval.py
+++ b/edb/ir/staeval.py
@@ -46,6 +46,7 @@
 from edb.schema import scalars as s_scalars
 from edb.schema import schema as s_schema
 from edb.schema import constraints as s_constr
+from edb.schema import pointers as s_pointers
 
 from edb.server import config
 
@@ -352,6 +353,7 @@ def object_type_to_python_type(
     subclasses = []
 
     for pn, p in objtype.get_pointers(schema).items(schema):
+        assert isinstance(p, s_pointers.Pointer)
         str_pn = str(pn)
         if str_pn in ('id', '__type__'):
             continue
@@ -374,8 +376,12 @@ def object_type_to_python_type(
         else:
             pytype = scalar_type_to_python_type(ptype, schema)
 
-        ptr_card = p.get_cardinality(schema)
-        is_multi = ptr_card.is_multi()
+        ptr_card: qltypes.SchemaCardinality = p.get_cardinality(schema)
+        if ptr_card.is_known():
+            is_multi = ptr_card.is_multi()
+        else:
+            raise UnsupportedExpressionError()
+
         if is_multi:
             pytype = FrozenSet[pytype]  # type: ignore
 


Test patch: