Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/serialization/compact/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ export class Schema {
constructor(typeName: string, fields: FieldDescriptor[]) {
this.typeName = typeName;
this.fields = fields;
this.fieldDefinitionMap = new Map<string, FieldDescriptor>();
this.fields.sort((field1, field2) => {
return field1.fieldName > field2.fieldName ? 1 : -1;
})

for (const field of fields) {
this.fieldDefinitionMap = new Map<string, FieldDescriptor>();
for (const field of this.fields) {
// map entries are sorted by insertion order
Copy link
Copy Markdown
Member

@emreyigit emreyigit Feb 9, 2026

Choose a reason for hiding this comment

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

Aren't fields already sorted by field name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The fields are sorted by name in the previous line.
This is just a comment to clarify that the order will be preserved in this.fieldDefinitionMap, since Javascript guarantees that for maps.
For example in Go, that wouldn't work, since the map entries are accessed randomly.

this.fieldDefinitionMap.set(field.fieldName, field);
}

Expand Down Expand Up @@ -101,7 +105,7 @@ export class Schema {
getFields() : IterableIterator<FieldDescriptor> {
return this.fieldDefinitionMap.values();
}

private hasSameFields(other: Schema): boolean {
if (other.fieldDefinitionMap.size !== this.fieldDefinitionMap.size) {
return false;
Expand Down
6 changes: 2 additions & 4 deletions src/serialization/compact/SchemaWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,12 @@ export class SchemaWriter implements CompactWriter {
'Field with the name ' + field.fieldName + ' already exists'
);
}

this.fieldNames.add(field.fieldName);
this.fields.push(field);
}

build() : Schema {
return new Schema(this.typeName, this.fields.sort((field1, field2) => {
return field1.fieldName > field2.fieldName ? 1 : -1;
}));
return new Schema(this.typeName, this.fields);
}
}
19 changes: 19 additions & 0 deletions test/unit/serialization/compact/RabinFingerprintTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const chai = require('chai');
const Long = require('long');
const { RabinFingerprint64, INIT } = require('../../../../lib/serialization/compact/RabinFingerprint');
const { SchemaWriter } = require('../../../../lib/serialization/compact/SchemaWriter');
const {Schema} = require('../../../../lib/serialization/compact/Schema');
const {FieldDescriptor} = require('../../../../lib/serialization/generic_record/FieldDescriptor');
const {FieldKind} = require('../../../../lib/serialization/generic_record/FieldKind');

chai.should();

describe('RabinFingerprintTest', function () {
Expand Down Expand Up @@ -88,4 +92,19 @@ describe('RabinFingerprintTest', function () {
const schema = writer.build();
schema.schemaId.eq(Long.fromString('3662264393229655598')).should.be.true;
});

it('should compute the same fingerprint for equivalent schemas regardless of the field order', function () {
const schema1 = new Schema('my-schema', [
new FieldDescriptor('field-1', FieldKind.ARRAY_OF_DATE),
new FieldDescriptor('field-2', FieldKind.ARRAY_OF_DATE),
]);
const fp1 = RabinFingerprint64.ofSchema(schema1);

const schema2 = new Schema('my-schema', [
new FieldDescriptor('field-2', FieldKind.ARRAY_OF_DATE),
new FieldDescriptor('field-1', FieldKind.ARRAY_OF_DATE),
]);
const fp2 = RabinFingerprint64.ofSchema(schema2);
fp1.eq(fp2).should.be.true;
});
});
4 changes: 3 additions & 1 deletion test/unit/serialization/compact/SchemaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ describe('SchemaTest', function () {
const boolCount = 100;
const boolFields = new Array(100);
for (let i = 0; i < boolCount; i++) {
boolFields[i] = new FieldDescriptor(i.toString(), FieldKind.BOOLEAN);
// the fields are sorted by name, so have to append 0 for numbers < 10
const name = i >= 10? i.toString() : '0' + i.toString();
boolFields[i] = new FieldDescriptor(name, FieldKind.BOOLEAN);
}

const fields = [
Expand Down
Loading