Skip to content

Commit 1bd2a62

Browse files
authored
Add DB_Table.offset for snowflake, postgres and sqlite (#12251)
* 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
1 parent fadaf44 commit 1bd2a62

File tree

8 files changed

+39
-8
lines changed

8 files changed

+39
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@
5151
`on_invalid_rows`. The default behaviour was also changed to add any extra
5252
columns instead of discarding them.
5353
- [Added DB_Table.Offset for SQLServer][12206]
54+
- [Added DB_Table.Offset for Snowflake, Postgres, SQLite][12251]
5455

5556
[11926]: https://github.com/enso-org/enso/pull/11926
5657
[12031]: https://github.com/enso-org/enso/pull/12031
5758
[12071]: https://github.com/enso-org/enso/pull/12071
5859
[12092]: https://github.com/enso-org/enso/pull/12092
5960
[12231]: https://github.com/enso-org/enso/pull/12231
6061
[12206]: https://github.com/enso-org/enso/pull/12206
62+
[12251]: https://github.com/enso-org/enso/pull/12251
6163

6264
#### Enso Language & Runtime
6365

distribution/lib/Standard/Database/0.0.0-dev/src/Internal/Base_Generator.enso

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ base_dialect_operations =
469469
contains = [["IS_IN", make_is_in], ["IS_IN_COLUMN", make_is_in_column]]
470470
types = [simple_cast]
471471
windows = [["ROW_NUMBER", make_row_number], ["ROW_NUMBER_IN_GROUP", make_row_number_in_group]]
472-
base_dict = Dictionary.from_vector (arith + logic + compare + functions + agg + counts + text + nulls + contains + types + windows)
472+
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"]]
473+
base_dict = Dictionary.from_vector (arith + logic + compare + functions + agg + counts + text + nulls + contains + types + windows + leadlag)
473474
Dialect_Operations.Value base_dict
474475

475476
## PRIVATE
@@ -740,6 +741,38 @@ default_fetch_types_query dialect expression context where_filter_always_false_l
740741
## PRIVATE
741742
default_generate_collate collation_name:Text quote_char:Text='"' -> Text = ' COLLATE ' + quote_char + collation_name + quote_char
742743

744+
_build_partition_sql grouping:SQL_Builder ordering:SQL_Builder -> SQL_Builder =
745+
group_part = if grouping.is_empty then "" else
746+
SQL_Builder.code "PARTITION BY " ++ grouping
747+
SQL_Builder.code "OVER(" ++ group_part ++ " ORDER BY " ++ ordering ++ ")"
748+
749+
## PRIVATE
750+
_build_lead_lag_sql lead_lag:Text n:SQL_Builder colName:SQL_Builder grouping:SQL_Builder ordering:SQL_Builder -> SQL_Builder =
751+
partition_sql = _build_partition_sql grouping ordering
752+
SQL_Builder.code "(" ++ lead_lag ++ "(" ++ colName ++ ", " ++ n ++ ", NULL) " ++ partition_sql ++ ")"
753+
754+
## PRIVATE
755+
_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
756+
n = arguments.at 0
757+
colName = arguments.at 1
758+
grouping = arguments.at 2
759+
ordering = arguments.at 3
760+
_build_lead_lag_sql lead_lag n colName grouping ordering
761+
762+
## PRIVATE
763+
_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
764+
n = arguments.at 0
765+
colName = arguments.at 1
766+
grouping = arguments.at 2
767+
ordering_for_lead_lag = arguments.at 3
768+
ordering_for_row_number = arguments.at 4
769+
770+
lead_lag_sql = _build_lead_lag_sql lead_lag n colName grouping ordering_for_lead_lag
771+
partition_sql_for_row_number = _build_partition_sql grouping ordering_for_row_number
772+
fill_sql = SQL_Builder.code "FIRST_VALUE(" ++ colName ++ ") " ++ partition_sql_for_row_number
773+
SQL_Builder.code "CASE WHEN ROW_NUMBER() " ++ partition_sql_for_row_number ++ " <= " ++ n ++ " THEN " ++ fill_sql ++ " ELSE " ++ lead_lag_sql ++ " END"
774+
775+
743776
## PRIVATE
744777
Helper class for shortening the binder names generated for WITH clauses.
745778

distribution/lib/Standard/Database/0.0.0-dev/src/Internal/Postgres/Postgres_Dialect.enso

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ type Postgres_Dialect
269269
Checks if a feature is supported by the dialect.
270270
is_feature_supported self feature:Feature -> Boolean =
271271
case feature of
272-
Feature.Offset -> False
273272
_ -> True
274273

275274
## PRIVATE

distribution/lib/Standard/Database/0.0.0-dev/src/Internal/SQLite/SQLite_Dialect.enso

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ type SQLite_Dialect
278278
Checks if a feature is supported by the dialect.
279279
is_feature_supported self feature:Feature -> Boolean =
280280
case feature of
281-
Feature.Offset -> False
282281
_ -> True
283282

284283
## PRIVATE

distribution/lib/Standard/Database/0.0.0-dev/src/Internal/SQLite/SQLite_Type_Mapping.enso

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ operations_dict =
214214
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"]
215215
same_as_first = ["TRUNCATE", "CEIL", "FLOOR", "FIRST", "LAST"]
216216
arithmetic_ops = ["ADD_NUMBER", "-", "*", "^", "%", "SUM"]
217-
merge_input_types_ops = ["ROW_MAX", "ROW_MIN", "MAX", "MIN", "FILL_NULL", "COALESCE"]
217+
merge_input_types_ops = ["ROW_MAX", "ROW_MIN", "MAX", "MIN", "FILL_NULL", "COALESCE", "LEAD", "LAG", "LEAD_CLOSEST", "LAG_CLOSEST"]
218218
others = [["IIF", handle_iif], ["CAST", handle_cast], ["CASE", handle_case], ["RUNTIME_ERROR", handle_runtime_error]]
219219
Dictionary.from_vector <|
220220
v1 = always_boolean_ops.map [_, const SQLite_Types.boolean]

distribution/lib/Standard/Snowflake/0.0.0-dev/src/Internal/Snowflake_Dialect.enso

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ type Snowflake_Dialect
257257
Checks if a feature is supported by the dialect.
258258
is_feature_supported self feature:Feature -> Boolean =
259259
case feature of
260-
Feature.Offset -> False
261260
_ -> True
262261

263262
## PRIVATE

distribution/lib/Standard/Table/0.0.0-dev/src/Table.enso

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3160,7 +3160,7 @@ type Table
31603160
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) =
31613161
Table.from_union ([self] + Vector.unify_vector_or_element tables) columns_to_keep match_columns on_problems
31623162

3163-
## ALIAS drop_missing_rows, dropna
3163+
## ALIAS drop_empty_rows, drop_missing_rows, dropna, filter_empty_rows, remove_blank_rows, remove_empty_rows, remove_missing_rows
31643164
GROUP Standard.Base.Selections
31653165
ICON preparation
31663166
Remove rows which are all blank or containing blank values.

test/Table_Tests/src/Common_Table_Operations/Util.enso

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ Error.should_equal_tz_agnostic self other =
8282
## PRIVATE
8383
Builds a table ensuring that the rows are in the order as given.
8484
build_sorted_table setup table_structure =
85-
# Workaround for https://github.com/enso-org/enso/issues/10321
86-
if setup.prefix.contains "Snowflake" . not && setup.prefix.contains "SQLServer" . not then setup.table_builder table_structure else
85+
if setup.is_database . not then setup.table_builder table_structure else
8786
row_count = case table_structure.first of
8887
def : Vector -> def.second.length
8988
col : Column -> col.length

0 commit comments

Comments
 (0)