Coder Social home page Coder Social logo

Comments (4)

lambdahands avatar lambdahands commented on June 3, 2024 4

I'm running into this issue as well. I thought I might be able to solve this by using the :modifiers key, but the current behavior simply uppercases the keys within the value, which is embedded in the format-clause implementation of :select. I chose to create a new implementation :selectd with added behavior to support DISTINCT ON (...):

(require '[honeysql.format :as f])
(require '[clojure.string :as string])

(defmethod f/format-clause :selectd [[_ fields] sql-map]
  (str "SELECT "
       (when (:distinct-on sql-map)
         (str "DISTINCT ON ("
              (f/comma-join (map name (:distinct-on sql-map)))
              ") "))
       (f/comma-join (map f/to-sql fields))))

(f/register-clause! :selectd 50)

It's used like so:

{:selectd     [:foo]
 :from        [:bar]
 :distinct-on [:baz]

This workaround has its limits as we lose the composability of HoneySQL with :selectd (the helper methods like merge-select no longer work here).

I think there are two ways to solve this:

  1. The :select clause could be augmented to handle a :distinct-on top-level clause, which I believe would violate the general design decision of the library to be database agnostic (DISTINCT ON is specific to Postgres).
  2. Open up the :modifiers to multi-methods, and allow users to extend the space between SELECT and its clauses as needed.

The second option seems intuitive to me. Something like this might work:

{:select    [:foo]
 :from      [:bar]
 :modifiers [[:distinct-on :baz]]}

from honeysql.

seancorfield avatar seancorfield commented on June 3, 2024

It seems like this should be pushed to honeysql-postgres?

from honeysql.

piranha avatar piranha commented on June 3, 2024

I think so, it's just unclear if it's possible to make it work, because in our codebase we're doing this: {:select [(sql/raw "distinct on (id) id") :other_field]}.

from honeysql.

seancorfield avatar seancorfield commented on June 3, 2024

Given HoneySQL's position of trying to be vendor-neutral and the presence of reasonable approaches to make this work (without changing HoneySQL), I'm closing this out.

from honeysql.

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.