Skip to content

Commit

Permalink
Wrap brittle operations in subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jan 9, 2023
1 parent 35e598a commit da71870
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions distribution/lib/Standard/Database/0.0.0-dev/src/Data/Table.enso
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ type Table
new_columns = partitioned.first
problems = partitioned.second
on_problems.attach_problems_before problems <|
self.updated_context_and_columns new_ctx new_columns
self.updated_context_and_columns new_ctx new_columns subquery=True

## Returns a new table with a chosen subset of columns left unchanged and
the other columns pivoted to rows with a single name field and a single
Expand Down Expand Up @@ -1020,8 +1020,24 @@ type Table
Arguments:
- ctx: The new context for this table.
- internal_columns: The new columns to include in the table.
- subquery: A boolean indicating whether the operation should be wrapped
in a subquery. This is a simple workaround for operations which may be
affected by further operations if not wrapped. For example, a group-by
may need to be wrapped in this way if a filter is to be performed on it
later on. Ideally, this should be done only on demand, if the
subsequent operation needs it and operations like join should try to
avoid nesting subqueries without necessity. However, for now, for
simplicity, we are always wrapping brittle operations. This may be
revised in the future, to generate better and more concise SQL code.
updated_context_and_columns : Context -> Vector Internal_Column -> Table
updated_context_and_columns self ctx internal_columns = Table.Value self.name self.connection internal_columns ctx
updated_context_and_columns self ctx internal_columns subquery=False = case subquery of
True ->
setup = ctx.as_subquery self.name [internal_columns]
new_ctx = Context.for_subquery setup.subquery
new_columns = setup.new_columns.first
Table.Value self.name self.connection new_columns new_ctx
False ->
Table.Value self.name self.connection internal_columns ctx

## PRIVATE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Postgres_Dialect
distinct_expressions = new_key_columns.map (Database_Distinct_Helper.make_distinct_expression text_case_insensitive problem_builder)
# TODO check order_by
new_context = Context.for_subquery setup.subquery . set_distinct_on distinct_expressions
table.updated_context_and_columns new_context new_columns
table.updated_context_and_columns new_context new_columns subquery=True

## PRIVATE
make_internal_generator_dialect =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type SQLite_Dialect
new_key_columns = key_columns.map c-> column_mapping.at c.name
distinct_expressions = new_key_columns.map (Database_Distinct_Helper.make_distinct_expression text_case_insensitive problem_builder)
new_context = Context.for_subquery setup.subquery . set_groups distinct_expressions
table.updated_context_and_columns new_context new_columns
table.updated_context_and_columns new_context new_columns subquery=True


## PRIVATE
Expand Down

0 comments on commit da71870

Please sign in to comment.