-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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 string concat with any non-array type #55611
Conversation
a7b8529
to
018b753
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)
pkg/sql/sem/tree/eval.go, line 479 at r1 (raw file):
return nil, errors.New("neither LHS or RHS matched DString") }, Volatility: VolatilityImmutable,
This is not correct. E.g. TIMESTAMPTZ to string is not an immutable cast. Look at the cast volatilities in tree/casts.go (validCasts). Perhaps looking up the volatility of the cast from the given type to string is the right way to initialize this.
Before this PR, we fully supported concat binary operator in the vectorized engine, so it'd be good to either confirm that it is still the case or open up an issue to add the support (if the latter is the case, the new issue should be referenced in #49463). |
f79b917
to
316a312
Compare
0eaca7e
to
3705e3c
Compare
@yuzefovich I created #55841 (the new overloads do not work with colexec) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @otan and @RaduBerinde)
pkg/sql/sem/tree/eval.go, line 479 at r1 (raw file):
Previously, RaduBerinde wrote…
This is not correct. E.g. TIMESTAMPTZ to string is not an immutable cast. Look at the cast volatilities in tree/casts.go (validCasts). Perhaps looking up the volatility of the cast from the given type to string is the right way to initialize this.
thanks for this tip. updated
sooooo how hard is it to get this to work with |
I had a local branch with the |
hmm although to Radu's point above, we can't treat all the |
so i did get past the issue of #43143 (comment) just by adding explicit string-byte and byte-string concat overloads. but after that, these cases failed:
with the |
this is what PostgreSQL does |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm if you can't get anynonarray to work.
@@ -417,9 +417,48 @@ func initArrayToArrayConcatenation() { | |||
} | |||
} | |||
|
|||
// initNonArrayToNonArrayConcatenation initializes string + nonarrayelement | |||
// and nonarrayelement + string concatenation. | |||
func initNonArrayToNonArrayConcatenation() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a test that checks <anyNonArrayElem> || NULL = anyNonArrayElem.String()
for each non array elem type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i may not be following, but in the operation you posted, the result should be NULL.
i added a test to pkg/sql/sem/tree/testdata/eval/concat
. See https://github.com/rafiss/cockroach/blob/3705e3c7f37effaa644d45344b5c89a461877401/pkg/sql/sem/tree/testdata/eval/concat#L73
i can add more tests for the other non array types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah woops, i meant anyNonArrayElem || string, as a loop over all current types to make sure it doesn't get resolved unexpectedly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it didn't seem very nice to loop over types and generate values for each type, so instead i just added more cases to concat
eval test. it turned up an issue so i updated eval.go to use the FmtBareStrings flag
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it didn't seem very nice to loop over types and generate values for each type
(i'm not opposed to this landing this as is, but curious)
does RandDatum
not give you something nice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i didn't know about RandDatum
hehehe. thank you for the pointer!
will use that, and anyway looks like i need to fix tuple-string concat:
encountered internal error:
no volatility for cast tuple{int, int}::tuple
(1) assertion failure
Wraps: (2) attached stack trace
-- stack trace:
| github.com/cockroachdb/cockroach/pkg/sql/opt/memo.BuildSharedProps
| /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/memo/logical_props_builder.go:1512
09ffe25
to
eae45c4
Compare
I've had to update |
pkg/sql/sem/tree/normalize.go
Outdated
} | ||
// Casting to an "any" tuple is not allowed because there is no way to define | ||
// the volatility in LookupCastVolatility. | ||
if wantedType.Family() == types.TupleFamily && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we not always use stable as a volatility? i think that's what Postgres does. in these circumstances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that sounds cool! will try.
d966b95
to
11dc8bb
Compare
Many tools and ORMs that work with Postgres expect this behavior. Release note (sql change): The concatenation operator || is now usable between strings and any other non-array type. Release note (backward-incompatible change): Concatenation between a non-null argument and a null argument is now typed as string concatenation. (It previously used to be typed as array concatentation.) This means that the result of `NULL || 1` will now be `NULL`. Previously, the result would be `{1}`. To preserve the old behavior, the NULL argument can be casted to an explicit type.
thanks for reviewing :) bors r=otan |
Build succeeded: |
Many tools and ORMs that work with Postgres expect this behavior.
this is retrying the idea from #43190
fixes #41872
fixes #34404
Release note (sql change): the concatenation operator || is now usable
between strings and any other non-array type.
Release note (backward-incompatible change): Concatenation between a
non-null argument and a null argument is now typed as string
concatenation. (It previously used to be typed as array concatentation.)
This means that the result of
NULL || 1
will now beNULL
.Previously, the result would be
{1}
. To preserve the old behavior, the NULLargument can be casted to an explicit type.