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

Retrieve function source description from comments #829

Merged
merged 1 commit into from
Aug 27, 2023

Conversation

sharkAndshark
Copy link
Collaborator

@sharkAndshark sharkAndshark commented Aug 21, 2023

If a PostgreSQL function has an SQL comment, it will try to parse as JSON and use its values to override the auto-generated TileJSON. It is recommended to use this form when creating comments to ensure valid JSON values.

DO $do$ BEGIN
    EXECUTE 'COMMENT ON FUNCTION YOUR_FUNCTION (ARG1_TYPE,ARG2_TYPE,..ARGN_TYPE) IS $tj$' || $$
    {
      "description": "description override",
      ...
    }
    $$::json || '$tj$';
END $do$;

Partially implements #822

@sharkAndshark sharkAndshark force-pushed the comments branch 4 times, most recently from f1969c3 to 37cb490 Compare August 21, 2023 16:36
@@ -13,6 +15,14 @@ use crate::pg::pool::PgPool;
use crate::pg::PgError::PostgresError;
use crate::pg::Result;

#[derive(Serialize, Deserialize, Debug)]
Copy link
Member

Choose a reason for hiding this comment

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

use TileJSON object here - no point in creating a new struct. Simply parse TileJSON from the comment

martin/src/pg/scripts/query_available_function.sql Outdated Show resolved Hide resolved
@@ -13,3 +13,25 @@ RETURNS TABLE("mVt" bytea, key text) AS $$
WHERE "Geom" && ST_Transform(ST_TileEnvelope("Z", x, y), 4326)
) as tile WHERE geom IS NOT NULL) src
$$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;

COMMENT ON FUNCTION "MixedCase"."function_Mixed_Name" (int4,INT4,INT4) IS $tilejson$
Copy link
Member

Choose a reason for hiding this comment

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

See if you can modify these somehow to auto-verified with '{...}'::json approach instead of a unverified string

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Weired. PostgreSQL complained syntax error. Need a little bit research on.

COMMENT ON FUNCTION osm_trees(INTEGER,INTEGER,INTEGER) IS ('{"a":1}'::json::TEXT)

Copy link
Collaborator Author

@sharkAndshark sharkAndshark Aug 22, 2023

Choose a reason for hiding this comment

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

Seems it only receive literal value? doc

Copy link
Member

Choose a reason for hiding this comment

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

@sharkAndshark use this method:

DO $do$ BEGIN
    EXECUTE 'COMMENT ON FUNCTION public.function_null (INT4,INT4,INT4) IS $tj$' || $$
    {
      "minzoom": 0,
      "maxzoom": 18,
      "bounds": [-180, -85, 180, 85],
      "vector_layers": []
    }
    $$::json || '$tj$';
END $do$;

@sharkAndshark sharkAndshark force-pushed the comments branch 2 times, most recently from eb929e0 to e786204 Compare August 22, 2023 06:36
@sharkAndshark
Copy link
Collaborator Author

sharkAndshark commented Aug 22, 2023

Hi @nyurik Could you give me some suggestions for fixing the errors in SQL files? I've tried many times.
It's weired. The setup-database step in CI failed.

Update:
Seems it's due to the case sentive issue.But I think I shouldn't double quoting every sql file.Fix it and do the method you suggested later

@nyurik
Copy link
Member

nyurik commented Aug 22, 2023

Hi @nyurik Could you give me some suggestions for fixing the errors in SQL files? I've tried many times. It's weired. The setup-database step in CI failed.

Update: Seems it's due to the case sentive issue.But I think I shouldn't double quoting every sql file.Fix it and do the method you suggested later

usually you don't need to quote any identifiers (namespaces, tables, functions and their arguments, etc) in SQL, as long as they don't use any special characters or reserved keywords. BUT, in some very rare cases, someone may have those weird cases. So in our testing, we must make sure Martin supports it properly. And for that, I have a few tests like "MixedCase" namespaces, as well as functions, etc.

Note that the generated SQL automatically always uses quotes identifiers to avoid ambiguity.

