pllua / pllua-deprecated

[DEPRECATED] This repository is no longer maintained. Please follow https://github.com/pllua/pllua
197 stars 16 forks source link

inserting binary data (bytea) issues with \x00 #58

Closed bjne closed 6 years ago

bjne commented 6 years ago

given the following test:

create table test_bytea(id serial, data bytea);

create function insert_bytea() returns void as $$
  local p =  server.prepare([[
    INSERT INTO test_bytea (data) values($1)
  ]], {'bytea'})
  p:execute{fromstring('bytea', "\x01\x01\x01")}
  p:execute{fromstring('bytea', "\x00\x00\x00")}
  p:execute{fromstring('bytea', "\x01\x00\x01")}
$$ language pllua;

select insert_bytea();
select id,length(data) from test_bytea;

expected:

 id | length
----+--------
  1 |      3
  2 |      3
  3 |      3
(3 rows)

actual:

 id | length
----+--------
  1 |      3
  2 |      0
  3 |      1
(3 rows)
eugwne commented 6 years ago

fromstring uses conversion from string to a value, not from bytes. Just compare the output

do $$  print(fromstring('bytea', "\x01\x00\x01")) $$ language pllua;

do $$  print(fromstring('bytea', "\\001\\000\\001")) $$ language pllua;

I suppose this will work as you expected:

create table test_bytea(id serial, data bytea);

create function insert_bytea() returns void as $$
  local p =  server.prepare([[
    INSERT INTO test_bytea (data) values($1)
  ]], {'bytea'})
  p:execute{fromstring('bytea', "\\001\\001\\001")}
  p:execute{fromstring('bytea', "\\000\\000\\000")}
  p:execute{fromstring('bytea', "\\001\\000\\001")}
$$ language pllua;

select insert_bytea();
select id,length(data) from test_bytea;
select * from test_bytea;
bjne commented 6 years ago

I made a pull request for it.

On Nov 18, 2017 11:12, "Eugene Sergeev" notifications@github.com wrote:

fromstring uses conversion from string to a value, not from bytes. Just compare output

do $$ print(fromstring('bytea', "\x01\x00\x01")) $$ language pllua; do $$ print(fromstring('bytea', "\001\000\001")) $$ language pllua;

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pllua/pllua/issues/58#issuecomment-345432330, or mute the thread https://github.com/notifications/unsubscribe-auth/ABU9uoAPj3R_5XGZ1s8QqvJRdvrUHdRtks5s3q1-gaJpZM4QfsS2 .

eugwne commented 6 years ago

nonthing to fix, it is how postgres inner function work and correct string for bytes [01 00 01] is "\001\000\001" (not \x01\x00\x01)