Skip to content

Commit

Permalink
fixes 2
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Apr 18, 2023
1 parent 94b50ee commit 5556b6a
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,6 @@ type Table
externally.
infer_return_type _ = SQL_Type_Reference.null
dialect.make_cast null_column result_type infer_return_type

corresponding_column_index : Integer ->
t.at corresponding_column_index . as_internal . rename column_name
pairs = columns_to_select.map c->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ type Column
iif : Any -> Any -> Column
iif self when_true when_false =
type_helpers.check_argument_type self Value_Type.expect_boolean <|
common_type = type_helpers.find_common_type_for_arguments [when_true, when_false]
common_type = type_helpers.find_common_type_for_arguments [when_true, when_false] . if_nothing Value_Type.Mixed
storage_type = Storage.from_value_type common_type Problem_Behavior.Report_Error
storage_type.catch Inexact_Type_Coercion _->
Panic.throw (Illegal_State.Error "Common type "+common_type.to_display_text+" is not supported by the Storage. This should never happen and is a bug in the Table library.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,16 @@ type Operation_Type_Helpers
Error.throw error

## PRIVATE
find_common_type_for_arguments : Vector Any -> Value_Type | Nothing ! No_Common_Type
find_common_type_for_arguments self arguments =
types = arguments.map self.find_argument_type
case find_common_type types strict=True of
common_type : Value_Type -> common_type
Nothing -> Error.throw <|
No_Common_Type.Error types related_column_name=Nothing
types = arguments.map self.find_argument_type . filter Filter_Condition.Not_Nothing
case types.is_empty of
# TODO [RW] what to do with `x.iif Nothing Nothing`?
True -> Nothing
False -> case find_common_type types strict=True of
common_type : Value_Type -> common_type
Nothing -> Error.throw <|
No_Common_Type.Error types related_column_name=Nothing


## PRIVATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ unify_result_type_for_union column_set all_tables allow_type_widening problem_bu
types = columns.filter Filter_Condition.Not_Nothing . map .value_type
common_type = Value_Type_Helpers.find_common_type types strict=True
if common_type.is_nothing then
problem_builder.report_other_warning (No_Common_Type.Error column_set.name)
problem_builder.report_other_warning (No_Common_Type.Error types related_column_name=column_set.name)
common_type
False ->
is_not_nothing c = case c of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ spec setup =
c2.to_vector . should_equal [22, 33, Nothing, 22]
c2.value_type . is_floating_point . should_be_true

c3 = t.at "X" . iif "A" "B"
c3.to_vector . should_equal ["A", "B", Nothing, "A"]
c3.value_type . is_text . should_be_true

c4 = t.at "X" . iif Nothing "B"
c4.to_vector . should_equal [Nothing, "B", Nothing, Nothing]
c4.value_type . is_text . should_be_true

c5 = t.at "X" . iif 42 Nothing
c5.to_vector . should_equal [42, Nothing, Nothing, 42]
c5.value_type . is_integer . should_be_true

c6 = t.at "X" . iif Nothing Nothing
c6.to_vector . should_equal [Nothing, Nothing, Nothing, Nothing]

t.at "X" . iif 22.0 False . to_vector . should_fail_with No_Common_Type
t.at "X" . iif 22 "0" . to_vector . should_fail_with No_Common_Type

Expand Down Expand Up @@ -378,18 +393,14 @@ spec setup =
(x / Nothing).to_vector . should_equal nulls

Test.specify "should correctly merge types in fill_nothing/coalesce" <|
x.fill_nothing y . value_type . is_floating_point . should_be_true
c = x.fill_nothing y
Test.with_clue "c.value_type="+c.value_type.to_display_text+": " <|
c.value_type . is_floating_point . should_be_true

x.fill_nothing 1 . value_type . is_integer . should_be_true
x.fill_nothing 1.0 . value_type . is_floating_point . should_be_true

y.fill_nothing 1 . value_type . is_floating_point . should_be_true
c2 = y.fill_nothing False
c2.value_type.is_floating_point.should_be_true
c2.to_vector . should_equal [2.0, 3.25, 5.0, 0.0]

c3 = x.coalesce [y, False]
c3.value_type.is_floating_point.should_be_true
c3.to_vector . should_equal [1, 4, 5, 0]

x.fill_nothing True . to_vector . should_fail_with No_Common_Type
y.fill_nothing True . to_vector . should_fail_with No_Common_Type
Expand Down Expand Up @@ -511,7 +522,8 @@ spec setup =

c5 = a.min b
c5.to_vector . should_equal [1, 2, 3]
c5.value_type.is_floating_point . should_be_true
Test.with_clue "c5.value_type="+c5.value_type.to_display_text+": " <|
c5.value_type.is_floating_point . should_be_true

c6 = a.max b
c6.to_vector . should_equal [4.5, 5.5, 6.5]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ spec setup =
expect_column_names ["A"] t3
t3.at "A" . to_vector . should_equal ["a", "b", "c", "xyz", "abc", "def"]
t3.at "A" . value_type . is_text . should_be_true
t3.at "A" . value_type . variable_length . should_be_true
Test.with_clue "t3[A].value_type="+(t3.at "A").value_type.to_display_text+": " <|
t3.at "A" . value_type . variable_length . should_be_true

Test.specify "should find a common type that will fit the merged columns" <|
t1 = table_builder [["int+float", [0, 1, 2]]]
Expand Down Expand Up @@ -268,7 +269,9 @@ spec setup =
(types == ["Char", "Integer"]) || (types == ["Boolean", "Char"]) . should_be_true

# A boolean column cannot be merged with integers.
r4 = t1.select_columns ["C"] . union (t2.select_columns ["A"]) match_columns=Match_Columns.By_Position on_problems=Problem_Behavior.Report_Error
t3 = t1.select_columns ["C", "A"] reorder=True
t4 = t2.select_columns ["B", "A"] reorder=True
r4 = t3.union t4 match_columns=Match_Columns.By_Position on_problems=Problem_Behavior.Report_Error
r4.should_fail_with No_Common_Type

Test.specify "if type widening is not allowed, should use the type from first table that contained the given column" <|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ spec setup =
c.value_type.is_floating_point.should_be_true

Test.specify "should keep String, Boolean, Long and Double type" <|
table = table_builder [["X", ["a", Nothing, "b", Nothing]], ["Y", [True, False, Nothing]], ["Z", [1, Nothing, 2, Nothing]], ["W", [0.5, Nothing, Nothing, 0.25]]]
table = table_builder [["X", ["a", Nothing, "b", Nothing]], ["Y", [True, False, Nothing, Nothing]], ["Z", [1, Nothing, 2, Nothing]], ["W", [0.5, Nothing, Nothing, 0.25]]]
strs = table.at "X"
strs_filled = strs.fill_nothing "X"
strs_filled.to_vector . should_equal ["a", "X", "b", "X"]
strs_filled.value_type.is_text.should_be_true

bools = table.at "Y"
bools_filled = bools.fill_nothing False
bools_filled.to_vector . should_equal [True, False, False]
bools_filled.to_vector . should_equal [True, False, False, False]
bools_filled.value_type . should_equal Value_Type.Boolean

ints = table.at "Z"
Expand Down
4 changes: 2 additions & 2 deletions test/Table_Tests/src/Database/Codegen_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ spec =

Test.group "[Codegen] Handling Missing Values" <|
Test.specify "fill_nothing should allow to replace missing values in a column with a constant" <|
c = t1.at "A" . fill_nothing "not-applicable"
c.to_sql.prepare . should_equal ['SELECT COALESCE("T1"."A", ?) AS "fill_nothing([A], \'not-applicable\')" FROM "T1" AS "T1"', ["not-applicable"]]
c = t1.at "B" . fill_nothing "not-applicable"
c.to_sql.prepare . should_equal ['SELECT COALESCE("T1"."B", ?) AS "fill_nothing([B], \'not-applicable\')" FROM "T1" AS "T1"', ["not-applicable"]]

Test.specify "filter_blank_rows should drop rows that contain at least one missing column in a Table" <|
t2 = t1.filter_blank_rows when_any=True
Expand Down
6 changes: 3 additions & 3 deletions test/Table_Tests/src/Helpers/Value_Type_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ spec =

Test.specify "should use correct in-memory logic to reconcile pairs of types for operations like union/iif" <|
Value_Type_Helpers.reconcile_types Value_Type.Boolean Value_Type.Boolean . should_equal Value_Type.Boolean
Value_Type_Helpers.reconcile_types Value_Type.Boolean Value_Type.Integer . should_equal Value_Type.Integer

Value_Type_Helpers.reconcile_types (Value_Type.Integer Bits.Bits_16) (Value_Type.Integer Bits.Bits_32) . should_equal (Value_Type.Integer Bits.Bits_32)
Value_Type_Helpers.reconcile_types (Value_Type.Float Bits.Bits_32) (Value_Type.Float Bits.Bits_32) . should_equal (Value_Type.Float Bits.Bits_32)
Value_Type_Helpers.reconcile_types (Value_Type.Float Bits.Bits_32) (Value_Type.Float Bits.Bits_64) . should_equal (Value_Type.Float Bits.Bits_64)

Value_Type_Helpers.reconcile_types Value_Type.Boolean Value_Type.Byte . should_equal Value_Type.Byte
Value_Type_Helpers.reconcile_types (Value_Type.Integer Bits.Bits_16) Value_Type.Byte . should_equal (Value_Type.Integer Bits.Bits_16)
# 64-bit floats are always used when unifying with integers
Value_Type_Helpers.reconcile_types (Value_Type.Float Bits.Bits_32) Value_Type.Byte . should_equal Value_Type.Float
Value_Type_Helpers.reconcile_types (Value_Type.Float Bits.Bits_32) Value_Type.Boolean . should_equal Value_Type.Float

Value_Type_Helpers.reconcile_types (Value_Type.Char 10 False) (Value_Type.Char 10 False) . should_equal (Value_Type.Char 10 False)
Value_Type_Helpers.reconcile_types (Value_Type.Char 10 False) (Value_Type.Char 10 True) . should_equal (Value_Type.Char 10 True)
Expand All @@ -61,5 +58,8 @@ spec =
Value_Type_Helpers.reconcile_types Value_Type.Float Value_Type.Binary . should_equal Value_Type.Mixed
Value_Type_Helpers.reconcile_types Value_Type.Char Value_Type.Binary . should_equal Value_Type.Mixed
Value_Type_Helpers.reconcile_types Value_Type.Char Value_Type.Boolean . should_equal Value_Type.Mixed
Value_Type_Helpers.reconcile_types Value_Type.Boolean Value_Type.Integer . should_equal Value_Type.Mixed
Value_Type_Helpers.reconcile_types Value_Type.Boolean Value_Type.Byte . should_equal Value_Type.Mixed
Value_Type_Helpers.reconcile_types (Value_Type.Float Bits.Bits_32) Value_Type.Boolean . should_equal Value_Type.Mixed

main = Test_Suite.run_main spec

0 comments on commit 5556b6a

Please sign in to comment.