Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InternalServerError when not using a variable defined in with in a query #6279

Closed
gregdan3 opened this issue Oct 11, 2023 · 4 comments · Fixed by #6287
Closed

InternalServerError when not using a variable defined in with in a query #6279

gregdan3 opened this issue Oct 11, 2023 · 4 comments · Fixed by #6287
Assignees
Labels

Comments

@gregdan3
Copy link

  • EdgeDB Version: 3.4+301ba34
  • EdgeDB CLI Version: EdgeDB CLI 3.5.0+907ff37
  • OS Version: ArchLinux 6.5.6-arch2-1

Steps to Reproduce:

  1. Construct the following schema:
module platform {
  type Actor {
    required name: str;
  }
  type Post {
    required text: str;
    single link parent_post: Post;
    single link author: Actor;
    multi link posts := .<parent_post[is Post];
  }
}
  1. Run the following queries (here as .py, but the result is the same in the repl):
import edgedb
client = edgedb.create_client()
for tx in client.transaction():
    with tx:
        tx.execute(
            """
            INSERT platform::Actor {
                name := "Foo"
            };

            INSERT platform::Actor {
                name := "Bar"
            };
            """
        )
        tx.execute(
            """
            with parent := (
                INSERT platform::Post {
                    text := "Foo's Post",
                    author := (SELECT platform::Actor filter .name = "Foo" limit 1)
                }
            )

            INSERT platform::Post {
                text := "Bar's Post",
                author := (SELECT platform::Actor filter .name = "Bar" limit 1),
            };
            """
        )
client.close()

This triggers the following error:

edgedb error: InternalServerError: None
  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/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/__init__.py", line 135, in compile_ir_to_sql_tree
          qtree = dispatch.compile(ir_expr, ctx=ctx)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 60, in compile_Set
          _compile_set_impl(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 115, in _compile_set_impl
          _compile_set(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 701, in _compile_set
          relgen.get_set_rvar(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 164, in get_set_rvar
          return _process_toplevel_query(ir_set, ctx=ctx)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 286, in _process_toplevel_query
          rvars = _get_set_rvar(ir_set, ctx=ctx)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 348, in _get_set_rvar
          return process_set_as_subquery(ir_set, ctx=ctx)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 1425, in process_set_as_subquery
          dispatch.visit(expr, ctx=newctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dispatch.py", line 44, in visit
          compile(ir, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/stmt.py", line 148, in compile_InsertStmt
          parts = dml.init_dml_stmt(stmt, ctx=ctx)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dml.py", line 95, in init_dml_stmt
          clauses.compile_dml_bindings(ir_stmt, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/clauses.py", line 251, in compile_dml_bindings
          dispatch.compile(binding, ctx=bctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 60, in compile_Set
          _compile_set_impl(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 111, in _compile_set_impl
          _compile_set(ir_set, ctx=scopectx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 701, in _compile_set
          relgen.get_set_rvar(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 222, in get_set_rvar
          rvars = _get_set_rvar(ir_set, ctx=subctx)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 348, in _get_set_rvar
          return process_set_as_subquery(ir_set, ctx=ctx)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 1425, in process_set_as_subquery
          dispatch.visit(expr, ctx=newctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dispatch.py", line 44, in visit
          compile(ir, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/stmt.py", line 156, in compile_InsertStmt
          dml.process_insert_body(
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dml.py", line 691, in process_insert_body
          external_inserts = process_insert_shape(
                             ^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dml.py", line 1017, in process_insert_shape
          compile_insert_shape_element(
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dml.py", line 1090, in compile_insert_shape_element
          dispatch.visit(shape_el, ctx=insvalctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 85, in visit_Set
          _compile_set_impl(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 111, in _compile_set_impl
          _compile_set(ir_set, ctx=scopectx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 701, in _compile_set
          relgen.get_set_rvar(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 222, in get_set_rvar
          rvars = _get_set_rvar(ir_set, ctx=subctx)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 348, in _get_set_rvar
          return process_set_as_subquery(ir_set, ctx=ctx)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 1425, in process_set_as_subquery
          dispatch.visit(expr, ctx=newctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dispatch.py", line 44, in visit
          compile(ir, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/stmt.py", line 88, in compile_SelectStmt
          outvar = clauses.compile_output(stmt.result, ctx=ictx)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/clauses.py", line 225, in compile_output
          dispatch.visit(ir_set, ctx=newctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 85, in visit_Set
          _compile_set_impl(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 111, in _compile_set_impl
          _compile_set(ir_set, ctx=scopectx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/expr.py", line 701, in _compile_set
          relgen.get_set_rvar(ir_set, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 222, in get_set_rvar
          rvars = _get_set_rvar(ir_set, ctx=subctx)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 348, in _get_set_rvar
          return process_set_as_subquery(ir_set, ctx=ctx)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/relgen.py", line 1425, in process_set_as_subquery
          dispatch.visit(expr, ctx=newctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/dispatch.py", line 44, in visit
          compile(ir, ctx=ctx)
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/functools.py", line 909, in wrapper
          return dispatch(args[0].__class__)(*args, **kw)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/stmt.py", line 99, in compile_SelectStmt
          clauses.compile_filter_clause(
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/clauses.py", line 263, in compile_filter_clause
          assert cardinality != qltypes.Cardinality.UNKNOWN
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      AssertionError

      The above exception was the direct cause of the following exception:

      Traceback (most recent call last):
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/server/compiler_pool/worker.py", line 161, in compile
          units, cstate = COMPILER.compile(
                          ^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 856, in compile
          unit_group = compile(ctx=ctx, source=source)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 2068, in compile
          return _try_compile(ctx=ctx, source=source)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 2136, in _try_compile
          comp, capabilities = _compile_dispatch_ql(
                               ^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 2042, in _compile_dispatch_ql
          query = _compile_ql_query(ctx, ql, script_info=script_info)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/server/compiler/compiler.py", line 1588, in _compile_ql_query
          qtree, sql_text, argmap = pg_compiler.compile_ir_to_tree_and_sql(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/__init__.py", line 170, in compile_ir_to_tree_and_sql
          qtree, _, argmap = compile_ir_to_sql_tree(
                             ^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/gregdan3/.local/share/edgedb/portable/3.4/lib/python3.11/site-packages/edb/pgsql/compiler/__init__.py", line 152, in compile_ir_to_sql_tree
          raise errors.InternalServerError(*args) from e
      edb.errors.InternalServerError: None

The expected behavior is that the syntax error (?) should be identified and warned against.

(?): Is it a syntax error to not use a variable, like it is in Go? Or is this not meant to be an error at all?
Either way, the error is resolved by using the parent variable to fill in the parent_post property of the second insert to Post.

@raddevon raddevon added the bug label Oct 11, 2023
@msullivan msullivan self-assigned this Oct 12, 2023
@msullivan
Copy link
Member

(?): Is it a syntax error to not use a variable, like it is in Go? Or is this not meant to be an error at all? Either way, the error is resolved by using the parent variable to fill in the parent_post property of the second insert to Post.

It used to be, but we started allowing it in 3.0.
I'm surprised this one doesn't work; I know there are similar tests. I'll take a look.

@msullivan
Copy link
Member

msullivan commented Oct 12, 2023

As a workaround, until this is fixed, wrapping the second insert in a select (...) fixes it. The bug is only when the body of the WITH is itself an INSERT.

msullivan added a commit that referenced this issue Oct 12, 2023
The problem was cardinality inference wasn't getting run on it.  If
the body of the WITH was something other than an INSERT, cardinality
inference happened somewhat by accident, since multiplicity inference
was done properly.

Fixes #6279.
msullivan added a commit that referenced this issue Oct 13, 2023
The problem was cardinality inference wasn't getting run on it.  If
the body of the WITH was something other than an INSERT, cardinality
inference happened somewhat by accident, since multiplicity inference
was done properly.

Fixes #6279.
msullivan added a commit that referenced this issue Oct 13, 2023
The problem was cardinality inference wasn't getting run on it.  If
the body of the WITH was something other than an INSERT, cardinality
inference happened somewhat by accident, since multiplicity inference
was done properly.

Fixes #6279.
aljazerzen pushed a commit that referenced this issue Oct 16, 2023
The problem was cardinality inference wasn't getting run on it.  If
the body of the WITH was something other than an INSERT, cardinality
inference happened somewhat by accident, since multiplicity inference
was done properly.

Fixes #6279.
@MartinCura
Copy link

Not sure if it's exactly the same but in many cases if i declare a variable inside a with and i don't use the variable, then InternalServerError: None is raised (without any explanation).

Example:

Processing /home/martin/Code/myapp/api/myapp/db/upsert_housing_from_search.edgeql
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/codegen/__main__.py", line 23, in <module>
    main()
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/codegen/cli.py", line 99, in main
    generator.Generator(args).run()
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/codegen/generator.py", line 212, in run
    self._process_dir(search_dir)
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/codegen/generator.py", line 235, in _process_dir
    self._process_file(file_or_dir)
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/codegen/generator.py", line 247, in _process_file
    dr = self._client._describe_query(query, inject_type_names=True)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/blocking_client.py", line 407, in _describe_query
    return self._iter_coroutine(self._describe(abstract.DescribeContext(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/blocking_client.py", line 360, in _iter_coroutine
    coro.send(None)
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/base_client.py", line 762, in _describe
    return await con.describe(describe_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/martin/Code/myapp/api/.venv/lib/python3.12/site-packages/edgedb/base_client.py", line 296, in describe
    cardinality, in_dc, out_dc, capabilities = await self._protocol._parse(
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "edgedb/protocol/protocol.pyx", line 322, in _parse
edgedb.errors.InternalServerError: None

@aljazerzen
Copy link
Contributor

Could you give an example query with the schema that you are using?

This should not be happening, we need to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants