Commit Graph

472 Commits (61a0227241b2a0bccfd7be3e11995b3b59ae53ff)

Author SHA1 Message Date
Eshton Robateau 20bf953a17 pull out changes into new public function 2023-12-09 11:20:14 -06:00
Eshton Robateau 12582a0fd4 bitsize largest option is 64 2023-12-09 11:20:14 -06:00
Eshton Robateau 905f252667 uncomment tests 2023-12-09 11:20:14 -06:00
Eshton Robateau 9927e14bbf remove dead line 2023-12-09 11:20:14 -06:00
Eshton Robateau 95b2f85e60 support scientific notation big floats 2023-12-09 11:20:14 -06:00
Simon Paredes 4678e69599 fix error message to print the unexpected rune 2023-12-01 18:23:23 -06:00
Simon Paredes 89d699c2e8 wrap errors instead of just formatting them 2023-12-01 18:23:23 -06:00
Jacopo 7ebced92b5 Fix issue with order of json encoding #1805 2023-11-24 19:01:48 -06:00
Kirill Mironov d3fb6e00da implement json.Marshaler and json.Unmarshaler for Float4, Float8 2023-11-04 10:25:31 -05:00
Jack Christensen 45f807fdb4 Special case the underlying type of []byte
Underlying types were already tried. But []byte is not a normal
underlying type. It is a slice. But since is can be treated as a scalar
instead of an array / slice we need to special case it.

https://github.com/jackc/pgx/issues/1763
2023-10-12 20:52:49 -05:00
Ville Skyttä c6c50110db Spelling and grammar fixes 2023-10-07 09:26:23 -05:00
Ville Skyttä 24ed0e4257 Make use of strings.Cut 2023-10-04 20:41:55 +03:00
horpto f4533dc906 optimize parseNumericString 2023-07-25 19:25:23 -05:00
Jack Christensen 492283b90b zeronull.Timestamptz should use pgtype.Timestamptz
https://github.com/jackc/pgx/issues/1694
2023-07-22 08:35:32 -05:00
Jack Christensen d58fe2d53c Fix json scan of non-string pointer to pointer
https://github.com/jackc/pgx/issues/1691
2023-07-19 20:54:05 -05:00
Christoph Engelbert (noctarius) bd3e0d422c Fixes #1684 QCharArrayOID being defined with the wrong OID 2023-07-15 09:44:48 -05:00
Jack Christensen 05440f9d3f Drastically increase allowed test times for potato CI
The context timeouts for tests are designed to give a better error
message when something hangs rather than the test just timing out.
Unfortunately, the potato CI frequently has some test or another
randomly take a long time. While the increased times are somewhat less
than optimal on a real computer, hopefully this will solve the
flickering CI.
2023-07-11 21:16:08 -05:00
Jack Christensen e0c70201dc Skip json format test on CockroachDB 2023-07-11 20:51:22 -05:00
Jack Christensen 524f661136 Fix JSON encoding for pointer to structs implementing json.Marshaler
https://github.com/jackc/pgx/issues/1681
2023-07-11 20:28:36 -05:00
Dan McGee 507a9e9ad3 Remove some now unused pgtype code
Most of this is in conversion, and I assume it became unused with some
of the v5 changes and refactors to a codec-based approach.

There are likely a few more cleanups to be made, but these ones seemed
easy and safe to start with.
2023-07-10 20:23:42 -05:00
Dan McGee 0328d314ea Use bytes.Equal rather than bytes.Compare ==/!= 0
As recommended by go-staticcheck, but also might be a bit more efficient
for the compiler to implement, since we don't care about which slice of
bytes is greater than the other one.
2023-07-08 12:08:05 -05:00
Evan Jones dc94db6b3d pgtype.Hstore: add a round-trip test for binary and text codecs
This ensures the output of Encode can pass through Scan and produce
the same input. This found two two minor problems with the text
codec. These are not bugs: These situations do not happen when using
pgx with Postgres. However, I think it is worth fixing to ensure the
code is internally consistent.

The problems with the text codec are:

* It did not correctly distinguish between nil and empty. This is not
  a problem with Postgres, since NULL values are marked separately,
  but the binary codec distinguishes between them, so it seems like
  the text codec should as well.
* It did not output spaces between keys. Postgres produces output in
  this format, and the parser now only strictly parses the Postgres
  format. This is not a bug, but seems like a good idea.
2023-06-29 17:25:47 -05:00
Lev Zakharov 7c386112e3 fix concurrency bug in pgtype.defaultMap (#1650) 2023-06-18 08:23:56 -05:00
Evan Jones e5db6a0467 pgtype array: Fix encoding of vtab \v
Arrays with values that start or end with vtab ("\v") must be quoted.
Postgres's array parser skips leading and trailing whitespace with
the array_isspace() function, which is slightly different from the
scanner_isspace() function that was previously linked. Add a test
that reproduces this failure, and fix the definition of isSpace.

This also includes a change to use strings.EqualFold which should
really not matter, but does not require copying the string.
2023-06-17 17:15:58 -05:00
Evan Jones bc8b1ca320 remove the single backing string optimization
This is a bit slower than using this optimization, but ensures this
version does not change garbage collection behavior. This does still
using a single []string for all the *string value pointers because
that is what text parsing already does. This makes the two behave
similarly.

benchstat results of master versus this version:

                               │  orig.txt   │   new-binary-no-share-string.txt    │
                               │   sec/op    │   sec/op     vs base                │
HstoreScan/databasesql.Scan-10   82.11µ ± 1%   81.71µ ± 2%        ~ (p=0.280 n=10)
HstoreScan/text-10               83.30µ ± 1%   82.45µ ± 1%   -1.02% (p=0.000 n=10)
HstoreScan/binary-10             15.99µ ± 2%   10.12µ ± 1%  -36.67% (p=0.000 n=10)
geomean                          47.82µ        40.86µ       -14.56%

                               │   orig.txt   │   new-binary-no-share-string.txt    │
                               │     B/op     │     B/op      vs base               │
HstoreScan/databasesql.Scan-10   56.23Ki ± 0%   56.23Ki ± 0%       ~ (p=0.128 n=10)
HstoreScan/text-10               65.12Ki ± 0%   65.12Ki ± 0%       ~ (p=0.541 n=10)
HstoreScan/binary-10             21.09Ki ± 0%   19.87Ki ± 0%  -5.75% (p=0.000 n=10)
geomean                          42.58Ki        41.75Ki       -1.95%

                               │  orig.txt  │    new-binary-no-share-string.txt    │
                               │ allocs/op  │ allocs/op   vs base                  │
HstoreScan/databasesql.Scan-10   744.0 ± 0%   744.0 ± 0%        ~ (p=1.000 n=10) ¹
HstoreScan/text-10               743.0 ± 0%   743.0 ± 0%        ~ (p=1.000 n=10) ¹
HstoreScan/binary-10             464.0 ± 0%   316.0 ± 0%  -31.90% (p=0.000 n=10)
geomean                          635.4        559.0       -12.02%
¹ all samples are equal

benchstat results of the version with one string and this version:

                               │ new-binary-share-everything.txt │    new-binary-no-share-string.txt    │
                               │             sec/op              │    sec/op     vs base                │
HstoreScan/databasesql.Scan-10                       81.80µ ± 1%    81.71µ ± 2%        ~ (p=1.000 n=10)
HstoreScan/text-10                                   82.77µ ± 1%    82.45µ ± 1%        ~ (p=0.063 n=10)
HstoreScan/binary-10                                 7.330µ ± 2%   10.124µ ± 1%  +38.13% (p=0.000 n=10)
geomean                                              36.75µ         40.86µ       +11.18%

                               │ new-binary-share-everything.txt │   new-binary-no-share-string.txt    │
                               │              B/op               │     B/op      vs base               │
HstoreScan/databasesql.Scan-10                      56.23Ki ± 0%   56.23Ki ± 0%       ~ (p=0.232 n=10)
HstoreScan/text-10                                  65.12Ki ± 0%   65.12Ki ± 0%       ~ (p=0.218 n=10)
HstoreScan/binary-10                                20.73Ki ± 0%   19.87Ki ± 0%  -4.11% (p=0.000 n=10)
geomean                                             42.34Ki        41.75Ki       -1.39%

                               │ new-binary-share-everything.txt │     new-binary-no-share-string.txt     │
                               │            allocs/op            │  allocs/op   vs base                   │
HstoreScan/databasesql.Scan-10                        744.0 ± 0%    744.0 ± 0%         ~ (p=1.000 n=10) ¹
HstoreScan/text-10                                    743.0 ± 0%    743.0 ± 0%         ~ (p=1.000 n=10) ¹
HstoreScan/binary-10                                  41.00 ± 0%   316.00 ± 0%  +670.73% (p=0.000 n=10)
geomean                                               283.0         559.0        +97.53%
¹ all samples are equal
2023-06-16 15:31:37 -05:00
Evan Jones 2de94187f5 hstore: Make binary parsing 2X faster
* use []string for value string pointers: one allocation instead of
  one per value.
* use one string for all key/value pairs, instead of one for each.

After this change, one Hstore will share two allocations: one string
and one []string. The disadvantage is that it cannot be deallocated
until all key/value pairs are unused. This means if an application
takes a single key or value from the Hstore and holds on to it, its
memory footprint will increase. I would guess this is an unlikely
problem, but it is possible.

The benchstat results from my M1 Max are below.

goos: darwin
goarch: arm64
pkg: github.com/jackc/pgx/v5/pgtype
                               │   orig.txt   │               new.txt               │
                               │    sec/op    │   sec/op     vs base                │
HstoreScan/databasesql.Scan-10    82.11µ ± 1%   82.66µ ± 2%        ~ (p=0.436 n=10)
HstoreScan/text-10                83.30µ ± 1%   84.24µ ± 3%        ~ (p=0.165 n=10)
HstoreScan/binary-10             15.987µ ± 2%   7.459µ ± 6%  -53.35% (p=0.000 n=10)
geomean                           47.82µ        37.31µ       -21.98%

                               │   orig.txt   │               new.txt               │
                               │     B/op     │     B/op      vs base               │
HstoreScan/databasesql.Scan-10   56.23Ki ± 0%   56.23Ki ± 0%       ~ (p=0.324 n=10)
HstoreScan/text-10               65.12Ki ± 0%   65.12Ki ± 0%       ~ (p=0.675 n=10)
HstoreScan/binary-10             21.09Ki ± 0%   20.73Ki ± 0%  -1.70% (p=0.000 n=10)
geomean                          42.58Ki        42.34Ki       -0.57%

                               │  orig.txt   │               new.txt                │
                               │  allocs/op  │ allocs/op   vs base                  │
HstoreScan/databasesql.Scan-10    744.0 ± 0%   744.0 ± 0%        ~ (p=1.000 n=10) ¹
HstoreScan/text-10                743.0 ± 0%   743.0 ± 0%        ~ (p=1.000 n=10) ¹
HstoreScan/binary-10             464.00 ± 0%   41.00 ± 0%  -91.16% (p=0.000 n=10)
geomean                           635.4        283.0       -55.46%
¹ all samples are equal
2023-06-16 15:31:37 -05:00
Evan Jones 07670dddca do not share the original input string
This allows the original input string to be garbage collected, so it
should not change the memory footprint. This is a slower than the
version that shares a string, but only a small amount. It is still
faster than binary parsing (until that is optimized).

benchstat difference of original versus this version:

                               │  orig.txt   │     new-do-not-share-string.txt     │
                               │   sec/op    │   sec/op     vs base                │
HstoreScan/databasesql.Scan-10   82.11µ ± 1%   14.24µ ± 2%  -82.66% (p=0.000 n=10)
HstoreScan/text-10               83.30µ ± 1%   14.97µ ± 1%  -82.03% (p=0.000 n=10)
HstoreScan/binary-10             15.99µ ± 2%   15.80µ ± 0%   -1.16% (p=0.024 n=10)
geomean                          47.82µ        14.99µ       -68.66%

                               │   orig.txt   │     new-do-not-share-string.txt      │
                               │     B/op     │     B/op      vs base                │
HstoreScan/databasesql.Scan-10   56.23Ki ± 0%   20.11Ki ± 0%  -64.24% (p=0.000 n=10)
HstoreScan/text-10               65.12Ki ± 0%   29.00Ki ± 0%  -55.47% (p=0.000 n=10)
HstoreScan/binary-10             21.09Ki ± 0%   21.09Ki ± 0%        ~ (p=0.722 n=10)
geomean                          42.58Ki        23.08Ki       -45.80%

                               │  orig.txt  │     new-do-not-share-string.txt      │
                               │ allocs/op  │ allocs/op   vs base                  │
HstoreScan/databasesql.Scan-10   744.0 ± 0%   340.0 ± 0%  -54.30% (p=0.000 n=10)
HstoreScan/text-10               743.0 ± 0%   339.0 ± 0%  -54.37% (p=0.000 n=10)
HstoreScan/binary-10             464.0 ± 0%   464.0 ± 0%        ~ (p=1.000 n=10) ¹
geomean                          635.4        376.8       -40.70%
¹ all samples are equal

benchstat difference of the shared string versus not:

                               │ new-share-string.txt │     new-do-not-share-string.txt     │
                               │        sec/op        │   sec/op     vs base                │
HstoreScan/databasesql.Scan-10            10.57µ ± 2%   14.24µ ± 2%  +34.69% (p=0.000 n=10)
HstoreScan/text-10                        11.60µ ± 2%   14.97µ ± 1%  +29.03% (p=0.000 n=10)
HstoreScan/binary-10                      15.87µ ± 2%   15.80µ ± 0%        ~ (p=0.280 n=10)
geomean                                   12.48µ        14.99µ       +20.07%

                               │ new-share-string.txt │     new-do-not-share-string.txt      │
                               │         B/op         │     B/op      vs base                │
HstoreScan/databasesql.Scan-10           11.68Ki ± 0%   20.11Ki ± 0%  +72.17% (p=0.000 n=10)
HstoreScan/text-10                       20.58Ki ± 0%   29.00Ki ± 0%  +40.93% (p=0.000 n=10)
HstoreScan/binary-10                     21.08Ki ± 0%   21.09Ki ± 0%        ~ (p=0.427 n=10)
geomean                                  17.17Ki        23.08Ki       +34.39%

                               │ new-share-string.txt │      new-do-not-share-string.txt       │
                               │      allocs/op       │  allocs/op   vs base                   │
HstoreScan/databasesql.Scan-10             44.00 ± 0%   340.00 ± 0%  +672.73% (p=0.000 n=10)
HstoreScan/text-10                         44.00 ± 0%   339.00 ± 0%  +670.45% (p=0.000 n=10)
HstoreScan/binary-10                       464.0 ± 0%    464.0 ± 0%         ~ (p=1.000 n=10) ¹
geomean                                    96.49         376.8       +290.47%
2023-06-16 15:30:54 -05:00
Evan Jones d48d36dc02 pgtype/hstore: Make text parsing about 6X faster
I am working on an application that uses hstore types, and we found
that returning the values is slow, particularly when using the text
protocol, such as when using database/sql. This improves parsing to
be about 6X faster (currently faster than binary). The changes are:

* referencing the original string instead of copying into new strings
  (very large win)
* using string.IndexByte to scan double quoted strings: it has
  architecture-specific assembly implementations, and most of the
  time is spent in key/value strings.
* estimating the number of key/value pairs to allocate the correct
  size of the slice and map up front. This reduces the number of
  allocations and bytes allocated by a factor of 2, and was a small
  CPU win.
* parsing directly into the Hstore, rather than copying into it.

This parser is stricter than the old one. It only accepts hstore
strings serialized by Postgres. The old one was already stricter
than Postgres's own parser, but previously accepted any whitespace
character after a comma. This one only accepts space. Example:

  "k1"=>"v1",\t"k2"=>"v2"

Postgres only ever uses ", " as the separator. See hstore_out:
https://github.com/postgres/postgres/blob/master/contrib/hstore/hstore_io.c

The result of using benchstat to compare the benchmark on my M1 Pro
with the following command line in below. The new text parser is now
faster than the binary parser. I will improve the binary parser in a
separate change.

for i in $(seq 10); do go test ./pgtype -run=none -bench=BenchmarkHstoreScan -benchtime=1s >> new.txt; done

goos: darwin
goarch: arm64
pkg: github.com/jackc/pgx/v5/pgtype
                               │  orig.txt   │               new.txt               │
                               │   sec/op    │   sec/op     vs base                │
HstoreScan/databasesql.Scan-10   82.11µ ± 1%   10.51µ ± 0%  -87.20% (p=0.000 n=10)
HstoreScan/text-10               83.30µ ± 1%   11.49µ ± 1%  -86.20% (p=0.000 n=10)
HstoreScan/binary-10             15.99µ ± 2%   15.77µ ± 1%   -1.35% (p=0.007 n=10)
geomean                          47.82µ        12.40µ       -74.08%

                               │   orig.txt   │               new.txt                │
                               │     B/op     │     B/op      vs base                │
HstoreScan/databasesql.Scan-10   56.23Ki ± 0%   11.68Ki ± 0%  -79.23% (p=0.000 n=10)
HstoreScan/text-10               65.12Ki ± 0%   20.58Ki ± 0%  -68.40% (p=0.000 n=10)
HstoreScan/binary-10             21.09Ki ± 0%   21.09Ki ± 0%        ~ (p=0.378 n=10)
geomean                          42.58Ki        17.18Ki       -59.66%

                               │  orig.txt   │               new.txt                │
                               │  allocs/op  │ allocs/op   vs base                  │
HstoreScan/databasesql.Scan-10   744.00 ± 0%   44.00 ± 0%  -94.09% (p=0.000 n=10)
HstoreScan/text-10               743.00 ± 0%   44.00 ± 0%  -94.08% (p=0.000 n=10)
HstoreScan/binary-10              464.0 ± 0%   464.0 ± 0%        ~ (p=1.000 n=10) ¹
geomean                           635.4        96.49       -84.81%
¹ all samples are equal
2023-06-16 15:30:54 -05:00
Jack Christensen 45520d5a11 Document pgtype.Map and pgtype.Type are immutable after registration 2023-06-14 08:27:04 -05:00
Lev Zakharov 90f9aad67f add singleton pgtype.Map for default type mappings 2023-06-14 08:21:28 -05:00
Klaus c542df4fb4 added MarshalJSON and UnmarshalJSON to timestamp and added their tests (based on timestamptz implementation) 2023-06-12 09:52:49 -05:00
Evan Jones 4d643b75f5 pgtype/hstore_test.go: Extend coverage of scan benchmark
I am working on an application that reads a lot of hstore values, and
have discovered that scanning it is fairly slow. I'm working on some
improvements, but first I wanted a better benchmark. This adds more
realistic data, and extends it to cover the three APIs: database/sql,
and pgconn.Rows.Scan with both text and binary protocols.
2023-06-12 09:17:24 -05:00
Evan Jones 1b68b5970e pgtype/hstore: Save 2 allocs in database/sql Scan implementation
Remove unneeded string to []byte to string conversion, which saves 2
allocs and should make Hstore text scanning slightly faster.

The Hstore.Scan() function takes a string as input, converts it to
[]byte, and calls scanPlanTextAnyToHstoreScanner.Scan(). That
function converts []byte back to string and calls parseHstore. This
refactors scanPlanTextAnyToHstoreScanner.Scan into
scanPlanTextAnyToHstoreScanner.scanString so the database/sql Scan
function can call it directly, bypassing this conversion.

The added Benchmark shows this saves 2 allocs for longer strings, and
saves about 5% CPU overall on my M1 Pro. benchstat output:

goos: darwin
goarch: arm64
pkg: github.com/jackc/pgx/v5/pgtype
              │  orig.txt   │              new.txt               │
              │   sec/op    │   sec/op     vs base               │
HstoreScan-10   1.334µ ± 2%   1.257µ ± 2%  -5.77% (p=0.000 n=10)

              │   orig.txt   │               new.txt               │
              │     B/op     │     B/op      vs base               │
HstoreScan-10   2.094Ki ± 0%   1.969Ki ± 0%  -5.97% (p=0.000 n=10)

              │  orig.txt  │              new.txt              │
              │ allocs/op  │ allocs/op   vs base               │
HstoreScan-10   36.00 ± 0%   34.00 ± 0%  -5.56% (p=0.000 n=10)
2023-06-07 15:35:22 -05:00
Evan Jones ee04d4a74d pgtype/hstore: Avoid Postgres Mac OS X parsing bug
Postgres on Mac OS X has a bug in how it parses hstore text values
that causes it to misinterpret some Unicode values as spaces. This
causes values sent by pgx to be misinterpreted. To avoid this, always
quote hstore values, which is how Postgres serializes them itself.
The test change fails on Mac OS X without this fix.

While I suspect this should not be performance critical for any
application, I added a quick benchmark to test the performance of the
encoding. This change actually makes encoding slightly faster on my
M1 Pro. The output from the benchstat program on this banchmark is:

goos: darwin
goarch: arm64
pkg: github.com/jackc/pgx/v5/pgtype
                          │   orig.txt   │           new-quotes.txt            │
                          │    sec/op    │   sec/op     vs base                │
HstoreSerialize/text-10      207.1n ± 0%   142.3n ± 1%  -31.31% (p=0.000 n=10)
HstoreSerialize/binary-10   100.10n ± 0%   99.64n ± 1%   -0.45% (p=0.013 n=10)
geomean                      144.0n        119.1n       -17.31%

I have also attempted to fix the Postgres bug, but it will take a
long time for this fix to get upstream:

https://www.postgresql.org/message-id/CA%2BHWA9awUW0%2BRV_gO9r1ABZwGoZxPztcJxPy8vMFSTbTfi4jig%40mail.gmail.com
2023-06-07 15:29:25 -05:00
Jack Christensen 9f00b6f750 Use context timeouts in more tests
Tests should timeout in a reasonable time if something is stuck. In
particular this is important when testing deadlock conditions such as
can occur with the copy protocol if both the client and the server are
blocked writing until the other side does a read.
2023-05-29 10:25:57 -05:00
Alek Anokhin 70a200cff4 Fix test failures
Add bool type alias conversion in `elemKindToPointerTypes` and `underlyingNumberType`
2023-05-20 08:53:23 -05:00
Wichert Akkerman c1c67e4e58 Fix: correctly handle bool type aliases
https://github.com/jackc/pgx/issue/1593
2023-05-20 08:53:23 -05:00
Evan Jones eab316e200 pgtype.Hstore: Fix quoting of whitespace; Add test
Before this change, the Hstore text protocol did not quote keys or
values containing non-space whitespace ("\r\n\v\t"). This causes
inserts with these values to fail with errors like:

    ERROR: Syntax error near "r" at position 17 (SQLSTATE XX000)

The previous version also quoted curly braces ("{}"), but they don't
seem to require quoting.

It is possible that it would be easier to just always quote the
values, which is what Postgres does when encoding its text protocol,
but this is a smaller change.
2023-05-16 07:02:55 -05:00
Evan Jones 8ceef73b84 pgtype.parseHstore: Reject invalid input; Fix error messages
The parseHstore function did not check the return value from
p.Consume() after a ', ' sequence. It expects a doublequote '"' that
starts the next key, but would accept any character. This means it
accepted invalid input such as:

    "key1"=>"b", ,key2"=>"value"

Add a unit test that covers this case
Fix a couple of the nearby error strings while looking at this.

Found by looking at staticcheck warnings:

    pgtype/hstore.go:434:6: this value of end is never used (SA4006)
    pgtype/hstore.go:434:6: this value of r is never used (SA4006)
2023-05-15 18:10:20 -05:00
Evan Jones bbcc4fc0b8 pgtype/hstore_test.go: Add coverage for text protocol
The existing test registers pgtype.Hstore in the text map, then uses
the query modes that use the binary protocol. The existing test did
not use the text parsing code. Add a version of the test that uses
pgtype.Hstore as the input and output argument in all query modes,
and tests it without registering the codec.
2023-05-15 18:09:31 -05:00
Evan Jones d8b38b28be pgtype/hstore.go: Remove unused quoteHstore{Element,Replacer}
These are unused. The code uses quoteArrayElement instead.
2023-05-13 10:03:22 -05:00
Evan Jones 2a86501e86 Fix hstore NULL versus empty
When running queries with the hstore type registered, and with simple
mode queries, the scan implementation does not correctly distinguish
between NULL and empty. Fix the implementation and add a test to
verify this.
2023-05-13 09:34:30 -05:00
Lev Zakharov c27b9b49ea support different bool string representations 2023-04-27 20:29:41 -05:00
Jack Christensen 67f2a41587 Fix scanning a table type into a struct
Table types have system / hidden columns like tableoid, cmax, xmax, etc.
These are not included when sending or receiving composite types.

https://github.com/jackc/pgx/issues/1576
2023-04-20 20:13:37 -05:00
Simon Paredes 2cf1541bb9 wrap error 2023-04-11 18:07:05 -05:00
Jack Christensen 847f888631 Fix scan array of record to pointer to slice of struct
https://github.com/jackc/pgx/issues/1570
2023-04-08 14:39:48 -05:00
cemre.mengu ca022267db add tests 2023-03-25 10:22:11 -05:00
Cemre Mengu 2a653b4a8d fix: handle null interface for json
When using `scany` I encountered the following case. This seems to fix it.

Looks like null `jsonb` columns cause the problem. If you create a table like below you can see that the following code fails. Is this expected?

```sql
CREATE TABLE test (
	a int4 NULL,
	b int4 NULL,
	c jsonb NULL
);

INSERT INTO test (a, b, c) VALUES (1, null, null);
```

```go
package main

import (
	"context"
	"log"

	"github.com/georgysavva/scany/v2/pgxscan"
	"github.com/jackc/pgx/v5"
)

func main() {
	var rows []map[string]interface{}
	conn, _ := pgx.Connect(context.Background(), , ts.PGURL().String())
	
	// this will fail with can't scan into dest[0]: cannot scan NULL into *interface {}
	err := pgxscan.Select(context.Background(), conn, &rows, `SELECT c from test`) 
	
	// this works
	// err = pgxscan.Select(context.Background(), conn, &rows, `SELECT a,b from test`)
	
	if err != nil {
		panic(err)
	}

	log.Printf("%+v", rows)
}
```
2023-03-25 10:22:11 -05:00
Sergej Brazdeikis 9ae852eb58 Fix typo in error message `uint32` -> `uint16` 2023-03-11 15:34:08 -06:00
Jack Christensen 38e09bda4c Fix *wrapSliceEncodePlan[T].Encode
It should pass a FlatArray[T] to the next step instead of a
anySliceArrayReflect. By using a anySliceArrayReflect, an encode of
[]github.com/google/uuid.UUID followed by []string into a PostgreSQL
uuid[] would crash. This was caused by a EncodePlan cache collision
where the second encoding used part of the cached plan of the first.

In proper usage a cache collision shouldn't be able to occur. If this
assertion proves incorrect it will be necessary to add an optional
interface to ScanPlan and EncodePlan that marks the plan as ineligable
for caching. But I have been unable to construct a failing case, and
given that ScanPlans have been cached for quite some time now without
incident I do not think it is possible. This issue only occurred due to
the bug in *wrapSliceEncodePlan[T].Encode.

https://github.com/jackc/pgx/issues/1502
2023-02-21 21:04:30 -06:00
Jack Christensen 42d327f660 Add text format jsonpath support 2023-02-14 19:52:47 -06:00
Jack Christensen a6ace8969b Fix: Prefer sql.Scanner before TryWrapScanPlanFuncs
This was already the case when the data type was unknown but should also
be the case when it is known.
2023-02-14 09:03:41 -06:00
Jack Christensen 2100a64dbe Fix broken benchmarks 2023-02-10 20:26:18 -06:00
Jack Christensen 4484831550 Prefer binary format for arrays
This improves performance decoding text[].
2023-02-10 20:21:25 -06:00
Jack Christensen 1f43e2e490 Fix text format array decoding with a string of "NULL"
It was incorrectly being treated as NULL instead of 'NULL'.

fixes https://github.com/jackc/pgx/issues/1494
2023-02-10 19:59:03 -06:00
Felix Röhrich a47e836471 make TestPointerPointerStructScan easier to read 2023-02-10 19:06:20 -06:00
Felix Röhrich 5cd8468b99 replace erroneous reflect.New with reflect.Zero in TryWrapStructScanPlan 2023-02-10 19:06:20 -06:00
Jack Christensen c875abea84 Fix encode []any to array
https://github.com/jackc/pgx/issues/1488
2023-02-04 07:28:52 -06:00
Yumin Xia 766d2bba4f add UnmarshalJSON for pgtype Numeric 2023-01-30 21:33:02 -06:00
Jack Christensen 42a47194a2 Memoize encode plans
This significantly reduces memory allocations in paths that repeatedly
encode the same type of values such as CopyFrom.

https://github.com/jackc/pgx/issues/1481
2023-01-27 20:19:06 -06:00
Alexey Palazhchenko f839d501a7 Apply `gofmt -s`
And add CI check for that.
2023-01-24 07:55:00 -06:00
Jack Christensen e48e7a7189 Fix scanning json column into **string
refs https://github.com/jackc/pgx/issues/1470
2023-01-20 18:38:11 -06:00
Mark Chambers c46d792c93 Numeric numberTextBytes() workaround...
This seems a bit of a hack. It fixes the problems demonstrated in my previous commit.

Maybe there's a cleaner way?

Associated: https://github.com/jackc/pgx/issues/1426
2023-01-14 08:42:42 -06:00
Mark Chambers 37c6f97b11 pgtype.Numeric numberTextBytes() encoding bug
Demonstrate the problem with the tests:

...for negative decimal values e.g. -0.01

This causes errors when encoding to JSON:

    "json: error calling MarshalJSON for type pgtype.Numeric"

It also causes scan failures of sql.NullFloat64:

    "converting driver.Value type string ("0.-1") to a float64"

As reported here: https://github.com/jackc/pgx/issues/1426
2023-01-14 08:42:42 -06:00
Jack Christensen d4fcd4a897 Support sql.Scanner on renamed base type
https://github.com/jackc/pgtype/issues/197
2022-12-23 14:22:59 -06:00
Jack Christensen e66ad1bcec Fix encode to json ignoring driver.Valuer
https://github.com/jackc/pgx/issues/1430
2022-12-23 13:44:09 -06:00
Jack Christensen 456a242f5c Unregistered OIDs are handled the same as unknown OIDs
This improves handling of unregistered types. In general, they should
"just work". But there are performance benefits gained and some edge
cases avoided by registering types. Updated documentation to mention
this.

https://github.com/jackc/pgx/issues/1445
2022-12-23 13:14:56 -06:00
Jack Christensen d737852654 Fix: driver.Value representation of bytea should be []byte not string
https://github.com/jackc/pgx/issues/1445
2022-12-21 17:54:42 -06:00
Jack Christensen f42af35884 Add support for single dimensional arrays
https://github.com/jackc/pgx/issues/1442
2022-12-20 20:12:12 -06:00
Mark Chambers 1ce3e0384a pgtype Int fix minimum error message.
Previously on the minimum condition the error would be:

  "is greater than maximum"

Also add encoding/json import into the .erb template as the import was
missing after running rake generate.
2022-12-17 09:10:02 -06:00
Jack Christensen 279c3c0a20 Fix: json values work with sql.Scanner
https://github.com/jackc/pgx/issues/1418
2022-12-06 19:44:55 -06:00
Jack Christensen f0a73424b1 Fix: Scan uint and uint64 ScanNumeric
fixes https://github.com/jackc/pgx/issues/1414
2022-12-05 20:34:46 -06:00
Jack Christensen b265fedd75 Correct error message 2022-11-12 07:06:54 -06:00
Jack Christensen 871f14e43b Fix text decoding of dates with 5 digit years 2022-11-12 07:01:11 -06:00
Jack Christensen 071d1c9467 DateCodec.DecodeValue can return pgtype.InfinityModifier
Previously, an infinite value was returned as a string. Other types
that can be infinite such as Timestamptz return a
pgtype.InfinityModifier. This change brings them into alignment.
2022-11-12 06:27:41 -06:00
Jack Christensen 29109487ec DateCodec.DecodeDatabaseSQLValue returns time.Time when possible
Previously it returned a string. However, this was an unintended
behavior change from pgx v4.

89f69aaea9 (commitcomment-89173737)
2022-11-12 06:21:48 -06:00
Jack Christensen daf570c752 Date text encoding pads year with 0 for at least 4 digits
e.g. 0007-01-02 instead of 7-01-02

89f69aaea9 (commitcomment-89173737)
2022-11-12 06:14:04 -06:00
Jack Christensen 6fabd8f5b1 Fix encoding uint64 larger than math.MaxInt64 into numeric
fixes https://github.com/jackc/pgx/issues/1357
2022-10-29 08:47:12 -05:00
Jack Christensen 48b4807b33 Fix some reflect Kind checks to first check for nil
fixes https://github.com/jackc/pgx/issues/1335
2022-10-22 08:57:49 -05:00
Jack Christensen f2e7c8144d reflect.TypeOf can return nil. Check before using
https://github.com/jackc/pgx/issues/1331
2022-10-12 20:03:51 -05:00
Jack Christensen aff180b192 Remove dead code 2022-10-12 19:58:06 -05:00
Jack Christensen a581124dea Encode with driver.Valuer after trying TryWrapEncodePlanFuncs
However, all builtin TryWrapEncodePlanFuncs check for driver.Valuer and
skip themselves if it is found.
2022-10-12 19:52:57 -05:00
Jack Christensen c4407fb36e Prevent infinite loop for driver.Valuer / Codec edge case
A `driver.Valuer()` results in a `string` that the `Codec` for the
PostgreSQL type doesn't know how to handle. That string is scanned into
whatever the default type for that `Codec` is. That new value is
encoded. If the new value is the same type as the original type than an
infinite loop occured. Check that the types are different.

https://github.com/jackc/pgx/issues/1331
2022-10-12 19:46:15 -05:00
Jack Christensen af0b896290 Allow scanning null even if PG and Go types are incompatible
refs https://github.com/jackc/pgx/issues/1326
2022-10-08 09:10:43 -05:00
Jack Christensen 5655f9d593 Fix scan to pointer to pointer to renamed type
refs https://github.com/jackc/pgx/issues/1326
2022-10-08 08:10:40 -05:00
Jack Christensen 222e3b37bc Prefer driver.Value over wrap plans when encoding
This is tricky due to driver.Valuer returning any. For example, we can
plan for fmt.Stringer because it always returns a string.

Because of this driver.Valuer was always handled as the last option. But
with pgx v5 now having the ability to find underlying types like a
string and supporting fmt.Stringer it meant that driver.Valuer was
often not getting called because something else was found first.

This change tries driver.Valuer immediately after the initial PlanScan
for the Codec. So a type that directly implements a pgx interface should
be used, but driver.Valuer will be prefered before all the attempts to
handle renamed types, pointer deferencing, etc.

fixes https://github.com/jackc/pgx/issues/1319
fixes https://github.com/jackc/pgx/issues/1311
2022-10-01 12:20:23 -05:00
Jack Christensen 89f69aaea9 Date text encoding includes leading zero for month and day
e.g. 2000-01-01 instead of 2000-1-1. PostgreSQL accepted it without
zeroes but our text decoder didn't. This caused a problem when we needed
to take a value and encode to text so something else could parse it as
if it had come from the PostgreSQL server in text format. e.g.
database/sql compatibility.
2022-10-01 10:41:40 -05:00
Jack Christensen 335c8621ff Fix sqlScannerWrapper NULL handling
https://github.com/jackc/pgx/issues/1312
2022-09-24 10:30:12 -05:00
Jack Christensen ac9d4f4d96 Encode text for Lseg includes [ and ]
https://github.com/jackc/pgtype/issues/187
2022-09-24 10:30:12 -05:00
yogipristiawan 72e4b88e56 feat: add marshalJSON for float8 type 2022-09-24 10:00:40 -05:00
Jack Christensen f8d088cfb6 Fix JSON scan not completely overwriting destination
See https://github.com/jackc/pgtype/pull/185 for original report in
pgx v4 / pgtype.
2022-09-02 18:37:02 -05:00
Jack Christensen fe3a4f3150 Standardize casing for NULL in error messages 2022-08-22 21:01:18 -05:00
Jack Christensen 2e73d1e8ee Improve error message when failing to scan a NULL::json 2022-08-22 20:56:36 -05:00
Jack Christensen 0d5d8e0137 Fallback to other format when encoding query arguments
The preferred format may not be possible for certain arguments. For
example, the preferred format for numeric is binary. But if
shopspring/decimal is being used without jackc/pgx-shopspring-decimal
then it will use the database/sql/driver.Valuer interface. This will
return a string. That string should be sent in the text format.

A similar case occurs when encoding a []string into a non-text
PostgreSQL array such as uuid[].
2022-08-22 20:26:38 -05:00
Jack Christensen 02d9a5acd8 Fix naming of some tests 2022-08-13 08:41:06 -05:00
Jack Christensen 8256ab147f Add build tag to skip default PG type registration
https://github.com/jackc/pgx/issues/1273#issuecomment-1207338136
2022-08-13 08:09:44 -05:00
Jack Christensen c3258b7f52 Fix scan pointer to pointer to nil slice
https://github.com/jackc/pgx/issues/1263
2022-07-30 09:10:50 -05:00
Jack Christensen 2da0a11c52 Skip some examples on CockroachDB 2022-07-23 10:52:35 -05:00
Jack Christensen ce378b4d9c Skip example on Cockroach DB 2022-07-23 10:21:01 -05:00
Jack Christensen 5cee04a026 Add child records docs and examples 2022-07-23 10:11:13 -05:00