diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a5fee6..157fa2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 3.2.1 (2026-12-01) + +- Fixed the weight variable index written by `Writer` for typed datasets. SPSS expects the 1-based 8-byte slot of the weight variable, but the writer used `VariableMetadata::$dictionaryIndex` or the logical position, so a weight placed after a string wider than 8 bytes pointed at the wrong slot and was lost on reading. The writer now counts slots from the written dictionary and no longer reads `dictionaryIndex` for this. +- No runtime API changes. + ## 3.2.0 (2026-11-09) - Restored PHP 8.3 support. The minimum had been raised to 8.4.1 with the v3 tooling update, but the library only used three PHP 8.4 constructs (`new` without parentheses in two places and `array_all()`); these are replaced with PHP 8.3 equivalents without behaviour changes. diff --git a/src/Sav/Writer.php b/src/Sav/Writer.php index d0783f6..58e83fd 100644 --- a/src/Sav/Writer.php +++ b/src/Sav/Writer.php @@ -506,11 +506,15 @@ private function datasetToWriterData(Dataset $dataset): array $weightIndex = 0; if (null !== $dataset->metadata->weightVariableName) { - foreach ($dataset->variables() as $index => $variable) { + // SPSS stores the weight as the 1-based 8-byte slot of the written dictionary, so + // count slots here; dictionaryIndex describes the source file and may be stale. + $slot = 1; + foreach ($dataset->variables() as $variable) { if ($variable->name === $dataset->metadata->weightVariableName) { - $weightIndex = $variable->dictionaryIndex ?? ($index + 1); + $weightIndex = $slot; break; } + $slot += VariableType::STRING === $variable->type ? Utils::widthToOcts($variable->width) : 1; } if (0 === $weightIndex) { throw new \InvalidArgumentException(sprintf( diff --git a/tests/DatasetRoundTripTest.php b/tests/DatasetRoundTripTest.php index 3a46c64..baa0834 100644 --- a/tests/DatasetRoundTripTest.php +++ b/tests/DatasetRoundTripTest.php @@ -230,4 +230,55 @@ public function testTypedZsavRoundTripPreservesCompressionBoundariesAndMetadataD [null], ], $actual->rows()); } + + public function testWeightIndexCountsStringSlotsAndIgnoresStaleDictionaryIndex(): void + { + $numeric = new VariableFormat(Variable::FORMAT_TYPE_F, 8, 2); + $string = static fn(int $width): VariableMetadata => new VariableMetadata( + name: 'text' . $width, + type: VariableType::STRING, + width: $width, + printFormat: new VariableFormat(Variable::FORMAT_TYPE_A, min($width, 255)), + writeFormat: new VariableFormat(Variable::FORMAT_TYPE_A, min($width, 255)), + ); + $source = new Dataset( + dictionary: new VariableDictionary([ + $string(20), + new VariableMetadata('count', VariableType::NUMERIC, 0, $numeric, $numeric), + $string(300), + // Logical ordinal, not the physical slot: the writer must not trust it. + new VariableMetadata('weight', VariableType::NUMERIC, 0, $numeric, $numeric, dictionaryIndex: 4), + ]), + rows: [['abc', 2.0, 'long', 1.5]], + metadata: new FileMetadata(weightVariableName: 'weight'), + ); + + $buffer = (new Writer($source))->getBuffer(); + $buffer->rewind(); + + $reader = Reader::fromString($buffer->getStream()); + $actual = $reader->readDataset(); + + // 20 bytes take 3 slots, a number 1, 300 bytes 32 + 6, so the weight is slot 43. + self::assertSame(43, $reader->header->weightIndex); + self::assertSame('weight', $actual->metadata->weightVariableName); + self::assertSame([['abc', 2.0, 'long', 1.5]], $actual->rows()); + } + + public function testWriterRejectsWeightVariableMissingFromDictionary(): void + { + $numeric = new VariableFormat(Variable::FORMAT_TYPE_F, 8, 2); + $source = new Dataset( + dictionary: new VariableDictionary([ + new VariableMetadata('count', VariableType::NUMERIC, 0, $numeric, $numeric), + ]), + rows: [[1.0]], + metadata: new FileMetadata(weightVariableName: 'weight'), + ); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Weight variable "weight" is not present in the dictionary.'); + + new Writer($source); + } }