How to cast a JSON value to its correspondent datatype? - postgresql

JSON-string is SQL-text, JSON-number is SQL-numeric, JSON-boolean is SQL-boolean... So I can do "real good cast"... But:
SELECT to_jsonb('hello'::text)::text
is not good. Returning with quotes. Worst case:
SELECT to_jsonb(1::int)::int;
-- ERROR: cannot cast type jsonb to integer
So, how to do casting?

  ((this is a Wiki, you can edit!))
(update for) Modern PostgreSQL do direct cast!
Example using pg 13.5:
SELECT (('{"x":123}'::jsonb)->'x')::int; -- ok no error!
SELECT (('{"x":123.44}'::jsonb)->'x')::float; -- ok no error!
SELECT (('{"x":123.44}'::jsonb)->'x')::numeric; -- ok no error!
SELECT (('{"x":"hello !"}'::jsonb)->'x')::text; -- no error, but quotes
SELECT ('{"x":"hello !"}'::jsonb)->>'x'; -- ok!
-- Modern versions not need cast for simple operations:
SELECT (('{"x":123}'::jsonb)->'x')::int + 3; -- Ok, but...
SELECT jsonb_path_query('{"x":123}', '$.x + 3'); -- How about this!
OLD answers for pg 9.4
Ugly ways only...
Today PostgreSQL is not so serious about JSON... So, let's workaround.
Sorry there are questions (mine!) and answers. The answer with the most simple solution is this one:
In pg v9.4.4+ using the #>> operator works for me: select to_json('test'::text)#>>'{}';
So, using the question's example:
SELECT (to_json(1::int)#>>'{}')::int; -- the cast works
JSONb question... Internal binary value was reused?
SELECT (to_jsonB(10.7::numeric)#>>'{}')::numeric; -- internal repres. preserved?
SELECT (to_jsonB(10.7::float)#>>'{}')::float; -- internal repres. preserved?
-- internal JSONb was float or numeric?
SELECT (to_jsonB(true)#>>'{}')::boolean; -- internal representation preserved?
... there are no guarantee that PostgreSQL's internal parser is doing the correct thing, reusing internal numerical representation of JSONb to avoid CPU-time consuption (converting jsonb-number to SQL-text, them casting text to SQL-number).
"JSONb cast optimization" or "SQL datatypes/JSONb datatypes convertion optionmization" seems to still be a big gap for PostgreSQL.

Here's a simpler solution that I've found for PostgreSQL.
Assuming:
{
"additionaldata" :
{
"Duration" : "123.44"
}
}
The following select works:
select
(additionaldata -> 'Duration')::jsonb::text::numeric as durationSeconds
from ...

Related

DataGrip: How to chop long 'returning' part?

I have the following problem with formatting a PostgreSQL query in Datagrip (and also in all other JetBrains products regarding PG queries):
update some_table
set some_column = 42
where id = 42
returning val_a as "valA",
val_b as "valB",
val_c as "valC",
val_d as "valD",
val_e as "valE",
val_f as "valF",
val_g as "valG";
When I now use the built-in SQL formatter, DataGrip produces this:
update some_table
set some_column = 42
where id = 42
returning val_a as "valA", val_b as "valB", val_c as "valC", val_d as "valD", val_e as "valE", val_f as "valF", val_g as "valG";
You see what the issue is: All returned values are in one line (it even ignores my max-line-length setting). I tried different settings in my IDE but to no avail. Note that for now, I don't care if the returned vals are indented or aligned or whatever, I just want the query to be "readable".
Looking forward to a solution and thanks in advance!
There is no option for that :( Please, file a request
https://youtrack.jetbrains.com/newIssue?project=DBE

Passing a row type to jsonb_to_recordset syntax errors

I'm trying to work out how to expand a JSONB field with a jsonb_to_recordset and a custom type.
Postgres 11.4.
This is just a test case, but for the minute I'm trying to work out the syntax. I've got a table named data_file_info with a JSONB field named table_stats. The JSONB field always includes a JSON array with the same structure:
[
{"table_name":"Activity","record_count":0,"table_number":214},
{"table_name":"Assembly","record_count":1,"table_number":15},
{"table_name":"AssemblyProds","record_count":0,"table_number":154}
]
The following code works correctly:
from data_file_info,
jsonb_to_recordset(table_stats) as table_stats_splat (
table_name text,
record_count integer,
table_number integer
)
What I would like to do is pass in a custm type definition instead of the long-form column definition list shown above. Here's the matching type:
create type data.table_stats_type as (
table_name text,
record_count integer,
table_number integer)
Some examples I've seen, and the docs say, that you can supply a type name using a null:row_type casting in the first parameter to jsonb_to_recordset. The examples that I've found use in-line JSON, while I'm trying to pull stored JSON. I've made a few attempts, all have failed. Below are two of the trials, with errors. Can someone point me towards the correct syntax?
FAIL:
select table_stats_splat.*
from data_file_info,
jsonb_populate_recordset(null::table_stats_type, data_file_info) as table_stats_splat;
-- ERROR: function jsonb_populate_recordset(table_stats_type, data_file_info) does not exist
-- LINE 4: jsonb_populate_recordset(null::table_stats_type, dat...
^
-- HINT: No function matches the given name and argument types. You might need to add explicit type casts. (Line 4)
FAIL:
select *
from jsonb_populate_recordset(NULL::table_stats_type, (select table_stats from data_file_info)) as table_stats_splat;
-- ERROR: more than one row returned by a subquery used as an expression. (Line 2)
I'm doubtlessly missing something pretty obvious, and am hoping someone can suggest what that is.
Use the column as the second parameter:
select table_stats_splat.*
from data_file_info,
jsonb_populate_recordset(null::table_stats_type, table_stats) as table_stats_splat;

How to use Postgresql ts_delete function

I am trying to use Postgresql Full Text Search. I read that the stop words (words ignored for indexing) are implemented via dictionary. But I would like to give the user a limited control over the stop words (insert new ones), so I grouped then in a table.
From the example below:
select strip(to_tsvector('simple', texto)) from longtxts where id = 23;
I can get the vector:
{'alta' 'aluno' 'cada' 'do' 'em' 'leia' 'livro' 'pedir' 'que' 'trecho' 'um' 'voz'}
And now I would like to remove the elements from the stopwords table:
select array(select palavra_proibida from stopwords);
That returns the array:
{a,as,ao,aos,com,default,e,eu,o,os,da,das,de,do,dos,em,lhe,na,nao,nas,no,nos,ou,por,para,pra,que,sem,se,um,uma}
Then, following documentation:
ts_delete(vector tsvector, lexemes text[]) tsvector remove any occurrence of lexemes in lexemes from vector ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat'])
I tried a lot. For example:
select ts_delete((select strip(to_tsvector('simple', texto)) from longtxts where id = 23), array[(select palavra_proibida from stopwords)]);
But I always receive the error:
ERROR: function ts_delete(tsvector, character varying[]) does not exist
LINE 1: select ts_delete((select strip(to_tsvector('simple', texto))...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Could anyone help me? Thanks in advance!
ts_delete was introduced in PostgreSQL 9.6. Based on the error message, you're using an earlier version. You may try select version(); to be sure.
When you land on the PostgreSQL online documentation with a web search, it may correspond to any version. The version is in the URL and there's a "This page in another version" set of links at the top of each page to help switching to the equivalent doc for a different version.

SQL query with XML parameter

EDIT: I have found a relevant answer already on stack overflow here:
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
I have not dealt with XML in T-SQL before, and I am modifying an existing legacy stored proc, and picking most if it up through trial and error.
however I have hit a problem where trial and error is proving fruitless, and very slow. Think it's time to appeal to stack overflow gurus!
Here is some XML
<?xml version=\"1.0\"?>
<Notification xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<NotificationId>0</NotificationId>
<UserNotifications>
<UserNotification>
<UserNotificationId>0</UserNotificationId>
<NotificationId>0</NotificationId>
<UserId>13514</UserId>
<MessageTypeId>1</MessageTypeId>
</UserNotification>
<UserNotification>
<UserNotificationId>0</UserNotificationId>
<NotificationId>0</NotificationId>
<UserId>13514</UserId>
<MessageTypeId>2</MessageTypeId>
</UserNotification>
</UserNotifications>
</Notification>
The Stored Proc in question accepts the above XML as a parameter:
CREATE PROCEDURE [dbo].[Notification_Insert]
#ParametersXml XML
AS
BEGIN
The XML contains child "UserNotification" elements. I would like to select the UserId, MessageTypeId of each UserNotification, into a table like this
UserId | MessageTypeId
13514 | 1
13514 | 2
Obviously the size of the collection is not fixed.
My current attempt (which doesn't work - is along these lines:
DECLARE #UserDetails TABLE ( UserId INT, MessageTypeId INT);
INSERT INTO #UserDetails (UserId, MessageTypeId)
SELECT Tab.Col.value('#UserId','INT'),
Tab.Col.value('#MessageTypeId','INT')
FROM #ParametersXml.nodes('/Notification/UserNotifications[not(#xsi:nil = "true")][1]/UserNotification') AS Tab(Col)
But this never inserts anything..
I have been playing around with this for a while now and not had any joy :(
I would suggest going through the links below. I found them short and quick to go through:
http://blog.sqlauthority.com/2009/02/12/sql-server-simple-example-of-creating-xml-file-using-t-sql/
http://blog.sqlauthority.com/2009/02/13/sql-server-simple-example-of-reading-xml-file-using-t-sql/
I found the solution to this problem through further searching stack overflow.
The query I need (thanks to XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *')
INSERT INTO #UserDetails (UserId, MessageTypeId)
SELECT UserNotification.value('UserId[1]','INT'),
UserNotification.value('MessageTypeId[1]','INT')
FROM #ParametersXml.nodes('//Notification/UserNotifications') AS x(Coll)
cross apply #ParametersXml.nodes('//Notification/UserNotifications/UserNotification') as un(UserNotification)

PostgreSQL jsonb, `?` and JDBC

I am using PostgreSQL 9.4 and the awesome JSONB field type. I am trying to query against a field in a document. The following works in the psql CLI
SELECT id FROM program WHERE document -> 'dept' ? 'CS'
When I try to run the same query via my Scala app, I'm getting the error below. I'm using Play framework and Anorm, so the query looks like this
SQL(s"SELECT id FROM program WHERE document -> 'dept' ? {dept}")
.on('dept -> "CS")
....
SQLException: : No value specified for parameter 5.
(SimpleParameterList.java:223)
(in my actual queries there are more parameters)
I can get around this by casting my parameter to type jsonb and using the #> operator to check containment.
SQL(s"SELECT id FROM program WHERE document -> 'dept' #> {dept}::jsonb")
.on('dept -> "CS")
....
I'm not too keen on the work around. I don't know if there are performance penalties for the cast, but it's extra typing, and non-obvious.
Is there anything else I can do?
As a workaround to avoid the ? operator, you could create a new operator doing exactly the same.
This is the code of the original operator:
CREATE OPERATOR ?(
PROCEDURE = jsonb_exists,
LEFTARG = jsonb,
RIGHTARG = text,
RESTRICT = contsel,
JOIN = contjoinsel);
SELECT '{"a":1, "b":2}'::jsonb ? 'b'; -- true
Use a different name, without any conflicts, like #-# and create a new one:
CREATE OPERATOR #-#(
PROCEDURE = jsonb_exists,
LEFTARG = jsonb,
RIGHTARG = text,
RESTRICT = contsel,
JOIN = contjoinsel);
SELECT '{"a":1, "b":2}'::jsonb #-# 'b'; -- true
Use this new operator in your code and it should work.
Check pgAdmin -> pg_catalog -> Operators for all the operators that use a ? in the name.
In JDBC (and standard SQL) the question mark is reserved as a parameter placeholder. Other uses are not allowed.
See Does the JDBC spec prevent '?' from being used as an operator (outside of quotes)? and the discussion on jdbc-spec-discuss.
The current PostgreSQL JDBC driver will transform all occurrences (outside text or comments) of a question mark to a PostgreSQL specific parameter placeholder. I am not sure if the PostgreSQL JDBC project has done anything (like introducing an escape as discussed in the links above) to address this yet. A quick look at the code and documentation suggests they didn't, but I didn't dig too deep.
Addendum: As shown in the answer by bobmarksie, current versions of the PostgreSQL JDBC driver now support escaping the question mark by doubling it (ie: use ?? instead of ?).
I had the same issue a couple of days ago and after some investigation I found this.
https://jdbc.postgresql.org/documentation/head/statement.html
In JDBC, the question mark (?) is the placeholder for the positional parameters of a PreparedStatement. There are, however, a number of PostgreSQL operators that contain a question mark. To keep such question marks in a SQL statement from being interpreted as positional parameters, use two question marks (??) as escape sequence. You can also use this escape sequence in a Statement, but that is not required. Specifically only in a Statement a single (?) can be used as an operator.
Using 2 question marks seemed to work well for me - I was using the following driver (illustrated using maven dependency) ...
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
... and MyBatis for creating the SQL queries and it seemed to work well. Seemed easier / cleaner than creating an PostgreSQL operator.
SQL went from e.g.
select * from user_docs where userTags ?| array['sport','property']
... to ...
select * from user_docs where userTags ??| array['sport','property']
Hopefully this works with your scenario!
As bob said just use ?? instead of ?
SQL(s"SELECT id FROM program WHERE document -> 'dept' ?? {dept}")
.on('dept -> "CS")