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

Array distinct unnest ::TEXT[] #53

Closed
jim108dev opened this issue Feb 10, 2021 · 3 comments
Closed

Array distinct unnest ::TEXT[] #53

jim108dev opened this issue Feb 10, 2021 · 3 comments

Comments

@jim108dev
Copy link

jim108dev commented Feb 10, 2021

Hi!
I want all tags (array) in every article.

SELECT ARRAY(SELECT DISTINCT unnest(tag_list) FROM article ORDER BY 1)::TEXT[] AS tags

I have tried a bunch of stuff which always results in double FROM and ::TEXT[] not in the right place. This non-selda-solution is the best I came up with.

CREATE VIEW TAG AS
SELECT
  ARRAY ( SELECT DISTINCT
      unnest(tag_list)
    FROM
      article
    ORDER BY
      1)::text[];
type TagTable
  = Table
      ( array :: Array Tag
      )

tagTable :: TagTable
tagTable = Table { name: "tag" }

findTags :: Pool -> Aff (Array Tag)
findTags pool =
  withConnection pool
    ( \conn ->
        runSelda conn
          $ do
              logQuery $ selectFrom tagTable pure
              query $ selectFrom tagTable pure
    )
    >>= validateTags

If you know something better, let me know.

@Kamirus
Copy link
Owner

Kamirus commented Feb 11, 2021

What about breaking it down into several operations:

SELECT ARRAY(SELECT DISTINCT unnest(tag_list) FROM article ORDER BY 1)::TEXT[] AS tags -- 0. original query
SELECT                                                                         AS tags -- 1. selectValues
       ARRAY(                                                        )::TEXT[]         -- 2. toTextArray
             SELECT DISTINCT unnest(tag_list) FROM article ORDER BY 1                  -- 3. use: distict, orderBy
  1. selectValues SELECT values without FROM clause #46
    I'll provide this function after we merge scope-as-backend, for now, consider the workaround:
  • create a dummy table singleton that contains just one value (e.g. use generate_series 1 1)
  • selectValues r = selectFrom singleton \_ -> r
  1. toTextArray should take a query with just one column (of type String, right?) and wrap it with ARRAY( ... ):TEXT[] treating it as a DB expression Col s String
toTextArray
    s
  . ( z. FullQuery z { xCol z String })
   Col s String
toTextArray subQ = Col $ Any do
  q ← showQuery subQ
  pure $ "ARRAY(" <> q <>")::TEXT[]"
  1. SELECT DISTINCT unnest(tag_list) FROM article ORDER BY 1
    this query should be expressible in selda, distinct and orderBy are available, unnest(_) implement like a custom pg function from the guide

distinct
s r
. FullQuery s { | r }
FullQuery s { | r }

orderBy s a. Order Col s a Query s Unit


I have not tested it myself, but it would be sth that I'd try first.
I hope it helps! 😄

@jim108dev
Copy link
Author

I actually tried that before resulting in

SELECT
  ARRAY (sub_q0.tagList)::text[] AS tagList
FROM ( SELECT DISTINCT
    UNNEST(article_0.tag_list) AS tagList
  FROM
    article article_0
  ORDER BY
    1 ASC) sub_q0;

ERROR: syntax error at or near "sub_q0"
LINE 1: SELECT ARRAY (sub_q0.tagList)::TEXT[] AS tagList

@Kamirus
Copy link
Owner

Kamirus commented Feb 11, 2021

I'm sorry I meant sth like this:

-- SELECT DISTINCT unnest(tag_list) FROM article ORDER BY 1
selectDistinctTagLists = distinct $ selectFrom article \a -> do
  let tagList = unnest a.tag_list
  orderBy asc tagList -- or `orderBy asc (lit 1)` should work
  pure { tagList }

singleton = generate_series 1 1

selectFrom singleton \_ ->
  pure
    { tags: toTextArray selectDistinctTagLists
    }

So that is should be
FROM generate_series(1,1) because we do selectFrom singleton
and the nested query should be between ARRAY ( and )::text[]
and no subquery alias should appear

AS tags appears because I named it so after the pure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants