-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sql: add crdb_internal.show_create_all_tables builtin #60154
sql: add crdb_internal.show_create_all_tables builtin #60154
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome! just have relatively minor comments
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @RichardJCai)
pkg/sql/logictest/testdata/logic_test/show_create_all_tables_builtin, line 2 at r1 (raw file):
query T SELECT crdb_internal.show_create_all_tables('d')
nice tests! maybe you did this already, but perhaps you could take a look to see if dump
used to have any other good test cases that we could pull in here too?
pkg/sql/sem/builtins/builtins.go, line 4829 at r1 (raw file):
), "crdb_internal.show_create_all_tables": makeBuiltin(
hmm random thing i just thought of -- is this OK to have in crdb_internal? aren't things in crdb_internal usually experimental/undocumented? (idk though maybe it's too sketchy to put into the normal namespace)
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 39 at r1 (raw file):
} func showCreateAllTablesBuiltin(evalCtx *tree.EvalContext, arg tree.Datums) (tree.Datum, error) {
nit: could you add a comment?
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 46 at r1 (raw file):
if len(mds) == 0 { return tree.NewDString("no tables found"), nil
is this what dump used to return? i wonder if it would be better for this function to always return valid SQL so that way a tool that relies on it doesn't get confused. (in this case it could just return NULL or an empty string
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 111 at r1 (raw file):
} func getMetadataForTablesInDB(evalCtx *tree.EvalContext, arg tree.Datums) ([]tableMetadata, error) {
need a comment
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 135 at r1 (raw file):
} func getTableMetadata(
need a comment
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 264 at r1 (raw file):
} func collect(tid int64, byID map[int64]tableMetadata, seen map[int64]bool, collected *[]int64) {
need a comment -- could you mention why a table could get collected twice?
ab26a3c
to
fd5578d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @RichardJCai)
pkg/sql/logictest/testdata/logic_test/show_create_all_tables_builtin, line 2 at r1 (raw file):
Previously, rafiss (Rafi Shamim) wrote…
nice tests! maybe you did this already, but perhaps you could take a look to see if
dump
used to have any other good test cases that we could pull in here too?
Yep pulled some more tests in, good call
pkg/sql/sem/builtins/builtins.go, line 4829 at r1 (raw file):
Previously, rafiss (Rafi Shamim) wrote…
hmm random thing i just thought of -- is this OK to have in crdb_internal? aren't things in crdb_internal usually experimental/undocumented? (idk though maybe it's too sketchy to put into the normal namespace)
After some offline discussion, I think leaving this in crdb_internal is fine and we'll expose this through SHOW CREATE ALL TABLES
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 46 at r1 (raw file):
Previously, rafiss (Rafi Shamim) wrote…
is this what dump used to return? i wonder if it would be better for this function to always return valid SQL so that way a tool that relies on it doesn't get confused. (in this case it could just return NULL or an empty string
Updated to return empty string
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 111 at r1 (raw file):
Previously, rafiss (Rafi Shamim) wrote…
need a comment
Done.
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 135 at r1 (raw file):
Previously, rafiss (Rafi Shamim) wrote…
need a comment
Done.
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 264 at r1 (raw file):
Previously, rafiss (Rafi Shamim) wrote…
need a comment -- could you mention why a table could get collected twice?
Done.
fd5578d
to
3b992e2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great! thanks
Reviewable status: complete! 0 of 0 LGTMs obtained
Thanks for the quick reviews! |
Build failed (retrying...): |
bors r- this needs a rebase and re- |
Canceled. |
3b992e2
to
64ab287
Compare
Updated! |
This PR was included in a batch that was canceled, it will be automatically retried |
Build failed (retrying...): |
Build failed (retrying...): |
Build failed (retrying...): |
bors r-
|
Canceled. |
looks like @yuzefovich just removed that function in #60428. it seems like we have to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @RichardJCai)
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 204 at r2 (raw file):
WHERE descriptor_id = $1 `, dbName, ts) rows, err := evalCtx.InternalExecutor.Query(
Yeah, you'll need to use QueryIterator
method here. The code should be refactored to something like:
it, err := evalCtx.InternalExecutor.QueryIterator(...)
if err != nil {
return tableMetadata{}, err
}
var refs []int64
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
id := tree.MustBeDInt(it.Cur()[0])
refs = append(refs, int64(id))
}
if err != nil {
return tableMetadata{}, err
}
...
64ab287
to
2d485c3
Compare
3f7b6c3
to
6285cd3
Compare
This time for sure! |
bors r- |
Canceled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @RichardJCai)
pkg/sql/sem/builtins/show_create_all_tables_builtin.go, line 205 at r3 (raw file):
WHERE descriptor_id = $1 `, dbName, ts) it, err := evalCtx.InternalExecutor.(sqlutil.InternalExecutor).QueryIterator(
we don't want to do a type assertion here -- instead add QueryIterator to the pkg/sql/sem/tree.InternalExecutor
interface
6285cd3
to
fdffa87
Compare
Can't add QueryIteratorEx to the InternalExecutor interface due to cyclic dependencies. |
Release note (sql change): crdb_internal.show_create_all_tables is a new builtin that takes in a database name (string) and returns a flat log of all the CREATE TABLE statements in the database followed by alter statements to add constraints. The output can be used to recreate a database. This builtin was added to replace old dump logic.
fdffa87
to
e77a25a
Compare
Ok this time for sure for sure. bors r=rafiss |
Build succeeded: |
sql: add crdb_internal.show_create_all_tables builtin
Making this a PR first before continuing with #53488, we can alias this builtin with SHOW CREATE ALL TABLES.
Example output:
Release note (sql change): crdb_internal.show_create_all_tables is
a new builtin that takes in a database name (string) and returns
a flat log of all the CREATE TABLE statements in the database followed
by alter statements to add constraints. The output can be used
to recreate a database. This builtin was added to replace old dump logic.