CardinalityViolation when trying to use nested subqueries to retrieve JSON from Postgres using SQLAlchemy - postgresql

I'm trying to translate the following Postgres query into Sqlalchemy 1.4:
SELECT json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', geom,
'properties', (
select json_build_object(
'id', hi.id,
'responses', (
select json_object_agg(e.etype, er.response)
from expert e
inner join expertresponse er on e.id = er.expert_id
where infra_id = hi.id
),
'protections', (
select json_object_agg(p.ptype, i.pscore)
from protection p
inner join infraprotection i on p.id = i.protection_id
where infra_id = hi.id
)
)
)
)
)
) as allinfra
from hardinfra hi;
So I define the subqueries in the same order:
responses = (
db.session.query(
(func.json_object_agg(Expert.etype, Expert_Response.response)).label(
"responses"
)
)
.join(Expert_Response.exp)
.filter(Expert_Response.infra_id == Hardinfra.id)
.scalar_subquery()
)
protections = (
db.session.query(
(func.json_object_agg(Protection.ptype, Infra_Protection.pscore)).label(
"protections"
)
)
.join(Infra_Protection.prot)
.filter(Infra_Protection.infra_id == Hardinfra.id)
.scalar_subquery()
)
properties = db.session.query(
(
func.json_build_object(
"id", Hardinfra.id, "protections", protections, "responses", responses
).label("properties")
)
).scalar_subquery()
features = db.session.query(
(
func.json_build_object(
"type",
"Feature",
"geometry",
Hardinfra.geom,
"properties",
properties,
).label("features")
)
).scalar_subquery()
q = db.session.query(
func.json_build_object(
"type", "FeatureCollection", "features", func.json_agg(features)
)
)
However I get a (psycopg2.errors.CardinalityViolation) more than one row returned by a subquery used as an expression error when features is evaluated. I'm not even sure that properties and features need to be scalar subqueries (responses and protections certainly do), but I'm clearly also doing something else wrong, perhaps to do with correlating the inner Hardinfra refs to the outer ref? I'm not sureā€¦

It might be caused by redundant subqueries, you could try to remove the last two subqueries.
responses = (
db.session.query(
(func.json_object_agg(Expert.etype, Expert_Response.response)).label(
"responses"
)
)
.join(Expert_Response.exp)
.filter(Expert_Response.infra_id == Hardinfra.id)
.scalar_subquery()
)
protections = (
db.session.query(
(func.json_object_agg(Protection.ptype, Infra_Protection.pscore)).label(
"protections"
)
)
.join(Infra_Protection.prot)
.filter(Infra_Protection.infra_id == Hardinfra.id)
.scalar_subquery()
)
properties = func.json_build_object(
"id", Hardinfra.id, "protections", protections, "responses", responses
)
features = func.json_build_object(
"type",
"Feature",
"geometry",
Hardinfra.geom,
"properties",
properties,
)
q = db.session.query(
func.json_build_object(
"type", "FeatureCollection", "features", func.json_agg(features)
)
)

Related

Convert date in jsonb (Postgres)

I have jsonb column data =
"{"history": [{"endDate": "30.06.2015", "classname": "Class A", "startDate": "2010-04-01", "numberAction": "0016", "positionName": "Teacher"},
{"endDate": "31.06.2010", "classname": "Class A", "startDate": "2005-08-10", "numberAction": "0015", "positionName": "Student"},
{"endDate": "2005.08.09", "classname": "Class B", "startDate": "2005-02-21", "numberAction": "0014", "positionName": " Student "}]}"
As you can see, the dates of the "endDate" in the array are not correct. Please tell me how they can be converted to the format YYYY-MM-DD?
My idle attempt:
UPDATE table
SET data = jsonb_set(data, '{endDate}', to_date('{endDate}', 'YYYY-MM-DD'), false)
WHERE id = 'UUID';
Answer =
update table
set data = data - 'history' ||
jsonb_build_object
( 'history'
, ( select jsonb_agg
( case when aa.value ->> 'endDate' like '%.%' then
aa.value - 'endDate' || jsonb_build_object
( 'endDate'
, to_date(aa.value ->> 'endDate','dd.mm.yyyy')
)
else
aa.value
end
)
from jsonb_array_elements(data -> 'history') as aa
)
)
WHERE uuid = 'UUID'
and exists ( select *
from jsonb_array_elements(data -> 'history') as aa
where aa.value ->> 'endDate' like '%.%'
);

org.postgresql.util.PSQLException: ERROR: set-returning functions are not allowed in WHERE

I need a help for the below mentioned detail,
I am using Postgres + Spring-Data-JPA. Moreover, I have used the jsonb data type for storing data.
I am trying to execute a query, but it gives me the following error:
ERROR: set-returning functions are not allowed in WHERE
The cause here is that I have added a jsonb condition in the WHERE clause (kindly refer to the below query for more detail).
Query (I have renamed column name just because of hiding the actual column name):
select distinct
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'firstName' as firstName,
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'lastName' as lastName,
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'country' as country
from
tale1 table10_
left outer join
table2 table21_
on table10_.end_user_id=table21_.end_user_id
left outer join
table3 table32_
on table10_.manufacturer_id=table32_.manufacturer_id
where
table21_.end_user_uuid=(
?
)
and table21_.is_active=true
and table32_.manufacturer_uuid=(
?
)
and table32_.is_active=true
and table10_.is_active=true
and table32_.is_active=true
and jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'action' = ('PENDING')
order by
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'firstName',
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'lastName'
limit ?
The following line in the above query is causing the error:
and jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'action' = ('PENDING')
Can anyone please guide me about how do fetch data from the inner JSON? Especially in my case I have an inner List and a few elements inside.
I recommend a lateral join with jsonb_array_elements for cases like that. Here is an example:
CREATE TABLE tale1 (
id integer PRIMARY KEY,
initiated_referral_detail jsonb NOT NULL
);
INSERT INTO tale1 VALUES
(1, '{
"name": "one",
"listOfAttribue": [
{ "id": 1, "action": "DONE"},
{ "id": 2, "action": "PENDING" },
{ "id": 3, "action": "ACTIVE" }
]
}');
INSERT INTO tale1 VALUES
(2, '{
"name": "two",
"listOfAttribue": [
{ "id": 1, "action": "DONE"},
{ "id": 2, "action": "ACTIVE" }
]
}');
To find all ids where the associated JSON contains an array element with action = PENDING, you can query like this:
SELECT DISTINCT id
FROM tale1 CROSS JOIN LATERAL
jsonb_array_elements(initiated_referral_detail -> 'listOfAttribue') AS attr
WHERE attr ->> 'action' = 'PENDING';

Recursive JSONB postgres

