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

Unable to pass null as a parameter value #915

Closed
pdfabbro opened this issue Jan 21, 2021 · 10 comments · Fixed by #920
Closed

Unable to pass null as a parameter value #915

pdfabbro opened this issue Jan 21, 2021 · 10 comments · Fixed by #920
Assignees
Labels
api: bigquery Issues related to the googleapis/nodejs-bigquery API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@pdfabbro
Copy link

Hi,

I am calling createQueryJob using this library ("@google-cloud/bigquery": "^5.5.0"). I have a parameter that is an array. Originally my code was failing for empty arrays because I wasn't providing the type for the param. So I added the type and all is good. However, if I pass null as the value of the param, the code breaks. I think the issue is with this line here:

https://github.com/googleapis/nodejs-bigquery/blob/master/src/bigquery.ts#L1107

Since value is null, value.type blows up. I think it should perhaps be updated to if (value !== null && value.type)?

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/nodejs-bigquery API. label Jan 21, 2021
@stephenplusplus
Copy link
Contributor

Thanks for reporting!

@steffnay I believe you added this in #873. Do you think _getValue() should check that value exists before checking for the type property, or is it an indication of a bigger problem that we are passing it null in the first place?

@stephenplusplus stephenplusplus added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Jan 21, 2021
@hammad2506
Copy link

hammad2506 commented Jan 21, 2021

Taking directly from BigQuery documentation, this would fail:

bigquery.query({
  query: [
    'SELECT url',
    'FROM `publicdata.samples.github_nested`',
    'WHERE repository.owner = ?'
  ].join(' '),

  params: [null],

  types: ['string']
}, function(err, rows) {});

bigquery.query(query).then(function(data) {
  const rows = data[0];
});


So would its named counterpart

bigquery.query({
  query: "SELECT url, FROM \`publicdata.samples.github_nested\` WHERE repository.owner = @owner",
  params: {owner: null},
  types: {owner: 'string'}
}, function(err, rows) {});

bigquery.query(query).then(function(data) {
  const rows = data[0];
});

@pdfabbro
Copy link
Author

As a side note, I think this documentation https://googleapis.dev/nodejs/bigquery/latest/BigQuery.html#createQueryJob should be updated to say that "query" can be a string or an object. Without looking at the source code you wouldn't know that you can pass types via the query property (or even params for that matter).

It does say "A query string, following the BigQuery query syntax, of the query to execute.", but I think it should probably reference this https://googleapis.dev/nodejs/bigquery/latest/BigQuery.html#query and say it can be a string that represents the query or an object with any of the Query properties

@steffnay
Copy link
Contributor

steffnay commented Jan 31, 2021

@stephenplusplus yeah, I did add this and the bug slipped through because we were missing a test case with the null value. Checking that value exists should be enough to fix it. I opened #920.

@hammad, the error your getting should be fixed with #920! Unfortunately, however, I think you still won't be able to use query parameters in the manner shown in the example code you provided. Those two queries will return Operands of = cannot be literal NULL. If you use the SQL syntax that is expected for handling null values like this, you will not be able to use a parameterized query.

Here are some examples of what I mean:

Case: Incorrect SQL syntax for null.
Resulting SQL error: Operands of = cannot be literal NULL

bigquery.query({
  query: [
    'SELECT url',
    'FROM `publicdata.samples.github_nested`',
    'WHERE repository.owner = ?'
  ].join(' '),
  params: [null],
  types: ['string']
}, function(err, rows) {});

Case: Correct SQL syntax, but query parameters not supported.
Resulting SQL error: Expected keyword FALSE or keyword NULL or keyword TRUE but got "?"

bigquery.query({
  query: [
    'SELECT url',
    'FROM `publicdata.samples.github_nested`',
    'WHERE repository.owner is ?'
  ].join(' '),
  params: [null],
 types: ['string']
}, function(err, rows) {});

My SQL is pretty basic, so this may be a nonissue for you, but figured I'd note those couple of things just in case you continue to encounter friction after my PR is merged.

@pdfabbro
Copy link
Author

Yup, in our case we're properly checking for null in the query, but we just can't pass it over via the api so we've had to do workarounds like casting to string, etc. Thanks very much @steffnay for fixing this.

@hammad2506
Copy link

@steffnay I think query parameters with null values should be supported. This is an essential use case, also the example in my original comment was taken from BQ documentation so anything else would be inconsistent.

I think we should follow @stephenplusplus's suggestion here and short circuit null values to support them
#920 (comment)

Thanks again for looking into this.

@radek-vizlib
Copy link

radek-vizlib commented Feb 3, 2021

Same issue here, #920 solves the problem

@yoshi-automation yoshi-automation added 🚨 This issue needs some love. and removed 🚨 This issue needs some love. labels May 5, 2021
@whiterook6
Copy link

Until this is merged, how are we supposed to support null parameters?

@steffnay
Copy link
Contributor

steffnay commented Jun 6, 2021

@whiterook6 Should be good to go now.

@whiterook6
Copy link

@whiterook6 Should be good to go now.

Thank for merging this. How do I get this updated code? Do I need to wait for the next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/nodejs-bigquery API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants