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

More MySQL rewrite rules #1661

Merged
merged 4 commits into from
May 2, 2024
Merged

More MySQL rewrite rules #1661

merged 4 commits into from
May 2, 2024

Conversation

serprex
Copy link
Contributor

@serprex serprex commented May 1, 2024

  1. also remove CAST from OFFSET
  2. remove explicit public schema. Needed for UDF passthrough
  3. jsonb isn't in MySQL, so rewrite '...'::jsonb literals, in particular mapping [1,2,3] to array for StarRocks

This can be leveraged to use array functions in StarRocks:

CREATE OR REPLACE FUNCTION public.cardinality(jsonb)
RETURNS int AS $$
  BEGIN
    RETURN jsonb_array_length($1);
  END
$$
LANGUAGE PLPGSQL IMMUTABLE;

CREATE OR REPLACE FUNCTION public.array_intersect(jsonb, jsonb)
RETURNS jsonb AS $$
  BEGIN
        RETURN (select jsonb_agg(a) from jsonb_array_elements($1) a join jsonb_array_elements($2) b on a = b);
  END
$$
LANGUAGE PLPGSQL IMMUTABLE;

-- where postgres_fdw is specified in `extensions` option of fdw server
ALTER EXTENSION postgres_fdw ADD FUNCTION public.cardinality(jsonb);
ALTER EXTENSION postgres_fdw ADD FUNCTION public.array_intersect(jsonb, jsonb);

After which select array_intersect(fdwtbl.col, '[1,2,3]') from fdwtbl can be pushed down,
but function will also work if postgres for some reason can't push down

1. remove explicit `public` schema. Needed for UDF passthrough
2. `jsonb` isn't in MySQL, so rewrite '...'::jsonb literals, in particular mapping [1,2,3] to array for StarRocks

This can be leveraged to use array functions in StarRocks:

```
CREATE OR REPLACE FUNCTION cardinality(jsonb)
RETURNS int AS $$
  BEGIN
    RETURN jsonb_array_length($1);
  END
$$
LANGUAGE PLPGSQL IMMUTABLE;

CREATE OR REPLACE FUNCTION public.array_intersect(jsonb, jsonb)
RETURNS jsonb AS $$
  BEGIN
        RETURN (select jsonb_agg(a) from jsonb_array_elements($1) a join jsonb_array_elements($2) b on a = b);
  END
$$
LANGUAGE PLPGSQL IMMUTABLE;

-- where postgres_fdw is specified in `extensions` option of fdw server
ALTER EXTENSION postgres_fdw ADD FUNCTION cardinality;
```

After which `select array_intersect(fdwtbl.col, '[1,2,3]') from fdwtbl` can be pushed down,
but function will also work if postgres for some reason can't push down
@serprex serprex requested a review from iskakaushik May 1, 2024 23:23
@serprex serprex force-pushed the mysql-more-rewrite branch from 475340e to ea10d3f Compare May 2, 2024 18:50
@serprex serprex force-pushed the mysql-more-rewrite branch from ea10d3f to 1f036a4 Compare May 2, 2024 19:04
@serprex serprex merged commit 5b6ca38 into main May 2, 2024
7 checks passed
@serprex serprex deleted the mysql-more-rewrite branch May 2, 2024 19:10
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