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

opt: Add array and tuple constant folding rules #37404

Merged
merged 2 commits into from
May 9, 2019

Conversation

andy-kimball
Copy link
Contributor

Add FoldIndirection and FoldColumnAccess rules to fold expressions
like this:

ARRAY[i, i+1][1]
ARRAY[1, 2, 3][2]
(((i, i+1) as foo, bar)).foo
(((1, 2) as foo, bar)).bar

Release note: None

Move all folding rules to fold_constants.opt. Move all custom functions to
a new fold_constants.go file. Consolidate tests.

Release note: None
@andy-kimball andy-kimball requested a review from a team as a code owner May 8, 2019 19:01
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@justinj justinj left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 7 of 7 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @justinj, @RaduBerinde, and @rytaft)

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @andy-kimball, @justinj, and @rytaft)


pkg/sql/opt/norm/testdata/rules/fold_constants, line 839 at r2 (raw file):

# NOTE: Use constant array access to avoid triggering ColumnAccess::TypeCheck
#       constant tuple folding.
opt

[nit] expect=FoldColumnAccess ?


pkg/sql/opt/norm/testdata/rules/fold_constants, line 868 at r2 (raw file):

 ├── key: ()
 ├── fd: ()-->(1)
 └── ('foo',) [type=tuple{string}]

Maybe add a test case where we get an evaluation error? (argument could be crdb_internal.force_error('', 'some-error'))

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 7 of 7 files at r1, 3 of 3 files at r2.
Reviewable status: :shipit: complete! 3 of 0 LGTMs obtained (waiting on @andy-kimball)


pkg/sql/opt/norm/fold_constants.go, line 276 at r2 (raw file):

	// Case 1: The input is a static tuple constructor.
	if tup, ok := input.(*memo.TupleExpr); ok {
		return tup.Elems[idx]

What if idx is outside the bounds of the tuple (similar to the array case above)? Is that even possible?


pkg/sql/opt/norm/fold_constants.go, line 283 at r2 (raw file):

		datum := memo.ExtractConstDatum(input)

		colName := input.DataType().TupleLabels()[idx]

same here


pkg/sql/opt/norm/testdata/rules/fold_constants, line 847 at r2 (raw file):

 ├── scan a
 └── projections
      └── const: 'foo' [type=string]

It seems odd that this results in a scalar while the query below results in a tuple... Is that expected?

@rytaft
Copy link
Collaborator

rytaft commented May 8, 2019


pkg/sql/opt/norm/testdata/rules/fold_constants, line 847 at r2 (raw file):

Previously, rytaft wrote…

It seems odd that this results in a scalar while the query below results in a tuple... Is that expected?

Never mind I see why

Copy link
Contributor Author

@andy-kimball andy-kimball 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! 3 of 0 LGTMs obtained (waiting on @andy-kimball, @RaduBerinde, and @rytaft)


pkg/sql/opt/norm/fold_constants.go, line 276 at r2 (raw file):

Previously, rytaft wrote…

What if idx is outside the bounds of the tuple (similar to the array case above)? Is that even possible?

It's not possible, because the index is derived during type checking according to the static tuple type.


pkg/sql/opt/norm/testdata/rules/fold_constants, line 868 at r2 (raw file):

Previously, RaduBerinde wrote…

Maybe add a test case where we get an evaluation error? (argument could be crdb_internal.force_error('', 'some-error'))

The evaluation only happens for DTuple. It should never fail, but I put the error check there for completeness. Adding force_error forces "Case 1", where the input is a static tuple constructor (and we don't eval the tuple field exprs).

@RaduBerinde
Copy link
Member


pkg/sql/opt/norm/testdata/rules/fold_constants, line 868 at r2 (raw file):

Previously, andy-kimball (Andy Kimball) wrote…

The evaluation only happens for DTuple. It should never fail, but I put the error check there for completeness. Adding force_error forces "Case 1", where the input is a static tuple constructor (and we don't eval the tuple field exprs).

Ah, thanks, that makes sense.

@andy-kimball
Copy link
Contributor Author

bors r+

@andy-kimball
Copy link
Contributor Author

bors r-

@craig
Copy link
Contributor

craig bot commented May 9, 2019

Canceled

Add FoldIndirection and FoldColumnAccess rules to fold expressions
like this:

  ARRAY[i, i+1][1]
  ARRAY[1, 2, 3][2]
  (((i, i+1) as foo, bar)).foo
  (((1, 2) as foo, bar)).bar

Release note: None
@andy-kimball
Copy link
Contributor Author

bors r+

craig bot pushed a commit that referenced this pull request May 9, 2019
37404: opt: Add array and tuple constant folding rules r=andy-kimball a=andy-kimball

Add FoldIndirection and FoldColumnAccess rules to fold expressions
like this:

  ARRAY[i, i+1][1]
  ARRAY[1, 2, 3][2]
  (((i, i+1) as foo, bar)).foo
  (((1, 2) as foo, bar)).bar

Release note: None

Co-authored-by: Andrew Kimball <andyk@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented May 9, 2019

Build succeeded

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.

5 participants