Skip to content

Commit

Permalink
Show a better message for SET/RESET/SHOW over SQL native protocol (#8056
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aljazerzen authored Dec 5, 2024
1 parent 535620f commit 82fac25
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion edb/pgsql/resolver/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _raise_unsupported(expr: pgast.Base) -> typing.Never:
# title case to spaces
pretty_name = re.sub(r'(?<!^)(?=[A-Z])', ' ', pretty_name).upper()

raise errors.QueryError(
raise errors.UnsupportedFeatureError(
f'not supported: {pretty_name}',
span=expr.span,
pgext_code=pgerror.ERROR_FEATURE_NOT_SUPPORTED,
Expand Down
2 changes: 1 addition & 1 deletion edb/server/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def compile_sql(
apply_access_policies_sql=apply_access_policies_sql,
disambiguate_column_names=False,
backend_runtime_params=self.state.backend_runtime_params,
protocol_version=(-3, 0), # emulated PG binary protocol version
protocol_version=defines.POSTGRES_PROTOCOL,
)

def compile_serialized_request(
Expand Down
12 changes: 12 additions & 0 deletions edb/server/compiler/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def compile_sql(
)

if isinstance(stmt, (pgast.VariableSetStmt, pgast.VariableResetStmt)):
if protocol_version != defines.POSTGRES_PROTOCOL:
from edb.pgsql import resolver as pg_resolver
pg_resolver.dispatch._raise_unsupported(stmt)

value: Optional[dbstate.SQLSetting]
if isinstance(stmt, pgast.VariableSetStmt):
value = pg_arg_list_to_python(stmt.args)
Expand Down Expand Up @@ -137,6 +141,10 @@ def compile_sql(
unit.capabilities |= enums.Capability.SESSION_CONFIG

elif isinstance(stmt, pgast.VariableShowStmt):
if protocol_version != defines.POSTGRES_PROTOCOL:
from edb.pgsql import resolver as pg_resolver
pg_resolver.dispatch._raise_unsupported(stmt)

unit.get_var = stmt.name
unit.frontend_only = (
stmt.name in FE_SETTINGS_MUTABLE
Expand All @@ -146,6 +154,10 @@ def compile_sql(
unit.command_complete_tag = dbstate.TagPlain(tag=b"SHOW")

elif isinstance(stmt, pgast.SetTransactionStmt):
if protocol_version != defines.POSTGRES_PROTOCOL:
from edb.pgsql import resolver as pg_resolver
pg_resolver.dispatch._raise_unsupported(stmt)

if stmt.scope == pgast.OptionsScope.SESSION:
unit.set_vars = {
f"default_{name}": (
Expand Down
3 changes: 3 additions & 0 deletions edb/server/defines.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
MIN_PROTOCOL: ProtocolVersion = (1, 0)
CURRENT_PROTOCOL: ProtocolVersion = (3, 0)

# Emulated PG binary protocol
POSTGRES_PROTOCOL: ProtocolVersion = (-3, 0)

MIN_SUGGESTED_CLIENT_POOL_SIZE = 10
MAX_SUGGESTED_CLIENT_POOL_SIZE = 100

Expand Down
7 changes: 6 additions & 1 deletion edb/server/protocol/pg_ext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ cdef class PgConnection(frontend.FrontendConnection):
str(exc), detail=exc.details, severity="FATAL"
)
elif isinstance(exc, errors.UnsupportedFeatureError):
exc = pgerror.FeatureNotSupported(str(exc))
exc = pgerror.new(
pgerror.ERROR_FEATURE_NOT_SUPPORTED,
str(exc),
L=str(exc.line) if exc.line >= 0 else None,
P=str(exc.position + 1) if exc.position >= 0 else None,
)
elif isinstance(exc, errors.EdgeDBError):
args = dict(hint=exc.hint, detail=exc.details)
if exc.line >= 0:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_sql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2720,3 +2720,32 @@ async def test_native_sql_query_15(self):
'SELECT title FROM "Content" ORDER BY title',
[{'title': 'Forrest Gump'}]
)

async def test_native_sql_query_16(self):
with self.assertRaisesRegex(
edgedb.UnsupportedFeatureError,
"not supported: VARIABLE SET",
_position=14, # this point to `1`, but hey, better than nothing
):
await self.assert_sql_query_result(
'SET my_var TO 1',
[]
)

with self.assertRaisesRegex(
edgedb.UnsupportedFeatureError,
"not supported: VARIABLE RESET",
):
await self.assert_sql_query_result(
'RESET my_var',
[]
)

with self.assertRaisesRegex(
edgedb.UnsupportedFeatureError,
"not supported: VARIABLE SHOW",
):
await self.assert_sql_query_result(
'SHOW my_var',
[]
)

0 comments on commit 82fac25

Please sign in to comment.