From 34edd05c771811df95a32352d0ca45c77dd72abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B5nis=20Ormisson?= Date: Tue, 1 Dec 2026 15:08:25 +0200 Subject: [PATCH 1/2] Write the weight index as the physical 8-byte slot (3.2.1) --- CHANGELOG.md | 5 +++++ src/Sav/Writer.php | 8 ++++++-- tests/DatasetRoundTripTest.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) 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..2993029 100644 --- a/tests/DatasetRoundTripTest.php +++ b/tests/DatasetRoundTripTest.php @@ -230,4 +230,37 @@ 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), + $string(300), + // Logical ordinal, not the physical slot: the writer must not trust it. + new VariableMetadata('weight', VariableType::NUMERIC, 0, $numeric, $numeric, dictionaryIndex: 3), + ]), + rows: [['abc', '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, 300 bytes take 32 + 6 slots, so the weight is slot 42. + self::assertSame(42, $reader->header->weightIndex); + self::assertSame('weight', $actual->metadata->weightVariableName); + self::assertSame([['abc', 'long', 1.5]], $actual->rows()); + } } From e5e388914596d872cbc7412ac5d3e504a07649aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B5nis=20Ormisson?= Date: Tue, 1 Dec 2026 16:05:21 +0200 Subject: [PATCH 2/2] Test numeric weight slots and a missing weight variable --- tests/DatasetRoundTripTest.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/DatasetRoundTripTest.php b/tests/DatasetRoundTripTest.php index 2993029..baa0834 100644 --- a/tests/DatasetRoundTripTest.php +++ b/tests/DatasetRoundTripTest.php @@ -244,11 +244,12 @@ public function testWeightIndexCountsStringSlotsAndIgnoresStaleDictionaryIndex() $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: 3), + new VariableMetadata('weight', VariableType::NUMERIC, 0, $numeric, $numeric, dictionaryIndex: 4), ]), - rows: [['abc', 'long', 1.5]], + rows: [['abc', 2.0, 'long', 1.5]], metadata: new FileMetadata(weightVariableName: 'weight'), ); @@ -258,9 +259,26 @@ public function testWeightIndexCountsStringSlotsAndIgnoresStaleDictionaryIndex() $reader = Reader::fromString($buffer->getStream()); $actual = $reader->readDataset(); - // 20 bytes take 3 slots, 300 bytes take 32 + 6 slots, so the weight is slot 42. - self::assertSame(42, $reader->header->weightIndex); + // 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', 'long', 1.5]], $actual->rows()); + 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); } }