-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine single-value column to treat it as that single value (#12120)
- First experiments with intersection types as refinements to our Table/Column - Closes #12095
- Loading branch information
Showing
10 changed files
with
311 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ution/lib/Standard/Table/0.0.0-dev/src/Internal/Type_Refinements/Single_Value_Column.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
private | ||
|
||
from Standard.Base import all | ||
|
||
import project.Column.Column | ||
import project.Value_Type.Value_Type | ||
from project.Internal.Type_Refinements.Single_Value_Column_Extensions import all | ||
|
||
refine_with_single_value (column : Column) = | ||
## We treat a column as single value if it contains a single not-nothing value. | ||
if is_single_value column . not then column else case column.inferred_precise_value_type of | ||
Value_Type.Integer _ -> | ||
# `inferred_precise_value_type` will return Integer if the column was Float (or Mixed) but contained integral values - e.g. [2.0] | ||
# We inspect the actual value to correctly deal with both Float and Mixed base type. | ||
value = column.at 0 | ||
case value of | ||
# If the value was really a float, we preserve that. | ||
_ : Float -> (column : Column & Float) | ||
# Otherwise we treat it as an integer. | ||
_ -> (column : Column & Integer) | ||
Value_Type.Float _ -> (column : Column & Float) | ||
Value_Type.Char _ _ -> (column : Column & Text) | ||
Value_Type.Boolean -> (column : Column & Boolean) | ||
Value_Type.Date -> (column : Column & Date) | ||
Value_Type.Time -> (column : Column & Time_Of_Day) | ||
Value_Type.Date_Time True -> (column : Column & Date_Time) | ||
Value_Type.Decimal _ scale -> | ||
is_integer = scale == 0 | ||
if is_integer then (column : Column & Integer) else (column : Column & Decimal) | ||
# Other types (e.g. Mixed) are not supported. | ||
_ -> column | ||
|
||
is_single_value column:Column -> Boolean = | ||
(column.length == 1) && (column.at 0 . is_nothing . not) |
51 changes: 51 additions & 0 deletions
51
...tandard/Table/0.0.0-dev/src/Internal/Type_Refinements/Single_Value_Column_Extensions.enso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
private | ||
|
||
from Standard.Base import all | ||
|
||
import project.Column.Column | ||
from project.Internal.Type_Refinements.Single_Value_Column import is_single_value | ||
|
||
## This conversion is internal and should never be exported. | ||
Integer.from (that : Column) -> Integer = | ||
Runtime.assert (is_single_value that) | ||
x = that.at 0 | ||
case x of | ||
_ : Integer -> x | ||
_ : Float -> | ||
Runtime.assert (x % 1.0 == 0.0) | ||
x.truncate | ||
|
||
## This conversion is internal and should never be exported. | ||
Float.from (that : Column) -> Float = | ||
Runtime.assert (is_single_value that) | ||
that.at 0 | ||
|
||
## This conversion is internal and should never be exported. | ||
Text.from (that : Column) -> Text = | ||
Runtime.assert (is_single_value that) | ||
that.at 0 | ||
|
||
## This conversion is internal and should never be exported. | ||
Boolean.from (that : Column) -> Boolean = | ||
Runtime.assert (is_single_value that) | ||
that.at 0 | ||
|
||
## This conversion is internal and should never be exported. | ||
Date.from (that : Column) -> Date = | ||
Runtime.assert (is_single_value that) | ||
that.at 0 | ||
|
||
## This conversion is internal and should never be exported. | ||
Time_Of_Day.from (that : Column) -> Time_Of_Day = | ||
Runtime.assert (is_single_value that) | ||
that.at 0 | ||
|
||
## This conversion is internal and should never be exported. | ||
Date_Time.from (that : Column) -> Date_Time = | ||
Runtime.assert (is_single_value that) | ||
that.at 0 | ||
|
||
## This conversion is internal and should never be exported. | ||
Decimal.from (that : Column) -> Decimal = | ||
Runtime.assert (is_single_value that) | ||
that.at 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.