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 getdatabaseencoding() function #41771

Closed
rafiss opened this issue Oct 21, 2019 · 12 comments · Fixed by #45129
Closed

sql: add getdatabaseencoding() function #41771

rafiss opened this issue Oct 21, 2019 · 12 comments · Fixed by #45129
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) good first issue

Comments

@rafiss
Copy link
Collaborator

rafiss commented Oct 21, 2019

Postgres has a getdatabaseencoding() function that we should support.

@rafiss rafiss added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-pgcompat Semantic compatibility with PostgreSQL labels Oct 21, 2019
@joshuabezaleel
Copy link

Are there any recommendations on where to start and references if someone is interested in this?
Thank you very much! 🙂

@avinashdhinwa
Copy link

avinashdhinwa commented Oct 29, 2019

Are there any recommendations on where to start and references if someone is interested in this?
Thank you very much! 🙂

@jordanlewis @rafiss

@rohany
Copy link
Contributor

rohany commented Oct 29, 2019

It looks like this information is stored in the database pg_database. You could get the information from there. To add a builtin function, you would need to add another function to the list in pkg/sql/sem/builtins/builtins.go. You could use the InternalExecutor object within the EvalContext to issue a query to that database.

Let me know if you have any questions!

@rafiss
Copy link
Collaborator Author

rafiss commented Nov 11, 2019

For reference, here are the encodings that Postgres uses:

 const pg_enc2gettext pg_enc2gettext_tbl[] =
 {
     {PG_SQL_ASCII, "US-ASCII"},
     {PG_UTF8, "UTF-8"},
     {PG_LATIN1, "LATIN1"},
     {PG_LATIN2, "LATIN2"},
     {PG_LATIN3, "LATIN3"},
     {PG_LATIN4, "LATIN4"},
     {PG_ISO_8859_5, "ISO-8859-5"},
     {PG_ISO_8859_6, "ISO_8859-6"},
     {PG_ISO_8859_7, "ISO-8859-7"},
     {PG_ISO_8859_8, "ISO-8859-8"},
     {PG_LATIN5, "LATIN5"},
     {PG_LATIN6, "LATIN6"},
     {PG_LATIN7, "LATIN7"},
     {PG_LATIN8, "LATIN8"},
     {PG_LATIN9, "LATIN-9"},
     {PG_LATIN10, "LATIN10"},
     {PG_KOI8R, "KOI8-R"},
     {PG_KOI8U, "KOI8-U"},
     {PG_WIN1250, "CP1250"},
     {PG_WIN1251, "CP1251"},
     {PG_WIN1252, "CP1252"},
     {PG_WIN1253, "CP1253"},
     {PG_WIN1254, "CP1254"},
     {PG_WIN1255, "CP1255"},
     {PG_WIN1256, "CP1256"},
     {PG_WIN1257, "CP1257"},
     {PG_WIN1258, "CP1258"},
     {PG_WIN866, "CP866"},
     {PG_WIN874, "CP874"},
     {PG_EUC_CN, "EUC-CN"},
     {PG_EUC_JP, "EUC-JP"},
     {PG_EUC_KR, "EUC-KR"},
     {PG_EUC_TW, "EUC-TW"},
     {PG_EUC_JIS_2004, "EUC-JP"},
     {PG_SJIS, "SHIFT-JIS"},
     {PG_BIG5, "BIG5"},
     {PG_GBK, "GBK"},
     {PG_UHC, "UHC"},
     {PG_GB18030, "GB18030"},
     {PG_JOHAB, "JOHAB"},
     {PG_SHIFT_JIS_2004, "SHIFT_JISX0213"},
     {0, NULL}
 };

This enum is referenced from https://github.com/postgres/postgres/blob/8e10405c745003c5c16acb2da847db9bed1a169e/src/backend/utils/mb/mbutils.c#L1048

@kul-amr
Copy link

kul-amr commented Nov 29, 2019

