Repository navigation
Description
From @smstuebe on October 27, 2015 9:55
When the Queries get complex, we often see '0X0X' in the SELECT statements.
Unfortunately Postgre fails to detect the type of constants if the SELECTS are inner SELECTS. A simple description of this Problem:
http://stackoverflow.com/questions/18073901/failed-to-find-conversion-function-from-unknown-to-text
Query from query log:
SELECT "Project2"."id",
"Project2"."C2" AS "C1",
"Project2"."C1" AS "C2",
"Project2"."description",
"Project2"."creation_date",
"Project2"."vehicle_brand_id",
"Project2"."country_variant_id",
"Project2"."hardware_variant_id",
"Project2"."type_id",
"Project2"."sop_date_id",
"Project2"."project_id",
"Project2"."release_date_id",
"Project2"."config",
"Project2"."released",
"Project2"."number_index",
"Project2"."deprecated",
"Project2"."field",
"Project2"."blocked",
"Project2"."link_teamforge",
"Project2"."link_infopool",
"Project2"."release_info",
"Project2"."release_info_short",
"Project2"."train_status",
"Project2"."created",
"Project2"."creator",
"Project2"."last_modified",
"Project2"."last_modifier",
"Project2"."name",
"Project2"."C3",
"Project2"."C5" AS "C4",
"Project2"."id1",
"Project2"."C4" AS "C5",
"Project2"."description1",
"Project2"."device_number_type",
"Project2"."kpmwebprnumber",
"Project2"."vehicle_brand_id1",
"Project2"."parent_id",
"Project2"."platform_id",
"Project2"."doors_name",
"Project2"."created1",
"Project2"."creator1",
"Project2"."last_modified1",
"Project2"."last_modifier1",
"Project2"."name1"
FROM (
SELECT "Alias1"."id",
"Alias1"."description",
"Alias1"."creation_date",
"Alias1"."vehicle_brand_id",
"Alias1"."country_variant_id",
"Alias1"."hardware_variant_id",
"Alias1"."type_id",
"Alias1"."sop_date_id",
"Alias1"."project_id",
"Alias1"."release_date_id",
"Alias1"."config",
"Alias1"."released",
"Alias1"."number_index",
"Alias1"."deprecated",
"Alias1"."field",
"Alias1"."blocked",
"Alias1"."link_teamforge",
"Alias1"."link_infopool",
"Alias1"."release_info",
"Alias1"."release_info_short",
"Alias1"."train_status",
"Alias1"."created",
"Alias1"."creator",
"Alias1"."last_modified",
"Alias1"."last_modifier",
"Alias1"."name",
"Alias1"."C1",
"Alias1"."C2",
"Alias1"."C3",
"Extent3"."id" AS "id1",
"Extent3"."description" AS "description1",
"Extent3"."device_number_type",
"Extent3"."kpmwebprnumber",
"Extent3"."vehicle_brand_id" AS "vehicle_brand_id1",
"Extent3"."parent_id",
"Extent3"."platform_id",
"Extent3"."doors_name",
"Extent3"."created" AS "created1",
"Extent3"."creator" AS "creator1",
"Extent3"."last_modified" AS "last_modified1",
"Extent3"."last_modifier" AS "last_modifier1",
"Extent3"."name" AS "name1",
CASE
WHEN (
"Extent2"."vehicle_id" IS NULL) THEN (Cast (NULL AS TEXT))
ELSE (e'bc1f6d3a-e7c0-4e64-a4a6-9238b7fb45bc')
END AS "C4",
CASE
WHEN (
"Extent2"."vehicle_id" IS NULL) THEN (cast (NULL AS int4))
ELSE (1)
END AS "C5"
FROM (
SELECT "Project1"."C1",
"Project1"."id",
"Project1"."description",
"Project1"."creation_date",
"Project1"."vehicle_brand_id",
"Project1"."country_variant_id",
"Project1"."hardware_variant_id",
"Project1"."type_id",
"Project1"."sop_date_id",
"Project1"."project_id",
"Project1"."release_date_id",
"Project1"."config",
"Project1"."released",
"Project1"."number_index",
"Project1"."deprecated",
"Project1"."field",
"Project1"."blocked",
"Project1"."link_teamforge",
"Project1"."link_infopool",
"Project1"."release_info",
"Project1"."release_info_short",
"Project1"."train_status",
"Project1"."created",
"Project1"."creator",
"Project1"."last_modified",
"Project1"."last_modifier",
"Project1"."name",
"Project1"."C2",
"Project1"."C3"
FROM (
SELECT "Extent1"."id",
"Extent1"."description",
"Extent1"."creation_date",
"Extent1"."vehicle_brand_id",
"Extent1"."country_variant_id",
"Extent1"."hardware_variant_id",
"Extent1"."type_id",
"Extent1"."sop_date_id",
"Extent1"."project_id",
"Extent1"."release_date_id",
"Extent1"."config",
"Extent1"."released",
"Extent1"."number_index",
"Extent1"."deprecated",
"Extent1"."field",
"Extent1"."blocked",
"Extent1"."link_teamforge",
"Extent1"."link_infopool",
"Extent1"."release_info",
"Extent1"."release_info_short",
"Extent1"."train_status",
"Extent1"."created",
"Extent1"."creator",
"Extent1"."last_modified",
"Extent1"."last_modifier",
"Extent1"."name",
e'0X0X' AS "C1",
e'bc1f6d3a-e7c0-4e64-a4a6-9238b7fb45bc' AS "C2",
e'Vehicles' AS "C3"
FROM "public"."train" AS "Extent1"
WHERE "Extent1"."Discriminator" = e'Train'
AND "Extent1"."config" != true) AS "Project1"
ORDER BY "Project1"."id" ASC limit 10) AS "Alias1"
LEFT OUTER JOIN "public"."vehicle_x_train" AS "Extent2"
INNER JOIN "public"."vehicle" AS "Extent3"
ON "Extent3"."id" = "Extent2"."vehicle_id"
ON
CASE
WHEN (
"Alias1"."C1" LIKE e'0X%') THEN ("Alias1"."id")
END = "Extent2"."train_id") AS "Project2"
ORDER BY "Project2"."id" ASC ,
"Project2"."C5" ASCWhen I specifiy the type implicitly with changing the line
e'0X0X' AS "C1",to
e'0X0X'::text AS "C1",the query is executed without any Problems.
Could you implement explicit typing for this constants? Or is there already any solution for this? Where does this Hugs and Kisses '0X0X' come from? Is this a EF or a npgsql 'feature'?
Our Environment:
Postgre 9.4
npgSQL 3.0.3
EntityFramework6.Npgsql 3.0.3
EntityFramework 6.1.3
Copied from original issue: npgsql/npgsql#837
Activity
roji commentedon May 12, 2016
The 0X0X definitely comes from EF and not from Npgsql. I guess on way to fix this would be to generate ::TEXT on all string literals, although there may be a better way. I'll try to dig deeper.
roji commentedon May 12, 2016
From @smstuebe on October 27, 2015 18:21
yep. I'll try to change it it on a fork. I had a look at the code after I submitted the issue. It should be a oneliner.
EDIT: Ohh VS 2015.
roji commentedon May 12, 2016
Yeah, sorry about that...
You should probably work on the branch hotfix/3.0.4 rather than develop, which is in a complicated state at the moment because of DNX/project.json.
I'll wait for your fix, it would be good to include this in 3.0.4.
roji commentedon May 12, 2016
From @smstuebe on October 30, 2015 15:1
Hi I have send a pull request. It fixes the problem in our environment. Unfortunately, I couldn't execute the unit tests. Could you please double check it?
roji commentedon May 12, 2016
From @Emill on October 30, 2015 19:14
We actually removed ::text to be able to support other datatypes represented as strings. It's a bit more complicated to get this working.
I'll try to figure out if anything can be done.
roji commentedon May 12, 2016
From @smstuebe on October 30, 2015 22:37
Ahh ok. I was wondering, why string is the only datatype that isn't explicitly typed. What other types do you mean?
roji commentedon May 12, 2016
@smstuebe, the idea was to allow string values to be sent for colulmn types such as xml, jsonb. If you type the values by adding ::text, PostgreSQL rejects them because it refuses to implicitly cast text to json. If you leave out the ::text, PostgreSQL treats it as "unknown" and is OK with implicitly casting.
@Emill, thanks for looking at this...
roji commentedon May 12, 2016
From @smstuebe on October 31, 2015 14:53
Ok. Just out of curiosity, does npgsql support json queries (e.g. http://schinckel.net/2014/05/25/querying-json-in-postgres/)?
roji commentedon May 12, 2016
@smstuebe, Npgsql doesn't really have anything to do with JSON queries or anything else that happens in the SQL you send to PostgreSQL; Npgsql's job is simply to send the query to PostgreSQL (with any parameters) and parse the results. So yes, JSON queries and any other PostgreSQL SQL feature is "supported" by Npgsql.
roji commentedon May 12, 2016
@Emill, any idea about this? I hope to release 3.0.4 soon.
roji commentedon May 12, 2016
From @setifo on December 2, 2015 20:36
hi guys, what's up with this topic? it's a bit annoying to have that kind of statements where npgsql couldn't help out :(
entitiy framework is creating this 0X0X-thing for us too, and postgres isn't able to select the right type :(
roji commentedon May 12, 2016
From @Emill on December 2, 2015 23:14
Sorry for not catching up on this...
Do you have example C# Linq code that reproduces this issue?
2015-12-02 21:36 GMT+01:00 setifo notifications@github.com:
19 remaining items
roji commentedon Jul 9, 2016
@rwasef1830 thanks for the long and patient explanation.
Regardless of anything else, the explicit cast function looks like a great addition to me.
Everything you say makes sense, but I still don't understand something. If we add back
::varcharto string literals, queries indeed start to break because unsupported types are no longer sent as unknown-typed strings. But, as I said above, the thing I don't understand is why any user data (e.g. text search, json, macaddr) gets sent as literals rather than as parameters. The current EF6 logic seems to be to send user data as literals for SELECT statements, and as parameters otherwise. This is what makes both user data and EF6 literals (like X0X0) end up in the same function.So my question is this: if we switch to parameters everywhere for user data (leaving literals for EF6 X0X0 et al), by changing
NpgsqlServices.TranslateCommandTree, would it not be safe to add back::varcharto literals (as it would now only be added to EF6 X0X0 et al).I tried to do this and quickly ran into test failures. Before really diving into it I wanted to share these thoughts to see if I'm really missing something fundamental.
roji commentedon Jul 9, 2016
Ah, it seems I had things a bit mixed up. It doesn't really seem possible to cleanly distinguish between user data and EF6-generated data. Will investigate a little bit more but your solution seems clear and probably correct to me.
rwasef1830 commentedon Jul 9, 2016
@roji Exactly. That is what I concluded after debugging. So the goal is to change it in a minimal way that covers this EF6 edge case while not changing any behavior as much as possible.
roji commentedon Jul 9, 2016
@rwasef1830 I think I understand. I'll investigate a bit more and will probably merge your PR very soon.
rwasef1830 commentedon Jul 9, 2016
@roji
I'm also not sure, but most likely to facilitate query plan caching or query caching on the DB side. Things such as column type discriminator computation strategy should only change with the schema, so I think it shouldn't be expressed as a variable.
Merge pull request #36 from rwasef1830/bug_27_literal_varchar_in_proj…
roji commentedon Jul 9, 2016
@rwasef1830, I've merged this into dev. Would you like to submit another PR against hotfix/3.1.1 to have this released soon?
rwasef1830 commentedon Jul 9, 2016
@roji Will do shortly
Fix npgsql#27 by casting literal strings in projection lists + explic…
Fix npgsql#27 by casting literal strings in projection lists + explic…
Merge pull request #42 from rwasef1830/fix_27_for_3.1.1
[-]Postgre can't detect type from const strings (add ::text)[/-][+]PostgreSQL can't detect type for string literals[/+]