@sharkAndshark sharkAndshark force-pushed the comments branch 2 times, most recently from 95ed89e to 7f6fa16 Compare August 23, 2023 08:04
@nyurik nyurik mentioned this pull request Aug 24, 2023
@nyurik
Copy link
Member

nyurik commented Aug 24, 2023

I reworked your patch in sharkAndshark#50 -- please take a look and merge if agree.

Basic idea:

  • tilejson from a comment is stored as a simple json value
  • when creating the final tilejson, it uses generated tilejson as the "base", and applies all fields from the comment's tilejson as a "patch" (override) using json-patch crate

P.S. I don't think you should add tilejson to ALL unit tests. Instead, add it to just a few, with some variation of which fields are added, and possibly even deleted (set them to null).

@sharkAndshark
Copy link
Collaborator Author

Agreed.I realized the tests part and I'm in a conference,merge it later.

@sharkAndshark
Copy link
Collaborator Author

sharkAndshark commented Aug 26, 2023

To achieve isolation, it is recommended to have one unit test for each observable behavior. Therefore, it might be necessary to remove some comments from the function. However, there is a concern that this level of granularity might be excessive?(And we can not see them on debug.html then) @nyurik I'm looking forward your guide.

@nyurik
Copy link
Member

nyurik commented Aug 27, 2023

@sharkAndshark lets keep things simple :) I think we need to cover these cases. And I think you can do this with just two function comments.

Test 1

  • set on "MixedCase"."function_Mixed_Name" - to test comments work on weird named functions
  • override a single field: set the description in a function comment -- this should override the whatever description is set from the full ID automatically
  • add a new field: set vector_layers field with an array of whatever stuff the function is producing

Test 2

  • set comment on public.function_zxy_query -- normal name, different param combination
  • delet a field - in another function comment test, set description to null. No need to set any other fields at all
  • add some field that is NOT part of tilejson, e.g. "myvalue": { "foo": "bar" } (or whatever similar). This will test that we can add arbitrary complex content

Just doing these two tests, you should be good enough.

@sharkAndshark sharkAndshark marked this pull request as ready for review August 27, 2023 07:15
@sharkAndshark
Copy link
Collaborator Author

I hope the doc is not too bad as English is not my first language and I'm still learning it.

@@ -33,6 +34,18 @@ pub async fn query_available_function(pool: &PgPool) -> Result<SqlFuncInfoMapMap
let output_record_names = jsonb_to_vec(&row.get("output_record_names"));
let input_types = jsonb_to_vec(&row.get("input_types")).expect("Can't get input types");
let input_names = jsonb_to_vec(&row.get("input_names")).expect("Can't get input names");
let tilejson = if let Some(text) = row.get("description") {
match serde_json::from_str::<Value>(text) {
Copy link
Member

Choose a reason for hiding this comment

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

parsing now works because it now uses from_str::<Value> instead of from_str::<TileJSON>. Value allows any valid json.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah. hehe. I realized that lol. Cool. ✅

Copy link
Member

@nyurik nyurik left a comment

Choose a reason for hiding this comment

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

great work, thanks!

@nyurik nyurik enabled auto-merge (squash) August 27, 2023 16:30
@nyurik nyurik disabled auto-merge August 27, 2023 16:30
@nyurik nyurik enabled auto-merge (squash) August 27, 2023 16:30
@nyurik
Copy link
Member

nyurik commented Aug 27, 2023

Thanks @sharkAndshark , i may do another round of docs cleanup later, but this is good to merge for now. Do you perhaps want to do the same thing for tables? I suspect it is possible to add comments to them too.

@sharkAndshark
Copy link
Collaborator Author

sharkAndshark commented Aug 27, 2023

Yeah sure. I'd love to do that. A issue ticket for discussion first maybe? @nyurik

@nyurik
Copy link
Member

nyurik commented Aug 27, 2023

technically that's the same issue #822 issue - although it is more of an umbrella issue to cover all tilejson modifications by the user. Eventually I think we should just add an extra field to the yaml config, and that field will work the same way as the function comment

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

Successfully merging this pull request may close these issues.

2 participants