Repository: edgedb/edgedb

Issue:
[is ...].id ?? fails with no attribute error
<!-- 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: 3.0-beta.1+4cdeba3
- EdgeDB CLI Version: 3.0.0-dev.982+9a1586d
- OS Version: macOS m1

Steps to Reproduce:

1. `create abstract type A;`
2. `create type B extending A;`
3. `select A[is B].id ?? <uuid>'00000000-0000-0000-0000-000000000000';` fails:

```
edgedb error: InternalServerError: 'TypeIntersectionPointerRef' object has no attribute 'id'
  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 "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/__init__.py", line 134, in compile_ir_to_sql_tree
          qtree = dispatch.compile(ir_expr, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/expr.py", line 60, in compile_Set
          _compile_set_impl(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/expr.py", line 114, in _compile_set_impl
          _compile_set(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/expr.py", line 700, in _compile_set
          relgen.get_set_rvar(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/relgen.py", line 164, in get_set_rvar
          return _process_toplevel_query(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/relgen.py", line 286, in _process_toplevel_query
          rvars = _get_set_rvar(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/relgen.py", line 348, in _get_set_rvar
          return process_set_as_subquery(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/relgen.py", line 1389, in process_set_as_subquery
          dispatch.visit(expr, ctx=newctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/dispatch.py", line 44, in visit
          compile(ir, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/stmt.py", line 88, in compile_SelectStmt
          outvar = clauses.compile_output(stmt.result, ctx=ictx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/clauses.py", line 225, in compile_output
          dispatch.visit(ir_set, ctx=newctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/expr.py", line 85, in visit_Set
          _compile_set_impl(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/expr.py", line 114, in _compile_set_impl
          _compile_set(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/expr.py", line 700, in _compile_set
          relgen.get_set_rvar(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/relgen.py", line 222, in get_set_rvar
          rvars = _get_set_rvar(ir_set, ctx=subctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/relgen.py", line 355, in _get_set_rvar
          return func.func(ir_set, ctx=ctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/relgen.py", line 1740, in process_set_as_coalesce
          left = dispatch.compile(left_ir, ctx=newctx)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/functools.py", line 889, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/expr.py", line 71, in compile_Set
          value = pathctx.get_path_value_var(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 552, in get_path_value_var
          return get_path_var(rel, path_id, aspect='value', env=env)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 286, in get_path_var
          outvar = get_path_output(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1120, in get_path_output
          return _get_path_output(rel, path_id=path_id, aspect=aspect,
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1181, in _get_path_output
          ref = get_path_var(rel, path_id, aspect=aspect, flavor=flavor, env=env)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 286, in get_path_var
          outvar = get_path_output(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1120, in get_path_output
          return _get_path_output(rel, path_id=path_id, aspect=aspect,
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1181, in _get_path_output
          ref = get_path_var(rel, path_id, aspect=aspect, flavor=flavor, env=env)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 286, in get_path_var
          outvar = get_path_output(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1120, in get_path_output
          return _get_path_output(rel, path_id=path_id, aspect=aspect,
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1181, in _get_path_output
          ref = get_path_var(rel, path_id, aspect=aspect, flavor=flavor, env=env)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 158, in get_path_var
          return _get_path_var_in_setop(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 375, in _get_path_var_in_setop
          test_vals = [
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 376, in <listcomp>
          maybe_get_path_var(q, env=env, path_id=path_id, aspect=aspect)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 579, in maybe_get_path_var
          return get_path_var(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 286, in get_path_var
          outvar = get_path_output(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1120, in get_path_output
          return _get_path_output(rel, path_id=path_id, aspect=aspect,
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1162, in _get_path_output
          id_output = maybe_get_path_output(rel, src_path_id,
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1303, in maybe_get_path_output
          return get_path_output(rel, path_id=path_id, aspect=aspect,
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1120, in get_path_output
          return _get_path_output(rel, path_id=path_id, aspect=aspect,
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1171, in _get_path_output
          return _get_rel_path_output(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/pathctx.py", line 1022, in _get_rel_path_output
          ptr_info = pg_types.get_ptrref_storage_info(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/types.py", line 482, in get_ptrref_storage_info
          return _get_ptrref_storage_info(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/types.py", line 542, in _get_ptrref_storage_info
          col_name = str(ptrref.id)
      AttributeError: 'TypeIntersectionPointerRef' object has no attribute 'id'

      The above exception was the direct cause of the following exception:

      Traceback (most recent call last):
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/server/compiler_pool/worker_proc.py", line 55, in worker
          res = meth(*args)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/server/compiler_pool/worker.py", line 161, in compile
          units, cstate = COMPILER.compile(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 745, in compile
          unit_group = compile(ctx=ctx, source=source)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1927, in compile
          return _try_compile(ctx=ctx, source=source)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1995, in _try_compile
          comp, capabilities = _compile_dispatch_ql(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1901, in _compile_dispatch_ql
          query = _compile_ql_query(ctx, ql, script_info=script_info)
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1473, in _compile_ql_query
          qtree, sql_text, argmap = pg_compiler.compile_ir_to_tree_and_sql(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/__init__.py", line 169, in compile_ir_to_tree_and_sql
          qtree, _ = compile_ir_to_sql_tree(
        File "/Users/daisy/Library/Application Support/edgedb/portable/3.0-beta.1/lib/python3.10/site-packages/edb/pgsql/compiler/__init__.py", line 151, in compile_ir_to_sql_tree
          raise errors.InternalServerError(*args) from e
      edb.errors.InternalServerError: 'TypeIntersectionPointerRef' object has no attribute 'id'
```

<!-- If the issue is about a query error, please also provide your schema -->

Schema:

```
    abstract type A;
    type B extending A;
```


Discussion:

Resolving patch:
diff --git a/edb/pgsql/compiler/pathctx.py b/edb/pgsql/compiler/pathctx.py
--- a/edb/pgsql/compiler/pathctx.py
+++ b/edb/pgsql/compiler/pathctx.py
@@ -1018,7 +1018,7 @@ def _get_rel_path_output(
             ptrref = actual_ptrref
 
     ptr_info = None
-    if ptrref:
+    if ptrref and not isinstance(ptrref, irast.TypeIntersectionPointerRef):
         ptr_info = pg_types.get_ptrref_storage_info(
             ptrref, resolve_type=False, link_bias=False)
 


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
@@ -7313,6 +7313,12 @@ async def test_edgeql_expr_type_intersection_03(self):
                 SELECT Object.id[IS uuid];
             ''')
 
+    async def test_edgeql_expr_type_intersection_04(self):
+        await self.con.execute('''\
+            SELECT Named[IS Issue].id
+                ?? <uuid>'00000000-0000-0000-0000-000000000000';
+        ''')
+
     async def test_edgeql_expr_comparison_01(self):
         with self.assertRaisesRegex(
                 edgedb.QueryError,