Skip to content

feat: configurable limit for sample values COMPASS-8984 #230

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

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function analyzeDocuments(
*/
async function parseSchema(
source: AnyIterable,
options?: SchemaParseOptions
options?: Partial<SchemaParseOptions>
): Promise<InternalSchema> {
return (await getCompletedSchemaAnalyzer(source, options)).getResult();
}
Expand Down
34 changes: 21 additions & 13 deletions src/schema-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,18 @@ type SemanticTypeMap = {
[typeName: string]: SemanticTypeFunction | boolean;
};

export type SchemaParseOptions = {
semanticTypes?: boolean | SemanticTypeMap;
storeValues?: boolean;
type AllSchemaParseOptions = {
semanticTypes: boolean | SemanticTypeMap;
storeValues: boolean;
signal?: AbortSignal;
sampleLengthLimit: number;
};
export type SchemaParseOptions = Partial<AllSchemaParseOptions>;

const defaultSchemaParseOptions: AllSchemaParseOptions = {
semanticTypes: false,
storeValues: true,
sampleLengthLimit: 10000
Copy link
Member

Choose a reason for hiding this comment

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

Changes look good, one small thought is that this variable name could mixed up with how many documents we might be sampling. We could mention its for the stored values, how about:
storedValuesLengthLimit

Copy link
Contributor Author

@paula-stacho paula-stacho Feb 18, 2025

Choose a reason for hiding this comment

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

good point, and it fits better with the other param!

};

/**
Expand Down Expand Up @@ -331,25 +339,25 @@ function simplifiedSchema(fields: SchemaAnalysisFieldsMap): SimplifiedSchema {

function cropString(value: string, limit: number) {
if (limit < 1) return '';
return value.charCodeAt(limit - 1) === value.codePointAt(10000 - 1)
return value.charCodeAt(limit - 1) === value.codePointAt(limit - 1)
? value.slice(0, limit)
: value.slice(0, limit - 1);
}

function getCappedValue(bsonType: SchemaBSONType, value: BSONValue) {
function getCappedValue(bsonType: SchemaBSONType, value: BSONValue, limit: number) {
if (bsonType === 'String') {
return cropString(value as string, 10000);
return cropString(value as string, limit);
}
if (bsonType === 'Binary') {
value = value as Binary;
return value.buffer.length > 10000
? new Binary(value.buffer.slice(0, 10000), value.sub_type)
return value.buffer.length > limit
? new Binary(value.buffer.slice(0, limit), value.sub_type)
: value;
}
if (bsonType === 'Code') {
value = value as Code;
return (value.code.length >= 10000)
? new Code(cropString(value.code, 10000), value.scope)
return (value.code.length >= limit)
? new Code(cropString(value.code, limit), value.scope)
: value;
}
return value;
Expand Down Expand Up @@ -459,7 +467,7 @@ function finalizeSchema(schemaAnalysis: SchemaAnalysisRoot): SchemaField[] {

export class SchemaAnalyzer {
semanticTypes: SemanticTypeMap;
options: SchemaParseOptions;
options: AllSchemaParseOptions;
documentsAnalyzed = 0;
schemaAnalysisRoot: SchemaAnalysisRoot = {
fields: Object.create(null),
Expand All @@ -474,7 +482,7 @@ export class SchemaAnalyzer {

constructor(options?: SchemaParseOptions) {
// Set default options.
this.options = { semanticTypes: false, storeValues: true, ...options };
this.options = { ...defaultSchemaParseOptions, ...options };

this.semanticTypes = {
...semanticTypes
Expand Down Expand Up @@ -555,7 +563,7 @@ export class SchemaAnalyzer {
}

type.values.pushSome(
getCappedValue(type.bsonType, value)
getCappedValue(type.bsonType, value, this.options.sampleLengthLimit)
);
}
};
Expand Down
9 changes: 9 additions & 0 deletions test/bloated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ describe('bloated documents', function() {
assert.ok(binary.length() <= 10000);
assert.strictEqual(binary.sub_type, 2);
});

it('the limit is configurable', async function() {
const documents = [{
str: generateRandomString(20000)
}];
const schema = await getSchema(documents, { sampleLengthLimit: 5 });
const stringLength = ((schema.fields[0].types[0] as PrimitiveSchemaType).values[0] as string).length;
assert.ok(stringLength === 5);
});
});
Loading