I am trying to build a recursive CTE in Postgres that supports both arrays and objects, to return a list of key-value pairs and don't seem to be able to find a good example. This is my current code.
with recursive jsonRecurse as
(
select
j.key as Path
,j.key
,j.value
from jsonb_each(to_jsonb('{
"key1": {
"key2": [
{
"key3": "test3",
"key4": "test4"
}
]
},
"key5": [
{
"key6":
[
{
"key7": "test7"
}
]
}
]
}'::jsonb)) j
union all
select
jr.path || '.' || jr2.Key
,jr2.key
,jr2.value
from jsonRecurse jr
left join lateral jsonb_each(jr.value) jr2 on true
where jsonb_typeof(jr.value) = 'object'
)
select
*
from jsonRecurse;
As you can see the code stops recursing as soon as I hit an array instead of an object. I've tried playing around with using a case statement and putting the function call to jsonb_each or jsonb_array_element in the case statement instead but I get an error telling me to use lateral joins instead.
I have used this example table to make the query more readable:
create table my_table(id serial primary key, jdata jsonb);
insert into my_table (jdata) values
('{
"key1": {
"key2": [
{
"key3": "test3",
"key4": "test4"
}
]
},
"key5": [
{
"key6":
[
{
"key7": "test7"
}
]
}
]
}');
You have to join both jsonb_each(value) and jsonb_array_elements(value) conditionally, depending on the type of value:
with recursive extract_all as
(
select
key as path,
value
from my_table
cross join lateral jsonb_each(jdata)
union all
select
path || '.' || coalesce(obj_key, (arr_key- 1)::text),
coalesce(obj_value, arr_value)
from extract_all
left join lateral
jsonb_each(case jsonb_typeof(value) when 'object' then value end)
as o(obj_key, obj_value)
on jsonb_typeof(value) = 'object'
left join lateral
jsonb_array_elements(case jsonb_typeof(value) when 'array' then value end)
with ordinality as a(arr_value, arr_key)
on jsonb_typeof(value) = 'array'
where obj_key is not null or arr_key is not null
)
select *
from extract_all;
Output:
path | value
--------------------+------------------------------------------------
key1 | {"key2": [{"key3": "test3", "key4": "test4"}]}
key5 | [{"key6": [{"key7": "test7"}]}]
key1.key2 | [{"key3": "test3", "key4": "test4"}]
key5.0 | {"key6": [{"key7": "test7"}]}
key1.key2.0 | {"key3": "test3", "key4": "test4"}
key5.0.key6 | [{"key7": "test7"}]
key1.key2.0.key3 | "test3"
key1.key2.0.key4 | "test4"
key5.0.key6.0 | {"key7": "test7"}
key5.0.key6.0.key7 | "test7"
(10 rows)
Elements of json arrays have no keys, we should use their indexes to build a path. Therefore the function jsonb_array_elements() should be called with ordinality. Per the documentation (see 7.2.1.4. Table Functions):
If the WITH ORDINALITY clause is specified, an additional column of type bigint will be added to the function result columns. This column numbers the rows of the function result set, starting from 1.
The function call
jsonb_array_elements(case jsonb_typeof(value) when 'array' then value end)
with ordinality as a(arr_value, arr_key)
returns pairs (value, ordinality) aliased as (arr_value, arr_key).

Sequelize order by two nested associations

I try to use the order for both my sub-queries. But sequelize generate a bad query and use this order by on main-query.
Here is my query:
Model.findAll({
order: sequelize.literal('("asset->forOrder"."value" - "asset.currenthours") desc),
offset: 0,
limit: 10,
include: [{
model: Asset,
as: 'asset',
include: [{
model: ThresholdService,
required: false,
as: 'thresholdservices'
}, {
model: ThresholdService,
where: {property: 'hours'},
required: false,
duplicating: false,
as: 'forOrder'
}],
}]
})
I expected to get query like this:
SELECT ..., "asset->forOrder"."asset_id", "asset->forOrder"."value"
FROM (
SELECT ...
FROM "machineidentity" AS "machineidentity"
LIMIT 10 OFFSET 0
) AS "machineidentity"
LEFT OUTER JOIN "asset" AS "asset" ON "machineidentity"."unique_id" = "asset"."machineIdentityId"
LEFT OUTER JOIN "thresholdservice" AS "asset->thresholdservices" ON "asset"."id" = "asset->thresholdservices"."asset_id"
LEFT OUTER JOIN "thresholdservice" AS "asset->forOrder" ON "asset"."id" = "asset->forOrder"."asset_id" AND "asset->forOrder"."property" = 'hours'
ORDER BY ("asset->forOrder"."value" - "asset"."currenthours") DESC;
but have:
SELECT ..., "asset->forOrder"."asset_id", "asset->forOrder"."value"
FROM (
SELECT ...
FROM "machineidentity" AS "machineidentity"
ORDER BY ("asset->forOrder"."value" - "asset"."currenthours") DESC
LIMIT 10 OFFSET 0
) AS "machineidentity"
LEFT OUTER JOIN "asset" AS "asset" ON "machineidentity"."unique_id" = "asset"."machineIdentityId"
LEFT OUTER JOIN "thresholdservice" AS "asset->thresholdservices" ON "asset"."id" = "asset->thresholdservices"."asset_id"
LEFT OUTER JOIN "thresholdservice" AS "asset->forOrder" ON "asset"."id" = "asset->forOrder"."asset_id" AND "asset->forOrder"."property" = 'hours'
ORDER BY ("asset->forOrder"."value" - "asset"."currenthours") DESC;
ORDER BY appear in machineidentity query.
I found in sequelize such code:
if (
subQuery
&& Array.isArray(order)
&& order[0]
&& !(order[0] instanceof Association)
&& !(typeof order[0] === 'function' && order[0].prototype instanceof Model)
&& !(typeof order[0].model === 'function' && order[0].model.prototype instanceof Model)
&& !(typeof order[0] === 'string' && model && model.associations !== undefined && model.associations[order[0]])
) {
subQueryOrder.push(this.quote(order, model, '->'));
}
mainQueryOrder.push(this.quote(order, model, '->'));
I have just created order, which not satisfied this if.
So, right now my order, looks like:
[['asset.forOrder', 'value', sequelize.literal('-(machine.asset.currentkilometers - machine.asset.initialvalueinkm)')]]

UNION and NOT IN Mysql Operation with Zend framework

I need to execute the following mysql query in Zend;I am not an excpert with Zend framework
SELECT `optionride`.`featureoption_id`,
`optionride`.`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,`optionride`.`value` FROM
`engine4_ride_featureoptionrides` AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption`
ON foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
UNION
SELECT `foption`.`featureoption_id`, null as
`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,null as `value` FROM `engine4_ride_featureoptions` AS `foption`
WHERE (foption.featureoptiontab_id= '2' ) AND `foption`.`featureoption_id` NOT IN
(
SELECT `optionride`.`featureoptionride_id` FROM `engine4_ride_featureoptionrides`
AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption` ON
foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
)
Anybody can help me please.
You can put all your query asis in the $db->fetch().
Also you can use $db->select()->union(array($sql1, $sql2)), where $sql1, $sql2 can be $db->select() or a string ("select...").
For the NOT IN you can use $db->where('someField NOT IN (?)', array());