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: correct the usage of UDF terms "arguments" and "parameters" #92758

Merged
merged 2 commits into from
Dec 2, 2022

Conversation

mgartner
Copy link
Collaborator

@mgartner mgartner commented Nov 30, 2022

sql: correct the usage of UDF terms "arguments" and "parameters"

A parameter is a variable in a function definition. An argument is
an expression passed to a function when the function is invoked.
UDF-related code was not using this nomenclature correctly, creating
confusion. This commit fixes up our usage of these terms within SQL
optimization and execution. I know this is a pedantic change, but it is
motivated by my struggles in writing clear code and comments with the
previously overloading of the term argument.

Epic: CRDB-20370

Release note: None

sql: correct the usage of terms "arguments" and "parameters" in descriptors

Release note: None

@mgartner mgartner requested review from a team as code owners November 30, 2022 17:59
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@mgartner
Copy link
Collaborator Author

The first commit is mostly SQL Queries related code and the second commit is mostly SQL Schema related code plus the parser changes.

Copy link
Contributor

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for doing this. This is very important!

A _parameter_ is a variable in a function definition. An _argument_ is
an expression passed to a function when the function is invoked.
UDF-related code was not using this nomenclature correctly, creating
confusion. This commit fixes up our usage of these terms within SQL
optimization and execution. I know this is a pedantic change, but it is
motivated by my struggles in writing clear code and comments with the
previously overloading of the term _argument_.

Release note: None
@mgartner mgartner force-pushed the udf-args-params branch 4 times, most recently from 0339ca1 to 898eb4b Compare November 30, 2022 21:53
Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm: Left a bunch of minor comments, but feel free to pick and choose whichever ones you think matter for clarity.

Reviewed 11 of 11 files at r1, 21 of 21 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner)


pkg/sql/create_function.go line 231 at r2 (raw file):

) (fnDesc *funcdesc.Mutable, isNew bool, err error) {
	// Resolve argument types.
	argTypes := make([]*types.T, len(n.cf.Params))

argTypes->paramTypes?


pkg/sql/create_function.go line 244 at r2 (raw file):

			argNameSeen[param.Name] = struct{}{}
		}
		pbArg, err := makeFunctionArg(params.ctx, param, params.p)

pbArg->pbParam

makeFunctionArg is only used here, so it could be renamed too.


pkg/sql/create_function.go line 454 at r2 (raw file):

func makeFunctionArg(
	ctx context.Context, arg tree.FuncParam, typeResolver tree.TypeReferenceResolver,

[nit] could rename arg to param


pkg/sql/create_function.go line 460 at r2 (raw file):

	}
	var err error
	pbParam.Class, err = funcdesc.ArgClassToProto(arg.Class)

ArgClassToProto is only used here, could rename it.


pkg/sql/pg_catalog.go line 2424 at r2 (raw file):

						isStrict := fnDesc.GetNullInputBehavior() != catpb.Function_CALLED_ON_NULL_INPUT
						argTypes := tree.NewDArray(types.Oid)
						argModes := tree.NewDArray(types.String)

A couple more places here where we're using arg instead of param:
argTypes->paramTypes
argModes->paramModes
argNames->paramNames
argNamesArray->paramNamesArray
foundAnyArgNames->foundAnyParamNames


pkg/sql/catalog/funcdesc/func_desc.go line 555 at r2 (raw file):

	}

	argTypes := make(tree.ArgTypes, 0, len(desc.Params))

argTypes->paramTypes
We could even change tree.ArgTypes since it only seems to be used here and one other place for functions, though that might be too far.


pkg/sql/catalog/funcdesc/func_desc.go line 676 at r2 (raw file):

}

func toTreeNodeArgClass(class catpb.Function_Param_Class) tree.FuncParamClass {

This is only used in one place, so it's name can be easily changed.


pkg/sql/delegate/show_grants.go line 261 at r2 (raw file):

				return nil, err
			}
			argTypes, err := fn.ParamTypes(d.ctx, d.catalog)

[nit] argTypes -> paramTypes


