Skip to content

Commit

Permalink
Add Coalesce tests and fix In-Memory version (#11242)
Browse files Browse the repository at this point in the history
Add tests for coalesce and fix the In-memory version

# Important Notes
The distribution/lib/Standard/Database/0.0.0-dev/src/Internal/IR/From_Spec.enso change isn't actually needed for this MR as I switched away from using literal tables for these tests as trying to mix 2 literal tables doesn't look to be supported. The change I added will let any future developers know that.
  • Loading branch information
AdRiley authored Oct 7, 2024
1 parent f1c2015 commit a4f800d
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -122,6 +122,9 @@ type From_Spec_Comparator
From_Spec.Table other_table_name _ _ ->
if table_name == other_table_name then Ordering.Equal else Nothing
_ -> Nothing
From_Spec.Literal_Values _ _ _ -> case y of
From_Spec.Literal_Values _ _ _ ->
Nothing
_ -> Ordering.compare x y

## PRIVATE
4 changes: 2 additions & 2 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso
Original file line number Diff line number Diff line change
@@ -1007,8 +1007,8 @@ type Column
coalesce self values =
vec = Vector.unify_vector_or_element values
new_name = self.naming_helper.function_name "coalesce" [self]+vec
result = if values.is_empty then self else
values.fold self acc-> v-> acc.fill_nothing v
result = if vec.is_empty then self else
vec.fold self acc-> v-> acc.fill_nothing v
result.rename new_name

## GROUP Standard.Base.Math
81 changes: 81 additions & 0 deletions test/Table_Tests/src/Common_Table_Operations/Coalesce_Spec.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from Standard.Base import all

from Standard.Table import all hiding Table
from Standard.Table.Errors import No_Common_Type
from Standard.Database.Errors import Integrity_Error

from Standard.Test import all

from project.Common_Table_Operations.Util import run_default_backend

main filter=Nothing = run_default_backend add_specs filter

add_specs suite_builder setup =
prefix = setup.prefix
table_builder = setup.table_builder

suite_builder.group prefix+"Table.coalesce" group_builder->
group_builder.specify "2 columns" <|
t = table_builder [["A", [1, 2, Nothing, Nothing]], ["B", [3, Nothing, 4, Nothing]]]
colA = t.get "A"
colB = t.get "B"
result = colA.coalesce colB
result.to_vector . should_equal [1, 2, 4, Nothing]
group_builder.specify "2 columns passing second as vector" <|
t = table_builder [["A", [1, 2, Nothing, Nothing]], ["B", [3, Nothing, 4, Nothing]]]
colA = t.get "A"
colB = t.get "B"
result = colA.coalesce [colB]
result.to_vector . should_equal [1, 2, 4, Nothing]
group_builder.specify "2 columns passing second and third as vector" <|
t = table_builder [["A", [1, 2, Nothing, Nothing]], ["B", [3, Nothing, 4, Nothing]], ["C", [5, 6, 7, 8]]]
colA = t.get "A"
colB = t.get "B"
colC = t.get "C"
result = colA.coalesce [colB, colC]
result.to_vector . should_equal [1, 2, 4, 8]
group_builder.specify "column and constant" <|
t = table_builder [["A", [1, 2, Nothing, Nothing]]]
colA = t.get "A"
result = colA.coalesce 42
result.to_vector . should_equal [1, 2, 42, 42]
group_builder.specify "column and constant and column" <|
t = table_builder [["A", [1, 2, Nothing, Nothing]], ["B", [3, Nothing, 4, Nothing]]]
colA = t.get "A"
colB = t.get "B"
result = colA.coalesce [42, colB]
result.to_vector . should_equal [1, 2, 42, 42]
group_builder.specify "2 columns of diffferent types" <|
t = table_builder [["A", [1, 2, Nothing, Nothing]], ["B", ["3", Nothing, "4", Nothing]]]
colA = t.get "A"
colB = t.get "B"
result = colA.coalesce colB
result.should_fail_with No_Common_Type
group_builder.specify "2 columns from different tables only works In-Memory" <|
t1 = table_builder [["A", [1, 2, Nothing, Nothing]], ["B", [Nothing, Nothing, Nothing, 99]]]
t2 = table_builder [["A", [99, Nothing, Nothing, Nothing]], ["B", [3, Nothing, 4, Nothing]]]
colA = t1.get "A"
colB = t2.get "B"
result = colA.coalesce colB
result2 = colB.coalesce colA
case setup.is_database of
True ->
result.should_fail_with Integrity_Error
result2.should_fail_with Integrity_Error
False ->
result.to_vector . should_equal [1, 2, 4, Nothing]
result2.to_vector . should_equal [3, 2, 4, Nothing]
group_builder.specify "2 columns from different length tables only works In-Memory" <|
t1 = table_builder [["A", [1, 2, Nothing, Nothing, 3]], ["B", [Nothing, Nothing, Nothing, 99, 99]]]
t2 = table_builder [["A", [99, Nothing, Nothing, Nothing]], ["B", [3, Nothing, 4, Nothing]]]
colA = t1.get "A"
colB = t2.get "B"
result = colA.coalesce colB
result2 = colB.coalesce colA
case setup.is_database of
True ->
result.should_fail_with Integrity_Error
result2.should_fail_with Integrity_Error
False ->
result.to_vector . should_equal [1, 2, 4, Nothing, 3]
result2.to_vector . should_equal [3, 2, 4, Nothing]
2 changes: 2 additions & 0 deletions test/Table_Tests/src/Common_Table_Operations/Main.enso
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ from Standard.Test import Test

import project.Common_Table_Operations.Add_Row_Number_Spec
import project.Common_Table_Operations.Aggregate_Spec
import project.Common_Table_Operations.Coalesce_Spec
import project.Common_Table_Operations.Column_Name_Edge_Cases_Spec
import project.Common_Table_Operations.Column_Operations_Spec
import project.Common_Table_Operations.Core_Spec
@@ -207,5 +208,6 @@ add_specs suite_builder setup =
Temp_Column_Spec.add_specs suite_builder setup
Nothing_Spec.add_specs suite_builder setup
Text_Cleanse_Spec.add_specs suite_builder setup
Coalesce_Spec.add_specs suite_builder setup

main filter=Nothing = run_default_backend add_specs filter

0 comments on commit a4f800d

Please sign in to comment.