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: allow array builtins to operate on tuples #70332

Merged
merged 1 commit into from
Sep 21, 2021

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Sep 16, 2021

fixes #70029

DistSQL does not support this because the DArray operations in eval.go
all result in arrays of AnyTuple, which cannot be decoded.

Release note (sql change): The array builtins (array_agg, array_cat,
array_position, etc) now operate on record types.

@rafiss rafiss added the do-not-merge bors won't merge a PR with this label. label Sep 16, 2021
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss force-pushed the array-tuple-builtins branch 2 times, most recently from 6f805ac to 21c84b6 Compare September 17, 2021 06:42
@rafiss
Copy link
Collaborator Author

rafiss commented Sep 17, 2021

Not sure why, but it seems like the SELECT array_cat(ARRAY[ROW(1,2)], ARRAY[NULL::record]) logic test is still using the vectorized engine under the fakedist config

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Note that there are failures in fakedist-vec-off and fakedist-metadata configs which don't use the vectorized engine, so the problem is applicable to both execution engines. My quick take is that fakedist config and DistsqlBlocklist don't work together, and it is a test harness issue; if that's true, we should just create a separate logic test file that would only run with local* configs.

Reviewed 8 of 9 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis, @nvanbenschoten, and @rafiss)


pkg/sql/sem/builtins/builtins.go, line 7060 at r2 (raw file):

		}
	}
	// Prevent usage in DistSQL/vectorized engine because they cannot handle

This doesn't prevent the usage of the vectorized engine, only of queries being distributed.

@rafiss
Copy link
Collaborator Author

rafiss commented Sep 17, 2021

The part I don't understand yet is why the other array_cat tests right above that one don't have the same issue?

@yuzefovich
Copy link
Member

fakedist is non-deterministic, maybe those got lucky (i.e. the fakeSpanResolver happened to behave as if the cluster was a single node)?

@rafiss rafiss force-pushed the array-tuple-builtins branch from 21c84b6 to 13b68fd Compare September 18, 2021 15:20
@rafiss
Copy link
Collaborator Author

rafiss commented Sep 18, 2021

I figured it out -- the issue is that the array_* builtin would be evaluated during constant-folding, so checking if the builtin function is blocklisted would not work. I added another check for arrays with untyped tuples.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis, @nvanbenschoten, and @rafiss)


pkg/sql/sem/builtins/builtins.go, line 7060 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

This doesn't prevent the usage of the vectorized engine, only of queries being distributed.

Is this needed given the new check?

Copy link
Collaborator Author

@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 also tried to avoid the array by avoiding creating an array with AnyTuple in https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/sem/tree/eval.go#L321-L327

and instead do

-        for _, e := range MustBeDArray(left).Array {
+        leftArray := MustBeDArray(left)
+        result = NewDArray(leftArray.ResolvedType().ArrayContents())
+        for _, e := range leftArray.Array {

but that gets messy because it still doesn't handle tuple(unknown, int) (and similar types) that well. and for array_cat, it's not clear whether to use the left array or right array's type.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis, @nvanbenschoten, and @yuzefovich)


pkg/sql/sem/builtins/builtins.go, line 7060 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

Is this needed given the new check?

don't think so

Copy link
Collaborator Author

@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 also tried changing the return types of the builtins, and it didn't seem to matter

-			ReturnType: tree.FixedReturnType(types.MakeArray(typ)),
+			ReturnType: tree.IdentityReturnType(0),

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis, @nvanbenschoten, and @yuzefovich)

Copy link
Collaborator Author

@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 @jordanlewis, @nvanbenschoten, and @yuzefovich)


pkg/sql/sem/builtins/builtins.go, line 7060 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

don't think so

actually, i was wrong. it's still needed. i didn't look too far into why, but when i removed this check, then the SELECT array_cat(ARRAY[ROW(10,'fish')], ARRAY[(k,v)]) FROM kv; (the first array of tuple builtin test) test case failed

Copy link
Member

@yuzefovich yuzefovich 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! 1 of 0 LGTMs obtained (waiting on @jordanlewis and @nvanbenschoten)


pkg/sql/sem/builtins/builtins.go, line 7060 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

actually, i was wrong. it's still needed. i didn't look too far into why, but when i removed this check, then the SELECT array_cat(ARRAY[ROW(10,'fish')], ARRAY[(k,v)]) FROM kv; (the first array of tuple builtin test) test case failed

Ok, thanks.

nit: drop "vectorized engine" from the comment because DistsqlBlocklist doesn't affect it.

Copy link
Collaborator Author

@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! 1 of 0 LGTMs obtained (waiting on @jordanlewis and @nvanbenschoten)


pkg/sql/sem/builtins/builtins.go, line 7060 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

Ok, thanks.

nit: drop "vectorized engine" from the comment because DistsqlBlocklist doesn't affect it.

done

DistSQL does not support this because the DArray operations in eval.go
all result in arrays of AnyTuple, which cannot be decoded.

Release note (sql change): The array builtins (array_agg, array_cat,
array_position, etc) now operate on record types.
@rafiss rafiss force-pushed the array-tuple-builtins branch from 13b68fd to 7341b5b Compare September 20, 2021 21:50
@rafiss rafiss removed the do-not-merge bors won't merge a PR with this label. label Sep 20, 2021
@rafiss rafiss marked this pull request as ready for review September 20, 2021 21:51
@rafiss rafiss requested a review from a team as a code owner September 20, 2021 21:51
@rafiss
Copy link
Collaborator Author

rafiss commented Sep 20, 2021

bors r=yuzefovich

@craig
Copy link
Contributor

craig bot commented Sep 21, 2021

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Sep 21, 2021

Build succeeded:

@rafiss rafiss deleted the array-tuple-builtins branch September 21, 2021 02:57
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: support array_agg(record)
3 participants