Hey - I am new to OSS contribution. Can I work on this if no one else is attempting?

@rohany
Copy link
Contributor

rohany commented Nov 29, 2019

Sure, it doesn't look like whoever asked before is still working on this!

@tclass
Copy link

tclass commented Feb 14, 2020

Hey, I thought I try to create a PR for this, but somehow I run into a stalling issue with my code and I can't find out why. Does anyone has a hint for me? When I call the function the query never ends and just stalls the whole system.

	"getdatabaseencoding": makeBuiltin(
		tree.FunctionProperties{Category: categorySystemInfo},
		tree.Overload{
			Types:      tree.ArgTypes{},
			ReturnType: tree.FixedReturnType(types.String),
			Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
				r, err := ctx.InternalExecutor.QueryRow(ctx.Ctx(),"getdatabaseencoding()",ctx.Txn,"SELECT getdatabaseencoding()")
				if err != nil || r == nil {
					return tree.NewDString("error"),err
				}
				return tree.NewDString("some result"), nil
			},
			Info: "Returns the encoding of the database",
		},
	),

@rohany
Copy link
Contributor

rohany commented Feb 14, 2020

You can't call getdatabaseencoding in the implementation of getdatabaseencoding -- its infinitely looping. You want to be getting rows from pg_catalog.pg_database.

@abhishek20123g
Copy link
Contributor

abhishek20123g commented Feb 14, 2020

hi, I have implemented ,

"getdatabaseencoding": makeBuiltin(
		tree.FunctionProperties{Category: categorySystemInfo},
		tree.Overload{
			Types:      tree.ArgTypes{},
			ReturnType: tree.FixedReturnType(types.String),
			Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
				r1, err1 := ctx.InternalExecutor.QueryRow(
					ctx.Ctx(), "getdatabaseencoding",
					ctx.Txn,
					"SELECT encoding FROM pg_database "+
						"WHERE datname = $1", ctx.SessionData.Database)
				encodingId := *(r1[0].(*tree.DInt))
				if err1 != nil {
					return tree.DNull, err1
				}

				// Using pg_encoding_to_char() to convert encodingId to encoding.
				r2, err2 := ctx.InternalExecutor.QueryRow(
					ctx.Ctx(), "getdatabaseencoding",
					ctx.Txn,
					"SELECT pg_encoding_to_char($1)", encodingId)
				if err2 != nil {
					return tree.DNull, err2
				}
				return r2[0], nil
			},
			Info: "Returns the current encoding used by database.",
		},
	),

thorw this i was able to provide query for database,
plz let me know if some improvement require.

I have tested it locally it working fine

@rohany
Copy link
Contributor

rohany commented Feb 14, 2020

If you think you have a working solution, please open a PR! We can review it there.

@abhishek20123g
Copy link
Contributor

Thanks
I will open PR for review

@tclass
Copy link

tclass commented Feb 15, 2020

@rohany oh man, can't believe I didn't get that it's calling itself (fiddled around for hours). Have to read up more about the architecture, sad that it's already solved now

abhishek20123g added a commit to abhishek20123g/cockroach that referenced this issue Feb 16, 2020
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.
abhishek20123g added a commit to abhishek20123g/cockroach that referenced this issue Feb 16, 2020
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.
abhishek20123g added a commit to abhishek20123g/cockroach that referenced this issue Feb 17, 2020
Resolves cockroachdb#41771.

This commit apply following changes.
* This commit adds builtin function, getdatabaseencoding().
* Created a helper function getEncodingNameForEncodingID which
  returns the Encoding name for a given Encoding ID.
* Modified defination of pg_encoding_to_char().

Release note (sql change): This PR is introduced to add builtin
function, getdatabaseencoding(), which returns the current encoding
name used by the database.
craig bot pushed a commit that referenced this issue 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 <[email protected]>
@craig craig bot closed this as completed in ecb7ffc Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants