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: default_int_size does not apply to integer literals #72138

Open
rafiss opened this issue Oct 29, 2021 · 8 comments
Open

sql: default_int_size does not apply to integer literals #72138

rafiss opened this issue Oct 29, 2021 · 8 comments
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL 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 Oct 29, 2021

Describe the problem

With the following:

root@:26257/defaultdb> set default_int_size = 4;
SET

root@:26257/defaultdb> select 1;
  ?column?
------------
         1

one would expect that the integer sent back over the wire would be an INT4. However, if you use Wireshark (or any client that cares about integer types) you'll see that it's actually an INT8.

To Reproduce

The following pgwire test:

send crdb_only
Query {"String": "SET default_int_size = 4"}
----

until crdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"SET"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "SELECT 1"}
----

until
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"?column?","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

run with make test PKG=./pkg/sql/pgwire TESTS='TestPGTest'

Expected behavior
The test should pass, but instead it returns a DataTypeOID of 20 (int8)

Additional context

  • This affects jasync-sql tests.
  • default_int_size is pretty silly anyway. consider this test case/comment:

# The setting doesn't affect explicit casts, only table definitions.
# (So in CockroachdB ::int is still ::int8, whereas it's ::int4 in postgres.)
send crdb_only
Query {"String": "SELECT 1::int, 2::int4, 3::int8"}
----

until crdb_only
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"int8","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0},{"Name":"int4","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"int8","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":20,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"1"},{"text":"2"},{"text":"3"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

Jira issue: CRDB-10964

@rafiss rafiss added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-sql-pgcompat Semantic compatibility with PostgreSQL labels Oct 29, 2021
@blathers-crl blathers-crl bot added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label Oct 29, 2021
@otan
Copy link
Contributor

otan commented Nov 22, 2021

should we just default int size to 4 anyway?

@rafiss
Copy link
Collaborator Author

rafiss commented Nov 22, 2021

Apparently that was the plan at one point in history:

#26925 (comment)

[2.2] Test: Ensure that cast operations '1'::INT result in the correct data width.

@otan
Copy link
Contributor

otan commented Nov 22, 2021

spicy

@otan
Copy link
Contributor

otan commented Nov 25, 2021

default_int_size is pretty silly anyway. consider this test case/comment:

this is more because of the family stuff (resolution logic relies on families rather than oids). casting anything atm assumes the same family and will default to the "family main type":

[email protected]:56449/defaultdb> select pg_typeof(1::int2);
  pg_typeof
-------------
  bigint
(1 row)


Time: 1ms total (execution 0ms / network 0ms)

@rafiss
Copy link
Collaborator Author

rafiss commented Nov 26, 2021

I think that problem is specific to how we've implemented pg_typeof.

In other words, if you do a datadriven pgwire test like

send
Query {"String": "SELECT 1::int2"}
----

(note the cast), then it does correctly show a DataTypeOID of 21.

But the DataTypeOID is not affected by just the default_int_size setting alone.


For some context, I filed this issue because the jasync-sql test suite does queries like SELECT 1 all over the place to check that the database is up, but it's very picky and requires that the result is a 32-bit java integer. Setting default_int_size=4 does not fix these tests because of this github issue.

@rafiss
Copy link
Collaborator Author

rafiss commented Nov 26, 2021

Ah I see what you're probably getting at. The type is determined in the optimizer here:

func (s *scope) addColumn(name scopeColumnName, expr tree.TypedExpr) *scopeColumn {
s.cols = append(s.cols, scopeColumn{
name: name,
typ: expr.ResolvedType(),
expr: expr,
})
return &s.cols[len(s.cols)-1]

Since expr is a DInt the resolved type is always a types.Int.

I wonder if we could enhance this part of the type-checking to be aware of default_int_size...

func (s *scope) resolveType(expr tree.Expr, desired *types.T) tree.TypedExpr {
expr = s.walkExprTree(expr)
texpr, err := tree.TypeCheck(s.builder.ctx, expr, s.builder.semaCtx, desired)
if err != nil {
panic(err)
}
return s.ensureNullType(texpr, desired)
}

An even lower-level would be to make typeCheckConstant be aware of default_int_size but that seems too invasive.

@ajwerner
Copy link
Contributor

ajwerner commented Feb 2, 2022

An even lower-level would be to make typeCheckConstant be aware of default_int_size but that seems too invasive.

I don't think that seems all that invasive.

@giangpham712
Copy link

This affects efcore.pf tests
@fqazi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL 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
None yet
Development

No branches or pull requests

5 participants