diff --git a/data/test/vtexplain/multi-output/options-output.txt b/data/test/vtexplain/multi-output/options-output.txt index cb21cc98a22..5e763a2e749 100644 --- a/data/test/vtexplain/multi-output/options-output.txt +++ b/data/test/vtexplain/multi-output/options-output.txt @@ -14,7 +14,7 @@ select * from user where id in (1,2,3,4,5,6,7,8) 1 ks_sharded/c0-: select * from user where id in (4, 6, 7, 8) limit 10001 ---------------------------------------------------------------------- -insert into user (id, name) values(2, 'bob') +insert into user (id, name) values (2, 'bob') 1 ks_sharded/c0-: begin 1 ks_sharded/c0-: insert into name_user_map(name, user_id) values ('bob', 2) /* _stream name_user_map (name user_id ) ('Ym9i' 2 ); */ /* vtgate:: keyspace_id:da8a82595aa28154c17717955ffeed8b */ diff --git a/data/test/vtexplain/multi-output/target-output.txt b/data/test/vtexplain/multi-output/target-output.txt new file mode 100644 index 00000000000..e7ed228009e --- /dev/null +++ b/data/test/vtexplain/multi-output/target-output.txt @@ -0,0 +1,18 @@ +---------------------------------------------------------------------- +select * from user where email='null@void.com' + +1 ks_sharded/40-80: select * from user where email = 'null@void.com' limit 10001 + +---------------------------------------------------------------------- +select * from user where id in (1,2,3,4,5,6,7,8) + +1 ks_sharded/40-80: select * from user where id in (1, 2, 3, 4, 5, 6, 7, 8) limit 10001 + +---------------------------------------------------------------------- +insert into user (id, name) values (2, 'bob') + +1 ks_sharded/40-80: begin +1 ks_sharded/40-80: insert into user(id, name) values (2, 'bob')/* vtgate:: filtered_replication_unfriendly */ +2 ks_sharded/40-80: commit + +---------------------------------------------------------------------- diff --git a/data/test/vtexplain/options-queries.sql b/data/test/vtexplain/options-queries.sql index 76a72a5adca..9289cae2db2 100644 --- a/data/test/vtexplain/options-queries.sql +++ b/data/test/vtexplain/options-queries.sql @@ -1,3 +1,3 @@ select * from user where email='null@void.com'; select * from user where id in (1,2,3,4,5,6,7,8); -insert into user (id, name) values(2, 'bob'); +insert into user (id, name) values (2, 'bob'); diff --git a/data/test/vtexplain/target-queries.sql b/data/test/vtexplain/target-queries.sql new file mode 100644 index 00000000000..fd2fc48951d --- /dev/null +++ b/data/test/vtexplain/target-queries.sql @@ -0,0 +1,8 @@ +/* + * These are the same set of queries from "options-queries.sql" but + * are run with an explicit shard target of ks_sharded/40-80 which + * bypasses the normal v3 routing. + */ +select * from user where email='null@void.com'; +select * from user where id in (1,2,3,4,5,6,7,8); +insert into user (id, name) values (2, 'bob'); diff --git a/go/cmd/vtexplain/vtexplain.go b/go/cmd/vtexplain/vtexplain.go index db00e3d06d6..77f7688f4f4 100644 --- a/go/cmd/vtexplain/vtexplain.go +++ b/go/cmd/vtexplain/vtexplain.go @@ -40,6 +40,7 @@ var ( replicationMode = flag.String("replication-mode", "ROW", "The replication mode to simulate -- must be set to either ROW or STATEMENT") normalize = flag.Bool("normalize", false, "Whether to enable vtgate normalization") outputMode = flag.String("output-mode", "text", "Output in human-friendly text or json") + dbName = flag.String("dbname", "", "Optional database target to override normal routing") // vtexplainFlags lists all the flags that should show in usage vtexplainFlags = []string{ @@ -53,6 +54,7 @@ var ( "sql-file", "vschema", "vschema-file", + "dbname", } ) @@ -153,6 +155,7 @@ func parseAndRun() error { ReplicationMode: *replicationMode, NumShards: *numShards, Normalize: *normalize, + Target: *dbName, } log.V(100).Infof("sql %s\n", sql) diff --git a/go/vt/vtexplain/vtexplain.go b/go/vt/vtexplain/vtexplain.go index ad063ba346f..8f118ec2f44 100644 --- a/go/vt/vtexplain/vtexplain.go +++ b/go/vt/vtexplain/vtexplain.go @@ -63,6 +63,10 @@ type Options struct { // StrictDDL is used in unit tests only to verify that the schema // is parsed properly. StrictDDL bool + + // Target is used to override the "database" target in the + // vtgate session to simulate `USE ` + Target string } // TabletQuery defines a query that was sent to a given tablet and how it was diff --git a/go/vt/vtexplain/vtexplain_flaky_test.go b/go/vt/vtexplain/vtexplain_flaky_test.go index fee1836aab9..190bd949894 100644 --- a/go/vt/vtexplain/vtexplain_flaky_test.go +++ b/go/vt/vtexplain/vtexplain_flaky_test.go @@ -144,6 +144,16 @@ func TestOptions(t *testing.T) { testExplain("options", opts, t) } +func TestTarget(t *testing.T) { + opts := &Options{ + ReplicationMode: "ROW", + NumShards: 4, + Normalize: false, + Target: "ks_sharded/40-80", + } + + testExplain("target", opts, t) +} func TestComments(t *testing.T) { testExplain("comments", defaultTestOpts(), t) diff --git a/go/vt/vtexplain/vtexplain_vtgate.go b/go/vt/vtexplain/vtexplain_vtgate.go index f2e914ccf84..4e90acf00f2 100644 --- a/go/vt/vtexplain/vtexplain_vtgate.go +++ b/go/vt/vtexplain/vtexplain_vtgate.go @@ -46,7 +46,7 @@ var ( healthCheck *discovery.FakeHealthCheck vtgateSession = &vtgatepb.Session{ - TargetString: "@master", + TargetString: "", Autocommit: true, } ) @@ -62,6 +62,8 @@ func initVtgateExecutor(vSchemaStr string, opts *Options) error { return err } + vtgateSession.TargetString = opts.Target + streamSize := 10 queryPlanCacheSize := int64(10) vtgateExecutor = vtgate.NewExecutor(context.Background(), explainTopo, vtexplainCell, "", resolver, opts.Normalize, streamSize, queryPlanCacheSize, false /* legacyAutocommit */)