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

Simplified Shared Fragment Calls #169

Open
RaoulFoaleng opened this issue Feb 22, 2023 · 0 comments
Open

Simplified Shared Fragment Calls #169

RaoulFoaleng opened this issue Feb 22, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@RaoulFoaleng
Copy link
Contributor

Rewrite shared fragment to look like function calls:

@attribute(cql:shared_fragment)
CREATE PROCEDURE ShapeDeclaration()
BEGIN
  SELECT
    NULL_TEXT              text_column,
    NULL_INT               int_column,
    TRUE                   bool_column
  WHERE FALSE;
END

@attribute(cql:shared_fragment)
CREATE PROCEDURE RowAdder()
BEGIN
  WITH source(*) LIKE ShapeDeclaration,
  rows_added(*) AS (
    SELECT * FROM source
    UNION ALL
    SELECT * FROM my_table_with_matching_columns
  )
  SELECT * FROM rows_added;
END

@attribute(cql:shared_fragment)
CREATE PROCEDURE ColumnAdder()
BEGIN
  WITH source(*) LIKE ShapeDeclaration,
  columns_added(*) AS (
    SELECT source.*, other_table.other_column
    FROM source
    INNER JOIN other_table ON source.id = other_table.id
  )
  SELECT * FROM columns_added;
END

CREATE PROCEDURE FinalQuery()
BEGIN
  WITH shape(*) AS (CALL ShapeDeclaration()),
  with_threads(*) AS (CALL RowAdder()) USING shape AS source),
  with_extra_column(*) AS (CALL ColumnAdder() USING with_threads AS source)
  SELECT * FROM with_extra_column;
END

To something like this:

interface ShapeDeclaration {
  text_column: text,
  int_column: int,
  bool_column: bool,
}

proc RowAdder(source: ShapeDeclaration)
{
  select * from source
  union all
  select * from my_table_with_matching_columns;
}

proc ColumnAdder(source: ShapeDeclaration)
{
  select source.*, other_table.other_column
  from source
  inner join other_table on source.id = other_table.id
}

proc FinalQuery()
{
  let base: ShapeDeclaration = {};
  let with_rows = RowAdder(base);
  let with_extra_columns = ColumnAdder(with_rows);
  select * from with_extra_columns;
}

We're introducing the notion of object interface (aka CQL shape) that represent a shared fragment result and those object can be composed.

sproc FinalQuery() implementation looks much more simpler in the second example.

Share fragment feature is a great feature that allow sproc composability but at the same the syntax to call them look confusing and congested:

...
WITH shape(*) AS (CALL ShapeDeclaration()),
  with_threads(*) AS (CALL RowAdder()) USING shape AS source),
  with_extra_column(*) AS (CALL ColumnAdder() USING with_threads AS source)
...
@RaoulFoaleng RaoulFoaleng added the enhancement New feature or request label Feb 22, 2023
@RaoulFoaleng RaoulFoaleng self-assigned this Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant