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: pg_typeof builtin should include type information from column definition #58252

Open
rafiss opened this issue Dec 23, 2020 · 2 comments
Open
Labels
A-sql-typing SQLtype inference, typing rules, type compatibility. A-tools-efcore C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@rafiss
Copy link
Collaborator

rafiss commented Dec 23, 2020

Postgres:

rafiss@127:postgres> create table typs(a int2, b int4, c int8, d char, e "char", f text);
CREATE TABLE

rafiss@127:postgres>  insert into typs values(1,2,3,'a','b','c');
INSERT 0 1

rafiss@127:postgres>  select pg_typeof(a), pg_typeof(b), pg_typeof(c), pg_typeof(d), pg_typeof(e), pg_typeof(f) from typs;
+-------------+-------------+-------------+-------------+-------------+-------------+
| pg_typeof   | pg_typeof   | pg_typeof   | pg_typeof   | pg_typeof   | pg_typeof   |
|-------------+-------------+-------------+-------------+-------------+-------------|
| smallint    | integer     | bigint      | character   | "char"      | text        |
+-------------+-------------+-------------+-------------+-------------+-------------+
SELECT 1

But in CockroachDB, the function reports the type of the datum used internally in the SQL execution engine. The type information is only added later when sending data over the wire.

root@:26257/defaultdb> create table typs(a int2, b int4, c int8, d char, e "char", f string);
CREATE TABLE

root@:26257/defaultdb> insert into typs values(1,2,3,'a','b','c');
INSERT 1

root@:26257/defaultdb> select pg_typeof(a), pg_typeof(b), pg_typeof(c), pg_typeof(d), pg_typeof(e), pg_typeof(f) from typs;
  pg_typeof | pg_typeof | pg_typeof | pg_typeof | pg_typeof | pg_typeof
------------+-----------+-----------+-----------+-----------+------------
  bigint    | bigint    | bigint    | text      | text      | text
(1 row)

The execution engine doesn't have access to the real type information. The pg_typeof builtin operates on Datums, which only can be one of a few "canonical types."

Epic CRDB-2474
Jira issue: CRDB-3415

@rafiss rafiss added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-sql-typing SQLtype inference, typing rules, type compatibility. labels Dec 23, 2020
@rafiss rafiss added this to Triage in SQL Sessions - Deprecated via automation Dec 23, 2020
@rafiss rafiss moved this from Triage to Longer term backlog in SQL Sessions - Deprecated Jan 19, 2021
@rafiss rafiss added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label May 12, 2021
@rafiss
Copy link
Collaborator Author

rafiss commented May 24, 2021

relates to #48613

@dvarrazzo
Copy link

Probably related to this: pg_typeof() returns text on crdb, whereas on Pg it returns regtype.

As a consequence, an expression such as pg_typeof(X) = 'text'::regtype works on pg but fails on crdb. Adding a ::regtype to the LHS makes the expression portable across the DBs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-typing SQLtype inference, typing rules, type compatibility. A-tools-efcore C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
No open projects
SQL Queries
Backlog (DO NOT ADD NEW ISSUES)
SQL Sessions - Deprecated
Longer term backlog
Development

No branches or pull requests

3 participants