Skip to content

Commit

Permalink
Small fixes from building up another demo. (#9385)
Browse files Browse the repository at this point in the history
- Fix `Excel_Workbook.sheet` and add a test.
- Add icon for `Table.row_count` and `DB_Table.row_count`.
- Make `join_kind` widget `Display.Always`.
- Add expression as an option to `Aggregate_Column`.
- Add `Simple_Calculation.Copy` to create a copy.
- Add defaults to `Simple_Expression` so less errory.
- Set period to default to day for `date_diff` allowing use in expressions.
- Add `Text_Left`, `Text_Right`, `Text_Length` and `Format` to `Simple_Expression`.
  • Loading branch information
jdunkerley authored Mar 13, 2024
1 parent f82e802 commit 19f15b8
Show file tree
Hide file tree
Showing 24 changed files with 63 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ type DB_Column
unusual events like DST.
@period Date_Time_Helpers.make_period_selector_for_column
date_diff : (DB_Column | Date | Date_Time | Time_Of_Day) -> Date_Period | Time_Period -> DB_Column ! Invalid_Value_Type | Illegal_Argument
date_diff self end (period : Date_Period | Time_Period) =
date_diff self end (period : Date_Period | Time_Period = Date_Period.Day) =
Value_Type.expect_type self .is_date_or_time "date/time" <|
my_type = self.inferred_precise_value_type
Value_Type.expect_type end (== my_type) my_type.to_display_text <|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ type DB_Table
allows to join the two tables on equality of corresponding columns with
the same name. So `table.join other on=["A", "B"]` is a shorthand for:
table.join other on=[Join_Condition.Equals "A" "A", Join_Condition.Equals "B" "B"]
@join_kind Widget_Helpers.make_join_kind_selector
@on Widget_Helpers.make_join_condition_selector
join : DB_Table -> Join_Kind -> Join_Condition | Text | Vector (Join_Condition | Text) -> Text -> Problem_Behavior -> DB_Table
join self right (join_kind : Join_Kind = Join_Kind.Left_Outer) (on : Join_Condition | Text | Vector (Join_Condition | Text) = (default_join_condition self join_kind)) (right_prefix:Text="Right ") (on_problems:Problem_Behavior=Report_Warning) =
Expand Down Expand Up @@ -2494,6 +2495,7 @@ type DB_Table

## ALIAS count
GROUP Standard.Base.Metadata
ICON metadata
Returns the amount of rows in this table.
row_count : Integer
row_count self = if self.internal_columns.is_empty then 0 else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ type Column
unusual events like DST.
@period Date_Time_Helpers.make_period_selector_for_column
date_diff : (Column | Date | Date_Time | Time_Of_Day) -> Date_Period | Time_Period -> Column ! Invalid_Value_Type | Illegal_Argument
date_diff self end (period : Date_Period | Time_Period) =
date_diff self end (period : Date_Period | Time_Period = Date_Period.Day) =
Value_Type.expect_type self .is_date_or_time "date/time" <|
my_type = self.inferred_precise_value_type
Value_Type.expect_type end (== my_type) my_type.to_display_text <|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from project.Internal.Filter_Condition_Helpers import make_filter_column
## Defines a simple expression based off an input column and an operation to perform.
type Simple_Expression
## A simple expression based off an input column and an operation to perform.
From (input : Column_Ref|Expression|Any = Column_Ref.Index) (operation : Simple_Calculation)
From (input : Column_Ref|Expression|Any = (Column_Ref.Index 0)) (operation : Simple_Calculation = Simple_Calculation.Copy)

## PRIVATE
Interprets the `Simple_Expression` as operation on columns of a provided
Expand All @@ -24,6 +24,7 @@ type Simple_Expression
evaluate self table:Table_Ref use_input_name:Boolean on_problems:Problem_Behavior =
input_column = table.resolve_as_column self.input
derived = case self.operation of
Simple_Calculation.Copy -> input_column . rename (input_column.name+" (Copy)")
Simple_Calculation.Add rhs -> input_column + (table.resolve rhs)
Simple_Calculation.Subtract rhs -> input_column - (table.resolve rhs)
Simple_Calculation.Multiply rhs -> input_column * (table.resolve rhs)
Expand All @@ -43,6 +44,10 @@ type Simple_Expression
Simple_Calculation.And rhs -> input_column && (table.resolve rhs)
Simple_Calculation.Or rhs -> input_column || (table.resolve rhs)
Simple_Calculation.Trim where what -> input_column.trim where (table.resolve what)
Simple_Calculation.Text_Left length -> input_column.text_left (table.resolve length)
Simple_Calculation.Text_Right length -> input_column.text_right (table.resolve length)
Simple_Calculation.Text_Length -> input_column.text_length
Simple_Calculation.Format format -> input_column.format format
Simple_Calculation.If condition true_value false_value ->
condition_column = make_filter_column input_column (table.resolve_condition condition) on_problems
condition_column.iif (table.resolve true_value) (table.resolve false_value)
Expand All @@ -69,6 +74,7 @@ type Simple_Expression

builder = Vector.new_builder
fqn = Meta.get_qualified_type_name Simple_Calculation
builder.append (Option "copy" fqn+".Copy")
builder.append (Option "add" fqn+".Add" [["rhs", with_number_text]])
builder.append (Option "subtract" fqn+".Subtract" [["rhs", with_number]])
builder.append (Option "multiply" fqn+".Multiply" [["rhs", with_number]])
Expand All @@ -89,6 +95,10 @@ type Simple_Expression
builder.append (Option "or" fqn+".Or" [["rhs", with_boolean]])
builder.append (Option "if" fqn+".If" [["condition", filter_cond], ["true_value", with_all_types], ["false_value", with_all_types]])
builder.append (Option "trim" fqn+".Trim" [["what", with_text]])
builder.append (Option "text_left" fqn+".Text_Left" [["length", with_number]])
builder.append (Option "text_right" fqn+".Text_Right" [["length", with_number]])
builder.append (Option "text_length" fqn+".Text_Length")
builder.append (Option "format" fqn+".Format")

fqn_column = Meta.get_qualified_type_name Simple_Expression
derived = Option "<Simple Expression>" fqn_column+".From" [["input", with_all_types], ["operation", Single_Choice builder.to_vector]]
Expand All @@ -97,6 +107,9 @@ type Simple_Expression

## Defines the operation on a derived column.
type Simple_Calculation
## Creates a copy of the input column.
Copy

## Add two values/columns.
Add (rhs : Column_Ref|Expression|Number|Text)

Expand Down Expand Up @@ -161,3 +174,15 @@ type Simple_Calculation
## Removes the specified characters, by default any whitespace, from the
start, the end, or both ends of the input.
Trim (where:Location = Location.Both) (what:Column_Ref|Expression|Text = "")

## Takes the first characters from the input column.
Text_Left (length : Column_Ref|Expression|Integer = 1)

## Takes the last characters from the input column.
Text_Right (length : Column_Ref|Expression|Integer = 1)

## Returns the character length of the input column.
Text_Length

## Formats a column with the specified format.
Format (format : Text = "")
2 changes: 2 additions & 0 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,7 @@ type Table
allows to join the two tables on equality of corresponding columns with
the same name. So `table.join other on=["A", "B"]` is a shorthand for:
table.join other on=[Join_Condition.Equals "A" "A", Join_Condition.Equals "B" "B"]
@join_kind Widget_Helpers.make_join_kind_selector
@on Widget_Helpers.make_join_condition_selector
join : Table -> Join_Kind -> Vector (Join_Condition | Text) | Text -> Text -> Problem_Behavior -> Table
join self right:Table (join_kind : Join_Kind = Join_Kind.Left_Outer) on=[Join_Condition.Equals self.column_names.first] right_prefix="Right " on_problems=Report_Warning = Out_Of_Memory.handle_java_exception "join" <|
Expand Down Expand Up @@ -2352,6 +2353,7 @@ type Table

## ALIAS count
GROUP Standard.Base.Metadata
ICON metadata
Returns the number of rows in this table.

> Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,11 @@ type Excel_Workbook

Arguments:
- name: the name of the worksheet to read.
- headers: whether to use the first row as headers (default is `Infer`).
@name (self-> Single_Choice display=Display.Always values=(self.sheet_names.map t-> Option t t.pretty))
sheet : Text | Integer -> Table
sheet self name:(Text | Integer) =
self.read_section (Excel_Section.Worksheet name 0 Nothing)
sheet : Text | Integer -> Boolean | Infer -> Table
sheet self name:(Text | Integer) headers:(Boolean | Infer)=Infer =
self.read_section (Excel_Section.Worksheet name headers 0 Nothing)

## PRIVATE
ADVANCED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ from project.Extensions.Table_Conversions import all
Make an aggregate column selector.
make_aggregate_column_selector : Table -> Display -> Widget
make_aggregate_column_selector table display=Display.Always =
col_names_selector = make_column_name_selector table display=Display.Always
col_names_selector = make_column_name_selector table display=Display.Always add_expression=True
column_widget = ["column", col_names_selector]

fqn = Meta.get_qualified_type_name Aggregate_Column
Expand Down Expand Up @@ -138,6 +138,14 @@ make_filter_condition_selector table display=Display.Always =
builder.append (Option "Is In" fqn+".Is_In")
Single_Choice builder.to_vector display=display

## PRIVATE
Make a join kind selector - Needed to override display.
make_join_kind_selector : Display -> Widget
make_join_kind_selector display=Display.Always =
meta = Meta.meta Value_Type
options = meta.constructors.map c-> Option c.name meta.qualified_name+"."+c.name
Single_Choice display=display values=options

## PRIVATE
Make a join condition selector.
make_join_condition_selector : Table -> Display -> Widget
Expand Down
3 changes: 1 addition & 2 deletions test/Benchmarks/src/Map/Hash_Map.enso
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from Standard.Base import all

from Standard.Table import Column, Value_Type, Auto
import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Column, Value_Type, Auto, Bits

from Standard.Test import Bench

Expand Down
3 changes: 1 addition & 2 deletions test/Benchmarks/src/Table/Column_From_Vector.enso
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from Standard.Base import all

from Standard.Table import Column, Value_Type, Auto
import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Column, Value_Type, Auto, Bits

from Standard.Test import Bench

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from Standard.Base import all
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument

import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Value_Type
from Standard.Table import Value_Type, Bits
from Standard.Table.Errors import Missing_Input_Columns, Conversion_Failure

from Standard.Database.Errors import Unsupported_Database_Operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ add_specs suite_builder setup =
t3 = table_builder [["X", [Time_Of_Day.new 10 15 0]], ["Y", [Time_Of_Day.new 12 30 20]]]

# There is no default period:
(t3.at "X").date_diff (t3.at "Y") . should_be_a Function
(t3.at "X").date_diff (t3.at "Y") Date_Period.Month . should_fail_with Illegal_Argument

# This will always be 0, should it be allowed?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ add_specs suite_builder setup =

group_builder.specify "arithmetics" <|
t = table_builder [["A", [1, 2]], ["B", [10, 40]]]
t.set (Simple_Expression.From (Column_Ref.Name "A") Simple_Calculation.Copy) "C" . at "C" . to_vector . should_equal [1, 2]
t.set (Simple_Expression.From (Column_Ref.Name "A") (Simple_Calculation.Add (Column_Ref.Name "B"))) "C" . at "C" . to_vector . should_equal [11, 42]
t.set (Simple_Expression.From 100 (Simple_Calculation.Add (Column_Ref.Name "B"))) "C" . at "C" . to_vector . should_equal [110, 140]
t.set (Simple_Expression.From (Column_Ref.Name "A") (Simple_Calculation.Add 100)) "C" . at "C" . to_vector . should_equal [101, 102]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from Standard.Base import all
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument

import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import all
from Standard.Table.Errors import all

Expand Down
3 changes: 1 addition & 2 deletions test/Table_Tests/src/Common_Table_Operations/Map_Spec.enso
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from Standard.Base import all
import Standard.Base.Errors.Illegal_State.Illegal_State

from Standard.Table import Value_Type, Auto
from Standard.Table import Value_Type, Auto, Bits
from Standard.Table.Errors import Invalid_Value_Type
import Standard.Table.Data.Type.Value_Type.Bits

from Standard.Database.Errors import Unsupported_Database_Operation

Expand Down
3 changes: 1 addition & 2 deletions test/Table_Tests/src/Database/Postgres_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
import Standard.Base.Errors.Illegal_State.Illegal_State
import Standard.Base.Runtime.Ref.Ref

import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Table, Value_Type, Aggregate_Column, expr
from Standard.Table import Table, Value_Type, Aggregate_Column, Bits, expr
from Standard.Table.Errors import Invalid_Column_Names, Inexact_Type_Coercion, Duplicate_Output_Column_Names

import Standard.Database.DB_Column.DB_Column
Expand Down
3 changes: 1 addition & 2 deletions test/Table_Tests/src/Database/Redshift_Spec.enso
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from Standard.Base import all
import Standard.Base.Runtime.Ref.Ref

import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Table, Value_Type
from Standard.Table import Table, Value_Type, Bits

from Standard.Database import all

Expand Down
3 changes: 1 addition & 2 deletions test/Table_Tests/src/Database/SQLite_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import Standard.Base.Errors.File_Error.File_Error
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument


import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Table, Value_Type
from Standard.Table import Table, Value_Type, Bits
from Standard.Table.Errors import Invalid_Column_Names, Duplicate_Output_Column_Names

import Standard.Database.Internal.Replace_Params.Replace_Params
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from Standard.Base import all
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument

from Standard.Table import Aggregate_Column, Value_Type, Table
import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Aggregate_Column, Value_Type, Table, Bits
from Standard.Table.Errors import Inexact_Type_Coercion

from Standard.Database import all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from Standard.Base import all

import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Aggregate_Column, Value_Type, Table
from Standard.Table import Aggregate_Column, Value_Type, Table, Bits
from Standard.Table.Errors import Invalid_Value_Type, Inexact_Type_Coercion

import Standard.Database.Dialect
Expand Down
1 change: 0 additions & 1 deletion test/Table_Tests/src/Database/Upload_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Standard.Base.Runtime.Context
import Standard.Base.Runtime.Managed_Resource.Managed_Resource
import Standard.Base.Runtime.Ref.Ref

import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import all
from Standard.Table.Errors import all

Expand Down
6 changes: 6 additions & 0 deletions test/Table_Tests/src/IO/Excel_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,9 @@ add_specs suite_builder =
table_3 = xlsx_sheet.read . read "Sheet1"
check_table table_3

table_4 = xlsx_sheet.read . sheet "Sheet1"
check_table table_4

group_builder.specify "should let you read XLS by sheet index" <|
table = xls_sheet.read (Excel_Format.Sheet 1)
check_table table
Expand All @@ -809,6 +812,9 @@ add_specs suite_builder =
table_2.row_count . should_equal col_a.length
check_table table_2

table_4 = xlsx_sheet.read . sheet 1
check_table table_4

group_builder.specify "should let you read XLS by sheet name" <|
table = xls_sheet.read (Excel_Format.Sheet "Sheet1")
check_table table
Expand Down
4 changes: 1 addition & 3 deletions test/Table_Tests/src/In_Memory/Column_Format_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import Standard.Base.Errors.Time_Error.Time_Error
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
import Standard.Base.Data.Time.Errors.Date_Time_Format_Parse_Error

import Standard.Table.Data.Type.Value_Type.Bits

from Standard.Table import Column, Value_Type
from Standard.Table import Column, Value_Type, Bits
from Standard.Table.Errors import Invalid_Value_Type
from Standard.Table.Internal.Column_Format import all

Expand Down
3 changes: 1 addition & 2 deletions test/Table_Tests/src/In_Memory/Column_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import Standard.Base.Errors.Common.Type_Error
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
import Standard.Test.Extensions

from Standard.Table import Column, Value_Type, Auto
import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import Column, Value_Type, Auto, Bits
from Standard.Table.Errors import Invalid_Value_Type, Invalid_Column_Names

from Standard.Test import all
Expand Down
1 change: 0 additions & 1 deletion test/Table_Tests/src/In_Memory/Integer_Overflow_Spec.enso
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from Standard.Base import all

import Standard.Table.Data.Type.Value_Type.Bits
from Standard.Table import all
from Standard.Table.Errors import Arithmetic_Overflow, Conversion_Failure, Invalid_Value_Type, No_Common_Type, Loss_Of_Integer_Precision

Expand Down

0 comments on commit 19f15b8

Please sign in to comment.