Skip to content
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

fix: numeric type evolution #126

Merged
merged 2 commits into from
May 4, 2022
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
24 changes: 24 additions & 0 deletions src/fieldAnalyser/fieldAnalyser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ describe('FieldAnalyser', () => {
},
]);
});

it('should always take the most englobing numeric type regardless of the transition direction', async () => {
const docBuilders = [
buildDocument({
from_long_to_double: 10,
from_double_to_long: 20.1,
}),
buildDocument({
from_long_to_double: 10.1,
from_double_to_long: 20,
}),
];
const {fields} = (await analyser.add(docBuilders)).report();
expect(fields).toStrictEqual([
{
name: 'from_long_to_double',
type: 'DOUBLE',
},
{
name: 'from_double_to_long',
type: 'DOUBLE',
},
]);
});
});

describe('when the batch contains inconsistent metadata', () => {
Expand Down
23 changes: 15 additions & 8 deletions src/fieldAnalyser/fieldAnalyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {listAllFieldsFromOrg} from './fieldUtils';
import {FieldStore} from './fieldStore';
import {Inconsistencies} from './inconsistencies';
import {InvalidPermanentId} from '../errors/fieldErrors';
import {getGuessedTypeFromValue, isValidTypeTransition} from './typeUtils';
import {getGuessedTypeFromValue, getMostEnglobingType} from './typeUtils';
import {ensureNecessaryCoveoPrivileges} from '../validation/preconditions/apiKeyPrivilege';
import {writeFieldsPrivilege} from '../validation/preconditions/platformPrivilege';

Expand Down Expand Up @@ -77,17 +77,24 @@ export class FieldAnalyser {

private storeMetadata(metadataKey: string, metadataValue: MetadataValue) {
const alreadyGuessedType = this.missingFields.get(metadataKey);
const newGuessedType = getGuessedTypeFromValue(metadataValue);
const firstTypeGuess = getGuessedTypeFromValue(metadataValue);

if (
!alreadyGuessedType ||
isValidTypeTransition(alreadyGuessedType, newGuessedType)
) {
this.missingFields.set(metadataKey, newGuessedType);
if (!alreadyGuessedType) {
this.missingFields.set(metadataKey, firstTypeGuess);
return;
}

const secondTypeGuess = getMostEnglobingType(
alreadyGuessedType,
firstTypeGuess
);

if (secondTypeGuess) {
this.missingFields.set(metadataKey, secondTypeGuess);
} else {
this.inconsistencies.add(metadataKey, [
alreadyGuessedType,
newGuessedType,
firstTypeGuess,
]);
}
}
Expand Down
60 changes: 47 additions & 13 deletions src/fieldAnalyser/typeUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {FieldTypes} from '@coveord/platform-client';
import {getGuessedTypeFromValue, isValidTypeTransition} from './typeUtils';
import {getGuessedTypeFromValue, getMostEnglobingType} from './typeUtils';

describe('typeUtils', () => {
const signedIntegerLimit = 0b1111111111111111111111111111111;
Expand All @@ -26,24 +26,58 @@ describe('typeUtils', () => {

describe('when the transition is authorized', () => {
it.each([
{from: FieldTypes.LONG, to: FieldTypes.LONG},
{from: FieldTypes.LONG, to: FieldTypes.LONG_64},
{from: FieldTypes.LONG, to: FieldTypes.DOUBLE},
{from: FieldTypes.LONG_64, to: FieldTypes.DOUBLE},
{from: FieldTypes.DOUBLE, to: FieldTypes.DOUBLE},
])('should return true', ({from, to}) => {
expect(isValidTypeTransition(from, to)).toBe(true);
{
from: FieldTypes.LONG,
to: FieldTypes.LONG,
expectation: FieldTypes.LONG,
},
{
from: FieldTypes.LONG,
to: FieldTypes.LONG_64,
expectation: FieldTypes.LONG_64,
},
{
from: FieldTypes.LONG,
to: FieldTypes.DOUBLE,
expectation: FieldTypes.DOUBLE,
},
{
from: FieldTypes.LONG_64,
to: FieldTypes.DOUBLE,
expectation: FieldTypes.DOUBLE,
},
{
from: FieldTypes.DOUBLE,
to: FieldTypes.DOUBLE,
expectation: FieldTypes.DOUBLE,
},
{
from: FieldTypes.LONG_64,
to: FieldTypes.LONG,
expectation: FieldTypes.LONG_64,
},
{
from: FieldTypes.DOUBLE,
to: FieldTypes.LONG_64,
expectation: FieldTypes.DOUBLE,
},
])('should return true', ({from, to, expectation}) => {
y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
expect(getMostEnglobingType(from, to)).toBe(expectation);
});
});

describe('when the transition is not authorized', () => {
it.each([
{from: FieldTypes.LONG_64, to: FieldTypes.LONG},
{from: FieldTypes.DOUBLE, to: FieldTypes.LONG_64},
{from: FieldTypes.LONG_64, to: FieldTypes.STRING},
{from: FieldTypes.STRING, to: FieldTypes.LONG},
{
from: FieldTypes.LONG_64,
to: FieldTypes.STRING,
},
{
from: FieldTypes.STRING,
to: FieldTypes.LONG,
},
])('should return true', ({from, to}) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

'should return true'? :suspect:

expect(isValidTypeTransition(from, to)).toBe(false);
expect(getMostEnglobingType(from, to)).toBe(null);
});
});
});
33 changes: 20 additions & 13 deletions src/fieldAnalyser/typeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import {FieldTypes} from '@coveord/platform-client';

const acceptedTransitions: Array<{from: FieldTypes; to: Array<FieldTypes>}> = [
{from: FieldTypes.LONG, to: [FieldTypes.LONG_64, FieldTypes.DOUBLE]},
{from: FieldTypes.LONG_64, to: [FieldTypes.DOUBLE]},
const acceptedTypeEvolutions = [
[FieldTypes.LONG, FieldTypes.LONG_64, FieldTypes.DOUBLE],
];

export function isValidTypeTransition(
currentState: FieldTypes,
nextState: FieldTypes
): boolean {
if (currentState === nextState) {
return true;
export function getMostEnglobingType(
possibleType1: FieldTypes,
possibleType2: FieldTypes
): FieldTypes | null {
if (possibleType1 === possibleType2) {
return possibleType1;
}
const differentStateTransition = acceptedTransitions
.find((a) => a.from === currentState)
?.to.includes(nextState);
return Boolean(differentStateTransition);

for (const acceptedTypeEvolution of acceptedTypeEvolutions) {
if (
acceptedTypeEvolution.includes(possibleType1) &&
acceptedTypeEvolution.includes(possibleType2)
) {
const idx1 = acceptedTypeEvolution.indexOf(possibleType1);
const idx2 = acceptedTypeEvolution.indexOf(possibleType2);
return acceptedTypeEvolution[Math.max(idx1, idx2)];
}
}
return null;
}

export function getGuessedTypeFromValue(obj: unknown): FieldTypes {
Expand Down