github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/show_stats_test.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package sql 12 13 import ( 14 "context" 15 "fmt" 16 "strings" 17 "testing" 18 19 "github.com/cockroachdb/cockroach/pkg/base" 20 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" 21 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 22 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 23 ) 24 25 func TestShowStatisticsJSON(t *testing.T) { 26 defer leaktest.AfterTest(t)() 27 28 s, db, _ := serverutils.StartServer(t, base.TestServerArgs{}) 29 defer s.Stopper().Stop(context.Background()) 30 31 r := sqlutils.MakeSQLRunner(db) 32 r.Exec(t, `SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false`) 33 34 r.Exec(t, ` 35 CREATE TABLE t ( 36 i INT, 37 f FLOAT, 38 d DECIMAL, 39 u UUID, 40 s STRING, 41 t TIMESTAMP, 42 INDEX (i), 43 INDEX (f), 44 INDEX (d), 45 INDEX (u), 46 INDEX (s), 47 INDEX (t) 48 )`) 49 50 r.Exec(t, ` 51 INSERT INTO t VALUES 52 (1, 1.0, 1.012034314, '00000000-0000-0000-0000-000000000000', 'string', '2020-01-01'), 53 (-1, -0, -0.00000000000, gen_random_uuid(), 'string with space', now()), 54 (10, 1.1, 100.1, gen_random_uuid(), 'spaces ''quotes'' "double quotes"', now())`) 55 56 r.Exec(t, `CREATE STATISTICS foo FROM t`) 57 58 row := r.QueryRow(t, `SHOW STATISTICS USING JSON FOR TABLE t`) 59 var stats string 60 row.Scan(&stats) 61 62 // TODO(radu): we should add support for placeholders for the statistics. 63 r.Exec(t, fmt.Sprintf( 64 `ALTER TABLE t INJECT STATISTICS '%s'`, strings.Replace(stats, "'", "''", -1), 65 )) 66 67 row = r.QueryRow(t, `SHOW STATISTICS USING JSON FOR TABLE t`) 68 var stats2 string 69 row.Scan(&stats2) 70 if stats != stats2 { 71 t.Errorf("after injecting back the same stats, got different stats:\n%s\nvs.\n%s", stats, stats2) 72 } 73 }