Skip to content

Commit

Permalink
Add DB_Table.offset for snowflake, postgres and sqlite (#12251)
Browse files Browse the repository at this point in the history
* Red

* Fix tests

* Remove comment

* checkpoint

* Checkpoint

* 6 red

* 3 red

* Green

* Checkpoint

* Remove wrap around tests

* Green

* Refactor

* Adjust limit

* Clean up

* Refactor

* Refactor

* refactor

* Refactor

* Refactor

* Documentation

* Cleanup

* Comment

* Checkpoint

* Refactor

* fix

* More tests

* Table.new

* Cleanup

* changelog

* Fix test

* Code review changes

* Code review

* Postgres

* SQLite

* Snowflake

* Add missing aliases

* changelog

* Fix SQLite test
  • Loading branch information
AdRiley authored Feb 11, 2025
1 parent fadaf44 commit 1bd2a62
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@
`on_invalid_rows`. The default behaviour was also changed to add any extra
columns instead of discarding them.
- [Added DB_Table.Offset for SQLServer][12206]
- [Added DB_Table.Offset for Snowflake, Postgres, SQLite][12251]

[11926]: https://github.com/enso-org/enso/pull/11926
[12031]: https://github.com/enso-org/enso/pull/12031
[12071]: https://github.com/enso-org/enso/pull/12071
[12092]: https://github.com/enso-org/enso/pull/12092
[12231]: https://github.com/enso-org/enso/pull/12231
[12206]: https://github.com/enso-org/enso/pull/12206
[12251]: https://github.com/enso-org/enso/pull/12251

#### Enso Language & Runtime

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ base_dialect_operations =
contains = [["IS_IN", make_is_in], ["IS_IN_COLUMN", make_is_in_column]]
types = [simple_cast]
windows = [["ROW_NUMBER", make_row_number], ["ROW_NUMBER_IN_GROUP", make_row_number_in_group]]
base_dict = Dictionary.from_vector (arith + logic + compare + functions + agg + counts + text + nulls + contains + types + windows)
leadlag = [["LEAD", _make_lead_lag "LEAD"], ["LAG", _make_lead_lag "LAG"], ["LEAD_CLOSEST", _make_lead_lag_closest_value "LEAD"], ["LAG_CLOSEST", _make_lead_lag_closest_value "LAG"]]
base_dict = Dictionary.from_vector (arith + logic + compare + functions + agg + counts + text + nulls + contains + types + windows + leadlag)
Dialect_Operations.Value base_dict

## PRIVATE
Expand Down Expand Up @@ -740,6 +741,38 @@ default_fetch_types_query dialect expression context where_filter_always_false_l
## PRIVATE
default_generate_collate collation_name:Text quote_char:Text='"' -> Text = ' COLLATE ' + quote_char + collation_name + quote_char

_build_partition_sql grouping:SQL_Builder ordering:SQL_Builder -> SQL_Builder =
group_part = if grouping.is_empty then "" else
SQL_Builder.code "PARTITION BY " ++ grouping
SQL_Builder.code "OVER(" ++ group_part ++ " ORDER BY " ++ ordering ++ ")"

## PRIVATE
_build_lead_lag_sql lead_lag:Text n:SQL_Builder colName:SQL_Builder grouping:SQL_Builder ordering:SQL_Builder -> SQL_Builder =
partition_sql = _build_partition_sql grouping ordering
SQL_Builder.code "(" ++ lead_lag ++ "(" ++ colName ++ ", " ++ n ++ ", NULL) " ++ partition_sql ++ ")"

## PRIVATE
_make_lead_lag lead_lag:Text arguments:Vector -> SQL_Builder = if arguments.length != 4 then Error.throw (Illegal_State.Error "Wrong amount of parameters in LEAD/LAG IR. This is a bug in the Database library.") else
n = arguments.at 0
colName = arguments.at 1
grouping = arguments.at 2
ordering = arguments.at 3
_build_lead_lag_sql lead_lag n colName grouping ordering

## PRIVATE
_make_lead_lag_closest_value lead_lag:Text arguments:Vector -> SQL_Builder = if arguments.length != 5 then Error.throw (Illegal_State.Error "Wrong amount of parameters in LEAD/LAG IR. This is a bug in the Database library.") else
n = arguments.at 0
colName = arguments.at 1
grouping = arguments.at 2
ordering_for_lead_lag = arguments.at 3
ordering_for_row_number = arguments.at 4

lead_lag_sql = _build_lead_lag_sql lead_lag n colName grouping ordering_for_lead_lag
partition_sql_for_row_number = _build_partition_sql grouping ordering_for_row_number
fill_sql = SQL_Builder.code "FIRST_VALUE(" ++ colName ++ ") " ++ partition_sql_for_row_number
SQL_Builder.code "CASE WHEN ROW_NUMBER() " ++ partition_sql_for_row_number ++ " <= " ++ n ++ " THEN " ++ fill_sql ++ " ELSE " ++ lead_lag_sql ++ " END"


## PRIVATE
Helper class for shortening the binder names generated for WITH clauses.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ type Postgres_Dialect
Checks if a feature is supported by the dialect.
is_feature_supported self feature:Feature -> Boolean =
case feature of
Feature.Offset -> False
_ -> True

## PRIVATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ type SQLite_Dialect
Checks if a feature is supported by the dialect.
is_feature_supported self feature:Feature -> Boolean =
case feature of
Feature.Offset -> False
_ -> True

## PRIVATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ operations_dict =
always_integer_ops = ["COUNT", "COUNT_IS_NULL", "COUNT_DISTINCT", "COUNT_DISTINCT_INCLUDE_NULL", "COUNT_EMPTY", "COUNT_NOT_EMPTY", "COUNT_ROWS", "COUNT_OVER_PARTITION", "ROW_NUMBER", "ROW_NUMBER_IN_GROUP", "LENGTH"]
same_as_first = ["TRUNCATE", "CEIL", "FLOOR", "FIRST", "LAST"]
arithmetic_ops = ["ADD_NUMBER", "-", "*", "^", "%", "SUM"]
merge_input_types_ops = ["ROW_MAX", "ROW_MIN", "MAX", "MIN", "FILL_NULL", "COALESCE"]
merge_input_types_ops = ["ROW_MAX", "ROW_MIN", "MAX", "MIN", "FILL_NULL", "COALESCE", "LEAD", "LAG", "LEAD_CLOSEST", "LAG_CLOSEST"]
others = [["IIF", handle_iif], ["CAST", handle_cast], ["CASE", handle_case], ["RUNTIME_ERROR", handle_runtime_error]]
Dictionary.from_vector <|
v1 = always_boolean_ops.map [_, const SQLite_Types.boolean]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ type Snowflake_Dialect
Checks if a feature is supported by the dialect.
is_feature_supported self feature:Feature -> Boolean =
case feature of
Feature.Offset -> False
_ -> True

## PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion distribution/lib/Standard/Table/0.0.0-dev/src/Table.enso
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@ type Table
union self tables:(Table | Vector) (columns_to_keep : Columns_To_Keep = ..In_Any_Warn_On_Missing) (match_columns : Match_Columns = ..By_Name) (on_problems : Problem_Behavior = ..Report_Warning) =
Table.from_union ([self] + Vector.unify_vector_or_element tables) columns_to_keep match_columns on_problems

## ALIAS drop_missing_rows, dropna
## ALIAS drop_empty_rows, drop_missing_rows, dropna, filter_empty_rows, remove_blank_rows, remove_empty_rows, remove_missing_rows
GROUP Standard.Base.Selections
ICON preparation
Remove rows which are all blank or containing blank values.
Expand Down
3 changes: 1 addition & 2 deletions test/Table_Tests/src/Common_Table_Operations/Util.enso
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ Error.should_equal_tz_agnostic self other =
## PRIVATE
Builds a table ensuring that the rows are in the order as given.
build_sorted_table setup table_structure =
# Workaround for https://github.com/enso-org/enso/issues/10321
if setup.prefix.contains "Snowflake" . not && setup.prefix.contains "SQLServer" . not then setup.table_builder table_structure else
if setup.is_database . not then setup.table_builder table_structure else
row_count = case table_structure.first of
def : Vector -> def.second.length
col : Column -> col.length
Expand Down

0 comments on commit 1bd2a62

Please sign in to comment.