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: untyped string array type #2011

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ interface FieldType extends Type {
* @private
*
* @param {*} value - The value.
* @param {boolean} isArrayValue - If the value is for an array type.
* @returns {object}
*
* @example
Expand All @@ -597,7 +598,7 @@ interface FieldType extends Type {
* // {type: 'float64'}
* ```
*/
function getType(value: Value): Type {
function getType(value: Value, isArrayValue = false): Type {
const isSpecialNumber =
is.infinite(value) || (is.number(value) && isNaN(value));

Expand Down Expand Up @@ -629,6 +630,10 @@ function getType(value: Value): Type {
return {type: 'bool'};
}

if (is.string(value) && isArrayValue) {
return {type: 'string'};
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this. Why are we returning type: 'string' if the value is an array and a string? What happens now if the value is a string, but not an array? Then the return type will be unspecified, right? While it now is string.

And why do we want to return type: 'string' for array values, and not array<string>?

}

if (Buffer.isBuffer(value)) {
return {type: 'bytes'};
}
Expand Down Expand Up @@ -663,7 +668,7 @@ function getType(value: Value): Type {

return {
type: 'array',
child: getType(child),
child: getType(child, true),
};
}

Expand Down
40 changes: 40 additions & 0 deletions system-test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,46 @@ describe('Spanner', () => {
);
});

it('GOOGLE_STANDARD_SQL should query untyped string array values', function (done) {
if (IS_EMULATOR_ENABLED) {
this.skip();
}

const value = ['ghi', 'jkl'];
const table = googleSqlTable;

const query: ExecuteSqlRequest = {
sql:
'SELECT * FROM `' +
table.name +
'` WHERE EXISTS (SELECT 1 FROM UNNEST(StringArray) AS c WHERE c IN UNNEST(@value))',
params: {
value,
},
};

insert(
{StringArray: value},
Spanner.GOOGLE_STANDARD_SQL,
(err, row) => {
assert.ifError(err);
assert.deepStrictEqual(row.toJSON().StringArray, ['ghi', 'jkl']);

DATABASE.run(query, (err, rows) => {
if (err) {
assert.ifError(err);
done();
}
assert.deepStrictEqual(rows!.shift()!.toJSON().StringArray, [
'ghi',
'jkl',
]);
done();
});
}
);
});

it('GOOGLE_STANDARD_SQL should read untyped string values', function (done) {
if (IS_EMULATOR_ENABLED) {
this.skip();
Expand Down
Loading