From 73a8fc7698ae2907478096f63b6d251f72cc6a7c Mon Sep 17 00:00:00 2001 From: Sean Wu <111744549+VWagen1989@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:28:58 +0800 Subject: [PATCH] fix: RESET ALL is not supported yet --- pgserver/connection_handler.go | 13 +++++- pgserver/sess_params_test.go | 75 +++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/pgserver/connection_handler.go b/pgserver/connection_handler.go index 00c27f45..3dc352e1 100644 --- a/pgserver/connection_handler.go +++ b/pgserver/connection_handler.go @@ -82,7 +82,7 @@ var showVariablesRegex = regexp.MustCompile(`(?i)^\s*SHOW\s+(ALL|[a-zA-Z_][a-zA- var setStmtRegex = regexp.MustCompile(`(?i)^\s*SET\s+(SESSION|LOCAL)?\s*([a-zA-Z_][a-zA-Z0-9_]+)\s*(TO|=)\s*(DEFAULT|'([^']*)'|"([^"]*)"|[^'"\s;]+)\s*;?\s*$`) // precompile a regex to match "RESET xxx;". -var resetStmtRegex = regexp.MustCompile(`(?i)^\s*RESET\s+([a-zA-Z_][a-zA-Z0-9_]+)\s*;?\s*$`) +var resetStmtRegex = regexp.MustCompile(`(?i)^\s*RESET\s+(ALL|[a-zA-Z_][a-zA-Z0-9_]+)\s*;?\s*$`) // precompile a regex to match any "from pg_catalog.xxx" in the query. var pgCatalogRegex = regexp.MustCompile(`(?i)\s+from\s+pg_catalog\.`) @@ -764,7 +764,16 @@ var pgCatalogHandlers = map[*regexp.Regexp]PGCatalogHandler{ // This is a configuration of DuckDB, it should be bypassed to DuckDB return false, nil } - return h.setPgSessionVar(key, nil, true, "RESET") + if key != "all" { + return h.setPgSessionVar(key, nil, true, "RESET") + } + // TODO(sean): Implement RESET ALL + _ = h.send(&pgproto3.ErrorResponse{ + Severity: string(ErrorResponseSeverity_Error), + Code: "0A000", + Message: "Statement 'RESET ALL' is not supported yet.", + }) + return true, nil }, }, pgCatalogRegex: { diff --git a/pgserver/sess_params_test.go b/pgserver/sess_params_test.go index 10b9348d..1c5ea7c4 100644 --- a/pgserver/sess_params_test.go +++ b/pgserver/sess_params_test.go @@ -173,25 +173,25 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to 'myDUCK' { - SQL: "SET application_name TO 'myDUCK';", + SQL: " \t\r\nSET application_name TO 'myDUCK'; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Set the application_name to "MYduck" { - SQL: "SET application_NAME TO \"MYduck\";", + SQL: " \t\r\nSET application_NAME TO \"MYduck\"; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be 'MYduck' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"MYduck"}}, WantErr: false, }, @@ -202,25 +202,25 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to 'myDUCK' { - SQL: "SET application_name TO 'myDUCK';", + SQL: " \t\r\nSET application_name TO 'myDUCK'; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Set the application_name to 'default' { - SQL: "SET application_NAME TO 'default';", + SQL: " \t\r\nSET application_NAME TO 'default'; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be 'default' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"default"}}, WantErr: false, }, @@ -231,25 +231,25 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to myDUCK { - SQL: "SET application_name TO myDUCK;", + SQL: " \t\r\nSET application_name TO myDUCK; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Set the application_name to default { - SQL: "SET application_NAME TO default;", + SQL: " \t\r\nSET application_NAME TO default; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be default value 'psql' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"psql"}}, WantErr: false, }, @@ -262,25 +262,25 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to 'myDUCK' { - SQL: "SET http_proxy_username TO 'myDUCK';", + SQL: " \t\r\nSET http_proxy_username TO 'myDUCK'; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Set the application_name to "MYduck" { - SQL: "SET http_proxy_USERNAME TO \"MYduck\";", + SQL: " \t\r\nSET http_proxy_USERNAME TO \"MYduck\"; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{"MYduck"}}, WantErr: false, }, @@ -291,25 +291,25 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to myDUCK { - SQL: "SET http_proxy_username TO myDUCK;", + SQL: " \t\r\nSET http_proxy_username TO myDUCK; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Set the application_name to 'default' { - SQL: "SET http_proxy_USERNAME TO 'default';", + SQL: " \t\r\nSET http_proxy_USERNAME TO 'default'; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be 'default' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{"default"}}, WantErr: false, }, @@ -320,25 +320,25 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to myDUCK { - SQL: "SET http_proxy_username TO myDUCK;", + SQL: " \t\r\nSET http_proxy_username TO myDUCK; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Set the application_name to default { - SQL: "SET http_proxy_USERNAME TO default;", + SQL: " \t\r\nSET http_proxy_USERNAME TO default; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be '' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{""}}, WantErr: false, }, @@ -351,25 +351,25 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to myDUCK { - SQL: "SET application_name TO myDUCK;", + SQL: " \t\r\nSET application_name TO myDUCK; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Reset the application_name to default { - SQL: "reSET application_NAME;", + SQL: " \t\r\nreSET application_NAME; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of application_name, it should be default value 'psql' { - SQL: "SELECT CURRENT_SETTING('application_name');", + SQL: " \t\r\nSELECT CURRENT_SETTING('application_name'); \t\r\n", Expected: [][]string{{"psql"}}, WantErr: false, }, @@ -382,30 +382,41 @@ func TestSessParam(t *testing.T) { executions: []Execution{ // Set the application_name to myDUCK { - SQL: "SET http_proxy_username TO myDUCK;", + SQL: " \t\r\nSET http_proxy_username TO myDUCK; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be 'myDUCK' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{"myDUCK"}}, WantErr: false, }, // Reset the application_name to default { - SQL: "reSET http_proxy_USERNAME;", + SQL: " \t\r\nreSET http_proxy_USERNAME; \t\r\n", Expected: nil, WantErr: false, }, // Get the value of http_proxy_username, it should be '' { - SQL: "SELECT CURRENT_SETTING('http_proxy_username');", + SQL: " \t\r\nSELECT CURRENT_SETTING('http_proxy_username'); \t\r\n", Expected: [][]string{{""}}, WantErr: false, }, }, }, + { + name: "Reset ALL", + executions: []Execution{ + { + // RESET ALL is not supported yet. + SQL: "RESET ALL;", + Expected: nil, + WantErr: true, + }, + }, + }, } // Setup MyDuck Server