Skip to content

Commit

Permalink
feat: improve BigQueryRange class with value and literalValue
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx committed Apr 8, 2024
1 parent c8768f5 commit 96274d3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
23 changes: 16 additions & 7 deletions src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,21 +1203,21 @@ export class BigQuery extends Service {
{}
);
} else if (typeName === 'RANGE') {
let rangeValue: bigquery.IRangeValue;
let rangeValue: BigQueryRange;
if (value instanceof BigQueryRange) {
rangeValue = value;
} else {
rangeValue = BigQuery.range(
value,
queryParameter.parameterType?.rangeElementType?.type
);
}
}

Check failure on line 1214 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `······`

Check failure on line 1214 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
queryParameter.parameterValue!.rangeValue = {
start: {
value: rangeValue.start && rangeValue.start.value,
value: rangeValue.value.start,
},
end: {
value: rangeValue.end && rangeValue.end.value,
value: rangeValue.value.end,
},
};
} else if (typeName === 'JSON' && is.object(value)) {
Expand Down Expand Up @@ -2479,8 +2479,7 @@ function convertSchemaFieldValue(
export class BigQueryRange {
elementType?: string;
start?: BigQueryTimestamp | BigQueryDate | BigQueryDatetime;
end?: BigQueryTimestamp | BigQueryDate | BigQueryDatetime;
value: string;
end?: BigQueryTimestamp | BigQueryDate | BigQueryDatetime;

Check failure on line 2482 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`

Check failure on line 2482 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
constructor(value: string | BigQueryRangeOptions, elementType?: string) {
if (typeof value === 'string') {
if (!elementType) {
Expand Down Expand Up @@ -2525,7 +2524,17 @@ export class BigQueryRange {
this.end = this.convertElement_(end, inferredType);
this.elementType = inferredType;
}
this.value = `[${this.start ? this.start.value : 'UNBOUNDED'}, ${this.end ? this.end.value : 'UNBOUNDED'})`;
}

public get literalValue() {
return `[${this.start ? this.start.value : 'UNBOUNDED'}, ${this.end ? this.end.value : 'UNBOUNDED'})`;
}

public get value(){

Check failure on line 2533 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
return {
start: this.start ? this.start.value : 'UNBOUNDED',
end: this.end ? this.end.value : 'UNBOUNDED'

Check failure on line 2536 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}

Check failure on line 2537 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
}

static fromSchemaValue_(value: string, elementType: string): BigQueryRange {
Expand Down
8 changes: 1 addition & 7 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,19 +576,13 @@ class Table extends ServiceObject {
return value.toFixed();
}

if (value instanceof BigQueryRange) {
return {
start: value.start && value.start.value,
end: value.end && value.end.value,
};
}

const customTypeConstructorNames = [
'BigQueryDate',
'BigQueryDatetime',
'BigQueryInt',
'BigQueryTime',
'BigQueryTimestamp',
'BigQueryRange',
'Geography',
];
const constructorName = value.constructor?.name;
Expand Down
51 changes: 33 additions & 18 deletions test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,54 +955,69 @@ describe('BigQuery', () => {

it('should accept a string literal', () => {
const dateRange = bq.range(INPUT_DATE_RANGE, 'DATE');
assert.strictEqual(dateRange.value, '[2020-01-01, 2020-12-31)');
assert.strictEqual(dateRange.start.value, '2020-01-01');
assert.strictEqual(dateRange.end.value, '2020-12-31');
assert.strictEqual(dateRange.literalValue, '[2020-01-01, 2020-12-31)');
assert.deepStrictEqual(dateRange.value, {
start: '2020-01-01',
end: '2020-12-31',
});

const datetimeRange = bq.range(INPUT_DATETIME_RANGE, 'DATETIME');
assert.strictEqual(
datetimeRange.value,
datetimeRange.literalValue,
'[2020-01-01 12:00:00, 2020-12-31 12:00:00)'
);
assert.strictEqual(datetimeRange.start.value, '2020-01-01 12:00:00');
assert.strictEqual(datetimeRange.end.value, '2020-12-31 12:00:00');
assert.deepStrictEqual(datetimeRange.value, {
start: '2020-01-01 12:00:00',
end: '2020-12-31 12:00:00',
});

const timestampRange = bq.range(INPUT_TIMESTAMP_RANGE, 'TIMESTAMP');
assert.strictEqual(
timestampRange.value,
timestampRange.literalValue,
'[2020-10-01T04:00:00.000Z, 2020-12-31T04:00:00.000Z)'
);
assert.strictEqual(
timestampRange.start.value,
'2020-10-01T04:00:00.000Z'
);
assert.strictEqual(timestampRange.end.value, '2020-12-31T04:00:00.000Z');
assert.deepStrictEqual(timestampRange.value, {
start: '2020-10-01T04:00:00.000Z',
end: '2020-12-31T04:00:00.000Z',
});
});

it('should accept a BigQueryDate|BigQueryDatetime|BigQueryTimestamp objects', () => {
const dateRange = bq.range({
start: bq.date('2020-01-01'),
end: bq.date('2020-12-31'),
});
assert.strictEqual(dateRange.value, INPUT_DATE_RANGE);
assert.strictEqual(dateRange.literalValue, INPUT_DATE_RANGE);
assert.strictEqual(dateRange.elementType, 'DATE');
assert.deepStrictEqual(dateRange.value, {
start: '2020-01-01',
end: '2020-12-31',
});

const datetimeRange = bq.range({
start: bq.datetime('2020-01-01 12:00:00'),
end: bq.datetime('2020-12-31 12:00:00'),
});
assert.strictEqual(datetimeRange.value, INPUT_DATETIME_RANGE);
assert.strictEqual(datetimeRange.literalValue, INPUT_DATETIME_RANGE);
assert.strictEqual(datetimeRange.elementType, 'DATETIME');
assert.deepStrictEqual(datetimeRange.value, {
start: '2020-01-01 12:00:00',
end: '2020-12-31 12:00:00',
});

const timestampRange = bq.range({
start: bq.timestamp('2020-10-01 12:00:00+08'),
end: bq.timestamp('2020-12-31 12:00:00+08'),
});
assert.strictEqual(
timestampRange.value,
timestampRange.literalValue,
'[2020-10-01T04:00:00.000Z, 2020-12-31T04:00:00.000Z)'
);
assert.strictEqual(timestampRange.elementType, 'TIMESTAMP');
assert.deepStrictEqual(timestampRange.value, {
start: '2020-10-01T04:00:00.000Z',
end: '2020-12-31T04:00:00.000Z',
});
});

it('should accept a start/end as string with element type', () => {
Expand All @@ -1013,7 +1028,7 @@ describe('BigQuery', () => {
},
'DATE'
);
assert.strictEqual(dateRange.value, INPUT_DATE_RANGE);
assert.strictEqual(dateRange.literalValue, INPUT_DATE_RANGE);
assert.strictEqual(dateRange.elementType, 'DATE');

const datetimeRange = bq.range(
Expand All @@ -1023,7 +1038,7 @@ describe('BigQuery', () => {
},
'DATETIME'
);
assert.strictEqual(datetimeRange.value, INPUT_DATETIME_RANGE);
assert.strictEqual(datetimeRange.literalValue, INPUT_DATETIME_RANGE);
assert.strictEqual(datetimeRange.elementType, 'DATETIME');

const timestampRange = bq.range(
Expand All @@ -1034,7 +1049,7 @@ describe('BigQuery', () => {
'TIMESTAMP'
);
assert.strictEqual(
timestampRange.value,
timestampRange.literalValue,
'[2020-10-01T04:00:00.000Z, 2020-12-31T04:00:00.000Z)'
);
assert.strictEqual(timestampRange.elementType, 'TIMESTAMP');
Expand Down

0 comments on commit 96274d3

Please sign in to comment.