From b648af277a7ddf9fc34c47c7ff3ba2b96095e23e Mon Sep 17 00:00:00 2001 From: Rafi Shamim Date: Mon, 23 Nov 2020 21:40:15 -0500 Subject: [PATCH] wip: sql: test decimals in pgwire binary format --- pkg/cmd/generate-binary/main.go | 2 ++ pkg/sql/pgwire/encoding_test.go | 11 +++++++++ pkg/sql/pgwire/testdata/encodings.json | 34 ++++++++++++++++++-------- pkg/sql/pgwire/testdata/pgtest/decimal | 33 +++++++++++++++++++++++++ pkg/testutils/pgtest/datadriven.go | 3 +++ 5 files changed, 73 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/generate-binary/main.go b/pkg/cmd/generate-binary/main.go index 7bd218561f28..fddbaf49b57c 100644 --- a/pkg/cmd/generate-binary/main.go +++ b/pkg/cmd/generate-binary/main.go @@ -200,6 +200,8 @@ var inputs = map[string][]string{ "42.0", "420000", "420000.0", + "10000", + "1E+4", }, "'%s'::float8": { diff --git a/pkg/sql/pgwire/encoding_test.go b/pkg/sql/pgwire/encoding_test.go index f5b32277fe5d..608db2161eaa 100644 --- a/pkg/sql/pgwire/encoding_test.go +++ b/pkg/sql/pgwire/encoding_test.go @@ -80,6 +80,9 @@ func readEncodingTests(t testing.TB) []*encodingTest { if err != nil { t.Fatal(err) } + if tc.SQL == "'1E+4'::decimal" { + fmt.Println() + } d, err := te.Eval(&evalCtx) if err != nil { t.Fatal(err) @@ -156,6 +159,9 @@ func TestEncodings(t *testing.T) { t.Run(pgwirebase.FormatBinary.String(), func(t *testing.T) { for _, tc := range tests { d := tc.Datum + if tc.SQL == "'1E+4'::decimal" { + fmt.Println() + } buf.reset() buf.writeBinaryDatum(ctx, d, time.UTC, tc.T) if buf.err != nil { @@ -218,6 +224,11 @@ func TestExoticNumericEncodings(t *testing.T) { {apd.New(0, 0), []byte{0, 0, 0, 0, 0, 0, 0, 0}}, {apd.New(0, 0), []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0}}, {apd.New(10000, 0), []byte{0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}}, + {apd.New(10000, 0), []byte{0, 1, 0, 1, 0, 0, 0, 0, 0, 1}}, + {apd.New(10000, 0), []byte{00, 0x01, 00, 01, 00, 00, 0xff, 0xfc, 00, 0x01}}, + {apd.New(1, 4), []byte{0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}}, + {apd.New(1, 4), []byte{0, 1, 0, 1, 0, 0, 0, 0, 0, 1}}, + {apd.New(1, 4), []byte{00, 0x01, 00, 01, 00, 00, 0xff, 0xfc, 00, 0x01}}, {apd.New(10001, 0), []byte{0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}}, {apd.New(1000000, 0), []byte{0, 2, 0, 1, 0, 0, 0, 0, 0, 100, 0, 0}}, {apd.New(1000001, 0), []byte{0, 2, 0, 1, 0, 0, 0, 0, 0, 100, 0, 1}}, diff --git a/pkg/sql/pgwire/testdata/encodings.json b/pkg/sql/pgwire/testdata/encodings.json index dde959771f71..755bc49bf8fa 100644 --- a/pkg/sql/pgwire/testdata/encodings.json +++ b/pkg/sql/pgwire/testdata/encodings.json @@ -566,6 +566,20 @@ "TextAsBinary": [52, 50, 48, 48, 48, 48, 46, 48], "Binary": [0, 1, 0, 1, 0, 0, 0, 1, 0, 42] }, + { + "SQL": "'10000'::decimal", + "Oid": 1700, + "Text": "10000", + "TextAsBinary": [49, 48, 48, 48, 48], + "Binary": [0, 1, 0, 1, 0, 0, 0, 0, 0, 1] + }, + { + "SQL": "'1E+4'::decimal", + "Oid": 1700, + "Text": "10000", + "TextAsBinary": [49, 48, 48, 48, 48], + "Binary": [0, 1, 0, 1, 0, 0, 0, 0, 0, 1] + }, { "SQL": "'{-000.000,-0000021234.23246346000000,-1.2,.0,.1,.1234}'::decimal[]", "Oid": 1231, @@ -702,8 +716,8 @@ { "SQL": "'1.4013e-45'::float4", "Oid": 700, - "Text": "1.4013e-45", - "TextAsBinary": [49, 46, 52, 48, 49, 51, 101, 45, 52, 53], + "Text": "1e-45", + "TextAsBinary": [49, 101, 45, 52, 53], "Binary": [0, 0, 0, 1] }, { @@ -758,29 +772,29 @@ { "SQL": "'3.4028234663852886e+38'::float8", "Oid": 701, - "Text": "3.40282346638529e+38", - "TextAsBinary": [51, 46, 52, 48, 50, 56, 50, 51, 52, 54, 54, 51, 56, 53, 50, 57, 101, 43, 51, 56], + "Text": "3.4028234663852886e+38", + "TextAsBinary": [51, 46, 52, 48, 50, 56, 50, 51, 52, 54, 54, 51, 56, 53, 50, 56, 56, 54, 101, 43, 51, 56], "Binary": [71, 239, 255, 255, 224, 0, 0, 0] }, { "SQL": "'1.401298464324817e-45'::float8", "Oid": 701, - "Text": "1.40129846432482e-45", - "TextAsBinary": [49, 46, 52, 48, 49, 50, 57, 56, 52, 54, 52, 51, 50, 52, 56, 50, 101, 45, 52, 53], + "Text": "1.401298464324817e-45", + "TextAsBinary": [49, 46, 52, 48, 49, 50, 57, 56, 52, 54, 52, 51, 50, 52, 56, 49, 55, 101, 45, 52, 53], "Binary": [54, 160, 0, 0, 0, 0, 0, 0] }, { "SQL": "'1.7976931348623157e+308'::float8", "Oid": 701, - "Text": "1.79769313486232e+308", - "TextAsBinary": [49, 46, 55, 57, 55, 54, 57, 51, 49, 51, 52, 56, 54, 50, 51, 50, 101, 43, 51, 48, 56], + "Text": "1.7976931348623157e+308", + "TextAsBinary": [49, 46, 55, 57, 55, 54, 57, 51, 49, 51, 52, 56, 54, 50, 51, 49, 53, 55, 101, 43, 51, 48, 56], "Binary": [127, 239, 255, 255, 255, 255, 255, 255] }, { "SQL": "'5e-324'::float8", "Oid": 701, - "Text": "4.94065645841247e-324", - "TextAsBinary": [52, 46, 57, 52, 48, 54, 53, 54, 52, 53, 56, 52, 49, 50, 52, 55, 101, 45, 51, 50, 52], + "Text": "5e-324", + "TextAsBinary": [53, 101, 45, 51, 50, 52], "Binary": [0, 0, 0, 0, 0, 0, 0, 1] }, { diff --git a/pkg/sql/pgwire/testdata/pgtest/decimal b/pkg/sql/pgwire/testdata/pgtest/decimal index 2ae73557a020..16ed3fec72da 100644 --- a/pkg/sql/pgwire/testdata/pgtest/decimal +++ b/pkg/sql/pgwire/testdata/pgtest/decimal @@ -35,3 +35,36 @@ ReadyForQuery {"Type":"DataRow","Values":[{"text":"0"}]} {"Type":"CommandComplete","CommandTag":"SELECT 1"} {"Type":"ReadyForQuery","TxStatus":"I"} + +send +Parse {"Name": "s1", "Query": "SELECT 1E+4::decimal, $1::decimal"} +Bind {"DestinationPortal": "p1", "PreparedStatement": "s1", "ParameterFormatCodes": [1], "Parameters": [[0, 1, 0, 1, 0, 0, 0, 0, 0, 1]]} +Execute {"Portal": "p1"} +Sync +---- + +until +ReadyForQuery +---- +{"Type":"ParseComplete"} +{"Type":"BindComplete"} +{"Type":"DataRow","Values":[{"text":"10000"},{"text":"10000"}]} +{"Type":"CommandComplete","CommandTag":"SELECT 1"} +{"Type":"ReadyForQuery","TxStatus":"I"} + +# ResultFormatCodes [1] = FormatBinary +send +Parse {"Name": "s2", "Query": "SELECT 10000::decimal, $1::decimal"} +Bind {"DestinationPortal": "p2", "PreparedStatement": "s2", "ParameterFormatCodes": [1], "Parameters": [[0, 1, 0, 1, 0, 0, 0, 0, 0, 1]], "ResultFormatCodes": [1,1]} +Execute {"Portal": "p2"} +Sync +---- + +until +ReadyForQuery +---- +{"Type":"ParseComplete"} +{"Type":"BindComplete"} +{"Type":"DataRow","Values":[{"binary":"00010001000000000001"},{"binary":"00010001000000000001"}]} +{"Type":"CommandComplete","CommandTag":"SELECT 1"} +{"Type":"ReadyForQuery","TxStatus":"I"} diff --git a/pkg/testutils/pgtest/datadriven.go b/pkg/testutils/pgtest/datadriven.go index bd9c733b0e41..6be4f07c3744 100644 --- a/pkg/testutils/pgtest/datadriven.go +++ b/pkg/testutils/pgtest/datadriven.go @@ -69,6 +69,9 @@ func RunTest(t *testing.T, path, addr, user string) { if err != nil { t.Fatal(err) } + if !strings.Contains(path, "decimal") { + return + } datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string { switch d.Cmd { case "only":