Open ajcvickers opened 5 years ago
@ajcvickers - What should be SQL translation of this?
@smitpatel You're asking the wrong person! @roji? @bricelam? @maumar? (Ideally I would be pinging Diego!)
SQLite:
SELECT *
FROM ProductPhotos AS e
WHERE substr(e.Photo, 0 + 1, 1) = x'65'
LIMIT 1
SUBSTRING also works on binary/varbinary (although a cast/convert may need to be applied to get a tinyint out.
(am I an SQL Server guy now??)
@bricelam - I tried producing following SQL both of them failed.
SELECT "p"."PhotoId", "p"."Photo", "p"."ProductId"
FROM "ProductPhoto" AS "p"
WHERE substr("p"."Photo", 0 + 1, 1) = 101
LIMIT 2
found no matching records
SELECT "p"."PhotoId", "p"."Photo", "p"."ProductId"
FROM "ProductPhoto" AS "p"
WHERE substr("p"."Photo", 0 + 1, 1) = x'101'
LIMIT 2
invalid token x'101' There is matching record at least when evaluated on client. What could be going wrong?
@roji - You are SqlServer guy now since your tip helped resolve this issue for SqlServer.
Blob constants are hex encoded:
SELECT "p"."PhotoId", "p"."Photo", "p"."ProductId"
FROM "ProductPhoto" AS "p"
WHERE substr("p"."Photo", 0 + 1, 1) = x'65'
LIMIT 2
Unfortunately, casting either side doesn't seem to work.
sqlite> SELECT x'65' = CAST(101 AS BLOB);
0
sqlite> SELECT CAST(x'65' AS INTEGER) = 101;
0
unicode
kind of works, until it tries to interpret 2-byte characters:
sqlite> SELECT unicode(x'65') = 101;
1
sqlite> SELECT unicode(x'c0') = 192;
0
@bricelam - What would be solution? I updated SqlServer. SUBSTR gives byte[] and conversion to byte on top of it works fine. Sqlite - I indeed tried same conversion and even unicode but unicode failed as you said in a different place in the test. there is hex function which can give hex code but how to convert that to int?
I think we’d need to translate the specific pattern—not just the array access. Like a binary operation with array access on one side and a constant on the other.
We could also add our own UDF to convert from BLOB correctly.
The pattern match can be done but it would only work for constants. It won't work for parameters or a byte column. Hence I was looking into making the return value of substr byte rather than making both side byte[].
Query
where
Photo
is abyte[]
property.See
MonsterFixupTestBase
Note: this was previously client eval.
Currently throws: