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

[YSQL] ysql_dump should consistently use quotes on column names #6061

Closed
ddorian opened this issue Oct 15, 2020 · 5 comments
Closed

[YSQL] ysql_dump should consistently use quotes on column names #6061

ddorian opened this issue Oct 15, 2020 · 5 comments
Assignees
Labels
area/ysql Yugabyte SQL (YSQL)

Comments

@ddorian
Copy link
Contributor

ddorian commented Oct 15, 2020

On 2.3.2.0:
1.

kirankg=# create table chat_user("chatID" text NOT NULL, PRIMARY KEY("chatID")); 
kirankg=# \d+ chat_user
 Column | Type | Collation | Nullable | Default | Storage  | Stats target | Description 
--------+------+-----------+----------+---------+----------+--------------+-------------
 chatID | text |           | not null |         | extended |              | 
Indexes:
    "chat_user_pkey" PRIMARY KEY, lsm ("chatID" HASH)
  1. Export with ysql_dump:
./postgres/bin/ysql_dump kirankg -f my_db.sql
  1. Output is:
CREATE TABLE public.chat_user (
    "chatID" text NOT NULL,
    PRIMARY KEY((chatID) HASH)
);

Which is not valid even in normal PostgreSQL:

kirankg=# CREATE TABLE public.chat_user ("chatID" text NOT NULL, PRIMARY KEY((chatID) HASH) );
ERROR:  column "chatid" named in key does not exist
LINE 1: ...E TABLE public.chat_user ("chatID" text NOT NULL, PRIMARY KE...

The column names have to be inside/outside quotes in both cases.

@ddorian ddorian added the area/ysql Yugabyte SQL (YSQL) label Oct 15, 2020
@ddorian ddorian changed the title [YSQL] ysql_dump doesn't correct use [YSQL] ysql_dump doesn't correctly use quotes on column names Oct 15, 2020
@ddorian ddorian changed the title [YSQL] ysql_dump doesn't correctly use quotes on column names [YSQL] ysql_dump doesn't consistently use quotes on column names Oct 15, 2020
@tedyu
Copy link
Contributor

tedyu commented Oct 15, 2020

We can utilize --quote-all-identifiers option. Though there is code change required.

Or, calling fmtId() is simpler fix.
The generated script becomes valid SQL.

@tedyu tedyu changed the title [YSQL] ysql_dump doesn't consistently use quotes on column names [YSQL] ysql_dump should consistently use quotes on column names Oct 15, 2020
@ddorian
Copy link
Contributor Author

ddorian commented Oct 19, 2020

@tedyu --quote-all-identifiers doesn't work:

$ ./postgres/bin/ysql_dump --quote-all-identifiers kirankg
.....
CREATE TABLE "public"."chat_user" (
    "chatID" "text" NOT NULL,
    PRIMARY KEY((chatID) HASH)
);


ALTER TABLE "public"."chat_user" OWNER TO "yugabyte";

--
-- Data for Name: chat_user; Type: TABLE DATA; Schema: public; Owner: yugabyte
--

COPY "public"."chat_user" ("chatID") FROM stdin;
\.
.....

It used quotes on ALTER TABLE & COPY but not in the PRIMARY KEY().

@tedyu
Copy link
Contributor

tedyu commented Oct 19, 2020

There used to be code change shown in my previous comment.

See the above PR for the current proposal.

tedyu added a commit that referenced this issue Nov 2, 2020
Summary:
As reported in #6061, currently the primary key column(s) in sql_dump output may not be properly quoted, leading to invalid SQL syntax.

This change utilizes fmtId to properly quote the column.

Test Plan:
Create table as shown in issue #6061, run sql_dump over the DB and observe proper quoting:
```
CREATE TABLE public.chat_user (
    "chatID" text NOT NULL,
    PRIMARY KEY(("chatID") HASH)
);
```

Reviewers: oleg, mihnea

Reviewed By: mihnea

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D9639
@tedyu tedyu closed this as completed Nov 2, 2020
@ddorian
Copy link
Contributor Author

ddorian commented Dec 10, 2020

@tedyu note that this isn't still available in 2.5.0.0 (just tested). Tried with --quote-all-identifiers too.
Can you recheck ?

@ddorian ddorian reopened this Dec 10, 2020
@ddorian
Copy link
Contributor Author

ddorian commented Dec 11, 2020

This is fixed but didn't enter 2.5.0.0 because of a mistaken commit tagging. It will be in the release. (verified that it works on master).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ysql Yugabyte SQL (YSQL)
Projects
None yet
Development

No branches or pull requests

3 participants