Skip to content

Limit Timestamp normalization #2684

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
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
17 changes: 17 additions & 0 deletions packages/firestore/src/model/proto_values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ export function equals(left: api.Value, right: api.Value): boolean {
}

function timestampEquals(left: api.Value, right: api.Value): boolean {
if (
typeof left.timestampValue === 'string' &&
typeof right.timestampValue === 'string' &&
left.timestampValue.length === right.timestampValue.length
) {
// Use string equality for ISO 8601 timestamps
return left.timestampValue === right.timestampValue;
}

const leftTimestamp = normalizeTimestamp(left.timestampValue!);
const rightTimestamp = normalizeTimestamp(right.timestampValue!);
return (
Expand Down Expand Up @@ -230,6 +239,14 @@ function compareTimestamps(
left: ProtoTimestampValue,
right: ProtoTimestampValue
): number {
if (typeof left === 'string' && typeof right === 'string') {
// Use string ordering for ISO 8601 timestamps, but strip the timezone
// suffix to ensure proper ordering for timestamps of different precision.
// The only supported timezone is UTC (i.e. 'Z') based on
// ISO_TIMESTAMP_REG_EXP.
return primitiveComparator(left.slice(0, -1), right.slice(0, -1));
}

const leftTimestamp = normalizeTimestamp(left);
const rightTimestamp = normalizeTimestamp(right);

Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/protos/firestore_proto_api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ export declare namespace firestoreV1ApiClientInterfaces {
nullValue?: ValueNullValue;
booleanValue?: boolean;
integerValue?: string | number;
doubleValue?: number;
timestampValue?: string | { seconds: string | number; nanos: number };
doubleValue?: string | number;
timestampValue?: string | { seconds?: string | number; nanos?: number };
stringValue?: string;
bytesValue?: string | Uint8Array;
referenceValue?: string;
Expand Down
18 changes: 6 additions & 12 deletions packages/firestore/src/remote/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ export class JsonProtoSerializer {
);
}

// TODO(mrschmidt): Even when we remove our custom FieldValues, we still
// need to re-encode field values to their expected type based on the
// `useProto3Json` setting.
toValue(val: fieldValue.FieldValue): api.Value {
if (val instanceof fieldValue.NullValue) {
return { nullValue: 'NULL_VALUE' };
Expand Down Expand Up @@ -412,18 +415,9 @@ export class JsonProtoSerializer {
} else if ('integerValue' in obj) {
return new fieldValue.IntegerValue(normalizeNumber(obj.integerValue!));
} else if ('doubleValue' in obj) {
if (this.options.useProto3Json) {
// Proto 3 uses the string values 'NaN' and 'Infinity'.
if ((obj.doubleValue as {}) === 'NaN') {
return fieldValue.DoubleValue.NAN;
} else if ((obj.doubleValue as {}) === 'Infinity') {
return fieldValue.DoubleValue.POSITIVE_INFINITY;
} else if ((obj.doubleValue as {}) === '-Infinity') {
return fieldValue.DoubleValue.NEGATIVE_INFINITY;
}
}

return new fieldValue.DoubleValue(obj.doubleValue!);
// Note: Proto 3 uses the string values 'NaN' and 'Infinity'.
const parsedNumber = Number(obj.doubleValue!);
return new fieldValue.DoubleValue(parsedNumber);
} else if ('stringValue' in obj) {
return new fieldValue.StringValue(obj.stringValue!);
} else if ('mapValue' in obj) {
Expand Down
92 changes: 92 additions & 0 deletions packages/firestore/test/unit/model/field_value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,44 @@ describe('FieldValue', () => {
expectEqualitySets(values, (v1, v2) => v1.isEqual(v2));
});

it('normalizes values for equality', () => {
// Each subarray compares equal to each other and false to every other value
const values: FieldValue[][] = [
[
new PrimitiveValue({ integerValue: '1' }),
new PrimitiveValue({ integerValue: 1 })
],
[
new PrimitiveValue({ doubleValue: '1.0' }),
new PrimitiveValue({ doubleValue: 1.0 })
],
[
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:01Z' }),
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:01.000Z' }),
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:01.000000Z' }),
new PrimitiveValue({
timestampValue: '2007-04-05T14:30:01.000000000Z'
}),
new PrimitiveValue({ timestampValue: { seconds: 1175783401 } }),
new PrimitiveValue({ timestampValue: { seconds: '1175783401' } }),
new PrimitiveValue({
timestampValue: { seconds: 1175783401, nanos: 0 }
})
],
[
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:01.100Z' }),
new PrimitiveValue({
timestampValue: { seconds: 1175783401, nanos: 100000000 }
})
],
[
new PrimitiveValue({ bytesValue: new Uint8Array([0, 1, 2]) }),
new PrimitiveValue({ bytesValue: 'AAEC' })
]
];
expectEqualitySets(values, (v1, v2) => v1.isEqual(v2));
});

it('orders types correctly', () => {
const groups = [
// null first
Expand Down Expand Up @@ -537,6 +575,60 @@ describe('FieldValue', () => {
);
});

it('normalizes values for comparison', () => {
const groups = [
[
new PrimitiveValue({ integerValue: '1' }),
new PrimitiveValue({ integerValue: 1 })
],
[
new PrimitiveValue({ doubleValue: '2' }),
new PrimitiveValue({ doubleValue: 2 })
],
[
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:01Z' }),
new PrimitiveValue({ timestampValue: { seconds: 1175783401 } })
],
[
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:01.999Z' }),
new PrimitiveValue({
timestampValue: { seconds: 1175783401, nanos: 999000000 }
})
],
[
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:02Z' }),
new PrimitiveValue({ timestampValue: { seconds: 1175783402 } })
],
[
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:02.100Z' }),
new PrimitiveValue({
timestampValue: { seconds: 1175783402, nanos: 100000000 }
})
],
[
new PrimitiveValue({ timestampValue: '2007-04-05T14:30:02.100001Z' }),
new PrimitiveValue({
timestampValue: { seconds: 1175783402, nanos: 100001000 }
})
],
[
new PrimitiveValue({ bytesValue: new Uint8Array([0, 1, 2]) }),
new PrimitiveValue({ bytesValue: 'AAEC' })
],
[
new PrimitiveValue({ bytesValue: new Uint8Array([0, 1, 3]) }),
new PrimitiveValue({ bytesValue: 'AAED' })
]
];

expectCorrectComparisonGroups(
groups,
(left: FieldValue, right: FieldValue) => {
return left.compareTo(right);
}
);
});

// TODO(mrschmidt): Fix size accounting
// eslint-disable-next-line no-restricted-properties
it.skip('estimates size correctly for fixed sized values', () => {
Expand Down