unexpected behaviour of Google BigQuery WHERE NOT

2019-07-15 03:07发布

问题:

I have a small example table temp.ty: From this table I want to only extract the rows where ipc is not present in not_ipc (and where exclude is not present in other_classes), which seems rather straigtforward. Yet the following query returns zero results:

SELECT * FROM temp.ty where not ipc contains not_ipc

If I repclace not_ipc with for example 'G01N 33' it doesn't work either yet if I replace not_ipc with 'G01N' it works just fine.

I have tried variations with wildcards and LIKE operations as well as the STRING() function, but so far I have not been able to manage. I have a strong feeling I am missing something, but I cant figure it out.

回答1:

Here is a sample query:

select * from
(select 'G01N 33/55' as ipc, 'G01N 34' as not_ipc),
(select 'G01N 33/55' as ipc, 'G01N 33' as not_ipc),
(select 'G01N 33/55' as ipc, string(null) as not_ipc)
where not ipc contains not_ipc or not_ipc is null

this returns:

+-----+------------+---------+---+
| Row |    ipc     | not_ipc |   |
+-----+------------+---------+---+
|   1 | G01N 33/55 | G01N 34 |   |
|   2 | G01N 33/55 | null    |   |
+-----+------------+---------+---+

Here is another one:

select * from
(select 'G01N 33/55' as ipc, 'G01N 33' as not_ipc, 'C08K 3/08' as exclude, 'C08K 3/08,C08K 77/02' as other_classes),
(select 'G01N 33/55' as ipc, 'G01N 34' as not_ipc, string(null) as exclude, 'C08K 3/08,C08K 77/02' as other_classes),
(select 'G01N 33/55' as ipc, 'G01N 33' as not_ipc, string(null) as exclude, string(null) as other_classes),
(select 'G01N 33/55' as ipc, 'G01N 36' as not_ipc, string(null) as exclude, string(null) as other_classes),
(select 'G01N 33/55' as ipc, string(null) as not_ipc, 'C08K 3/08' as exclude, string(null) as other_classes)
where (not ipc contains not_ipc or not_ipc is null) and (not other_classes contains exclude or exclude is null or other_classes is null)

returns:

+-----+------------+---------+-----------+----------------------+---+
| Row |    ipc     | not_ipc |  exclude  |    other_classes     |   |
+-----+------------+---------+-----------+----------------------+---+
|   1 | G01N 33/55 | G01N 34 | null      | C08K 3/08,C08K 77/02 |   |
|   2 | G01N 33/55 | G01N 36 | null      | null                 |   |
|   3 | G01N 33/55 | null    | C08K 3/08 | null                 |   |
+-----+------------+---------+-----------+----------------------+---+