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 sequence option info for identity columns under information_schema #84034

Merged
merged 1 commit into from
Jul 18, 2022

Conversation

ZhouXing19
Copy link
Collaborator

@ZhouXing19 ZhouXing19 commented Jul 7, 2022

Previously, for a column created with the GENERATED ... AS IDENTITY (seq_options) syntax, the info for the sequence option is not saved in the
information schema.

This commit is to fix it. We parse the sequence options saved as a string in the
the descriptor, so that it's much easier to extract specific options such as
sequence's start value or increment size.

To make sure that we get the same sequence option to generate
the sequence, we reuse assignSequenceOptions() by breaking it into several
helper functions.

fixes #82064
fixes #83208

Release note (sql): add sequence option info for identity columns under
information_schema

@ZhouXing19 ZhouXing19 requested a review from a team July 7, 2022 20:24
@ZhouXing19 ZhouXing19 requested a review from a team as a code owner July 7, 2022 20:24
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@ZhouXing19 ZhouXing19 force-pushed the parse-seq-options branch from 3eb336d to f3c4e6f Compare July 7, 2022 21:46
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @ZhouXing19)


pkg/sql/information_schema.go line 481 at r1 (raw file):

				}

				var identityStart, identityIncrement, identityMax, identityMin tree.DString

nit: the type here can be *tree.DString


