Skip to content

Reusable queries, first step - in CTEs#174

Merged
jongleb merged 1 commit intoygrek:masterfrom
jongleb:reusable-queries-fst-step-ctes-4
Apr 25, 2025
Merged

Reusable queries, first step - in CTEs#174
jongleb merged 1 commit intoygrek:masterfrom
jongleb:reusable-queries-fst-step-ctes-4

Conversation

@jongleb
Copy link
Copy Markdown
Collaborator

@jongleb jongleb commented Apr 7, 2025

Description

This PR adds a possibility to reuse queries, at the current step with only CTE syntax.

Syntax:

Before the query as usual after a name you can specify a kind of it inclusion

Generate a query that can only be reused

-- @name | include: reuse

OR

Generate a query that can be reused and be executable

-- @name | include: reuse-and-execute

Keep a query as usual or specify explicitly to have it only as executable

-- @name |
-- @name |  include: executable

Let's take a look at the example from tests

We write the following queries (in the same file, in the proper order, so that the query that was declared below is clearly accessible to the one declared above)

-- @abcd | only-reuse
SELECT 
    1 as y, 
    4 + @five as y1
FROM ( 
        SELECT 1 AS id, 'l' AS creator_name
        UNION ALL
        SELECT 2, 'k'
) AS x )
WHERE @param { None { TRUE } | Some { FALSE } };

-- @test2 
WITH x AS &abcd
SELECT 1 + @test - @test2 + @test5 + x.y1 as y2
FROM x;

this way, we say that the first one is available as only a reusable query, which means that the it doesn't have self function in the generated code. And will be substituted in place of its use (&abcd) in this case. that is, in order to substitute this query (in the current implementation, embedding is only available as cte) you need to specify & and its name together.

Usage of parameters

The use of all parameter types as seen in the example in functions for reuse is also available as for regular ones, the type inference of parameters will be unified further

In result the following function will be generated
  let test2 db ~five ~param ~test ~test2 ~test5 callback =
    let invoke_callback stmt =
      callback
        ~y2:(T.get_column_Int stmt 0)
    in
    let set_params stmt =
      let p = T.start_params stmt (4 + (match param with `None -> 0 | `Some -> 0)) in
      T.set_param_Int p five;
      begin match param with
      | `None -> ()
      | `Some -> ()
      end;
      T.set_param_Int p test;
      T.set_param_Int p test2;
      T.set_param_Int p test5;
      T.finish_params p
    in
    T.select db ("WITH x AS (SELECT \n\
    1 as y, \n\
    4 + ? as y1\n\
FROM ( \n\
        SELECT 1 AS id, 'l' AS creator_name\n\
        UNION ALL\n\
        SELECT 2, 'k'\n\
) AS x \n\
WHERE " ^ (match param with `None -> " TRUE " | `Some -> " FALSE ") ^ ")\n\
SELECT 1 + ? - ? + ? + x.y1 as y2\n\
FROM x") set_params invoke_callback

it works similarly for reusable and simultaneously executable ones, the only difference being that for such requests both a function and a substitution are generated, for only executable ones nothing changes

@jongleb jongleb changed the base branch from master to ahrefs April 7, 2025 18:38
@jongleb jongleb force-pushed the reusable-queries-fst-step-ctes-4 branch 2 times, most recently from 77ce54d to a570922 Compare April 7, 2025 18:58
@jongleb jongleb changed the title Reusable queries fst step ctes Reusable queries, first step - CTEs Apr 7, 2025
@jongleb jongleb changed the title Reusable queries, first step - CTEs Reusable queries, first step - in CTEs Apr 7, 2025
@jongleb jongleb marked this pull request as ready for review April 7, 2025 19:04
@jongleb jongleb changed the base branch from ahrefs to master April 25, 2025 07:34
@jongleb jongleb changed the base branch from master to ahrefs April 25, 2025 07:35
@jongleb jongleb force-pushed the reusable-queries-fst-step-ctes-4 branch from e1609bc to ccedd90 Compare April 25, 2025 10:04
@jongleb jongleb changed the base branch from ahrefs to master April 25, 2025 10:08
@jongleb jongleb merged commit f7d558f into ygrek:master Apr 25, 2025
1 check passed
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.

1 participant