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

Increase Depth Limit in ptrFromMapping Function for Deeper Structure Access in reflect.go #1359

Open
doscode-kr opened this issue Mar 1, 2024 · 0 comments

Comments

@doscode-kr
Copy link

What version of SQLBoiler are you using (sqlboiler --version)?

  • SQLBoiler v4.16.2

What is your database and version (eg. Postgresql 10)

  • MariaDB 10.6.16

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

  • ( )

If this happened at runtime what code produced the issue? (if not applicable leave blank)

...
	records, err := models.AnyTableWithMoreThan255Columns(
		Select(),
	).All(ctx, db)
...

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

  • ( )

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

CREATE TABLE `AnyTableWithMoreThan255Columns` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  `column1` text NOT NULL DEFAULT '',
  `column2` text NOT NULL DEFAULT '',
  ....
  `column255` text NOT NULL DEFAULT '',
  ...
  `column500` text NOT NULL DEFAULT '',

Further information. What did you do, what did you expect?

I have designed a special-purpose table to store hundreds of text columns.
However, when creating the table and querying the data, I encountered the following error message:

[models/research_data_vault.go:2388] models: failed to assign all query results to ResearchDataVault slice: 
[sqlboiler/[email protected]/queries/reflect.go:262] failed to bind pointers to obj:
sql: Scan error on column index 255, name "r7_use_bispho": unsupported Scan, storing driver.Value type []uint8 into type *models.ResearchDataVault

The key takeaway from this error message is the number 255. The table I created during testing had about 300 columns.

in reflect.go

...
    //reflect.go:231
    mapping, err := getMappingCache(structType).mapping(cols)
...
    //reflect.go:471
    func (b *mappingCache) mapping(cols []string) ([]uint64, error) {
...
    //reflect.go:491
    mapping, err := BindMapping(b.typ, b.structMap, cols)
...
}

The mapping is generated as a result of calling the BindMapping function, and its value ranges from 0xff00 to 0xffff.
In the case of the 256th column, the value is mapped to 0xff00 again. This is an overflow from 255.

It appears that a variable related to this issue is a sentinel, which seems to have been set to detect a value of 255.
Along with the magic number 8, it works as intended for tables with 255 or fewer columns, but an error occurs for tables with more columns.

This issue can be resolved by modifying both the sentinel and the magic number.
However, while I have solved the problem for myself, I am not fully aware of the other site effects.

MySQL has a hard limit of 4096 columns that can be created. However, InnoDB allows up to 1017 columns.
Based on this fact, I believe it is a good idea to modify the sentinel to 4096.
In the case of InnoDB, an error will occur in the DBMS itself if more than 1017 columns are created anyway.
(see - https://dev.mysql.com/doc/mysql-reslimits-excerpt/8.0/en/column-count-limit.html)

I'll submit a PR related to this issue soon.

Thank you for the great work, everyone on your team.

Dos

doscode-kr added a commit to doscode-kr/sqlboiler that referenced this issue Mar 1, 2024
- Increase Depth Limit in ptrFromMapping Function
  for Deeper Structure Access in reflect.go
- This fix addresses errors encoutered with tables
  exceeding 255 columns.
- this fixes issue volatiletech#1359
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

No branches or pull requests

1 participant