pkg/sql/information_schema.go line 484 at r1 (raw file):

				generatedAsIdentitySeqOpt := column.GetGeneratedAsIdentitySequenceOption()
				if generatedAsIdentitySeqOpt != nil {
					identityStart = *tree.NewDString(strconv.FormatInt(generatedAsIdentitySeqOpt.Start, 10))

nit: with the above comment, we won't need to dereference everything with *, which will avoid having to copy the string contents.


pkg/sql/catalog/tabledesc/column.go line 252 at r1 (raw file):

// GetGeneratedAsIdentitySequenceOption returns the column's `GENERATED AS
// IDENTITY` sequence option if it exists, empty string otherwise.
func (w column) GetGeneratedAsIdentitySequenceOption() *descpb.TableDescriptor_SequenceOpts {

nit: this should return an error as a second argument, and also the comment needs to be updated

also i wonder if the planner needs to be passed in here


pkg/sql/catalog/tabledesc/column.go line 258 at r1 (raw file):

	seqOpts, err := transformStringToSeqOption(*w.desc.GeneratedAsIdentitySequenceOption)
	if err != nil {
		return nil

nit: this should return err


pkg/sql/catalog/tabledesc/column.go line 267 at r1 (raw file):

// We don't do extra check for the values following the keywords, since they
// should have been checked via sql.go.
func transformStringToSeqOption(s string) (*descpb.TableDescriptor_SequenceOpts, error) {

nit: perhaps this can be named parseSequenceOpts


pkg/sql/catalog/tabledesc/column.go line 271 at r1 (raw file):

	arr := strings.Fields(strings.ToUpper(s))
	i := 0
	for i < len(arr) {

instead of this, i wonder if we can re-use the original SQL parser.

what if we do something like parser.ParseOne("CREATE SEQUENCE fake_name " + s)

then use a type assertion to turn the result into *tree.CreateSequence

then we can use the existing assignSequenceOptions function.

overall, it seems like that would be a less invasive change


pkg/sql/types/types.go line 2792 at r1 (raw file):

	case "int2", "smallint":
		return Int2
	case "int4", "integer":

i don't think this is correct if default_int_size is set to 8 (which is the default)


pkg/sql/types/types.go line 2794 at r1 (raw file):

	case "int4", "integer":
		return Int4
	case "int8", "bigint", "int64":

nit: i don't think int64 is valid here


pkg/sql/types/types.go line 2797 at r1 (raw file):

		return Int
	default:
		panic(fmt.Sprintf("unknown string %s for conversion to integet type", s))

nit: integer is misspelled

@ZhouXing19 ZhouXing19 force-pushed the parse-seq-options branch 2 times, most recently from a34e3b6 to b27bb08 Compare July 14, 2022 18:13
Copy link
Collaborator Author

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @ZhouXing19)


pkg/sql/information_schema.go line 481 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: the type here can be *tree.DString

I don't think so, seems that it will cause the nil pointer dereference if generatedAsIdentitySeqOpt == nil.


pkg/sql/catalog/tabledesc/column.go line 252 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: this should return an error as a second argument, and also the comment needs to be updated

also i wonder if the planner needs to be passed in here

Updated the returned types and the comments.
I don't think we need planner here. The planner is only used to get the sequence owner. We don't need this info in the information schema table.


pkg/sql/catalog/tabledesc/column.go line 258 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: this should return err

Done.


pkg/sql/catalog/tabledesc/column.go line 267 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: perhaps this can be named parseSequenceOpts

Done.


pkg/sql/catalog/tabledesc/column.go line 271 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

instead of this, i wonder if we can re-use the original SQL parser.

what if we do something like parser.ParseOne("CREATE SEQUENCE fake_name " + s)

then use a type assertion to turn the result into *tree.CreateSequence

then we can use the existing assignSequenceOptions function.

overall, it seems like that would be a less invasive change

Yup good idea, changed it to parser.ParseOne. I incline to use the new AssignSequenceOptions function here because we don't need owner info, so no need to pass in the planner.


pkg/sql/types/types.go line 2792 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i don't think this is correct if default_int_size is set to 8 (which is the default)

Deleted the whole function since we're using parse.ParseOne() now.

@ZhouXing19 ZhouXing19 requested a review from rafiss July 14, 2022 19:40
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

just had small nits

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @ZhouXing19)


pkg/sql/information_schema.go line 481 at r1 (raw file):

Previously, ZhouXing19 (Jane Xing) wrote…

I don't think so, seems that it will cause the nil pointer dereference if generatedAsIdentitySeqOpt == nil.

hm, actually maybe this needs to be different. i think right now, this will cause these columns to default to the empty string, but they actually should default to DNull

so initialize them as

				identityStart := tree.DNull
				identityIncrement := tree.DNull
				identityMax := tree.DNull
				identityMin := tree.DNull

then you won't need the copy with *tree.NewDString and & below


pkg/sql/sequence.go line 483 at r2 (raw file):

	}
	if err := tabledesc.AssignSequenceOptions(opts, optsNode, setDefaults, existingType); err != nil {
		return pgerror.Wrap(err, pgcode.InvalidParameterValue, "")

nit: you shouldn't wrap with an empty string. so either use pgerror.WithCandidateCode, or populate the error string passed into pgerror.Wrap


pkg/sql/sequence.go line 486 at r2 (raw file):

	}
	if err := assignSequenceOwner(ctx, p, opts, optsNode, sequenceID, sequenceParentID); err != nil {
		return pgerror.Wrap(err, pgcode.InvalidParameterValue, "")

ditto


pkg/sql/catalog/table_elements.go line 376 at r2 (raw file):

	// GetGeneratedAsIdentitySequenceOption returns the column's `GENERATED AS
	// IDENTITY` sequence option if it exists, empty string otherwise.

nit it should say "or nil otherwise"


pkg/sql/catalog/tabledesc/column.go line 271 at r2 (raw file):

// Note that this function is used to acquire the sequence option for the
// information schema table, so it doesn't parse for the sequence owner info.
func parseSequenceOpts(s string) (*descpb.TableDescriptor_SequenceOpts, error) {

i wonder if this parsing logic should live in the schemaexpr package instead. there could be a new sequence_options.go file there. what do you think makes sense?


pkg/sql/catalog/tabledesc/column.go line 583 at r2 (raw file):

	// Set the default integer type of a sequence.
	var integerType = types.Int

i wonder if this should be respecting default_int_size... well it looks like it wasn't before, so could you just leave a TODO (sql-exp) comment saying that we should decide if we want to make this use default_int_size

Copy link
Collaborator Author

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)


pkg/sql/information_schema.go line 481 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

hm, actually maybe this needs to be different. i think right now, this will cause these columns to default to the empty string, but they actually should default to DNull

so initialize them as

				identityStart := tree.DNull
				identityIncrement := tree.DNull
				identityMax := tree.DNull
				identityMin := tree.DNull

then you won't need the copy with *tree.NewDString and & below

Ah right, thanks for the explanation! Done.


pkg/sql/information_schema.go line 484 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: with the above comment, we won't need to dereference everything with *, which will avoid having to copy the string contents.

Done.


pkg/sql/sequence.go line 483 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: you shouldn't wrap with an empty string. so either use pgerror.WithCandidateCode, or populate the error string passed into pgerror.Wrap

Done.


pkg/sql/sequence.go line 486 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

ditto

Done.


pkg/sql/catalog/table_elements.go line 376 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit it should say "or nil otherwise"

Updated the comment.


pkg/sql/catalog/tabledesc/column.go line 271 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i wonder if this parsing logic should live in the schemaexpr package instead. there could be a new sequence_options.go file there. what do you think makes sense?

Yeah I agree it makes more sense to move the parsing logic to schemaexpr. Also moved AssignSequenceOptions in this new sequence_options.go as well, to avoid dependency loop.


pkg/sql/catalog/tabledesc/column.go line 583 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i wonder if this should be respecting default_int_size... well it looks like it wasn't before, so could you just leave a TODO (sql-exp) comment saying that we should decide if we want to make this use default_int_size

Yeah, nice catch! I also noticed this may be a bug for general sequences as well, so I made the issue #84554 to track. Also made a PR to fix it: #84555.

@ZhouXing19 ZhouXing19 requested a review from rafiss July 18, 2022 03:32
…schema

Previously, for a column created with the `GENERATED ... AS IDENTITY
(seq_options)` syntax, the info for the sequence option is not saved in the
information schema.

This commit is to fix it. We parse the sequence options saved as a string in
the descriptor, so that it's much easier to extract specific option such as
sequence's start value or increment size.

To make sure that we get the same sequence option to generate
the sequence, we reuse `assignSequenceOptions()` by breaking it into several
helper functions.

fixes cockroachdb#82064

Release note (sql change): add sequence option info for identity columns under
information_schema
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

nice work!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)


pkg/sql/catalog/tabledesc/column.go line 583 at r2 (raw file):

Previously, ZhouXing19 (Jane Xing) wrote…

Yeah, nice catch! I also noticed this may be a bug for general sequences as well, so I made the issue #84554 to track. Also made a PR to fix it: #84555.

nice!

@ZhouXing19
Copy link
Collaborator Author

TFTR!
bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Jul 18, 2022

Build succeeded:

@craig craig bot merged commit 116c5aa into cockroachdb:master Jul 18, 2022
craig bot pushed a commit that referenced this pull request Aug 18, 2022
84555: sql: make sequence integer bound consistent with `default_int_size` r=ZhouXing19 a=ZhouXing19

This PR is based on #84034. 
Please just look at the 2nd commit. I'll rebase this PR once #84034 is merged.

Previously, the default bounds of sequence are always`math.MaxInt64` 
or `math.MinInt64` (depending on the sequence's order). This
can be inconsistent with the cluster setting `default_int_size`. This commit
is to fix it.

fixes #84554

Release note (bug fix): make sequence integer bound consistent with the
cluster setting `default_int_size`.

Release justification: fix a bug of sequence integer bound

86253: changefeedccl: make core changefeeds more resilient r=jayshrivastava a=jayshrivastava

This change updates core changefeeds to save checkpoints
inside the EvalCtx. With this change, core changefeeds
can retry from the last checkpoint instead of restarting
from the beginning.

Fixes: #84511

Release note (general change): This change updates core/experimental
changefeeds to be more resilient to transient errors
(ex. network blips) by adding checkpointing.

Previously, transient errors would result in a core changefeed
stopping and terminating the underlying SQL statement. This
would require the SQL statement to be restarted by a user.
Furtheremore, if the core changefeed were restarted during an
inital scan, the initial scan would start from the beginning.
For large initial scans, transient errors are more likely,
so restarting from the beginning would likely see more transient
errors and restarts which would not progress the changefeed.

Now, an experimental changefeed will automatically take
frequent checkpoints and retry from the last checkpoint
when a transient errors occurs.

Release justification: This change updates an experimental
feature.

86272: insights: execution_insights_capacity cluster setting r=matthewtodd a=matthewtodd

Fixes #79450.

Whereas we previously retained only the 10 most recent insights, we now
let the user choose how many they'd like to hang onto.

It may make sense to be more sophisticated than a simple LRU cache here:
perhaps we won't want a single fingerprint to dominate the list. That
work is captured in #86271.

Release justification: Category 2: Bug fixes and low-risk updates to new
functionality.

Release note (ops change): The
`sql.insights.execution_insights_capacity` cluster setting was
introduced, limiting the number of SQL execution insights retained in
memory per node.

86329: ui/cluster-ui: show status as waiting when txn is waiting for lock r=xinhaoz a=xinhaoz

Now that we have surfaced contention information in the UI, we can
update the stmt / txn status field for active executions to be
'Waiting' when the stmt or txn is waiting to acquire a lock.

Release justification: low risk update to existing functionality
Release note (ui change): txns and stmts in active exec pages that
are waiting for a lock will now have the status 'Waiting'

<img width="654" alt="image" src="https://user-images.githubusercontent.com/20136951/185226858-8c194582-d405-4c8b-aec9-7a21a4bc1c22.png">


86348: sql/stats: remove NumRange-stealing behavior from histogram prediction r=yuzefovich,rytaft a=michae2

We should be able to handle NumEq=0 just fine everywhere that uses
histograms, so delete this NumRange-stealing code.

Fixes: #86344

Release justification: low-risk updates to new functionality.

Release note: None

86383: sql: move UDF execution tests to bottom of test file r=mgartner a=mgartner

This commit moves UDF execution logic tests to the bottom of the UDF
test file so that execution-related tests add in the future will not
change the output of schema-related tests.

Release justification: This is a test-only change.

Release note: None

86385: opt: clarify the return type of Index.Ordinal() r=mgartner a=mgartner

The return type of `Index.Ordinal()` is now the type alias
`cat.IndexOrdinal` to be consistent with other functions that use an
index ordinal, like `Table.Index(i cat.IndexOrdinal)`. It is now more
clear that `idx == Table().Index(idx.Ordinal())`

Release justification: This is a very small, low-risk change.

Release note: None

Co-authored-by: Jane Xing <[email protected]>
Co-authored-by: Jayant Shrivastava <[email protected]>
Co-authored-by: Matthew Todd <[email protected]>
Co-authored-by: Xin Hao Zhang <[email protected]>
Co-authored-by: Michael Erickson <[email protected]>
Co-authored-by: Marcus Gartner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants