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

Formatting unhashable objects #509

Closed
jcf opened this issue Oct 16, 2023 · 1 comment
Closed

Formatting unhashable objects #509

jcf opened this issue Oct 16, 2023 · 1 comment
Assignees
Labels
bug It's broken, I'll fix it!

Comments

@jcf
Copy link

jcf commented Oct 16, 2023

I have an unusual setup where I'm trying to transact numpy ndarrays returned by libpython-clj.

I've added a SettableParameter to next.jdbc to serialise these fancy lists so I can pass them to pgvector, but ran into a problem with HoneySQL where the inability to hash an ndarray broke format-values.

(ns example
  (:require
   [clojure.string :as str]
   [libpython-clj2.python :as py]
   [libpython-clj2.python.protocols]
   [libpython-clj2.require :refer [require-python]]
   [next.jdbc.prepare :as prepare])
  (:import
   (org.postgresql.util PGobject)))

(require-python
 '[numpy :as np])

(extend-protocol prepare/SettableParameter
  libpython_clj2.python.protocols.PPythonType
  (set-parameter [v
                  ^java.sql.PreparedStatement s
                  ^long i]
    (case (py/python-type v)
      :ndarray
      (.setObject s i (doto (PGobject.)
                        (.setType "vector")
                        (.setValue (str "[" (str/join "," (py/py. v tolist)) "]"))))

      ;; Default to setting the object as is.
      (.setObject s i v))))

https://clojurians.zulipchat.com/#narrow/stream/151924-data-science/topic/next.2Ejdbc.20and.20libpython-clj

@seancorfield seancorfield self-assigned this Oct 16, 2023
@seancorfield seancorfield added the bug It's broken, I'll fix it! label Oct 16, 2023
@jcf
Copy link
Author

jcf commented Oct 16, 2023

Massive thank you to @joinr for helping work out the SettableParameter extension via PPythonType, too. 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It's broken, I'll fix it!
Projects
None yet
Development

No branches or pull requests

2 participants