Skip to content

Commit

Permalink
Fix SQL and empty queries (#5776)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen committed Jul 17, 2023
1 parent 4578bfd commit ae3c982
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions edb/common/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ class flags(metaclass=FlagsMeta):
"Requires pydebug to be installed."
)

sql_input = Flag(
doc="Enable logging of SQL incoming requests (pg compiler input)."
)

sql_output = Flag(
doc="Enable logging of SQL requests, compiled to the internal SQL"
"(pg compiler output)."
)


@contextlib.contextmanager
def timeit(title='block'):
Expand Down
17 changes: 16 additions & 1 deletion edb/server/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,13 @@ def compute_stmt_name(text: str) -> str:
sql_units = []
for stmt in stmts:
orig_text = pg_gen_source(stmt)

if debug.flags.sql_input:
debug.header('SQL Input')
debug.dump_code(
pg_codegen.generate_source(stmt, pretty=True), lexer='sql'
)

unit_ctor = functools.partial(
dbstate.SQLQueryUnit,
orig_query=orig_text,
Expand Down Expand Up @@ -765,14 +772,22 @@ def compute_stmt_name(text: str) -> str:
translation_data=source.translation_data,
)

if debug.flags.sql_output:
debug.header('SQL Output')
debug.dump_code(unit.query, lexer='sql')

unit.stmt_name = compute_stmt_name(unit.query).encode("utf-8")

tx_state.apply(unit)
sql_units.append(unit)

if not sql_units:
# Cluvio will try to execute an empty query
sql_units.append(dbstate.SQLQueryUnit(query=""))
sql_units.append(dbstate.SQLQueryUnit(
orig_query='',
query='',
fe_settings=tx_state.current_fe_settings()
))

return sql_units

Expand Down
3 changes: 3 additions & 0 deletions tests/test_sql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,3 +1043,6 @@ async def test_sql_query_prepare_error_01(self):
position=str(len(query) - 3),
):
await self.scon.execute(query)

async def test_sql_query_empty(self):
await self.scon.executemany('', args=[])

0 comments on commit ae3c982

Please sign in to comment.