From c1a31a92fec434fe07498bc998680c9370cccd7c Mon Sep 17 00:00:00 2001 From: Noy Date: Tue, 19 Nov 2024 15:40:27 +0800 Subject: [PATCH] chore: refine the replica-setup-pg and its doc --- devtools/replica-setup-pg/README.md | 4 ++-- devtools/replica-setup-pg/install_psql.sh | 3 --- devtools/replica-setup-pg/replica_setup.sh | 11 ++++------ .../{snapshot.sh => snapshot_dump.sh} | 0 pgserver/connection_handler.go | 6 ++++- pgserver/duck_handler.go | 3 +++ pgserver/query_modifier.go | 22 +++++++++++++++++++ 7 files changed, 36 insertions(+), 13 deletions(-) delete mode 100644 devtools/replica-setup-pg/install_psql.sh rename devtools/replica-setup-pg/{snapshot.sh => snapshot_dump.sh} (100%) create mode 100644 pgserver/query_modifier.go diff --git a/devtools/replica-setup-pg/README.md b/devtools/replica-setup-pg/README.md index 715b4d7c..f9da8bed 100644 --- a/devtools/replica-setup-pg/README.md +++ b/devtools/replica-setup-pg/README.md @@ -27,7 +27,7 @@ bash replica_setup.sh --pg_dump /path/to/pg_dump ## Example ```bash -bash replica_setup.sh --pg_dump ../../backup.sql +bash replica_setup.sh --pg_dump pg_dump.sql ``` -This command sets up MyDuck Server as a replica of the PostgreSQL instance running at `192.168.1.100` on port `3306` with the user `root` and password `mypassword`. \ No newline at end of file +This command sets up MyDuck Server as a replica of the PostgreSQL instance running at `192.168.1.100` on port `5432` with the user `root` and password `mypassword`. \ No newline at end of file diff --git a/devtools/replica-setup-pg/install_psql.sh b/devtools/replica-setup-pg/install_psql.sh deleted file mode 100644 index 32fdc721..00000000 --- a/devtools/replica-setup-pg/install_psql.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -# TODO: Implement the install_psql function \ No newline at end of file diff --git a/devtools/replica-setup-pg/replica_setup.sh b/devtools/replica-setup-pg/replica_setup.sh index ad9d7b0e..60bb36c5 100644 --- a/devtools/replica-setup-pg/replica_setup.sh +++ b/devtools/replica-setup-pg/replica_setup.sh @@ -62,13 +62,10 @@ if [[ -z $MYDUCK_DB ]]; then usage fi -# Step 1: Check if psql exists, if not, install it +# Step 1: Check if psql exists if ! command -v psql &> /dev/null; then - echo "psql not found, attempting to install..." - bash install_psql.sh - check_command "psql installation" -else - echo "psql is already installed." + echo "Error: psql is not installed." + exit 1 fi # Step 2: Check if replication has already been started @@ -92,7 +89,7 @@ fi # Step 5: Load the existing data from pg_dump file if [[ -n "$PG_DUMP" ]]; then echo "Loading the snapshot from pg_dump to MyDuck Server..." - source snapshot.sh + source snapshot_dump.sh check_command "loading a snapshot from pg_dump" else echo "No pg_dump file specified. Skipping snapshot." diff --git a/devtools/replica-setup-pg/snapshot.sh b/devtools/replica-setup-pg/snapshot_dump.sh similarity index 100% rename from devtools/replica-setup-pg/snapshot.sh rename to devtools/replica-setup-pg/snapshot_dump.sh diff --git a/pgserver/connection_handler.go b/pgserver/connection_handler.go index 85c018ff..47a9c62b 100644 --- a/pgserver/connection_handler.go +++ b/pgserver/connection_handler.go @@ -1034,7 +1034,11 @@ func (h *ConnectionHandler) sendError(err error) { } // convertQuery takes the given Postgres query, and converts it as an ast.ConvertedQuery that will work with the handler. -func (h *ConnectionHandler) convertQuery(query string) (ConvertedQuery, error) { +func (h *ConnectionHandler) convertQuery(query string, modifiers ...QueryModifier) (ConvertedQuery, error) { + for _, modifier := range modifiers { + query = modifier(query) + } + parsable := true stmts, err := parser.Parse(query) if err != nil { diff --git a/pgserver/duck_handler.go b/pgserver/duck_handler.go index 63e3058e..6884c619 100644 --- a/pgserver/duck_handler.go +++ b/pgserver/duck_handler.go @@ -638,6 +638,9 @@ func resultForOkIter(ctx *sql.Context, iter sql.RowIter) (*Result, error) { // resultForEmptyIter ensures that an expected empty iterator returns no rows. func resultForEmptyIter(ctx *sql.Context, iter sql.RowIter) (*Result, error) { defer trace.StartRegion(ctx, "DuckHandler.resultForEmptyIter").End() + if iter == nil { + return &Result{Fields: nil}, nil + } if _, err := iter.Next(ctx); err != io.EOF { return nil, fmt.Errorf("result schema iterator returned more than zero rows") } diff --git a/pgserver/query_modifier.go b/pgserver/query_modifier.go new file mode 100644 index 00000000..f9f18559 --- /dev/null +++ b/pgserver/query_modifier.go @@ -0,0 +1,22 @@ +package pgserver + +import "regexp" + +type QueryModifier func(string) string + +func NewQueryReplacer(regex string, replacement string) QueryModifier { + r := regexp.MustCompile(regex) + return func(query string) string { + return r.ReplaceAllString(query, replacement) + } + +} + +func NewQueryRemover(regex string) QueryModifier { + return NewQueryReplacer(regex, "") +} + +var ( + removeLocaleProvider = NewQueryRemover(`(?i)LOCALE_PROVIDER = [^ ;]*`) + removeLocale = NewQueryRemover(`(?i)LOCALE = [^ ;]*`) +)