Repository: edgedb/edgedb

Issue:
ISE when fetching computed property on UNION of inserts
I'm getting the error `edgedb.errors.InternalServerError: column Person~2.7adf6daf-a0c8-11ec-8579-db52b929793f does not exist` when I'm bulk adding `Person` objects.

My code for adding (the field values are meaningless as I created a testing system that uses random values):

```
WITH
    model_0 := (INSERT Person { first_name := <str>$first_namegraphORM0, last_name := <str>$last_namegraphORM0, tags := array_unpack(<array<str>>$tagsgraphORM0), ordered_tags := <array<str>>$ordered_tagsgraphORM0, created_at := <datetime>$created_atgraphORM0, slug := <str>$sluggraphORM0 }),

    model_1 := (INSERT Person { first_name := <str>$first_namegraphORM1, last_name := <str>$last_namegraphORM1, tags := array_unpack(<array<str>>$tagsgraphORM1), ordered_tags := <array<str>>$ordered_tagsgraphORM1, created_at := <datetime>$created_atgraphORM1, slug := <str>$sluggraphORM1 }),

    models := (model_0 UNION model_1)

 SELECT models { slug, last_name, inserted_at, tags, ordered_tags, created_at, full_name, id, first_name }
```
and the query variables are:

```
{'first_namegraphORM0': 'Jon0', 'last_namegraphORM0': 'Quack0', 'tagsgraphORM0': ['yaaas', 'hey'], 'ordered_tagsgraphORM0': ['coding', 'python'], 'created_atgraphORM0': datetime.datetime(2022, 3, 22, 17, 36, 29, 903700, tzinfo=zoneinfo.ZoneInfo(key='America/New_York')), 'sluggraphORM0': '73uAr7gH5p', 'first_namegraphORM1': 'Jon1', 'last_namegraphORM1': 'Quack1', 'tagsgraphORM1': ['yaaas', 'hey'], 'ordered_tagsgraphORM1': ['coding', 'python'], 'created_atgraphORM1': datetime.datetime(2022, 3, 22, 17, 36, 29, 904712, tzinfo=zoneinfo.ZoneInfo(key='America/New_York')), 'sluggraphORM1': '8NHUm8YEtN'}
```

I don't get this error every time, but I get it most times right now. I am using a Digital Ocean server running v1.2.

The person schema is:

```
  type Person {
    required property first_name -> str;
    property last_name -> str;
    multi property tags -> str;
    property ordered_tags -> array<str>;
    required property inserted_at -> datetime {
        readonly := true;
        default := datetime_current();
    }
    property created_at -> datetime;
    multi link people_i_follow -> Person {
        on target delete allow;
    }
    required property slug -> str {
        constraint exclusive;
    }
    multi link my_followers := .<people_i_follow[is Person];
    link best_friend -> Person {
        on target delete allow;
    }
    property full_name :=
      .first_name ++ ' ' ++ .last_name
      if exists .last_name
      else .first_name;
  }
```


Discussion:
It appears to be something to do with the SELECT at the end because when I just bulk add I'm currently not getting this error.
The bug seems to be in fetching a computed property of a union of inserts.

Looks like it works if you drop `full_name` from the select.

Resolving patch:
diff --git a/edb/schema/pointers.py b/edb/schema/pointers.py
--- a/edb/schema/pointers.py
+++ b/edb/schema/pointers.py
@@ -2700,6 +2700,9 @@ def get_or_create_union_pointer(
             f'with other versions of the same {p.get_verbosename(schema)}',
         )
 
+    if len(components) == 1 and direction is PointerDirection.Outbound:
+        return schema, components[0]
+
     far_endpoints = [p.get_far_endpoint(schema, direction)
                      for p in components]
     targets: List[s_types.Type] = [p for p in far_endpoints


Test patch:
diff --git a/tests/test_edgeql_select.py b/tests/test_edgeql_select.py
--- a/tests/test_edgeql_select.py
+++ b/tests/test_edgeql_select.py
@@ -2629,6 +2629,40 @@ async def test_edgeql_select_setops_22(self):
         for row in res:
             self.assertNotEqual(row[1].id, None)
 
+    async def test_edgeql_select_setops_23(self):
+        await self.assert_query_result(
+            r"""
+            WITH X := (insert Publication { title := "x" }),
+                 Y := (insert Publication { title := "y" }),
+                 foo := X union Y,
+            select foo { title1 };
+            """,
+            tb.bag([
+                {'title1': 'x'},
+                {'title1': 'y'},
+            ])
+        )
+
+        await self.assert_query_result(
+            r"""
+            WITH X := (select Publication filter .title = 'x'),
+                 Y := (select Publication filter .title = 'y'),
+                 foo := X union Y,
+            select foo { title1 };
+            """,
+            tb.bag([
+                {'title1': 'x'},
+                {'title1': 'y'},
+            ])
+        )
+
+        await self.assert_query_result(
+            r"""
+            SELECT (Issue UNION <Issue>{}).number;
+            """,
+            {'1', '2', '3', '4'},
+        )
+
     async def test_edgeql_select_order_01(self):
         await self.assert_query_result(
             r'''