Skip to content

Commit

Permalink
Merge pull request cockroachdb#91780 from rytaft/backport22.1-91717
Browse files Browse the repository at this point in the history
release-22.1: opt: update statement bundle and EXPLAIN (opt, env) to include FK tables (cockroachdb#91780)
  • Loading branch information
rytaft authored Nov 15, 2022
2 parents 7b98d9c + ccd397a commit 5d6b871
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {
err := b.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
var err error
tables, sequences, views, err = mem.Metadata().AllDataSourceNames(
func(ds cat.DataSource) (cat.DataSourceName, error) {
ctx, b.plan.catalog, func(ds cat.DataSource) (cat.DataSourceName, error) {
return b.plan.catalog.fullyQualifiedNameWithTxn(ctx, ds, txn)
},
)
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
t.Fatalf("warning not found in %v", rows)
}
})

t.Run("foreign keys", func(t *testing.T) {
r.Exec(t, "CREATE TABLE parent (pk INT PRIMARY KEY, v INT);")
r.Exec(t, "CREATE TABLE child (pk INT PRIMARY KEY, fk INT REFERENCES parent(pk));")
rows := r.QueryStr(t, "EXPLAIN ANALYZE (DEBUG) SELECT * FROM child")
checkBundle(
t, fmt.Sprint(rows), "child", base, plans,
"stats-defaultdb.public.parent.sql", "stats-defaultdb.public.child.sql",
"distsql.html vec.txt vec-v.txt",
)
})
}

