Skip to content

Fix fixed-length CHAR encoding in batches - #58

Open
RozmiDan wants to merge 1 commit into
mainfrom
38_fix_batch_char_encoding
Open

RozmiDan wants to merge 1 commit into
mainfrom
38_fix_batch_char_encoding

Conversation

@RozmiDan

Copy link
Copy Markdown
Contributor

Use declared CHAR lengths when encoding batch rows to match the batch BLR.
Pad shorter values and reject values that exceed the declared length.

Closes #38

@RozmiDan RozmiDan added this to the v0.8.0 milestone Nov 24, 2026
@RozmiDan
RozmiDan requested a review from MrTeeett November 24, 2026 08:07
@RozmiDan RozmiDan self-assigned this Nov 24, 2026
@RozmiDan RozmiDan added the confirmed Confirmed bug label Nov 24, 2026
@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: Go

Go / code-coverage/gotest

main 3525ff6 38_fix_batch_char_en... 1a6db23 +/-
79% 79% 0%
View coverage of the 7 most impacted files.
File main 3525ff6 38_fix_batch_char_en... 1a6db23 +/-
fbtype/null.go 92% 89% -3%
fbconn/fbconn.go 78% 78% 0%
fbtype/array.go 83% 83% 0%
batch.go 87% 88% +1%
fbconn/statement.go 72% 73% +1%
fbtype/timetz.go 76% 77% +1%
fbproto/sqldata.go 79% 82% +3%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

Comment thread fbproto/sqldata.go
l := field.CalculateIoLength(valueBuf)
if useDeclaredTextLength && field.Type == SQL_TYPE_TEXT {
if valueBufLen > field.Length {
return fmt.Errorf("batch CHAR parameter %d: value length %d exceeds declared length %d", n, valueBufLen, field.Length)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this length check fails, PreparedBatch.Add returns before clearing b.eqb. Passing a 16 MiB byte slice to CHAR(5) CHARACTER SET OCTETS leaves the batch holding the entire copy even after Cancel and Close. Could we set b.eqb = ExtendedQueryBuilder{} on the EncodeBatchRow error path? Previously accepted rows are already in b.encodedRows, so they stay queued.

Comment thread fbproto/sqldata.go

// WriteBatchRowData writes rows using the declared CHAR lengths, matching
// the batch BLR generated by CalculateBLR(rowDescriptor, nil).
func WriteBatchRowData(x *XDRWriter, rowDescriptor []FieldDescriptor, values [][]byte) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could WriteBatchRowData check that len(values) == len(rowDescriptor) before writing the NULL bitmap? With one CHAR descriptor and no values, it writes four bytes and then panics at values[0]. An extra non-nil value is silently dropped. Returning an error in both cases would match CalculateBLR and leave the writer untouched.

Comment thread fbproto/sqldata.go
Comment on lines 183 to 199
realFieldLen := l - 1

if valueBufLen >= realFieldLen {
if err = x.WriteBytes(valueBuf[:realFieldLen]); err != nil {
return err
}
if err = x.WriteCustomPadding((4-realFieldLen)&3, field.GetPaddingByte()); err != nil {
return err
}
} else {
if err = x.WriteBytes(valueBuf); err != nil {
return err
}
if err = x.WriteCustomPadding(realFieldLen-valueBufLen+((4-realFieldLen)&3), field.GetPaddingByte()); err != nil {
return err
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both arms of the fixed-width branch in writeRowData call WriteBytes and WriteCustomPadding. Could we slice the value first and share those calls? This reduces the block after realFieldLen := l - 1 from 15 lines to 8. The batch and row-format tests passed with -race using this version.

Suggested change
valueBuf = valueBuf[:min(valueBufLen, realFieldLen)]
if err = x.WriteBytes(valueBuf); err != nil {
return err
}
alignedLen := (realFieldLen + 3) &^ 3
if err = x.WriteCustomPadding(alignedLen-len(valueBuf), field.GetPaddingByte()); err != nil {
return err
}

Comment thread fbproto/sqldata.go
if valueBufLen > field.Length {
return fmt.Errorf("batch CHAR parameter %d: value length %d exceeds declared length %d", n, valueBufLen, field.Length)
}
l = field.GetIOLength()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WriteCustomPadding allocates a temporary slice the size of the padding. For CHAR(32767) CHARACTER SET ASCII, both a one-byte value and a full-width value produce the same number of encoded bytes. In a WriteBatchRowData benchmark writing to io.Discard, "x" allocated 32,784 B/op and the full-width value allocated 16 B/op. Could we write the padding in chunks from reusable blocks of spaces and zero bytes? That would avoid the per-row padding allocation for wide columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

confirmed Confirmed bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix fixed-length CHAR encoding in batches

2 participants