这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@rakeshkky
Copy link
Member

@rakeshkky rakeshkky commented Sep 21, 2019

Description

Computed fields are extra fields added to a table whose value is computed from other columns. It facilitates users to define an additional field to an existing table whose return value is computed via an SQL function.

How it is different from Postgres generated columns

Postgres, from version 12, is introducing Generated Columns.
Postgres' generated columns come with its own limitations. Hasura's computed fields are defined via an SQL function, which allows users to define any complex business logic in a function. Generated columns will go together with computed fields where Hasura treats generated columns as normal Postgres columns.

Refer to docs here

Refer to API reference here

Affected components

  • Server
  • Docs
  • Tests

Related Issues

close #1387

Solution and Design

This PR is a significant change which touches code base for metadata, GraphQL schema generation and resolving and SQL generation.

Metadata

  • Added two new query types to manage computed fields. See module Hasura.RQL.DDL.ComputedField
    1. add_computed_field
    2. drop_computed_field
  • Detect changes to functions associated with computed fields in run_sql query
  • Add computed fields in create_select_permission

GraphQL

  • Either is being used before to represent whether a field is a column or a relationship. Now, those types are replaced with proper sum types to represent computed fields too.
  • Resolve the computed field

SQL generation

Modules:-

  • Hasura.RQL.DML.Select.Internal
  • Hasura.RQL.DML.Select.Types

Changes:-

  • The BaseNode now contains table computed fields as sub-nodes.
  • TableFrom changed to SelectFromG to include a selection from rows returned by a function execution
  • While resolving a BaseNode to SQL Select keep track of parent node prefix to execute the computed field function with the parent table row identifier.

In module Hasura.SQL.DML add FunctionExp to SQLExp AST.

The following function is added to the author table as a computed field with name
computed_articles

CREATE FUNCTION fetch_computed_articles(author_arg author, search text)
RETURNS SETOF article AS $$
    SELECT *
    FROM article
    WHERE
      (title ilike ('%' || search || '%')
      OR content ilike ('%' || search || '%'))
      AND article.author_id = author_arg.id
$$ LANGUAGE sql STABLE;

Querying data from author table along with computed field computed_articles

query {
  author{
    id
    name
    computed_articles(args:{search:"search_text"}){
      id
      title
    }
  }
}

Generates the SQL below.

SELECT
  coalesce(json_agg("root"), '[]') AS "root"
FROM
  (
    SELECT
      row_to_json(
        (
          SELECT
            "_5_e"
          FROM
            (
              SELECT
                "_0_root.base"."id" AS "id",
                "_0_root.base"."name" AS "name",
                "_4_root.cf.root.computed_articles"."computed_articles" AS "computed_articles"
            ) AS "_5_e"
        )
      ) AS "root"
    FROM
      (
        SELECT
          *
        FROM
          "public"."author"
        WHERE
          ('true')
      ) AS "_0_root.base"
      LEFT OUTER JOIN LATERAL (
        SELECT
          coalesce(json_agg("computed_articles"), '[]') AS "computed_articles"
        FROM
          (
            SELECT
              row_to_json(
                (
                  SELECT
                    "_2_e"
                  FROM
                    (
                      SELECT
                        "_1_root.cf.root.computed_articles.base"."id" AS "id",
                        "_1_root.cf.root.computed_articles.base"."title" AS "title"
                    ) AS "_2_e"
                )
              ) AS "computed_articles"
            FROM
              (
                SELECT
                  *
                FROM
                  "public"."fetch_computed_articles"(author_arg => "_0_root.base", search => 'search_text')
                WHERE
                  (("_0_root.base"."id") = ("author_id"))
              ) AS "_1_root.cf.root.computed_articles.base"
          ) AS "_3_root.cf.root.computed_articles"
      ) AS "_4_root.cf.root.computed_articles" ON ('true')
  ) AS "_6_root"

Steps to test and verify

Follow any test case or example

Limitations, known bugs & workarounds

  • Aggregations are not available on computed fields which returns setof table
  • Scalar computed fields are not allowed in where clause (bool exps)
  • The SQL selection for computed fields need to be optimised (merge sub-queries for same computed fields)

@rakeshkky rakeshkky added c/server Related to server c/docs Related to docs labels Sep 21, 2019
@rakeshkky rakeshkky self-assigned this Sep 21, 2019
@netlify
Copy link

netlify bot commented Sep 21, 2019

Deploy preview for hasura-docs ready!

Built with commit 2fda8a8

https://deploy-preview-2917--hasura-docs.netlify.com

@hasura-bot
Copy link
Contributor

Review app for commit 28465c8 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-28465c83

@hasura-bot
Copy link
Contributor

Review app for commit 2ce80d8 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-2ce80d89

@rakeshkky rakeshkky changed the title support computed columns returning scalars and set of tables (close #1378) support computed columns returning scalars or set of tables (close #1378) Sep 21, 2019
@hasura-bot
Copy link
Contributor

Review app for commit 8831cb5 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-8831cb51

@hasura-bot
Copy link
Contributor

Review app for commit 7f187e5 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-7f187e5b

@hasura-bot
Copy link
Contributor

Review app for commit 32ce5a9 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-32ce5a99

@rakeshkky rakeshkky changed the title support computed columns returning scalars or set of tables (close #1378) support computed columns returning scalars or set of tables (close #1387) Sep 27, 2019
@hasura-bot
Copy link
Contributor

Review app for commit 465eb82 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-465eb82d

@hasura-bot
Copy link
Contributor

Review app for commit 7316063 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-7316063d

@hasura-bot
Copy link
Contributor

Review app for commit 392c26d deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-392c26d3

@hasura-bot
Copy link
Contributor

Review app for commit 31bb10d deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-31bb10d9

@rakeshkky rakeshkky force-pushed the comp-col-new branch 2 times, most recently from 1f283e0 to 070c551 Compare September 30, 2019 13:54
@hasura-bot
Copy link
Contributor

Review app for commit 070c551 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-070c551a

@hasura-bot
Copy link
Contributor

Review app for commit 6e3646c deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-6e3646cf

@hasura-bot
Copy link
Contributor

Review app for commit 0d54b01 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-0d54b012

@hasura-bot
Copy link
Contributor

Review app for commit 84c0a8b deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-84c0a8b3

@rakeshkky rakeshkky marked this pull request as ready for review October 1, 2019 12:25
@rakeshkky rakeshkky requested a review from lexi-lambda as a code owner October 1, 2019 12:25
@wawhal wawhal requested review from wawhal and removed request for wawhal October 16, 2019 07:49
@rikinsk rikinsk dismissed stale reviews from marionschleifer and lexi-lambda via 1c90172 October 16, 2019 11:10
rikinsk
rikinsk previously approved these changes Oct 16, 2019
@hasura-bot
Copy link
Contributor

Review app for commit 1c90172 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-1c901725

@rikinsk
Copy link
Member

rikinsk commented Oct 16, 2019

Just went through the docs and seems like there is a change in the select permissions api. Won't this need changes in the console for adding select permissions for computed fields?

edit: realised a whole lot of console changes will be needed. assuming a separate PR will be handling that

@hasura-bot
Copy link
Contributor

Review app for commit c2401a9 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-c2401a9e

@rikinsk rikinsk self-requested a review October 16, 2019 14:25
Resolve Conflicts:
	server/src-lib/Hasura/GraphQL/Resolve/Types.hs
	server/tests-py/test_graphql_queries.py
@hasura-bot
Copy link
Contributor

Review app for commit 341fc6d deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-341fc6d8

@hasura-bot
Copy link
Contributor

Review app for commit 2fda8a8 deployed to Heroku: https://hge-ci-pull-2917.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2917-2fda8a86

@shahidhk shahidhk merged commit d8d21c1 into hasura:master Oct 18, 2019
@hasura-bot
Copy link
Contributor

Review app https://hge-ci-pull-2917.herokuapp.com is deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c/docs Related to docs c/server Related to server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support computed columns in queries

6 participants