From a4f800dbf04360f0e70b2b89efdde90ceb2c82e9 Mon Sep 17 00:00:00 2001 From: AdRiley Date: Mon, 7 Oct 2024 10:02:11 +0100 Subject: [PATCH] Add Coalesce tests and fix In-Memory version (#11242) 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. --- .../0.0.0-dev/src/Internal/IR/From_Spec.enso | 3 + .../Standard/Table/0.0.0-dev/src/Column.enso | 4 +- .../Coalesce_Spec.enso | 81 +++++++++++++++++++ .../src/Common_Table_Operations/Main.enso | 2 + 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 test/Table_Tests/src/Common_Table_Operations/Coalesce_Spec.enso diff --git a/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/IR/From_Spec.enso b/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/IR/From_Spec.enso index b203f6606603..272597e81f1e 100644 --- a/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/IR/From_Spec.enso +++ b/distribution/lib/Standard/Database/0.0.0-dev/src/Internal/IR/From_Spec.enso @@ -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 diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso index 7fc554965718..42e3c9bb9cd8 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Column.enso @@ -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 diff --git a/test/Table_Tests/src/Common_Table_Operations/Coalesce_Spec.enso b/test/Table_Tests/src/Common_Table_Operations/Coalesce_Spec.enso new file mode 100644 index 000000000000..ca8c3b875c8a --- /dev/null +++ b/test/Table_Tests/src/Common_Table_Operations/Coalesce_Spec.enso @@ -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] diff --git a/test/Table_Tests/src/Common_Table_Operations/Main.enso b/test/Table_Tests/src/Common_Table_Operations/Main.enso index 08ddb2f90d3c..0037ae5a6d9b 100644 --- a/test/Table_Tests/src/Common_Table_Operations/Main.enso +++ b/test/Table_Tests/src/Common_Table_Operations/Main.enso @@ -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