Comments (5)
It seems that postgres requires an explicit cast for both:
yuzefovich=# CREATE TYPE debug_t AS ENUM('test1', 'test2');
CREATE TYPE
yuzefovich=# CREATE TABLE debug (
id INT NOT NULL,
enum_col debug_t DEFAULT NULL,
bytes_col TEXT DEFAULT NULL,
PRIMARY KEY(id)
);
INSERT INTO debug ("id", "enum_col", "bytes_col") VALUES (1, 'test1', 'some_bytes_yo');
CREATE TABLE
INSERT 0 1
yuzefovich=# UPDATE
debug
SET
enum_col = (CASE id WHEN 1 THEN NULL ELSE NULL END),
bytes_col = (CASE id WHEN 1 THEN NULL ELSE NULL END)
WHERE
id IN (1);
ERROR: column "enum_col" is of type debug_t but expression is of type text
LINE 4: enum_col = (CASE id WHEN 1 THEN NULL ELSE NULL END),
^
HINT: You will need to rewrite or cast the expression.
However, I took a closer look at what CRDB does, and I'm a bit concerned about what I'm seeing. In particular, it seems that in your example we actually optimize away the second CASE expression (because two CASE expressions are exactly the same), so we try to use the single CASE expression that is typed as debug_t
for both columns, and that fails. Adding an explicit cast to either expression makes them distinct, so both CASE expressions end up being used.
I'm wondering whether we shouldn't be applying this optimization rule to remove one of the seemingly-redundant expressions depending on the context. I'll check with my colleagues.
from cockroach.
It looks like this behavior might not be easy to change, and it has existed for a long time. Also since this behavior matches postgres, we're going to treat this as "enhancement", but it's unlikely we'll get to it soon.
from cockroach.
Hi @AllyMarthaJ, please add branch-* labels to identify which branch(es) this C-bug affects.
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.
from cockroach.
Thanks for the report!
IIUC this is the expected behavior, and the error contains a hint on how to go around it:
ERROR: value type debug_t doesn't match type bytes of column "bytes_col"
SQLSTATE: 42804
HINT: you will need to rewrite or cast the expression
I think what happens is that we have two untyped expressions, both of them having NULL value, and we perform the type checking on the first one (get debug_t
type) and automatically apply the same type to the second expression.
If we add an explicit cast to either of the expressions, then it works:
UPDATE
debug
SET
enum_col = (CASE id WHEN 1 THEN NULL ELSE NULL END)::debug_t,
bytes_col = (CASE id WHEN 1 THEN NULL ELSE NULL END)
WHERE
id IN (1);
-- or
UPDATE
debug
SET
enum_col = (CASE id WHEN 1 THEN NULL ELSE NULL END),
bytes_col = (CASE id WHEN 1 THEN NULL ELSE NULL END)::BYTES
WHERE
id IN (1);
I'll close this as behaving as expected -- cc @cockroachdb/sql-queries in case my understanding of type checking is incorrect.
from cockroach.
Hmm, that makes sense as an explanation, but I fail to understand why applying the same type to the second expression is expected behaviour! Especially since that the cast needn't happen on the entire expression — just one of the values inside a when
is sufficient.
Ideally I wouldn't have to cast either expression, but maybe my understanding of types in the context of postgres/crdb is wrong.
Thanks for taking a look regardless 🙂
from cockroach.
Related Issues (20)
- ccl/backupccl: TestRestoreCheckpointing failed
- acceptance: TestDockerCLI_test_error_hints failed HOT 2
- : failed HOT 1
- ccl/sqlproxyccl: TestFailedConnection failed
- ccl/streamingccl/streamingest: TestTenantStatusWithFutureCutoverTime failed HOT 1
- streamingccl: add even more LDR metrics to DB console HOT 2
- util/log: TestHTTPSinkHeadersAndCompression failed
- roachtest: disagg-rebalance/aws/n4cpu4 failed HOT 1
- kv/kvnemesis: TestKVNemesisSingleNode failed HOT 3
- roachtest: backup-restore/mixed-version failed HOT 2
- pkg/sql/logictest/tests/cockroach-go-testserver-23.2/cockroach-go-testserver-23_2_test: TestLogic_mixed_version_can_login failed HOT 1
- ccl/streamingccl/streamingest: TestProtectedTimestampManagement failed HOT 1
- Cannot restore backup unknown type kind COMPOSITE HOT 7
- roachtest: jepsen/monotonic/majority-ring-subcritical-skews failed HOT 1
- roachtest: jepsen/g2/majority-ring-subcritical-skews failed HOT 1
- roachtest: jepsen/g2/strobe-skews failed
- schema: report user friendly status message during a schema change HOT 1
- ccl/changefeedccl: TestChangefeedPanicRecovery failed HOT 1
- ccl/changefeedccl: TestChangefeedHandlesDrainingNodes failed HOT 9
- ccl/changefeedccl: TestChangefeedPropagatesTerminalError failed HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cockroach.