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

Add target_table_info in tables.list_joinable's response #3718

Merged
merged 8 commits into from
Aug 3, 2024

Conversation

Anish9901
Copy link
Member

Fixes #3713

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@Anish9901 Anish9901 added the pr-status: review A PR awaiting review label Jul 30, 2024
Copy link
Contributor

@seancolsen seancolsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh — I see why you requested a different structure. Previously we were returning a JSON object with stringified Django column ids as keys. Now columns require two id values to be uniquely identified, so the old structure is problematic. Interesting!

I'm okay with nesting them.

But in running the code in this PR, I'm seeing some problems.

I'm running this request where 698589 is the oid of the "Books" table in our library management sample data.

{
  "jsonrpc": "2.0",
  "method": "tables.list_joinable",
  "id": 0,
  "params": {
    "database_id": 1,
    "table_oid": 698589,
    "max_depth": 1
  }
}

Here are some changes I'd like to see:

  • Ensure that the joinable_tables and target_table_info arrays don't contain duplicates. There's something fishy going on somewhere. I have a hunch it's some kind of SQL join or aggregation problem. I'm expecting to see three results in each of these arrays. But instead I see 9.

  • Restructure the target_table_info value. Currently it's an array of objects. An each object contains an entry which has a key of the table OID and a value of the table name. This would be very bad JSON schema design, so I don't imagine you did this intentionally. It looks like a bug. Instead of an array of objects, I'd like to see a single object where each entry has the table OID as the key and then an object as the value.

    You have:

    {
      "target_table_info": [
        {
          "698581": "Authors",
          "columns": []
        }
      ]
    }

    I want:

    {
      "target_table_info": {
        "698581": {
          "name": "Authors",
          "columns": []
        }
      }
    }

@Anish9901
Copy link
Member Author

Anish9901 commented Jul 31, 2024

I think I've addressed your feedback @seancolsen, Here is the response for the "Books" table with depth=1.

Response
{
  "joinable_tables": [
    {
      "base": 2254321,
      "depth": 1,
      "target": 2254313,
      "fkey_path": [
        [2254389, false]
      ],
      "join_path": [
        [
          [2254321, 10],
          [2254313,  1]
        ]
      ],
      "multiple_results": false
    },
    {
      "base": 2254321,
      "depth": 1,
      "target": 2254358,
      "fkey_path": [
        [2254394, false]
      ],
      "join_path": [
        [
          [2254321, 11],
          [2254358,  1]
        ]
      ],
      "multiple_results": false
    },
    {
      "base": 2254321,
      "depth": 1,
      "target": 2254334,
      "fkey_path": [
        [2254399, true]
      ],
      "join_path": [
        [
          [2254321, 1],
          [2254334, 5]
        ]
      ],
      "multiple_results": true
    }
  ],
  "target_table_info": {
    "2254313": {
      "name": "Authors",
      "columns": [
        {"name": "id"        , "type": "integer", "attnum": 1},
        {"name": "Last Name" , "type": "text"   , "attnum": 3},
        {"name": "First Name", "type": "text"   , "attnum": 2}
      ]
    },
    "2254334": {
      "name": "Items",
      "columns": [
        {"name": "id"              , "type": "integer", "attnum": 1},
        {"name": "Book"            , "type": "integer", "attnum": 5},
        {"name": "Acquisition Date", "type": "date"   , "attnum": 3},
        {"name": "Barcode"         , "type": "text"   , "attnum": 2}
      ]
    },
    "2254358": {
      "name": "Publishers",
      "columns": [
        {"name": "id"  , "type": "integer", "attnum": 1},
        {"name": "Name", "type": "text"   , "attnum": 2}
      ]
    }
  }
}

Do you have an opinion on whether the columns should be a key-value pair of attnum and {name, type} or are you fine with the approach of the above response where I've nested the attnum instead?

@seancolsen
Copy link
Contributor

Good question, @Anish9901. Can you please put the columns data in an object too? Stringified attnums as keys.

@Anish9901
Copy link
Member Author

Can you please put the columns data in an object too? Stringified attnums as keys.

Done @seancolsen.

Here is the updated response:

{
  "joinable_tables": [
    {
      "base": 2254321,
      "depth": 1,
      "target": 2254313,
      "fkey_path": [
        [2254389, false]
      ],
      "join_path": [
        [
          [2254321, 10],
          [2254313,  1]
        ]
      ],
      "multiple_results": false
    },
    {
      "base": 2254321,
      "depth": 1,
      "target": 2254358,
      "fkey_path": [
        [2254394, false]
      ],
      "join_path": [
        [
          [2254321, 11],
          [2254358,  1]
        ]
      ],
      "multiple_results": false
    },
    {
      "base": 2254321,
      "depth": 1,
      "target": 2254334,
      "fkey_path": [
        [2254399, true]
      ],
      "join_path": [
        [
          [2254321, 1],
          [2254334, 5]
        ]
      ],
      "multiple_results": true
    }
  ],
  "target_table_info": {
    "2254313": {
      "name": "Authors",
      "columns": {
        "1": {"name": "id"        , "type": "integer"},
        "2": {"name": "First Name", "type": "text"   },
        "3": {"name": "Last Name" , "type": "text"   }
      }
    },
    "2254334": {
      "name": "Items",
      "columns": {
        "1": {"name": "id"              , "type": "integer"},
        "2": {"name": "Barcode"         , "type": "text"   },
        "3": {"name": "Acquisition Date", "type": "date"   },
        "5": {"name": "Book"            , "type": "integer"}
      }
    },
    "2254358": {
      "name": "Publishers",
      "columns": {
        "1": {"name": "id"  , "type": "integer"},
        "2": {"name": "Name", "type": "text"   }
      }
    }
  }
}

Copy link
Contributor

@mathemancer mathemancer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, not bad. Given that we're waiting for feedback from Sean, I think it's a good idea to take another pass through the SQL function and tighten a couple of screws. See my specific comments.

joinable_tables jsonb;
target_table_info jsonb;
BEGIN
CREATE TEMP TABLE jt_cte AS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this a temp table rather than a proper CTE here? It's

  • slightly less performant under some conditions (i.e., when it's small, and
  • affects the volatility setting, meaning it's slower for callers

'target_table_info', target_table_info
);
END;
$$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the temp table to a CTE, you should mark this as STABLE.

@Anish9901 Anish9901 requested a review from mathemancer August 1, 2024 10:21
Copy link
Contributor

@mathemancer mathemancer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice!

@Anish9901 Anish9901 requested a review from seancolsen August 1, 2024 19:55
Copy link
Contributor

@seancolsen seancolsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. Thanks!

@seancolsen seancolsen added this pull request to the merge queue Aug 3, 2024
Merged via the queue into develop with commit abc58ba Aug 3, 2024
39 checks passed
@seancolsen seancolsen deleted the fix_joinable_resp branch August 3, 2024 21:38
@kgodey kgodey added this to the Pre-beta test build #1 milestone Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: review A PR awaiting review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tables and columns properties to RPC joinable_tables API
4 participants