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: remove InternalExecutor from EvalCtx #71246

Merged

Conversation

RichardJCai
Copy link
Contributor

@RichardJCai RichardJCai commented Oct 6, 2021

Minor refactor to remove duplicate InternalExecutor definition
on EvalCtx and in the tree package.

We already have 2 interface declarations for InternalExecutor
in sql and sqlutil, this refactor hopefully makes the dependencies
a little more clear.

Resolves #60507

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@RichardJCai RichardJCai force-pushed the remove_internal_executor_on_eval_ctx branch 8 times, most recently from ce68e91 to 7d2431d Compare October 10, 2021 04:53
@RichardJCai RichardJCai marked this pull request as ready for review October 11, 2021 19:42
@RichardJCai RichardJCai requested review from a team October 11, 2021 19:42
@RichardJCai RichardJCai changed the title wip: Remove InternalExecutor from EvalCtx sql: remove InternalExecutor from EvalCtx Oct 11, 2021
@RichardJCai RichardJCai requested a review from ajwerner October 11, 2021 19:42
@RichardJCai
Copy link
Contributor Author

Need to do a bit more cleanup but what are the thoughts on using the InternalExecutor on ExecCfg for builtins.

The downside is that we need to specify more overrides to use the InternalExecutor for builtins but I think this is somewhat more clear of an interface.

@ajwerner
Copy link
Contributor

I'm torn. I think removing the complexity as we have it is fine, but I don't know that this the direction I'd like for us to go longer term. Maybe this is a good stepping stone towards the better future.

I think my preference would be for us to rework the interfaces such that you always have to inject a bunch of information into the internal executor to use it. Like, imagine you always have a factory hanging off of the ExecCfg and you never have a thing that you can directly issue queries off of. Then, in the context of a transaction you you have a fully constructed internal executor that is tightly bound to a bunch of your conn executor state like the transaction, session data, extra txn state (including the descs collection). In that way, everything would be totally explicit and properly bound. What do you think of that sort of mental model? I tried, perhaps poorly, to express these ideas in #69495 (comment) and the subsequent comment.

@RichardJCai
Copy link
Contributor Author

I'm torn. I think removing the complexity as we have it is fine, but I don't know that this the direction I'd like for us to go longer term. Maybe this is a good stepping stone towards the better future.

I think my preference would be for us to rework the interfaces such that you always have to inject a bunch of information into the internal executor to use it. Like, imagine you always have a factory hanging off of the ExecCfg and you never have a thing that you can directly issue queries off of. Then, in the context of a transaction you you have a fully constructed internal executor that is tightly bound to a bunch of your conn executor state like the transaction, session data, extra txn state (including the descs collection). In that way, everything would be totally explicit and properly bound. What do you think of that sort of mental model? I tried, perhaps poorly, to express these ideas in #69495 (comment) and the subsequent comment.

Yeah I'd argue this is a good stepping stone towards the future. In this pr, we essentially override every time we use the InternalExecutor for builtins. Going from here to using a factory hanging off ExecCfg that takes some params (session data, desc Collections, txn, state) to create a new InternalExecutor feels natural.

I'll see how viable / tricky that would be to do right now.

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.

i like the direction of this. definitely feels like an improvement.

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


