-
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 the getdatabaseencoding() builtin function #45129
sql: add the getdatabaseencoding() builtin function #45129
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.
Thanks for the contribution! This needs a test before it can land. You should add a logic test for it (perhaps in pkg/sql/logictest/testdata/logic_test/pg_builtins
.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @abhishek20123g and @otan)
pkg/sql/sem/builtins/builtins.go, line 3073 at r1 (raw file):
ReturnType: tree.FixedReturnType(types.String), Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) { rows1, err1 := ctx.InternalExecutor.QueryRow(
nit: i'd name these just rows, err
.
pkg/sql/sem/builtins/builtins.go, line 3081 at r1 (raw file):
encodingID, ok := rows1[0].(*tree.DInt) if !ok { return tree.DNull, errors.New("failed to type assert Datum to DInt")
this should return nil rather than DNull
. Additionally, I'd make this an errors.AssertionFailure
with more detail. In this case we already know that we couldn't perform the type assertion, but we should have more context on what exactly we expected vs what we found.
pkg/sql/sem/builtins/builtins.go, line 3083 at r1 (raw file):
return tree.DNull, errors.New("failed to type assert Datum to DInt") } if err1 != nil {
You need to check if the error is not nil before accessing the result of the query.
pkg/sql/sem/builtins/builtins.go, line 3084 at r1 (raw file):
} if err1 != nil { return tree.DNull, err1
same, this should be nil.
pkg/sql/sem/builtins/builtins.go, line 3088 at r1 (raw file):
// Using pg_encoding_to_char() to convert an encoding ID to an encoding name. rows2, err2 := ctx.InternalExecutor.QueryRow(
Why not just call pg_encoding_to_char
in your first query?
78733d0
to
ed32ba7
Compare
Hi @rohany wrt Adding test-cases for getdatabaseencoding(), |
The test looks good! We do only support UTF8 right now. Some more nits:
|
Absolutely, I will squash all the commit into one, and make them look as recommended. |
8251bfe
to
e17cd68
Compare
pkg/sql/sem/builtins/builtins.go
Outdated
Types: tree.ArgTypes{}, | ||
ReturnType: tree.FixedReturnType(types.String), | ||
Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) { | ||
// computing encoding name bypassing encoding_id which is |
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.
Can you change this comment to
"Compute encoding name by calling pg_encoding_to_char on the encoding from pg_database."
All comments should be complete sentences!
Aside from my nit about the comment, this looks good! Once you fix that I will merge the PR. |
e17cd68
to
00e87fa
Compare
Hi @rohany, And thank you for guiding me, at various points in this PR. |
Can you rebase this onto master? It looks like you're running into some sort of test flake. |
pkg/sql/sem/builtins/builtins.go
Outdated
row, err := ctx.InternalExecutor.QueryRow( | ||
ctx.Ctx(), "getdatabaseencoding", | ||
ctx.Txn, | ||
"SELECT pg_encoding_to_char(encoding) "+ |
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.
i think this should go in pg_builtins.go
, and we should probably try re-using
cockroach/pkg/sql/sem/builtins/pg_builtins.go
Line 567 in bb14400
if args[0].Compare(ctx, DatEncodingUTFId) == 0 { |
rather than trying to call a nested database query.
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.
Sure, I will move it to pg_builtins.go
and you mean rather calling pg_encoding_to_char
inside the query, I should use the same logic in pg_encoding_to_char
to convert encodingID to encoding name.
can we do it by creating some kind of external helper function which converts encodingID to the encoding name which is being called by both, pg_encoding_to_char
and getdatabaseencoding
?
because otherwise, we would have update logics inside both functions for adding support to another encoding type.
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.
can we do it by creating some kind of external helper function which converts encodingID to the encoding name which is being called by both, pg_encoding_to_char and getdatabaseencoding?
yes that would be my suggestion :)
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.
sure, I will update.
8895468
to
9850f90
Compare
hi @otan, Do I have to rebase changes to master as suggested by rohany ? |
9850f90
to
7986c87
Compare
pkg/sql/sem/builtins/pg_builtins.go
Outdated
row, err := ctx.InternalExecutor.QueryRow( | ||
ctx.Ctx(), "getdatabaseencoding", | ||
ctx.Txn, | ||
"SELECT encoding FROM pg_database WHERE datname = $1", ctx.SessionData.Database) |
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.
sorry bit slow to realise this - looks like we're always returning UTF-8 for this anyway. We populate pg_database
with a virtual schema, and we always return the same value of UTF-8 for this query anyway:
cockroach/pkg/sql/pg_catalog.go
Line 1084 in bb14400
builtins.DatEncodingUTFId, // encoding |
Perhaps we should also just return UTF-8 here as well. Make a comment on the virtual schema that if we ever change the value of that, we should change it here and vice versa.
Sorry for the round-aboutness with getting this out there.
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.
(note: i would generally try avoid calling another query inside here unless you had to, which led me down this path to finding this :] ideally, if it's configurable, we'd have a function that lets us figure this out without shelling out to another query)
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.
So, rather than using InternalExecutor to execute the query you are recommending me to
just return UTF-8 and add a comment to virtual schema to that if we change value there we update it here too.
like use in pg_client_encoding
in pg_builtins.go
7986c87
to
400afb1
Compare
400afb1
to
3c2dcbc
Compare
Flaked on #44944. |
Resolves cockroachdb#41771. This commit adds builtin function, getdatabaseencoding(), and the unit-test case for it. Release note (sql change): This PR is introduced to add builtin function, getdatabaseencoding(), which returns the current encoding name used by the database.
400afb1
to
ecb7ffc
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 good to me - i made a minor jiggle with the comments for you.
thanks for contributing and hope to see more!
bors r=otan |
45129: sql: add the getdatabaseencoding() builtin function r=otan a=abhishek20123g Resolves #41771. This commit adds builtin function, getdatabaseencoding(), and the unit-test case for it. Release note (sql change): This PR is introduced to add builtin function, getdatabaseencoding(), which returns the current encoding name used by the database. Co-authored-by: abhishek20123g <abhishek20123g@gmail.com>
Build succeeded |
Our good first issue page has a ton of issues that you could take a look at! |
Resolves #41771.
This commit adds builtin function, getdatabaseencoding(),
and the unit-test case for it.
Release note (sql change): This PR is introduced to add builtin
function, getdatabaseencoding(), which returns the current encoding
name used by the database.