// checkBundle searches text strings for a bundle URL and then verifies that the
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,7 @@ func (b *Builder) getEnvData() exec.ExplainEnvData {
envOpts := exec.ExplainEnvData{ShowEnv: true}
var err error
envOpts.Tables, envOpts.Sequences, envOpts.Views, err = b.mem.Metadata().AllDataSourceNames(
b.evalCtx.Ctx(), b.catalog,
func(ds cat.DataSource) (cat.DataSourceName, error) {
return b.catalog.FullyQualifiedName(context.TODO(), ds)
},
Expand Down
36 changes: 31 additions & 5 deletions pkg/sql/opt/exec/execbuilder/testdata/explain_env
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ SET reorder_joins_limit = 63
query T
EXPLAIN (OPT, ENV) SELECT * FROM y WHERE u = 3
----
https://cockroachdb.github.io/text/decode.html#eJyUkt9O2zAYxa_xU3x3tBMZoEoTasVFCGbLVlKUeAyEkGUSJ_XqxsF_QtMrHqJPyJNMScoNWlftMtHvd3x09Hke3HJthCrHEKh0oRVL55cXwFc8fXJCZlyD5cZC3VMIJZiA5kpnXNPfSpSGSrEUFs7hy2gC4HmQ8Zw5aaFm0vExnCHPA16yJ8npWhRrVnQezJkBO-cfcVW2vKqsWIo119QZTufCWFVotjT_Yy2dtCJVkhrL7B5TqpRJYRv6HpHRimkrrFAlz6goM76iJmV7aldaVaxgllNRVs7SbiZRFjutPO81nnNNpVILV21XzZWm-WJ37d7MhGFSqheaOymp7UZua-7zJNMF76UWp1q97FROT05OWidVxvbh9EXYOd1itJtYrPm-qlvcalYalrbL0me3XT2nhutapDtDNC-cZLo7v0wYa54lnLfRf7055qxq36x5apX-V7cSoSDGPsFA_Isphso9SZF-bmCADhyEETmDaEYg-jmdHqGDevun_wpmUUJiP4wINLRa8AZu4vDaj-_hB76HgQM_CYYfuZrmLXk1i3H4NerJeggxvsIxjgKcvFdYDVgrh9Elvus8ka1gUHehaDhByJ8SHH-sHUbfcUAgIT4JExIGCRw-PB5OEMJ3N1M_jGAwuyFHgKPbISR42rKf4CqeXUMDv77hGIODcxhNkOd5HuouvkHwttm8bV7fNq-QqtJYzURpx3B8OoaH4xF4cDx6RH8CAAD__ygWcbs=
https://cockroachdb.github.io/text/decode.html#eJy0lN9u2zYUxq_DpzjwTZ3BWuQYGAIFuXBdZtPmyoGsdS2CgKAlyuZCkyr_KFaGAX2IPGGeZKDkJF4XNxiK-MIAj7_v8JzPPykI4APThisZwUTl11rRfPXuLbANyxeOi4JpsMxYqDsVQnOcgWZKF0yTPxWXhgi-5hbO4KfRKUAQQMFK6oSFmgrHIjhBQQBM0oVg5JYvb-my9cGKGrAr9rVcSa9XleVrfss0cYaRFTdWLTVdm__jWjthea4EMZbaF5xC5VRw25CHFgWpqLbcciVZQbgs2IaYnL4wdqVVRZfUMsJl5SxpY-JyuddVlp2NlUwTodS1q7aplkqT8nr_2J2z4IYKoW5I6YQgtg3Zj_mST1C9ZJ3Jy4lWN3stwzAMvSdXxnbNyQ23K7KVkTZifsteGnUrt5pKQ3OfLPnstqmXxDBd83xvE82WTlDd4ldwY81nAWe-9bPMUWeVv7NmuVX6W7NJhCYpHmcYsvHbKYbKLQTPf9xAHx1QiJPsBJJZBsnv0-kAHSy2le40mSXzLB3HSQYbUl2zBi7S-P04_QS_4U_QpzCeTw4H6CBO3uGPsCELwosN9BdtHR2eIjSeZjj9-uY4-RVPMphn4yyeZ_FkDm8uEQDAX-23__RovWwz70UQDp7KuRJuLU0vgsvHYqfvPZ6vdvWaUcsKQm0vgt5xODwJwmEQDiEcRmEYhWFvR-xj5zL3f7iT3jAMd-9uH9IWBttUfrDerll61B6Muzatbp4aHo-Gx6P2t78H37vy4lVWbid8va3R1ZvT54lsPJHuP0TW-4hsniHSPRD5L11NSq88n6U4_jnplPUhpPgcpziZ4Pkjmn36hLP3tTjX38S5eRbndkv88WI6jhPozy6yAeDkwyHM8dRrf4DzdPYeGvjjF5xicHAGo1MUBEGA2ndwg-D-7u7-7sv93RfIlTRWUy5tBEfDCC6PRhDA0egK_RMAAP__IZgN_Q==

# Make sure it shows up correctly even if it matches the cluster setting.
statement ok
Expand All @@ -81,31 +81,31 @@ SET CLUSTER SETTING sql.defaults.reorder_joins_limit = 63
query T
EXPLAIN (OPT, ENV) SELECT * FROM y WHERE u = 3
----
https://cockroachdb.github.io/text/decode.html#eJyUkt9O2zAYxa_xU3x3tBMZoEoTasVFCGbLVlKUeAyEkGUSJ_XqxsF_QtMrHqJPyJNMScoNWlftMtHvd3x09Hke3HJthCrHEKh0oRVL55cXwFc8fXJCZlyD5cZC3VMIJZiA5kpnXNPfSpSGSrEUFs7hy2gC4HmQ8Zw5aaFm0vExnCHPA16yJ8npWhRrVnQezJkBO-cfcVW2vKqsWIo119QZTufCWFVotjT_Yy2dtCJVkhrL7B5TqpRJYRv6HpHRimkrrFAlz6goM76iJmV7aldaVaxgllNRVs7SbiZRFjutPO81nnNNpVILV21XzZWm-WJ37d7MhGFSqheaOymp7UZua-7zJNMF76UWp1q97FROT05OWidVxvbh9EXYOd1itJtYrPm-qlvcalYalrbL0me3XT2nhutapDtDNC-cZLo7v0wYa54lnLfRf7055qxq36x5apX-V7cSoSDGPsFA_Isphso9SZF-bmCADhyEETmDaEYg-jmdHqGDevun_wpmUUJiP4wINLRa8AZu4vDaj-_hB76HgQM_CYYfuZrmLXk1i3H4NerJeggxvsIxjgKcvFdYDVgrh9Elvus8ka1gUHehaDhByJ8SHH-sHUbfcUAgIT4JExIGCRw-PB5OEMJ3N1M_jGAwuyFHgKPbISR42rKf4CqeXUMDv77hGIODcxhNkOd5HuouvkHwttm8bV7fNq-QqtJYzURpx3B8OoaH4xF4cDx6RH8CAAD__ygWcbs=
https://cockroachdb.github.io/text/decode.html#eJy0lN9u2zYUxq_DpzjwTZ3BWuQYGAIFuXBdZtPmyoGsdS2CgKAlyuZCkyr_KFaGAX2IPGGeZKDkJF4XNxiK-MIAj7_v8JzPPykI4APThisZwUTl11rRfPXuLbANyxeOi4JpsMxYqDsVQnOcgWZKF0yTPxWXhgi-5hbO4KfRKUAQQMFK6oSFmgrHIjhBQQBM0oVg5JYvb-my9cGKGrAr9rVcSa9XleVrfss0cYaRFTdWLTVdm__jWjthea4EMZbaF5xC5VRw25CHFgWpqLbcciVZQbgs2IaYnL4wdqVVRZfUMsJl5SxpY-JyuddVlp2NlUwTodS1q7aplkqT8nr_2J2z4IYKoW5I6YQgtg3Zj_mST1C9ZJ3Jy4lWN3stwzAMvSdXxnbNyQ23K7KVkTZifsteGnUrt5pKQ3OfLPnstqmXxDBd83xvE82WTlDd4ldwY81nAWe-9bPMUWeVv7NmuVX6W7NJhCYpHmcYsvHbKYbKLQTPf9xAHx1QiJPsBJJZBsnv0-kAHSy2le40mSXzLB3HSQYbUl2zBi7S-P04_QS_4U_QpzCeTw4H6CBO3uGPsCELwosN9BdtHR2eIjSeZjj9-uY4-RVPMphn4yyeZ_FkDm8uEQDAX-23__RovWwz70UQDp7KuRJuLU0vgsvHYqfvPZ6vdvWaUcsKQm0vgt5xODwJwmEQDiEcRmEYhWFvR-xj5zL3f7iT3jAMd-9uH9IWBttUfrDerll61B6Muzatbp4aHo-Gx6P2t78H37vy4lVWbid8va3R1ZvT54lsPJHuP0TW-4hsniHSPRD5L11NSq88n6U4_jnplPUhpPgcpziZ4Pkjmn36hLP3tTjX38S5eRbndkv88WI6jhPozy6yAeDkwyHM8dRrf4DzdPYeGvjjF5xicHAGo1MUBEGA2ndwg-D-7u7-7sv93RfIlTRWUy5tBEfDCC6PRhDA0egK_RMAAP__IZgN_Q==

statement ok
SET enable_zigzag_join = false

query T
EXPLAIN (OPT, ENV) SELECT * FROM y WHERE u = 3
----
https://cockroachdb.github.io/text/decode.html#eJyMks9O4zoYxdf4Kb4d7RW5gCpdoVYsQjD35k5JUeJhQAhZJnFST107-E9ou-Ih-oQ8yShJ2aApzDLR73d8dPQFAdxyY4VWY4h0vjCa5fPLC-Arnj95IQtuwHHroOkphDJMwHBtCm7oTy2UpVIshYNz-Gc0AQgCKHjJvHTQMOn5GM46hSv2JDndiGrDqk6Ec9Bl-VtFKxQEoGsnlmLDDfWW07mwTleGLS3MmQU3539iLb10IteSWsfcF6bUOZPCrel7REFrZpxwQiteUKEKvqI2Z-rzmNromlXMcSpU7R3tph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bPs3gyh3eXCADgD__dfnq0XnrPexGEg6dyroRbS9OL4PKx2Ol7j-erXb1m1LKCUNuLoHccDk-CcBiEQwiHURhGYdjbEbe2c5m3f7iTLTAMd-_2D6kPg22qdrDeLizbqD2Au5hWN08Nj0fD45H_7c_B_1158SYr-wnfbmt09e70-UQ2bSLdvxJZ70tk80wi3UMi_6arSdkqz2cpjn9MOmV9CCk-xylOJnj-GM0-fYpzy_k41y_GuXk2zn5L_PliOo4T6M8usgHg5NMhzPG01X4H5-nsIzTw2084xeDgDEanKAiCAPl3cIPg_u7u_u7b_d03yJU0VlMubQRHwwguj0YQwNHoCv0VAAD__0fwDl8=

statement ok
SET optimizer_use_histograms = false

query T
EXPLAIN (OPT, ENV) SELECT * FROM y WHERE u = 3
----
https://cockroachdb.github.io/text/decode.html#eJyEks1O4zwYhdf4Kt4d7SfyAao0Qq1YhGJmMlNSlHgYEEKWSZzUU9cO_gltV1xEr5ArGSUpG0TLMtHzHB8dvUEAt9xYodUQxjqbG82y2eUF8CXPnryQOTfguHVQdxRCKSZguDY5N_SvFspSKRbCwTl8G4wAggByXjAvHdRMej6Es1bhij1JTteiXLOyFeEcdFF8qmjVOrpyYiHW3FBvOZ0J63Rp2MLuN4Pgg7jw0olMS2odcxZmzIKb8c9NqTMmhVvR94icVsw44YRWPKdC5XxJbcbU_pjK6IqVzHEqVOUdbecSqtxpFUWn8YIbKrWe-2q7bqENLea7a3dmLiyTUr_QwktJXbt1U_MrTzJT8k5qcGr0y07l9OTkpHEybV0XTl-Em9EtRtuJxZp_VXWLO8OUZVmzLH3229ULarmpRbYzxPDSS2ba-8iFdfZZ7jkH5p1u3qx55rTZ100hNE5wSDCQ8GKCofJPUmT_r6CHDjxEMTmDeEog_j2ZHKGDevun-xpP45QkYRQTWNFqzldwk0TXYXIPv_A99DyE6bj_katp0ZBX0wRH3-OOrPuQ4Cuc4HiM0_cKyx5r5Ci-xHetJ_Il9Oo2FPVHCIUTgpOPtaP4Jx4TSElIopRE4xQO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enzRfIlTRWUy5tBGf9CG7PBhDA2eAO_RMAAP__q2YOwQ==

statement ok
SET optimizer_use_multicol_stats = false

query T
EXPLAIN (OPT, ENV) SELECT * FROM y WHERE u = 3
----
https://cockroachdb.github.io/text/decode.html#eJyUkt9O2zAYxa_xU3x3tBMZoEoTasVFCGbLVlKUeAyEkGUSJ_Xq2sF_QtsrHqJPyJNMScsNAqpdJvr9To7OlyCAa26s0GoIkc5nRrN8en4GfMHzBy9kwQ04bh00GwqhDBMwXJuCG_pXC2WpFHPh4BS-DUYAQQAFL5mXDhomPR_CSadwxR4kpytRrVjViXAKuizfVbTqHF07MRcrbqi3nE6FdboybG7_15x76USuJbWOuR12EIDUOZPCLelrSkFrZpxwQiteUKEKvqA2ZwqmzIKb8vdjaqNrVjHHqVC1d7SbTKjqQ6ssNxovuaFS65mvtwuX2tByZneYhbBMSv1ESy8ldd3ebc1dnmSm4hupxanRTx8qx0dHR62Ta-s24fRJuCndYrRbWaz4rqpb3BmmLMvbZemj365eUstNI_IPQwyvvGSmu3QhrLOP8pOjMu90-82G506bz7ophKIUhwQDCc_GGGr_IEX-dQk9tOchTsgJJBMCye_x-ADtNds3m6dokmQkDeOEwJLWM76EqzS-DNNb-IVvoechzKL-W66hZUteTFIcf082ZNOHFF_gFCcRzl4rLHqslePkHN90nigW0Gu6UNQfIRSOCU7f1o6TnzgikJGQxBmJowz27-73Rwjhm6txGCfQm1yRA8DJdR8yPG7ZL3CRTi5hCX9-4BSDh1MYjFAQBAHq_vglgpf1-mX9_LJ-hlwr6wwTyg3h8HgId4cDCOBwcI_-BQAA__9j8nLh
https://cockroachdb.github.io/text/decode.html#eJy0lN9O40YUxq-ZpzjKzYYqLg6RKmTERTY7tG6zDnLc7a4QGk3ssTNlMuOdPyamqrQPwRPyJJUnAdI2gKqKXETyyfc7c843XxwE8Ilpw5WMYKLya61ovvzwHtia5QvHRcE0WGYsNBsVQnOcgWZKF0yT3xWXhgi-4hbO4IfRKUAQQMFK6oSFhgrHIjjxCJN0IRi55dUtrTwIZ6DKci-ipGdUbfmK3zJNnGFkyY1VlaYr81_JlROW50oQY6l9hQ4CECqngtuWPHQpSE215ZYryQrCZcHWxORUwpIasEu2v02tVU0rahnhsnaWeMu4rJ6lynKDsZJpIpS6dvXW4VJpUl6bV8iCGyqEuiGlE4JY73c35mucoLpiG6iTE61unkWGYRh2TK6M3TQnN9wuyVZGvMv8lr026lZuNZWG5p2z5Kvbul4Sw3TD82ebaFY5QbW_6YIba76KFy6VOqu6MxuWW6Vfmk0iNEnxOMOQjd9PMdRuIXj-_Rr66IBCnGQnkMwySH6dTgfoYLGtbJ4ms2SepeM4yWBN6mvWwkUafxynX-AX_AX6FMbzyeEAHcTJB_wZ1mRBeLGG_sLX0eEpQuNphtN_nhwnP-NJBvNsnMXzLJ7M4d0lAgD4w393nx5tKu95L4Jw8FTOlXAraXoRXD4WN_re4_PVrl4zallBqO1F0DsOhydBOAzCIYTDKAyjMOztiDvbucy7C3eyA4bh7tn-v-rDYNu6G6y3C8suag_gLqbVzVPD49HweOR_-3Pwf1devMnKfsK32xpdvTvdn8i2S6T7VyKb5xLZ7kmke0jk33QNKTvl-SzF8Y_JRtkcQorPcYqTCZ4_RrNPn-LccT7OzYtxbvfG2W-JP19Mx3EC_dlFNgCcfDqEOZ522u_gPJ19hBZ--wmnGBycwegUBUEQIP8ObhHc393d3327v_sGuZLGasqljeBoGMHl0QgCOBpdob8CAAD__1FNDyM=

statement ok
RESET reorder_joins_limit
Expand Down Expand Up @@ -175,3 +175,29 @@ query T
EXPLAIN (OPT, ENV) VALUES(1);
----
https://cockroachdb.github.io/text/decode.html#eJyUksFq20AQhu96ijnaEIFNoZQEH9LWh0JIA0lNb8NmNZKnXu8os7N26lMewk_oJymSnEuJY3L_vn9-fqYsYUGaWOIlfBO_UnF--f0r0DP5x8yhIgWjZLAZqKK4nz-AkmhFin-EY8LAazaYwedPVwBlCRXVLgeDjQuZLuFLUZZA0T0Gwh03O9f0HixdAlvS_7jEjpfWeM07UsyJcMnJpFG3Th-x1jkYewmYzNkZM4h3ge0vvkZU2Do1NpZIFXKs6BmTd2dqtyqta5wRcmyzYT8Tx-akVdeDRjUpBpFVbo-r1qJYr07XHsyKkwtBtljnEND6kbua57zgtKFB6nBU2Z5UppPJpHO8JBvCccu2xCOG_cS8o_dOdl_zypu6mJzvpsWnfJy9xkS6YU8wA69s7F14852UmhycDomcLD0FmHU33qRdNum6b8ib6HslY1HMf9_dXP-4hdHPu4cLmN8uxrC4vvk1v4fRdHxVlGVZFj2eCjjs94f9y2H_AqPpxbj4FwAA__9iIypU

#
# Test recursive table references from foreign keys.
#

statement ok
CREATE TABLE z (
pk INT PRIMARY KEY,
ref INT,
CONSTRAINT fk FOREIGN KEY (ref) REFERENCES y(u),
FAMILY "primary" (pk, ref)
)

query T
EXPLAIN (OPT, ENV) SELECT * FROM z;
----
https://cockroachdb.github.io/text/decode.html#eJy0VV1vm0gUfc78iiu_1FmZFU6kVeQoD647WbHr4gjYqlVVjcYw4FmPGTofxGa1_301ECduC4lWVXhAmss5995zOAjPgw9MaS7LGSxkulWSppt3b4HtWbq2XGRMgWHaQN2hEIpxAopJlTFF_pa81ETwHTdwA79dXgN4HmQsp1YYqKmwbAZXyPOAlXQtGGl40dCi5cGGajAb9j1clg4vK8N3vGGKWM3IhmsjC0V3-v-wdlYYnkpBtKHmBaaQKRXcHMixRUYqqgw3XJYsI7zM2J7olL6wdqVkRQtqGOFlZQ1pbeJlMcjK847GcqaIkHJrqwdXc6lIvh1eu2NmXFMh5D3JrRDEtCa7NV_iCaoK1pEcnCh5P0iZ-r7vOKnUpmtO7rnZkAcYaS3mDXtupEvNEW8ULTVNnbXkq32wPSeaqZqnDG4gVdzwlIreOClWWEFV15Fro78KuHEzetHUGul2r1lqpHpuyRKhRYTnCYZk_naJobJrwdNf9zBGZxSCMLmCcJVA-NdyOUFn64dKd1qswjiJ5kGYwJ5UW3aAuyh4P48-wZ_4E4wpzOPF-QSdBeE7_BH2ZE14tofxuq2j82uE5ssER99PDsI_8CKBOJknQZwEixjefEYAAP-0d3eNaF205o9m4E-eyqkUdlfq0Qw-PxY7_Ojx_OUUrxg1LCPUjGYwuvCnV54_9fwp-NOZ7898f3QCdrbzMnVv3paOMPVPZ7dfa5sKc6jcYqNTcukydySe0pS8f2p4cTm9uGyf_Tv5WcnrV5Hcbvh6qtGXN9f9iTy4RNofElkPJfLQk0h7TOQ3uJrkDnm7inDwe9gh63OI8C2OcLjA8WM0x_Qpzo7Xxrl-Ns6H3jgPqmycymr7g0zF8iGhTY_QatujNN9-q1GxvE_lYWwH1TTDavDHu-U8CGG8uksmgMMP5xDjpcP-ArfR6j0018jzPA-1v5MG_RcAAP__OmE4gQ==

# A foreign key cycle shouldn't cause infinite recursion.
statement ok
ALTER TABLE y ADD CONSTRAINT fk FOREIGN KEY (v) REFERENCES z (pk);

query T
EXPLAIN (OPT, ENV) SELECT * FROM y;
----
https://cockroachdb.github.io/text/decode.html#eJy0Vd9v2zYYfA7_ig9-qTNYg5wAQ-AgD67LDNpcOZC1okVRELRE2ZxoUeUPxdKw_32gFDdeK8UYhvjBgOg7fnfn-yDPgw9MaS6LGSxkkitJk927t8AOLNlYLlKmwDBtoOpQCK1xDIpJlTJF_pS80ETwPTdwB79c3wJ4HqQso1YYqKiwbAY3yPOAFXQjGGn4tqHblgc7qsHs2PdwWTi8LA3f84YpYjUjO66N3Cq61_-FtbfC8EQKog01Z5hCJlRwU5PjFSkpqTLccFmwlPAiZQeiE3pGdqlkSbfUMMKL0hrSxsSL7SAryzoay5giQsrclk-pZlKRLB-W3TFTrqkQ8pFkVghi2pCdzHM8QdWWdSQHJ0o-DlKmvu87TiK16S4nj9zsyBOMtBHzhr000rXmiDeKFpomLlry1T7FnhHNVMUTBneQKG54QkVvnRTbWkFVdyPXRn8VcOdm9KKpNdJpr1hipHpJZIHQIsLzGEM8f7vEUNqN4MnPBxijCwpBGN9AuIoh_GO5nKCLzdNJ97RYhes4mgdhDAdS5qyGhyh4P48-we_4E4wpzNeLywm6CMJ3-CMcyIbw9ADjTXuOLm8Rmi9jHH0_OQh_w4sY1vE8DtZxsFjDm88IAOCv9tt9RrTatuGPZuBPno8TKey-0KMZfP522OFH356_nOIVo4alhJrRDEZX_vTG86eePwV_OvP9me-PTsAudl4k7p-3hSNM_dPZ7ba2rTB16YSNTsmF69yReEpT8vH5wqvr6dV1-9vfk_9refMqlluFr-cafXlz29_IxjWyzH-opGLZUCmbnlKW-bGVJ8Ash_tVhINfww6kWHYJEb7HEQ4XeH2UUI_tYG2b3toOuqmdG_uDmWrISt1jxfY4qUlFMof8l5-qz81hTM_E0EtrxmX-vNRuXrvU1YtLXQ-ngz8-LOdBCOPVQzwBHH64hDVeOuxPcB-t3kN9izzP81D7GqrRPwEAAP__rNtIQw==
61 changes: 58 additions & 3 deletions pkg/sql/opt/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
Expand Down Expand Up @@ -687,9 +688,62 @@ func (md *Metadata) AllViews() []cat.View {
return md.views
}

// getAllReferencedTables returns all the tables referenced by the metadata.
// This includes all tables that are directly stored in the metadata in
// md.tables, as well as recursive references from foreign keys. The tables are
// returned in sorted order so that later tables reference earlier tables. This
// allows tables to be re-created in order (e.g., for statement-bundle recreate)
// using the output from SHOW CREATE TABLE without any errors due to missing
// tables.
// TODO(rytaft): if there is a cycle in the foreign key references,
// statement-bundle recreate will still hit errors. To handle this case, we
// would need to first create the tables without the foreign keys, then add the
// foreign keys later.
func (md *Metadata) getAllReferencedTables(
ctx context.Context, catalog cat.Catalog,
) []cat.DataSource {
var tableSet util.FastIntSet
var tableList []cat.DataSource
var addForeignKeyReferencedTables func(tab cat.Table)
addForeignKeyReferencedTables = func(tab cat.Table) {
for i := 0; i < tab.OutboundForeignKeyCount(); i++ {
tabID := tab.OutboundForeignKey(i).ReferencedTableID()
if !tableSet.Contains(int(tabID)) {
tableSet.Add(int(tabID))
ds, _, err := catalog.ResolveDataSourceByID(ctx, cat.Flags{}, tabID)
if err != nil {
// This is a best-effort attempt to get all the tables, so don't error.
continue
}
refTab, ok := ds.(cat.Table)
if !ok {
// This is a best-effort attempt to get all the tables, so don't error.
continue
}
addForeignKeyReferencedTables(refTab)
tableList = append(tableList, ds)
}
}
}
for i := range md.tables {
tabMeta := md.tables[i]
tabID := tabMeta.Table.ID()
if !tableSet.Contains(int(tabID)) {
tableSet.Add(int(tabID))
addForeignKeyReferencedTables(tabMeta.Table)
tableList = append(tableList, tabMeta.Table)
}
}
return tableList
}

// AllDataSourceNames returns the fully qualified names of all datasources
// referenced by the metadata.
// referenced by the metadata. This includes all tables, sequences, and views
// that are directly stored in the metadata, as well as tables that are
// recursively referenced from foreign keys.
func (md *Metadata) AllDataSourceNames(
ctx context.Context,
catalog cat.Catalog,
fullyQualifiedName func(ds cat.DataSource) (cat.DataSourceName, error),
) (tables, sequences, views []tree.TableName, _ error) {
// Catalog objects can show up multiple times in our lists, so deduplicate
Expand All @@ -712,8 +766,9 @@ func (md *Metadata) AllDataSourceNames(
return result, nil
}
var err error
tables, err = getNames(len(md.tables), func(i int) cat.DataSource {
return md.tables[i].Table
refTables := md.getAllReferencedTables(ctx, catalog)
tables, err = getNames(len(refTables), func(i int) cat.DataSource {
return refTables[i]
})
if err != nil {
return nil, nil, nil, err
Expand Down

0 comments on commit 5d6b871

Please sign in to comment.