How to compare two arrays in Standard SQL (BigQuery)? - sql

I have two tables with the exact same schema, one of the fields is a record and I try to compare this field between the two tables.
For example:
Table A:
{ Name: 'Mary',
DOB: '06.06.1970',
Children:[
{ Name: John, Age: 6 },
{ Name: Agatha, Age: 10}
]
},
{ Name: 'Bob',
DOB: '30.03.1982',
Children:[
{ Name: Anthony, Age: 9 },
{ Name: William, Age: 4 },
{ Name: Rose, Age: 2 }
]
},
{ Name: 'Adam',
DOB: '312.10.1980',
Children:[
{ Name: Andrew, Age: 9 },
{ Name: Kate, Age: 4 }
]
}
Table B:
{ Name: 'Mary',
DOB: '06.06.1970',
Children:[
{ Name: John, Age: 6 },
{ Name: Agatha, Age: 10}
]
},
{ Name: 'Bob',
DOB: '30.03.1982',
Children:[
{ Name: Anthony, Age: 9 },
{ Name: George, Age: 4 },
{ Name: Frank, Age: 2 }
]
},
{ Name: 'Adam',
DOB: '312.10.1980',
Children:[
{ Name: Andrew, Age: 9 },
{ Name: Kate, Age: 4 },
{ Name: Jonathan, Age: 2 }
]
}
The output I'd like to get is:
Name
----------
Bob
Adam
Since the Children details are not the same for Bob or Adam in table A and table B (whenever it's the size of the record (num of children) or the details (children names etc...))
I tried to use SELECT UNION DISTINCT but it doesn't work on an array, I also tried != and <> but it doesn't work on arrays...
It would be ideal to compare two arrays like you can compare two lists in Python (comparing the structure and the content).
Is there a way to implement something similar ?
Thank you

select name
from `project.dataset.tableA` a
join `project.dataset.tableB` b
using(name)
where a.dob != b.dob
or (select string_agg(format('%t', s) order by name) from a.children s)
!= (select string_agg(format('%t', s) order by name) from b.children s)

Related

How do I search in redis json

I'm currently new to ioredis and I was wondering how o I can search all the json object that has a key value of something.
Example
rooms:{
roomId1: {
name: "room1",
users: [{userId: 1, name: "bob"}, {userId: 2, name: "joe}]
},
roomId2: {
name: "room2",
users: [{userId: 1, name: "jill"}, {userId: 2, name: "joe}]
},
roomId3: {
name: "room3",
users: [{userId: 6, name: "hoi"}, {userId: 1, name: "bob}]
}
}
and I want find all the rooms that has the user "bob" so I want the output to be
[roomId1: {
name: "room1",
users: [{userId: 1, name: "bob"}, {userId: 2, name: "joe}]
}, roomId3: {
name: "room3",
users: [{userId: 6, name: "hoi"}, {userId: 1, name: "bob}]
}]
How cna i achievee this? I guess I can do something like this
const redis = new Redis();
const roomStates = await redis.get("rooms");
//for loop and inner for loop to find the users name bob
but I was wondeirng if theres a faster way of doing it using the redis built in functions

Deep assertion on JSON array with contains in Karate

Scenario:
Given def cat =
"""
{
name: 'Billie',
kittens: [
{ id: 23, name: 'Bob', age: 35 },
{ id: 42, name: 'Wild', age: 25 }
]
}
"""
Then match cat.kittens contains [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }]
As seen in the example, I am not worried about 'age'. I understand that I can use '#ignore'. Is there any other way, if I have lot of such nodes that I have to ignore.
I am just thinking, can it work with JSON arrays as it does with JSON Objects. asserting only specified nodes.
error:
assert.feature:24 - path: $.kittens[*], actual: [{"id":23,"name":"Bob","age":35},{"id":42,"name":"Wild","age":25}], expected: {id=42, name=Wild}, reason: actual value does not contain expected
EDIT:
I tried something suggested in Karate - how to check if array contains values?
but that did not help me. I am not looking for schema-validation instead I am looking for functional validation where every object may have different values for keys.
Below one fails
Scenario:
Given def cat =
"""
{
name: 'Billie',
kittens: [
{ id: 23, name: 'Bob', age: 35 },
{ id: 42, name: 'Wild', age: 25 }
]
}
"""
* def expected = [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }]
Then match cat.kittens contains '#(^expected)'
This one works fine but this is not helping me.
Scenario:
Given def cat =
"""
{
name: 'Billie',
kittens: [
{ id: 23, name: 'Bob', age: 35 },
{ id: 42, name: 'Wild', age: 25 }
]
}
"""
* def expected = { id: 42, name: 'Wild' }
Then match cat.kittens contains '#(^expected)'
Currently, I am reading arrays separately and asserting them with loop.
Just adding the word deep works in 0.9.6.RC4
Then match cat.kittens contains deep [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }]
I don't understand why the linked answer when I closed your first question did not help you. Maybe I don't understand, and someone else can provide a better answer.

How do I perform a SQL update on a list inside one of my table rows?

I have a table with parameters as shown below.
{
id: 1,
title: 'waccos',
description: 'Making an operating system in wacc',
est_duration: 420,
exp_req: 'any',
langs: [ 1 ],
owner: 1,
members: [ 1, 2 ]
},
I want to use SQL to add a new member into the member list, but I do not know how to. The table is called testprojects. I've tried using
(update testprojects
set members = members || $1
where title is $2;', [name, title])
but I think that is wrong. Could anyone help me please?
This is the table of members so far.
[
{ id: 1, name: 'A' },
{ id: 2, name: 'K' },
{ id: 3, name: 'S' },
{ id: 5, name: 'd' },
{ id: 6, name: 'J' },
{ id: 7, name: 'E' },
{ id: 8, name: 'a' }
]

How do I mock a computed method to stop json method error in my test (Vue-test-utils and vuetify)

I'm having an issue running a test on a large complicated Vuetify component with many smaller components, the important parts:
<cat-form
:dogs="dogs"
:cats="cats"
>
</cat-form>
.....
props: [
'availablecats',
'avaliabledogs'
],
.....
computed: {
advertisers() {
var dogs = JSON.parse(this.avaliabledogs)
return dogs
},
cats() {
var cats = JSON.parse(this.availablecats)
return cats
},
And then the test using Vue-utils:
describe('CreateCat', function () {
let props = {
avaliablecats : [{
name: 'cat1',
age: 2
},
{
name: 'cat2',
age: 4
}],
avaliabledogs : [{
name: 'dog1',
age: 3
},
{
name: 'dog2',
age: 8
}],
}
beforeEach(() => {
wrapper = mount(CreateCat, {
propsData: props,
computed: {
dogs() {
return props.avaliabledogs
},
cats() {
return props.avaliablecats
},
}
});
});
test('true is true', () => {
expect(true).toEqual(true)
})
});
This is the error I receive:
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
143 |
144 | cats() {
> 145 | var cats= JSON.parse(this.avaliablecats)
| ^
146 | return cats
147 | },
Firstly, I thought if I mocked the props data, then the computed method could be calculated and wouldn't consider this.avaliabledogs and this.avaliblecats as undefined. But that didn't work.
I next tried to mock the computed methods then the function wouldn't be called at all. But I'm still receiving exactly the same error.
I've also tried the below methods as an alternate way to set computed methods and props after mounting the wrapper rather than including them when I first mount the component.
wrapper.setProps({
avaliablecats : [{
name: 'cat1',
age: 2
},
{
name: 'cat2',
age: 4
}],
avaliabledogs : [{
name: 'dog1',
age: 3
},
{
name: 'dog2',
age: 8
}],
})
wrapper.setComputed({
cats: [{
name: 'cat1',
age: 2
},
{
name: 'cat2',
age: 4
}],
dogs: [{
name: 'dog1',
age: 3
},
{
name: 'dog2',
age: 8
}],
})
I then tried stubbing the entire 'cat-form' component that used dogs and cats. Again, still getting the error.
wrapper = mount(CreateCat, {
stubs: {
CatForm: true,
},
I'm at a bit of a loss now - I feel like I've used the right syntax but I am getting confused why the methods in the computed property are still being called when i've mocked the computed function?
Any help would be very much appreciated!
JSON.parse(this.avaliabledogs) implies that avaliabledogs prop is valid JSON string. While it's an array of objects in this test, converting it to a string results in something like:
[object Object],[object Object]
This is what infamous SyntaxError: Unexpected token o in JSON error indicates.
In this case there's no need to mock computed properties (can be done like shown here) because they are too small to be considered separate units that need to be isolated; this can be achieved with properly mocked props:
let props = {
avaliablecats : JSON.stringify([{
name: 'cat1',
age: 2
},
{
name: 'cat2',
age: 4
}]),
avaliabledogs : JSON.stringify([{
name: 'dog1',
age: 3
},
{
name: 'dog2',
age: 8
}]),
}

How To Check In Array Vuejs

I have object like this:
data() {
return {
headings: ['id','remark'],
rows: [
{
id: 1,
FirstName: 'Jhon',
LastName: 'Doe',
remark: 0
},
{
id: 2,
FirstName: 'Foo',
LastName: 'Bar',
remark: 1
}
]
}}
Is there away how do i check if key headings object exist in rows object.
Thanks and sorry for my bad english.
Yes, There's a way
function (row) {
return this.headings.reduce((res, heading) => res &= row.hasOwnProperty(heading), true)
}
This method should return true - if the row has the heading.