Skip to content
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

Merged

Conversation

abhishek20123g
Copy link
Contributor

@abhishek20123g abhishek20123g commented Feb 15, 2020

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.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@claassistantio
Copy link

claassistantio commented Feb 15, 2020

CLA assistant check
All committers have signed the CLA.

rohany
rohany previously requested changes Feb 15, 2020
Copy link
Contributor

@rohany rohany left a 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: :shipit: 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?

@abhishek20123g abhishek20123g force-pushed the sql-add-getdatabaseencoding-function branch from 78733d0 to ed32ba7 Compare February 16, 2020 01:59
@abhishek20123g
Copy link
Contributor Author

abhishek20123g commented Feb 16, 2020

Hi @rohany
Thanks for suggesting changes, changes had been made as requested.
I have combined two separate queries for evaluating encoding_id and converting encoding_id to encoding name into one query (which makes more sense).
I am returning nil instead of DNull.
Refactored row1, err1 to row, err, respectively.

wrt Adding test-cases for getdatabaseencoding(),
I had added test-case in pkg/sql/logictest/testdata/logic_test/builtin_function, I am not sure about whether i should add unit test-case in this file or in other file. I have decided to add a test-case in this file because getdatabasencoding() is a builtin function, and most of the functions related to it are also present in this file.
And I was only able to add test-case corresponding to UTF8 because perhaps CockroachDB currently only supports UTF8 encoding.

@rohany
Copy link
Contributor

rohany commented Feb 16, 2020

The test looks good! We do only support UTF8 right now.

Some more nits:

  • Can you squash these commits into a single commit?
  • Can you rename the commit to "sql: add the getdatabaseencoding() builtin function"
  • Please use the commit template within cockroach to write a commit message. Importantly, your commit needs a release note. The instructions for this are in the commit template.

@abhishek20123g
Copy link
Contributor Author

Absolutely, I will squash all the commit into one, and make them look as recommended.

@abhishek20123g abhishek20123g force-pushed the sql-add-getdatabaseencoding-function branch from 8251bfe to e17cd68 Compare February 16, 2020 06:50
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
Copy link
Contributor

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!

@rohany
Copy link
Contributor

rohany commented Feb 16, 2020

Aside from my nit about the comment, this looks good! Once you fix that I will merge the PR.

@abhishek20123g abhishek20123g force-pushed the sql-add-getdatabaseencoding-function branch from e17cd68 to 00e87fa Compare February 16, 2020 18:12
@abhishek20123g
Copy link
Contributor Author

Hi @rohany,
I have changed comment as recommended.

And thank you for guiding me, at various points in this PR.
For my future PR's, I will try to make them easy to merge.

@rohany
Copy link
Contributor

rohany commented Feb 16, 2020

Can you rebase this onto master? It looks like you're running into some sort of test flake.

row, err := ctx.InternalExecutor.QueryRow(
ctx.Ctx(), "getdatabaseencoding",
ctx.Txn,
"SELECT pg_encoding_to_char(encoding) "+
Copy link
Contributor

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

if args[0].Compare(ctx, DatEncodingUTFId) == 0 {

rather than trying to call a nested database query.

Copy link
Contributor Author

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.

Copy link
Contributor

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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I will update.

@abhishek20123g abhishek20123g force-pushed the sql-add-getdatabaseencoding-function branch 2 times, most recently from 8895468 to 9850f90 Compare February 17, 2020 07:22
@abhishek20123g
Copy link
Contributor Author

hi @otan,
I have moved getdatabaseencoding() form bulitins.go to pg_builtins.go .
Created a helper function called getEncodingNameForEncodingID.
Updated the defination of pg_encoding_to_char and getdatabaseencoding .

Do I have to rebase changes to master as suggested by rohany ?

@abhishek20123g abhishek20123g force-pushed the sql-add-getdatabaseencoding-function branch from 9850f90 to 7986c87 Compare February 17, 2020 08:16
row, err := ctx.InternalExecutor.QueryRow(
ctx.Ctx(), "getdatabaseencoding",
ctx.Txn,
"SELECT encoding FROM pg_database WHERE datname = $1", ctx.SessionData.Database)
Copy link
Contributor

@otan otan Feb 17, 2020

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:

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.

Copy link
Contributor

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)

Copy link
Contributor Author

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

@abhishek20123g abhishek20123g force-pushed the sql-add-getdatabaseencoding-function branch from 7986c87 to 400afb1 Compare February 17, 2020 10:30
@abhishek20123g abhishek20123g force-pushed the sql-add-getdatabaseencoding-function branch from 400afb1 to 3c2dcbc Compare February 17, 2020 12:29
@ajwerner
Copy link
Contributor

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.
@otan otan changed the title sql: Adding getdatabaseencoding() function, with respect to #issue 41771 sql: add the getdatabaseencoding() builtin function Feb 18, 2020
@otan otan force-pushed the sql-add-getdatabaseencoding-function branch from 400afb1 to ecb7ffc Compare February 18, 2020 08:05
Copy link
Contributor

@otan otan left a 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!

@otan otan requested a review from rohany February 18, 2020 08:06
@otan
Copy link
Contributor

otan commented Feb 18, 2020

bors r=otan

craig bot pushed a commit that referenced this pull request Feb 18, 2020
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>
@otan otan dismissed rohany’s stale review February 18, 2020 08:07

i have taken over!

@craig
Copy link
Contributor

craig bot commented Feb 18, 2020

Build succeeded

@craig craig bot merged commit ecb7ffc into cockroachdb:master Feb 18, 2020
@abhishek20123g
Copy link
Contributor Author

thanks, @rohany, and @otan for helping me out

Can you recommend to me some issues where I could start working?
thanks

@rohany
Copy link
Contributor

rohany commented Feb 18, 2020

Our good first issue page has a ton of issues that you could take a look at!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: add getdatabaseencoding() function
6 participants