nextjournal / clerk

⚡️ Moldable Live Programming for Clojure
https://clerk.vision
ISC License
1.74k stars 75 forks source link

honey.sql format crashes in clerk notebook #646

Closed borkdude closed 2 months ago

borkdude commented 2 months ago

The following form:

(honey.sql/format {:select [:AlbumId :Bytes :Name :TrackID :UnitPrice]
             :from :tracks})

crashes in a Clerk notebook (see stacktrace below).

version of honeysql used:

com.github.seancorfield/honeysql {:mvn/version "2.6.1126"}

When I eval the form as passed to eval+cache I can reproduce the same exception. When I manually craft the form, I can't reproduce the exception. Perhaps something related to metadata.

Stack trace ``` 1. Caused by clojure.lang.ExceptionInfo Execution error (ClassCastException) at honey.sql/sql-kw (sql.cljc:342). class java.lang.Integer cannot be cast to class clojure.lang.Named (java.lang.Integer is in module java.base of loader 'bootstrap'; clojure.lang.Named is in unnamed module of loader 'app') {:clojure.error/cause "class java.lang.Integer cannot be cast to class clojure.lang.Named (java.lang.Integer is in module java.base of loader 'bootstrap'; clojure.lang.Named is in unnamed module of loader 'app')", :clojure.error/phase :execution, :clojure.error/symbol honey.sql/sql-kw, :clojure.error/line 342, :clojure.error/class java.lang.ClassCastException, :clojure.error/source "sql.cljc", :line 16, :col 1, :clojure.core/eval-file "/Users/borkdude/dev/clerk-table-stats/notebooks/table_stats.clj", :form (honey.sql/format {:select [:AlbumId :Bytes :Name :TrackID :UnitPrice], :from :tracks})} REPL: 158 nextjournal.clerk.eval/eval+cache! REPL: 124 nextjournal.clerk.eval/eval+cache! eval.clj: 198 nextjournal.clerk.eval/read+eval-cached eval.clj: 168 nextjournal.clerk.eval/read+eval-cached eval.clj: 238 nextjournal.clerk.eval/eval-analyzed-doc/fn PersistentVector.java: 418 clojure.lang.PersistentVector/reduce core.clj: 6953 clojure.core/reduce core.clj: 6936 clojure.core/reduce eval.clj: 233 nextjournal.clerk.eval/eval-analyzed-doc eval.clj: 230 nextjournal.clerk.eval/eval-analyzed-doc eval.clj: 257 nextjournal.clerk.eval/+eval-results eval.clj: 250 nextjournal.clerk.eval/+eval-results clerk.clj: 72 nextjournal.clerk/show!/fn/fn clerk.clj: 71 nextjournal.clerk/show!/fn clerk.clj: 71 nextjournal.clerk/show! clerk.clj: 23 nextjournal.clerk/show! clerk.clj: 36 nextjournal.clerk/show! clerk.clj: 23 nextjournal.clerk/show! ```
borkdude commented 2 months ago

It seems honey.sql stumbles over the metadata which seems to be added by rewrite-clj?

"^{:end-row 8, :end-column 54, :column 1, :line 17, :col 1, :clojure.core/eval-file \"/Users/borkdude/dev/clerk-table-stats/notebooks/table_stats.clj\", :end-line 24, :end-col 54, :row 1} (^{:row 1, :col 2, :end-row 1, :end-col 5} def ^{:row 1, :col 6, :end-row 1, :end-col 19} query-results ^{:row 2, :col 3, :end-row 8, :end-col 53} (^{:row 2, :col 4, :end-row 2, :end-col 7} let ^{:row 2, :col 8, :end-row 3, :end-col 74} [^{:row 2, :col 9, :end-row 2, :end-col 16} _run-at #inst \"2021-05-20T08:28:29.445-00:00\" ^{:row 3, :col 9, :end-row 3, :end-col 11} ds ^{:row 3, :col 12, :end-row 3, :end-col 73} (^{:row 3, :col 13, :end-row 3, :end-col 32} jdbc/get-datasource ^{:row 3, :col 33, :end-row 3, :end-col 72} {:dbtype \"sqlite\", :dbname \"chinook.db\"})] ^{:row 4, :col 5, :end-row 8, :end-col 52} (^{:row 4, :col 6, :end-row 4, :end-col 15} with-open ^{:row 4, :col 16, :end-row 4, :end-col 47} [^{:row 4, :col 17, :end-row 4, :end-col 21} conn ^{:row 4, :col 22, :end-row 4, :end-col 46} (^{:row 4, :col 23, :end-row 4, :end-col 42} jdbc/get-connection ^{:row 4, :col 43, :end-row 4, :end-col 45} ds)] ^{:row 5, :col 7, :end-row 8, :end-col 51} (^{:row 5, :col 8, :end-row 5, :end-col 21} jdbc/execute! ^{:row 5, :col 22, :end-row 5, :end-col 26} conn ^{:row 7, :col 22, :end-row 8, :end-col 50} (^{:row 7, :col 23, :end-row 7, :end-col 33} sql/format ^{:row 7, :col 34, :end-row 8, :end-col 49} {:select ^{:row 7, :col 43, :end-row 7, :end-col 86} [:AlbumId :Bytes :Name :TrackID :UnitPrice], :from :tracks})))))"
borkdude commented 2 months ago

Ah yes, got a repro here:

(honey.sql/format
 {:select ^{:row 7, :col 43, :end-row 7, :end-col 86} [:AlbumId :Bytes :Name :TrackID :UnitPrice]
  :from :tracks})
borkdude commented 2 months ago

There's probably a workaround in honey.sql to ignore certain metadata (via options) but I think it would be better if clerk evaluated values exactly how the user wrote it, without having to work around it. What do yo think @mk? Depending on your answer I could take a look to strip the rewrite-clj metadata.

mk commented 2 months ago

I think it would be better if clerk evaluated values exactly how the user wrote it, without having to work around it

I agree.

I could take a look to strip the rewrite-clj metadata

That would be great, please do.