Repository: edgedb/edgedb

Issue:
name field: expected <class 'edb.schema.name.QualName'> but got <UnqualName array<default|MultiLinkPerson>>
## Summary
While working on my query builder, I've encounter this exception thrown by the server

### Versions
EdgeDB running in WSL:
- EdgeDB CLI 1.1.2+f38a6e0
- Instance: 1.4+5ffe34b

### OS
Windows 10 21H2

<details>
<summary>Server stack trace</summary>

```
edgedb error: InternalServerError: name field: expected <class 'edb.schema.name.QualName'> but got <UnqualName array<default|MultiLinkPerson>>
  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/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/server/compiler_pool/worker.py", line 309, in worker
          res = meth(*args)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/server/compiler_pool/worker.py", line 177, in compile
          units, cstate = COMPILER.compile(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 2105, in compile
          units = self._compile(ctx=ctx, source=source)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1667, in _compile
          return self._try_compile(ctx=ctx, source=source)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1720, in _try_compile
          comp, capabilities = self._compile_dispatch_ql(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1641, in _compile_dispatch_ql
          query = self._compile_ql_query(ctx, ql)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 568, in _compile_ql_query
          ir = qlcompiler.compile_ast_to_ir(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/__init__.py", line 179, in wrapper
          return func(*args, **kwargs)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/__init__.py", line 231, in compile_ast_to_ir
          ir_set = dispatch_mod.compile(tree, ctx=ctx)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 212, in compile_ForQuery
          compile_result_clause(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 1083, in compile_result_clause
          expr = dispatch.compile(result_expr, ctx=ectx)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 303, in compile_InsertQuery
          stmt.subject = compile_query_subject(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 1219, in compile_query_subject
          view_scls, expr = viewgen.process_view(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/viewgen.py", line 93, in process_view
          view_scls, ir = _process_view(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/viewgen.py", line 178, in _process_view
          pointer, ptr_set = _normalize_view_ptr_expr(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/viewgen.py", line 728, in _normalize_view_ptr_expr
          irexpr, sub_view_rptr = _compile_qlexpr(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/viewgen.py", line 459, in _compile_qlexpr
          irexpr = dispatch.compile(qlexpr, ctx=shape_expr_ctx)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 110, in compile_SelectQuery
          stmt.result = compile_result_clause(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/stmt.py", line 1083, in compile_result_clause
          expr = dispatch.compile(result_expr, ctx=ectx)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/expr.py", line 411, in compile_IfElse
          return func.compile_operator(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/func.py", line 434, in compile_operator
          matched = polyres.find_callable(opers, args=args, kwargs={}, ctx=ctx)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/polyres.py", line 88, in find_callable
          call = try_bind_call_args(args, kwargs, candidate, ctx=ctx)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/polyres.py", line 333, in try_bind_call_args
          cd = _get_cast_distance(arg_val, arg_type, param_type)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/edgeql/compiler/polyres.py", line 185, in _get_cast_distance
          resolved_poly_base_type.find_common_implicitly_castable_type(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/schema/types.py", line 1258, in find_common_implicitly_castable_type
          return type(self).from_subtypes(schema, [subtype])
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/schema/types.py", line 1310, in from_subtypes
          return cls.create(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/schema/types.py", line 1150, in create
          schema, result = super().create_in_schema(
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/schema/objects.py", line 1125, in create_in_schema
          value = field.coerce_value(schema, value)
        File "/home/edgedb/.local/share/edgedb/portable/1.4/lib/python3.10/site-packages/edb/schema/objects.py", line 383, in coerce_value
          raise TypeError(
      TypeError: name field: expected <class 'edb.schema.name.QualName'> but got <UnqualName array<default|MultiLinkPerson>>
```
</details>

Query (its a bit messy)

```sql
with JWRDXTXIRIIP_d1 := array_agg((for MLLRWMMHUGAF in json_array_unpack(<json>$OHUETNLZEXWG) union (insert MultiLinkPerson { name := <str>json_get(MLLRWMMHUGAF, 'Name'), email := <str>jso
n_get(MLLRWMMHUGAF, 'Email'), best_friends := {} } unless conflict on .email else (select MultiLinkPerson)))) for x in json_array_unpack(<json>$WINVJKUCXDIV) union (insert MultiLinkPerson { name :
= <str>json_get(x, 'Name'), email := <str>json_get(x, 'Email'), best_friends := JWRDXTXIRIIP_d1[<int64>json_get(x, 'BestFriends', 'JWRDXTXIRIIP_depth_from'):<int64>json_get(x, 'BestFriends', 'JWRD
XTXIRIIP_depth_to')] if json_typeof(json_get(x, 'BestFriends')) != 'null' else <array<MultiLinkPerson>>[] });
```

Type used within the query
```
  type MultiLinkPerson {
    required property name -> str;
    required property email -> str {
      constraint exclusive;
    }
    multi link best_friends -> MultiLinkPerson;
  }
```

The error gets thrown by the `x if y else z` clause. Replacing the value with an empty set `{}` causes `InvalidTypeError: operator 'std::IF' cannot be applied to operands of type 'array<default::MultiLinkPerson>', 'std::bool', 'array<anytype>'`. Casting the empty set to `array<default::MultiLinkPerson>` causes the server exception to be thrown.

Simple query to reproduce
```
with test := [<MultiLinkPerson>{}]
select test if false else <array<MultiLinkPerson>>[];
```


Discussion:

Resolving patch:
diff --git a/edb/schema/types.py b/edb/schema/types.py
--- a/edb/schema/types.py
+++ b/edb/schema/types.py
@@ -1301,10 +1301,10 @@ def castable_to(
             other.get_element_type(schema), schema)
 
     def find_common_implicitly_castable_type(
-        self: Array_T,
+        self,
         other: Type,
         schema: s_schema.Schema,
-    ) -> typing.Tuple[s_schema.Schema, Optional[Array_T]]:
+    ) -> typing.Tuple[s_schema.Schema, Optional[Array]]:
 
         if not isinstance(other, Array):
             return schema, None
@@ -1319,7 +1319,7 @@ def find_common_implicitly_castable_type(
         if subtype is None:
             return schema, None
 
-        return type(self).from_subtypes(schema, [subtype])
+        return Array.from_subtypes(schema, [subtype])
 
     def _resolve_polymorphic(
         self,


Test patch:
diff --git a/tests/test_edgeql_expressions.py b/tests/test_edgeql_expressions.py
--- a/tests/test_edgeql_expressions.py
+++ b/tests/test_edgeql_expressions.py
@@ -6875,6 +6875,14 @@ async def test_edgeql_expr_if_else_09(self):
             [],
         )
 
+        await self.assert_query_result(
+            r"""
+                with test := ['']
+                select test if false else <array<str>>[];
+            """,
+            [[]],
+        )
+
     async def test_edgeql_expr_setop_01(self):
         await self.assert_query_result(
             r"""SELECT EXISTS <str>{};""",