Code Monkey home page Code Monkey logo

Comments (3)

iamed2 avatar iamed2 commented on June 26, 2024 1

Only columns with type numeric/decimal will appear this way; float4/real and float8/double precision will not.

The numeric type is an arbitrary-precision number that cannot be represented fully in a Float64. However, Decimals.Decimal is not a great type, and a replacement (or improvements to that package) would be welcome.

You can either force the type in Julia:

julia> execute(conn, sql; type_map=Dict(:numeric => Float64)).column_types
3-element Vector{Type}:
 Float64
 Float64
 Float64

or in SQL:

julia> sql="""
       select avg(1.0)::float8 as average, 1.0::float8 as float_number, 0.0000000001::float8 as really_small_number;
       """
"select avg(1.0)::float8 as average, 1.0::float8 as float_number, 0.0000000001::float8 as really_small_number;\n"

julia> execute(conn, sql).column_types
3-element Vector{Type}:
 Float64
 Float64
 Float64

from libpq.jl.

svilupp avatar svilupp commented on June 26, 2024 1

Thank you for the explanation, @iamed2!

I've been eyeing type_map in the docs to force the conversion, so the example is much appreciated.

I'll close the issue as LibPQ works as expected.


Some additional background:
I've used a standard pattern of download SQL -> DataFrame -> save to .arrow for persistence/version control.
Upon opening, my saved data was rubbish.
It turns out that Arrow.write(...;compress=:lz4) didn't know what to do with Decimals type and saved it however without any warning (an issue opened here).

As for the python side, you're right.
I've just checked that both psycopg2 and sqlalchemy (using the former as the driver) return decimals.
The challenge is that I (and perhaps others) use Pandas to download data (pd.read_sql_query), which forces the Float type, so I will have never seen the decimal type come out of a SQL / I didn't know it's the expected behaviour.

Good learning!

from libpq.jl.

iamed2 avatar iamed2 commented on June 26, 2024

psycopg2 in Python does the same thing, using the decimal module in Python's standard library.

https://www.psycopg.org/docs/faq.html#faq-float

from libpq.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.