Skip to content

Commit

Permalink
pgwire/pgtest: make the tests work both on pg and crdb
Browse files Browse the repository at this point in the history
There was some accumulated incompatibilities in the test files.
This patch fixes it, and in the process of doing so discovers
two new bugs (cockroachdb#49639 and cockroachdb#49640).

Summary of changes to the DSL:

- the new `only` directive skips an entire test file if the db
  does not match (used for the crdb-specific portal bug test file)

- the new flags `crdb_only` and `noncrdb_only` skip over a
  test directive if the db does not match.

- the new flags `ignore_table_oids` and `ignore_type_oids` replace
  the corresponding OIDs in the RowDescription message by 0
  prior to comparing with the expected value.

Release note: None
  • Loading branch information
knz committed May 28, 2020
1 parent 96ec98f commit 8c09ff6
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 90 deletions.
14 changes: 14 additions & 0 deletions pkg/sql/pgwire/testdata/pgtest/char
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Prepare the environment.

send
Query {"String": "DROP TABLE IF EXISTS a"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Start of test.

send
Query {"String": "CREATE TABLE a (a INT PRIMARY KEY)"}
----
Expand Down
31 changes: 26 additions & 5 deletions pkg/sql/pgwire/testdata/pgtest/enum
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
# This test verifies some of the pgwire encoding process for ENUMs.

send
# Prepare the environment.
send noncrdb_only
Query {"String": "DROP TYPE IF EXISTS te"}
----

until noncrdb_only ignore=NoticeResponse
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"DROP TYPE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send crdb_only
Query {"String": "SET experimental_enable_enums=true;"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"SET"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "CREATE TYPE t AS ENUM ('hi', 'hello')"}
Query {"String": "CREATE TYPE te AS ENUM ('hi', 'hello')"}
----

until
Expand All @@ -22,17 +33,27 @@ ReadyForQuery

# Use the enum now.
send
Query {"String": "SELECT 'hi'::t"}
Query {"String": "SELECT 'hi'::te"}
----

# PostgreSQL uses float4 under the hood.
until ignore_type_oids noncrdb_only
RowDescription
----
{"Type":"RowDescription","Fields":[{"Name":"te","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":0,"DataTypeSize":4,"TypeModifier":-1,"Format":0}]}

# Note that this is slightly different than Postgres -- in Postgres the
# DataTypeSize for an enum is 4, as floats are used to represent enums
# internally (4 bytes). Since our encodings are variable size, we report
# the DataTypeSize to be -1, which is the variable length size.
until ignore_type_oids crdb_only
RowDescription
----
{"Type":"RowDescription","Fields":[{"Name":"te","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":0,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}

until
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"t","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":100052,"DataTypeSize":-1,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"hi"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}
48 changes: 34 additions & 14 deletions pkg/sql/pgwire/testdata/pgtest/int_size
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
# The output can be a little hard to read but the important part is the DataTypeOIDs.
# OID 20 is INT8, OID 23 is INT4.

# By default, int == int8.
# Clean up the environment.
send
Query {"String": "DROP TABLE IF EXISTS t1; DROP TABLE IF EXISTS t2"}
----

until ignore=NoticeResponse
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Start of test.


# By default, int == int8.
send crdb_only
Query {"String": "SELECT 1::int, 2::int4, 3::int8"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"int8","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0},{"Name":"int4","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"int8","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
Expand All @@ -16,51 +31,56 @@ ReadyForQuery
{"Type":"ReadyForQuery","TxStatus":"I"}

# Same results when selecting from a table.
send
send crdb_only
Query {"String": "CREATE TABLE t1 (a int, b int4, c int8)"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
send crdb_only
Query {"String": "SELECT * FROM t1"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"a","TableOID":52,"TableAttributeNumber":1,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0},{"Name":"b","TableOID":52,"TableAttributeNumber":2,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"c","TableOID":52,"TableAttributeNumber":3,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
{"Type":"CommandComplete","CommandTag":"SELECT 0"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Now change the default_int_size setting.
send
send crdb_only
Query {"String": "SET default_int_size=4"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"SET"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# The setting doesn't affect explicit casts, only table definitions.
send
# (So in CockroachdB ::int is still ::int8, whereas it's ::int4 in postgres.)
send crdb_only
Query {"String": "SELECT 1::int, 2::int4, 3::int8"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"int8","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0},{"Name":"int4","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"int8","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"1"},{"text":"2"},{"text":"3"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

## Everything after this point should be the same between CockroachDB
## and PostgreSQL.


# Create a new table with the new setting.
send
Query {"String": "CREATE TABLE t2 (a integer, b int4, c int8)"}
Expand All @@ -77,19 +97,19 @@ send
Query {"String": "SELECT * FROM t2"}
----

until
until ignore_table_oids
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"a","TableOID":53,"TableAttributeNumber":1,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"b","TableOID":53,"TableAttributeNumber":2,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"c","TableOID":53,"TableAttributeNumber":3,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
{"Type":"RowDescription","Fields":[{"Name":"a","TableOID":0,"TableAttributeNumber":1,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"b","TableOID":0,"TableAttributeNumber":2,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"c","TableOID":0,"TableAttributeNumber":3,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
{"Type":"CommandComplete","CommandTag":"SELECT 0"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# t1 is unchanged. It was created under the old configuration so its int column is int8.
send
send crdb_only
Query {"String": "SELECT * FROM t1"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"a","TableOID":52,"TableAttributeNumber":1,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0},{"Name":"b","TableOID":52,"TableAttributeNumber":2,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"c","TableOID":52,"TableAttributeNumber":3,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
Expand Down
79 changes: 53 additions & 26 deletions pkg/sql/pgwire/testdata/pgtest/notice
Original file line number Diff line number Diff line change
@@ -1,65 +1,92 @@
# Test notices work as expected by creating a VIEW on a TEMP TABLE.


# Prepare the environment.

send
Parse {"Query": "CREATE TABLE t(x INT, y INT, INDEX (x), INDEX (y))"}
Bind
Execute
Sync
Query {"String": "DROP TABLE IF EXISTS t CASCADE"}
----

until ignore=NoticeResponse
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Start of test.

send
Query {"String": "CREATE TABLE t(x INT, y INT)"}
----

until
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "CREATE INDEX t_x_idx ON t(x)"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"CREATE INDEX"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Parse {"Query": "DROP INDEX t@t_x_idx"}
Bind
Execute
Sync
Query {"String": "CREATE INDEX t_y_idx ON t(y)"}
----

until
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"CREATE INDEX"}
{"Type":"ReadyForQuery","TxStatus":"I"}


# Check that crdb reports a notice upon drop index.

send
Query {"String": "DROP INDEX t_x_idx"}
----

until crdb_only
CommandComplete
----
{"Severity":"NOTICE","Code":"00000","Message":"the data for dropped indexes is reclaimed asynchronously","Detail":"","Hint":"The reclamation delay can be customized in the zone configuration for the table.","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"drop_index.go","Line":500,"Routine":"dropIndexByName","UnknownFields":null}
{"Type":"CommandComplete","CommandTag":"DROP INDEX"}

until noncrdb_only
CommandComplete
----
{"Type":"CommandComplete","CommandTag":"DROP INDEX"}

until
ReadyForQuery
----
{"Type":"ReadyForQuery","TxStatus":"I"}

# Disable notices and assert now it is not sent.
send
Parse {"Query": "SET CLUSTER SETTING sql.notices.enabled = false"}
Bind
Execute
Sync
send crdb_only
Query {"String": "SET CLUSTER SETTING sql.notices.enabled = false"}
----

until
until crdb_only
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"SET CLUSTER SETTING"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# Check that the notice is not printed any more.

send
Parse {"Query": "DROP INDEX t@t_y_idx"}
Bind
Execute
Sync
Query {"String": "DROP INDEX t_y_idx"}
----

until
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"DROP INDEX"}
{"Type":"ReadyForQuery","TxStatus":"I"}
Loading

0 comments on commit 8c09ff6

Please sign in to comment.