pkg/sql/backfill.go, line 1882 at r3 (raw file):

			return ie.WithSyntheticDescriptors([]catalog.Descriptor{desc}, func() error {
				cnt, err := ie.QueryRowEx(ctx, "VERIFY INDEX", txn,
					sessiondata.NodeUserSessionDataOverride, query)

do you know when we are supposed to use the node user versus the root user?


pkg/sql/exec_util.go, line 1176 at r3 (raw file):

// InternalExecutorFactory is a function that produces a "session
// bound" internal executor.

does session bound in this context mean that it should always inherit session variables from the parent session?

thinking of #70888

speaking of which, @cucaroach you'd probably want to be aware of this change (at the moment still in draft state)


pkg/sql/planner.go, line 828 at r3 (raw file):

	opName string,
	txn *kv.Txn,
	session *sessiondata.SessionData,

nit: name it sd maybe?


pkg/sql/planner.go, line 840 at r3 (raw file):

	opName string,
	txn *kv.Txn,
	session *sessiondata.SessionData,

nit: name it sd


pkg/sql/sem/tree/eval.go, line 3248 at r3 (raw file):

// InternalRows is an iterator interface that's exposed by the internal
// executor. It provides access to the rows from a query.
type InternalRows interface {

hm, is tree the right package for InternalRows? i wonder if it'd be better to have it in a new package. well then again it makes sense to be in the same package as EvalPlanner. i'm fine to leave it here if there's no better place

anyway, would be worth adding a var _ InternalRows = rowsIterator{} assertion somewhere

@RichardJCai RichardJCai force-pushed the remove_internal_executor_on_eval_ctx branch 3 times, most recently from f891b79 to a51ea40 Compare October 21, 2021 20:31
Copy link
Contributor Author

@RichardJCai RichardJCai 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 @ajwerner, @cucaroach, and @rafiss)


pkg/sql/backfill.go, line 1882 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

do you know when we are supposed to use the node user versus the root user?

I think root should be sufficient but I thought this fit similarly to how the other NodeUserSessionDataOverrides were used which is for "system" ops. I think backfill falls under that.


pkg/sql/exec_util.go, line 1176 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

does session bound in this context mean that it should always inherit session variables from the parent session?

thinking of #70888

speaking of which, @cucaroach you'd probably want to be aware of this change (at the moment still in draft state)

Yeah session data and some txn state should be passed when creating it.


pkg/sql/sem/tree/eval.go, line 3248 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

hm, is tree the right package for InternalRows? i wonder if it'd be better to have it in a new package. well then again it makes sense to be in the same package as EvalPlanner. i'm fine to leave it here if there's no better place

anyway, would be worth adding a var _ InternalRows = rowsIterator{} assertion somewhere

Added the var _ InternalRows = rowsIterator{}

The deps are pretty messy here, I'd like to make InternalRows standalone but the original one has colinfo.ResultColumns which has a dependency on tree and I can't find a nice way to make it work.

@RichardJCai RichardJCai requested a review from rafiss October 21, 2021 20:32
2,ORMQueries/pg_attribute
2,ORMQueries/pg_class
4,ORMQueries/pg_is_other_temp_schema
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why these disappeared before, they're defined in the orm_queries_bench_test.go file

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 some relatively small comments

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @cucaroach, @rafiss, and @RichardJCai)


pkg/sql/conn_executor.go, line 1500 at r5 (raw file):

	}

	if !ex.extraTxnState.keepDescCollectionOnClose {

should this change be done as a separate PR?


pkg/sql/internal.go, line 131 at r5 (raw file):

func (ie *InternalExecutor) SetExtraTxnState(extraTxnState ExtraTxnState) {
	ie.extraTxnState = extraTxnState

hm, i wonder if the extraTxnState stuff should be a different PR. I don't feel strongly, if it would be hard to separate into a different change I wouldn't worry much about it.


pkg/sql/planner.go, line 820 at r5 (raw file):

// QueryRowEx is like QueryRow, but allows the caller to override some session data
// fields (e.g. the user).

nit: the comment refers to QueryRow, but that isn't defined on this type


pkg/sql/resolve_oid.go, line 60 at r5 (raw file):

	toResolve tree.Datum,
	user security.SQLUsername,
	database string,

why did we need to add these two parameters?


pkg/sql/sem/tree/eval.go, line 3228 at r5 (raw file):

	ExternalWriteFile(ctx context.Context, uri string, content []byte) error

	QueryRowEx(

nit: comment this function and the next


pkg/sql/sem/tree/eval.go, line 3232 at r5 (raw file):

		opName string,
		txn *kv.Txn,
		session *sessiondata.SessionData,

nit: sessionData or sd instead of session

but does this actually need to be a param? won't the planner always have one of these available on it already? so we can just get it from the receiver

@RichardJCai RichardJCai force-pushed the remove_internal_executor_on_eval_ctx branch from a51ea40 to 67a87e3 Compare November 23, 2021 17:13
@RichardJCai RichardJCai requested a review from a team as a code owner November 23, 2021 17:13
Copy link
Contributor Author

@RichardJCai RichardJCai 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 @ajwerner, @cucaroach, @rafiss, and @RichardJCai)


pkg/sql/backfill.go, line 1566 at r6 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nvm, changed in the rebase

Done.


pkg/sql/backfill.go, line 1688 at r6 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nvm, changed in the rebase

Done.


pkg/sql/backfill.go, line 1787 at r6 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

ah i see it happened in a rebase. this seems fine then

Done.


pkg/sql/unsplit.go, line 110 at r6 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

this used to use root, but now doesn't have an override. does it still need to be root or node?

This actually used NoSessionDataOverride or rahter just an empty sessiondata.InternalExecutorOverride{} before - this should continue to be sufficient. (Look at first commit)

@RichardJCai RichardJCai force-pushed the remove_internal_executor_on_eval_ctx branch 2 times, most recently from a35fbf0 to c61e3fc Compare November 24, 2021 19:10
@RichardJCai RichardJCai requested a review from rafiss November 24, 2021 19:10
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 @ajwerner, @cucaroach, @rafiss, and @RichardJCai)


pkg/sql/resolve_oid.go, line 78 at r7 (raw file):

	results, err := ie.QueryRowEx(ctx, "queryOid", txn,
		sessiondata.NoSessionDataOverride, q, toResolve)
	//results, err := ie.QueryRowEx(ctx, "queryOid", txn,

commented code (and just confirming that the user override is right?)


pkg/sql/unsplit.go, line 110 at r6 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

This actually used NoSessionDataOverride or rahter just an empty sessiondata.InternalExecutorOverride{} before - this should continue to be sufficient. (Look at first commit)

ah OK then i guess i got mixed up -- but now it looks like r7 is using NodeUser again?


pkg/bench/rttanalysis/testdata/benchmark_expectations, line 54 at r7 (raw file):

20,Grant/grant_all_on_3_tables
17,GrantRole/grant_1_role
20,GrantRole/grant_2_roles

looks like between r6 and r7 some of these disappeared again

10,InternalExecutor/has_schema_privilege_1_col
14,InternalExecutor/has_schema_privilege_2_col
18,InternalExecutor/has_schema_privilege_3_col
48,InternalExecutor/has_schema_privilege_3_schemas_3_col
72,InternalExecutor/has_schema_privilege_5_schemas_3_col

3,ORMQueries/information_schema._pg_index_position

4,ORMQueries/pg_is_other_temp_schema
4,ORMQueries/pg_is_other_temp_schema_multiple_times
2,ORMQueries/pg_my_temp_schema
2,ORMQueries/pg_my_temp_schema_multiple_times

Minor refactor to remove duplicate InternalExecutor definition
on EvalCtx and in the tree package.

We already have 2 interface declarations for InternalExecutor
in sql and sqlutil, this refactor hopefully makes the dependencies
a little more clear.

For builtins, we now use an InternalExecutorFactory where
we must pass in the txn, session data and desc.Collections
to use the IE.

Release note: None
@RichardJCai RichardJCai force-pushed the remove_internal_executor_on_eval_ctx branch from c61e3fc to 203c7f4 Compare November 24, 2021 20:30
Copy link
Contributor Author

@RichardJCai RichardJCai 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 @ajwerner, @cucaroach, @rafiss, and @RichardJCai)


pkg/sql/resolve_oid.go, line 78 at r7 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

commented code (and just confirming that the user override is right?)

Yeah before this used to just be QueryRow so I think NoSessionDataOverride should work here


pkg/sql/unsplit.go, line 110 at r6 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

ah OK then i guess i got mixed up -- but now it looks like r7 is using NodeUser again?

Updated.


pkg/bench/rttanalysis/testdata/benchmark_expectations, line 54 at r7 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

looks like between r6 and r7 some of these disappeared again

10,InternalExecutor/has_schema_privilege_1_col
14,InternalExecutor/has_schema_privilege_2_col
18,InternalExecutor/has_schema_privilege_3_col
48,InternalExecutor/has_schema_privilege_3_schemas_3_col
72,InternalExecutor/has_schema_privilege_5_schemas_3_col

3,ORMQueries/information_schema._pg_index_position

4,ORMQueries/pg_is_other_temp_schema
4,ORMQueries/pg_is_other_temp_schema_multiple_times
2,ORMQueries/pg_my_temp_schema
2,ORMQueries/pg_my_temp_schema_multiple_times

Yeah I reverted all my benchmark changes since this shouldn't actually change anything if I'm not passing desc cols. Will re-add in future desc colsPPR.

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.

lgtm! i still don't really get why those benchmark changes were missing, but seems fine to add them later

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @cucaroach, @rafiss, and @RichardJCai)

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.

maybe a followup is to remove ExecCfg.InternalExecutor entirely?

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @cucaroach, @rafiss, and @RichardJCai)

@RichardJCai
Copy link
Contributor Author

TFTR!
bors r=rafiss

@craig craig bot merged commit 8fa3a38 into cockroachdb:master Nov 24, 2021
@craig
Copy link
Contributor

craig bot commented Nov 24, 2021

Build succeeded:

@adityamaru
Copy link
Contributor

@RichardJCai do you have a follow up to inject extra txn state into the IE sitting around, or should I write one up? I kinda need something similar for some IMPORT work I'm doing.

@RichardJCai
Copy link
Contributor Author

@RichardJCai do you have a follow up to inject extra txn state into the IE sitting around, or should I write one up? I kinda need something similar for some IMPORT work I'm doing.

If you look at the reviewable on r5, I had something to inject extra txn state, however I'll probably not get to it until Tuesday, if you need it more urgently I'd recommend taking inspiration from r5 here and optionally pass in ExtraTxnState into SessionBoundInternalExecutorFactory it shouldn't be too much to add on I believe. I left it out of this PR to limit the scope of it. I'd recommend doing something like passing in optional functions into InternalExecutorFactory where the function calls takes an InternalExecutor and mutates it in some way (injecting state in this case)

@adityamaru
Copy link
Contributor

adityamaru commented Nov 26, 2021

I took a stab at #73193. I'm conflicted whether we should pass in a closure and leave it up to the user of the internal executor to set the appropriate fields, or if we should require the user to inject the state by having typed arguments in the factory?

@adityamaru
Copy link
Contributor

Regardless of what we decide above, I think the next step as pointed out by rafi is to remove the InternalExecutor from ExecCfg entirely and force everyone to initialize a new one when they need one. This way we move closer to our eventual goal of tightly binding statement execution with the external state. I'll try putting that out next before trying to inject ExtraTxnState.

ajwerner added a commit to ajwerner/cockroach that referenced this pull request Dec 22, 2021
Any time we use WithSyntheticDescriptors, it has to be on an unshared internal
executor. The move in cockroachdb#71246 to not have an internal executor hanging around
for the current session hurts here.

Fixes cockroachdb#73788

Release note: None
craig bot pushed a commit that referenced this pull request Jan 3, 2022
71910: sql: add a cluster setting to avoid system config triggers r=ajwerner a=ajwerner

This is intended as a short-term workaround to improve performance in
situations of repeated schema changes, like ORM tests.

We have a plan to disable the system config trigger altogether in 22.1 with
#70560. 

This PR provides a cluster setting which allows schema change transactions
to bypass triggerring an update to the system config span. These updates
currently drive only the propagation of zone configs to KV and cluster
settings. The cluster setting behavior is retained until we address #70566.

We have a history of these sorts of unsafe settings in
`kv.raft_log.disable_synchronization_unsafe`.

Release note: None

74188: sql: fix InternalExecutor bug r=ajwerner a=ajwerner

Any time we use WithSyntheticDescriptors, it has to be on an unshared internal
executor. The move in #71246 to not have an internal executor hanging around
for the current session hurts here.

Fixes #73788

Release note: None

Co-authored-by: Andrew Werner <[email protected]>
gustasva pushed a commit to gustasva/cockroach that referenced this pull request Jan 4, 2022
Any time we use WithSyntheticDescriptors, it has to be on an unshared internal
executor. The move in cockroachdb#71246 to not have an internal executor hanging around
for the current session hurts here.

Fixes cockroachdb#73788

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Apr 22, 2022
A regression was introduced in cockroachdb#71246 that caused errors when running
internal queries to populate files in statement bundles. As a result,
critical information was missing from the `env.sql`, `schema.sql`, and
`stats*.sql` files. This commit fixes the issue by using an
`InternalExecutorOverride` with a root user override. A root user is
required because a node user is insufficient - it does not have
privileges on user-defined relations which is needed for the
`SHOW CREATE` queries that populate `schema.sql`.

Fixes cockroachdb#80396

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Apr 22, 2022
A regression was introduced in cockroachdb#71246 that caused errors when running
internal queries to populate files in statement bundles. As a result,
critical information was missing from the `env.sql`, `schema.sql`, and
`stats*.sql` files. This commit fixes the issue by using the internal
executor factory to create an internal executor with the current
session's session data.

Fixes cockroachdb#80396

Release note: None
craig bot pushed a commit that referenced this pull request Apr 24, 2022
80405: sql: fix "no user specified" errors in statement bundles r=mgartner a=mgartner

A regression was introduced in #71246 that caused errors when running
internal queries to populate files in statement bundles. As a result,
critical information was missing from the `env.sql`, `schema.sql`, and
`stats*.sql` files. This commit fixes the issue by using the internal
executor factory to create an internal executor with the current
session's session data.

Fixes #80396

Release note: None


Co-authored-by: Marcus Gartner <[email protected]>
blathers-crl bot pushed a commit that referenced this pull request Apr 24, 2022
A regression was introduced in #71246 that caused errors when running
internal queries to populate files in statement bundles. As a result,
critical information was missing from the `env.sql`, `schema.sql`, and
`stats*.sql` files. This commit fixes the issue by using the internal
executor factory to create an internal executor with the current
session's session data.

Fixes #80396

Release note: None
blathers-crl bot pushed a commit that referenced this pull request Apr 24, 2022
A regression was introduced in #71246 that caused errors when running
internal queries to populate files in statement bundles. As a result,
critical information was missing from the `env.sql`, `schema.sql`, and
`stats*.sql` files. This commit fixes the issue by using the internal
executor factory to create an internal executor with the current
session's session data.

Fixes #80396

Release note: None
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: get rid of InternalExecutor interface in tree package (eval.go)
5 participants