pkg/sql/opt/exec/execbuilder/scalar.go line 677 at r1 (raw file):

	// represents. If the column does not represent an argument, then ok=false
	// is returned.
	argOrd := func(col opt.ColumnID) (ord int, ok bool) {

Does it make sense to say that the column ID represents an argument, rather than a parameter?


pkg/sql/opt/exec/execbuilder/scalar.go line 703 at r1 (raw file):

		stmt := udf.Body[stmtIdx]

		// Copy the expression into a new memo. Replace argument references with

Should it be 'parameter references' rather than 'argument references'? The idea is to replace parameter references within the function body with resolved arguments, right?


pkg/sql/opt/optbuilder/scalar.go line 632 at r1 (raw file):

	// start with an empty scope because a statement in the function body cannot
	// refer to anything from the outer expression. If there are function
	// arguments, we add them as columns to the scope so that references to them

[nit] 'function arguments' -> 'function parameters'


pkg/sql/opt/optbuilder/scope_column.go line 122 at r1 (raw file):

const maxFuncParams = 100

// funcParamOrd is a 1-based ordinal of a function parameters.

[nit] parameters->parameter


pkg/sql/opt/optbuilder/scope_column.go line 138 at r1 (raw file):

}

// funcParamReferencedBy returns true if the scopeColumn is a function argument

argument->parameter


pkg/sql/opt/testutils/testcat/function.go line 72 at r2 (raw file):

	// Resolve the parameter names and types.
	argTypes := make(tree.ArgTypes, len(c.Params))

argTypes->paramTypes

@mgartner mgartner requested review from a team as code owners December 1, 2022 15:13
@mgartner mgartner requested review from benbardin and HonoreDB and removed request for a team December 1, 2022 15:13
Copy link
Collaborator Author

@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.

Thanks for the thorough review! I made a bunch of changes, so please take another look.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @benbardin, @DrewKimball, and @HonoreDB)


pkg/sql/create_function.go line 231 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

argTypes->paramTypes?

Done.


pkg/sql/create_function.go line 244 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

pbArg->pbParam

makeFunctionArg is only used here, so it could be renamed too.

Done.


pkg/sql/create_function.go line 454 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] could rename arg to param

Done.


pkg/sql/create_function.go line 460 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

ArgClassToProto is only used here, could rename it.

Done.


pkg/sql/pg_catalog.go line 2424 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

A couple more places here where we're using arg instead of param:
argTypes->paramTypes
argModes->paramModes
argNames->paramNames
argNamesArray->paramNamesArray
foundAnyArgNames->foundAnyParamNames

I'm inclined to keep these as-is because they map to similarly named columns below, like proargtypes and proargmodes. It's unfortunate that PG isn't super consistent with the terms "arguments" and "parameters" - they'll likely always be some cases where we are also inconsistent to match PG.


pkg/sql/catalog/funcdesc/func_desc.go line 555 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

argTypes->paramTypes
We could even change tree.ArgTypes since it only seems to be used here and one other place for functions, though that might be too far.

tree.ArgTypes was the main reason I left a lot of the "args" that you pointed out. I've updated the second commit which renames tree.ArgType[s] to tree.ParamType[s] and addresses your comments.


pkg/sql/catalog/funcdesc/func_desc.go line 676 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

This is only used in one place, so it's name can be easily changed.

Done.


pkg/sql/delegate/show_grants.go line 261 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] argTypes -> paramTypes

Done.


pkg/sql/opt/exec/execbuilder/scalar.go line 677 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Does it make sense to say that the column ID represents an argument, rather than a parameter?

Fixed.


pkg/sql/opt/exec/execbuilder/scalar.go line 703 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Should it be 'parameter references' rather than 'argument references'? The idea is to replace parameter references within the function body with resolved arguments, right?

Yes good catch! Done.


pkg/sql/opt/optbuilder/scalar.go line 632 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] 'function arguments' -> 'function parameters'

Done.


pkg/sql/opt/optbuilder/scope_column.go line 122 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] parameters->parameter

Done.


pkg/sql/opt/optbuilder/scope_column.go line 138 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

argument->parameter

Done.


pkg/sql/opt/testutils/testcat/function.go line 72 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

argTypes->paramTypes

Done.

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm: Looks great! Thanks for taking this on!

Reviewed 47 of 47 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @benbardin and @HonoreDB)

@mgartner
Copy link
Collaborator Author

mgartner commented Dec 2, 2022

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Dec 2, 2022

Build succeeded:

@craig craig bot merged commit a475947 into cockroachdb:master Dec 2, 2022
@mgartner mgartner deleted the udf-args-params branch December 2, 2022 19:39
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