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/opt: add index on pg_proc(oid) and support regproc/procedure in foldOIDFamilyCast #92590

Merged
merged 1 commit into from
Dec 7, 2022

Conversation

e-mbrown
Copy link
Contributor

@e-mbrown e-mbrown commented Nov 28, 2022

Informs: #91022

Add index on pgproc(oc).
Typecast to regproc and regprocedure can use the
index on pgproc(oid).

Release note: None

@e-mbrown e-mbrown requested a review from a team November 28, 2022 17:21
@e-mbrown e-mbrown requested a review from a team as a code owner November 28, 2022 17:21
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@e-mbrown e-mbrown requested a review from rafiss November 28, 2022 17:21
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

Can you reword the commit/PR title so it's clear that there is more to this change than just the foldOIDFamilyCast function. The index added to the table was a surprise after reading the commit title.

query T
select '12345'::regprocedure::string
----
12345
Copy link
Collaborator

Choose a reason for hiding this comment

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

The tests in this file are verifying query plans to test that we can correctly build query plans, so I don't think these two tests should live here too. Perhaps pkg/sql/logictest/testdata/logic_test/cast would be a better place?

@@ -408,6 +408,34 @@ func (c *CustomFuncs) foldOIDFamilyCast(
default:
return nil, false, nil
}
case oid.T_regproc:
cDatum, err := eval.PerformCast(c.f.ctx, c.f.evalCtx, datum, typ)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not convinced we need to use PerformCast here. If we use it, we'll need some more checks to ensure that the cast is valid, e.g. use cast.ValidCast, otherwise we'll be attempting to fold expressions like 12.3::FLOAT::REGPROC which I think would fail.

Can we instead use similar logic as in the case oid.T_oid: at the top of the switch?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i think we should be able to use PerformCast as long as we add similar cases like the ones we have in the Regtype case

@e-mbrown e-mbrown force-pushed the eb/regproc branch 2 times, most recently from da12760 to a82a4cf Compare November 30, 2022 19:47
@e-mbrown e-mbrown changed the title sql/opt: support regproc/procedure in foldOIDFamilyCast sql/opt: add index on pg_proc(oid) and support regproc/procedure ... Nov 30, 2022
@e-mbrown e-mbrown changed the title sql/opt: add index on pg_proc(oid) and support regproc/procedure ... sql/opt: add index on pg_proc(oid) and support regproc/procedure in foldOIDFamilyCast Nov 30, 2022
Copy link
Contributor Author

@e-mbrown e-mbrown 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 @mgartner and @rafiss)


pkg/sql/opt/exec/execbuilder/testdata/scalar line 1055 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

The tests in this file are verifying query plans to test that we can correctly build query plans, so I don't think these two tests should live here too. Perhaps pkg/sql/logictest/testdata/logic_test/cast would be a better place?

A test like this exists in pkg/sql/logictest/testdata/logic_test/pgoidtype so i'll remove them.

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.

looks close!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @e-mbrown and @mgartner)


pkg/sql/opt/norm/fold_constants_funcs.go line 418 at r3 (raw file):

				return nil, false, err
			}
			dOid = tree.NewDOidWithType(tree.MustBeDOid(cDatum).Oid, types.RegProc)

we should be able to replace all of this:

			dOid = tree.NewDOidWithType(tree.MustBeDOid(cDatum).Oid, types.RegProcedure)

			procName, overload, err := c.f.catalog.ResolveFunctionByOID(c.f.ctx, dOid.Oid)
			if err != nil {
				return nil, false, err
			}

			dOid = tree.NewDOidWithTypeAndName(overload.Oid, types.RegProcedure, procName)

with this:

dOid = tree.MustBeDOid(cDatum)

(The PerformCast call already takes care of resolving the function, so no need to do it again)


pkg/sql/opt/norm/fold_constants_funcs.go line 434 at r3 (raw file):

				return nil, false, err
			}
			dOid = tree.NewDOidWithType(tree.MustBeDOid(cDatum).Oid, types.RegProcedure)

ditto, replace with dOid = tree.MustBeDOid(cDatum)

@e-mbrown e-mbrown force-pushed the eb/regproc branch 3 times, most recently from 994a57f to c214e7e Compare December 1, 2022 21:22
Copy link
Contributor Author

@e-mbrown e-mbrown 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 @mgartner and @rafiss)


pkg/sql/opt/norm/fold_constants_funcs.go line 418 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

we should be able to replace all of this:

			dOid = tree.NewDOidWithType(tree.MustBeDOid(cDatum).Oid, types.RegProcedure)

			procName, overload, err := c.f.catalog.ResolveFunctionByOID(c.f.ctx, dOid.Oid)
			if err != nil {
				return nil, false, err
			}

			dOid = tree.NewDOidWithTypeAndName(overload.Oid, types.RegProcedure, procName)

with this:

dOid = tree.MustBeDOid(cDatum)

(The PerformCast call already takes care of resolving the function, so no need to do it again)

Done.


pkg/sql/opt/norm/fold_constants_funcs.go line 434 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

ditto, replace with dOid = tree.MustBeDOid(cDatum)

Done.

@e-mbrown e-mbrown requested a review from rafiss December 2, 2022 15:59
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! you just need to rewrite the create_statements logic test to account for the index you added

Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

It looks like some unintended changes made it into the create_statements file.

@rafiss
Copy link
Collaborator

rafiss commented Dec 5, 2022

@e-mbrown you can use this command to rewrite the file:

./dev testlogic base --config=local --files=create_statements --rewrite

Copy link
Contributor Author

@e-mbrown e-mbrown left a comment

Choose a reason for hiding this comment

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

When I rewrite the test, tables other thanpg_proc change in the output. Not sure why.

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

@rafiss
Copy link
Collaborator

rafiss commented Dec 5, 2022

this is also failing a benchmark test, probably due to #90842. i will look into this

10:47:17       --- FAIL: BenchmarkAsJSON/tsquery
10:47:17           json_test.go:56: unexpected type *tree.DTSQuery for AsJSON
10:47:17       --- FAIL: BenchmarkAsJSON/tsvector
10:47:17           json_test.go:56: unexpected type *tree.DTSVector for AsJSON

as far as the create_statements diff, maybe it's expected after #91702 ... but i'm not sure

@rafiss
Copy link
Collaborator

rafiss commented Dec 5, 2022

@msirek fixed the benchmark issue in 5506cf8 - thank you!

@e-mbrown e-mbrown force-pushed the eb/regproc branch 2 times, most recently from 5ab105f to 0142df1 Compare December 6, 2022 15:55
@rafiss
Copy link
Collaborator

rafiss commented Dec 7, 2022

i think this query will work. the issue is with the newlines in the result

SELECT
  regexp_replace(create_statement, '\n', ' ', 'g'),
  regexp_replace(create_nofks, '\n', ' ', 'g'),
  alter_statements,
  validate_statements
FROM
  crdb_internal.create_statements
WHERE
  database_name = 'test'
AND
  schema_name NOT IN ('pg_catalog', 'pg_extension', 'crdb_internal', 'information_schema')

 in foldOIDFamilyCast

Add index on pgproc(oid).

Type cast to regproc and regprocedure can use the
index on pgproc(oid).

Release note: None
@e-mbrown
Copy link
Contributor Author

e-mbrown commented Dec 7, 2022

Thanks for all the help!

bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Dec 7, 2022

Build succeeded:

@craig craig bot merged commit 4096288 into cockroachdb:master Dec 7, 2022
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.

4 participants