Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG::TextDecoder::Array decodeы incorrectly arrays with nonstandard lower bound index #272

Closed
ged opened this issue Mar 12, 2018 · 6 comments

Comments

@ged
Copy link
Owner

ged commented Mar 12, 2018

Original report by hbda NA (Bitbucket: hbda, GitHub: hbda).


By default, in Postgresql the lower bound index value of an array's dimensions is set to one. To represent arrays with other lower bounds, the array subscript ranges can be specified explicitly before writing the array contents. This decoration consists of square brackets ([]) around each array dimension's lower and upper bounds, with a colon (:) delimiter character in between. The array dimension decoration is followed by an equal sign (=). This is described in documentation.
For example:

[0:1]={foo,bar}

This is one-dimensional array. But if try decode it by PG::TextDecoder::Array

#!ruby

PG::TextDecoder::Array.new(name: "text[]", delimiter: ',').decode("[0:1]={foo,bar}")

it returns two-dimensional array

#!ruby

[["foo", "bar"]]

Decoder parse arrays without bounds description correctly.

#!ruby

PG::TextDecoder::Array.new(name: "text[]", delimiter: ',').decode("{foo,bar}")
=> ["foo", "bar"]

The problem is reproduced in versions 0.18.4 and 1.0.0.

@ged
Copy link
Owner Author

ged commented Mar 12, 2018

Original comment by Lars Kanis (Bitbucket: larskanis, GitHub: larskanis).


Thanks for the report!

I wonder how [-3:-2]={foo,bar} could be mapped to ruby, so that it's useful? I think PG::TextDecoder::Array should just ignore the bounds and return a (possibly multidimensional) Array. We could then introduce something like PG::TextDecoder::ArrayWithBounds which returns an instance of Struct.new(:values, :dimensions) with the possibly multidimensional Array and its bounds.

@ged
Copy link
Owner Author

ged commented Mar 12, 2018

Original comment by hbda NA (Bitbucket: hbda, GitHub: hbda).


I think PG::TextDecoder::Array should ignore the bounds and return array with correct numbers of dimensions. It is better solution than current behavior.

@ged
Copy link
Owner Author

ged commented Mar 13, 2018

Original comment by hbda NA (Bitbucket: hbda, GitHub: hbda).


I thought a little more. Decoder shouldn't just ignore the bounds. This leads to the loss of some information. For example we use this syntax to compress arrays with leading nils.

#!ruby

[3:3]={foo} => [nil, nil, "foo"]

But this is a special case.
I think returning an instance of Struct.new(:values, :dimensions) is a good solution.

@ged
Copy link
Owner Author

ged commented Apr 21, 2018

Original comment by Lars Kanis (Bitbucket: larskanis, GitHub: larskanis).


This is fixed per https://bitbucket.org/ged/ruby-pg/commits/0567662910977452ad8b9b5a6eb67bd5dd8e67ec .

@ged ged closed this as completed Apr 21, 2018
@ged
Copy link
Owner Author

ged commented Aug 29, 2018

Original comment by Jonathan Allard (Bitbucket: joallard, GitHub: joallard).


Is there an issue I can track for decoding of a leading nils array?

@ged
Copy link
Owner Author

ged commented Aug 29, 2018

Original comment by Lars Kanis (Bitbucket: larskanis, GitHub: larskanis).


@joallard Please open a new issue!

@ged ged added this to the Pending milestone Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant