Skip to content

Commit

Permalink
Remove table aliases (#9098)
Browse files Browse the repository at this point in the history
Now Table and DB_Table have different names we can remove a lot of aliases. This closes #8981
  • Loading branch information
AdRiley authored Feb 20, 2024
1 parent c811a5a commit 4cbdc9d
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Standard.Base.Runtime.Ref.Ref
from Standard.Base.Metadata.Choice import Option
from Standard.Base.Metadata.Widget import Single_Choice, Vector_Editor

import Standard.Table.Data.Table.Table as Materialized_Table
import Standard.Table.Data.Table.Table
import Standard.Table.Data.Type.Value_Type.Value_Type
import Standard.Table.Internal.Column_Naming_Helper.Column_Naming_Helper

Expand All @@ -18,8 +18,8 @@ import project.Data.Dialect.Dialect
import project.Data.SQL_Query.SQL_Query
import project.Data.SQL_Statement.SQL_Statement
import project.Data.SQL_Type.SQL_Type
import project.Data.DB_Table as Database_Table_Module
import project.Data.DB_Table.DB_Table as Database_Table
import project.Data.DB_Table as DB_Table_Module
import project.Data.DB_Table.DB_Table
import project.Internal.Connection.Entity_Naming_Properties.Entity_Naming_Properties
import project.Internal.Hidden_Table_Registry
import project.Internal.IR.Context.Context
Expand Down Expand Up @@ -152,7 +152,7 @@ type Connection
- all_fields: Return all the fields in the metadata table.
@types make_table_types_selector
@schema make_schema_selector
tables : Text -> Text -> Text -> Vector Text | Text | Nothing -> Boolean -> Materialized_Table
tables : Text -> Text -> Text -> Vector Text | Text | Nothing -> Boolean -> Table
tables self name_like=Nothing database=self.database schema=Nothing types=self.dialect.default_table_types all_fields=False =
self.get_tables_advanced name_like=name_like database=database schema=schema types=types all_fields=all_fields include_hidden=False

Expand Down Expand Up @@ -210,7 +210,7 @@ type Connection
- If provided with a `Table_Name` or a text short-hand and the table is
not found, a `Table_Not_Found` error is raised.
@query make_table_name_selector
query : Text | SQL_Query -> Text -> Database_Table ! Table_Not_Found | SQL_Error
query : Text | SQL_Query -> Text -> DB_Table ! Table_Not_Found | SQL_Error
query self query alias="" = case query of
_ : Text ->
result = self.query alias=alias <|
Expand All @@ -235,7 +235,7 @@ type Connection
will actually result in both columns `A` and `A 1` containing
the value 1; and value 2 being lost. That is why such queries
must fail.
r = Database_Table_Module.make_table self name columns ctx on_problems=Problem_Behavior.Report_Error
r = DB_Table_Module.make_table self name columns ctx on_problems=Problem_Behavior.Report_Error
r.catch Any error->
Error.throw (Illegal_Argument.Error "The provided custom SQL query is invalid and may suffer data corruption when being processed, especially if it contains aliased column names, it may not be interpreted correctly. Please ensure the names are unique. The original error was: "+error.to_display_text cause=error)
SQL_Query.Table_Name name ->
Expand Down Expand Up @@ -265,7 +265,7 @@ type Connection
`execute_update` for DML queries, or if they are supposed to return
results, the `read` should be wrapped in an execution context check.
@query make_table_name_selector
read : Text | SQL_Query -> Integer | Nothing -> Boolean -> Materialized_Table ! Table_Not_Found
read : Text | SQL_Query -> Integer | Nothing -> Boolean -> Table ! Table_Not_Found
read self query (limit : Integer | Nothing = 1000) (warn_if_more_rows : Boolean = True) =
self.query query . read max_rows=limit warn_if_more_rows=warn_if_more_rows

Expand Down Expand Up @@ -311,8 +311,8 @@ type Connection
(like missing columns) and if successful, return a temporary table with
a `Dry_Run_Operation` warning attached.
@structure make_structure_creator
create_table : Text -> Vector Column_Description | Database_Table | Materialized_Table -> Vector Text | Nothing -> Boolean -> Boolean -> Problem_Behavior -> Database_Table ! Table_Already_Exists
create_table self (table_name : Text) (structure : Vector Column_Description | Database_Table | Materialized_Table) (primary_key : (Vector Text | Nothing) = [first_column_name_in_structure structure]) (temporary : Boolean = False) (allow_existing : Boolean = False) (on_problems:Problem_Behavior = Problem_Behavior.Report_Warning) =
create_table : Text -> Vector Column_Description | DB_Table | Table -> Vector Text | Nothing -> Boolean -> Boolean -> Problem_Behavior -> DB_Table ! Table_Already_Exists
create_table self (table_name : Text) (structure : Vector Column_Description | DB_Table | Table) (primary_key : (Vector Text | Nothing) = [first_column_name_in_structure structure]) (temporary : Boolean = False) (allow_existing : Boolean = False) (on_problems:Problem_Behavior = Problem_Behavior.Report_Warning) =
create_table_implementation self table_name structure primary_key temporary allow_existing on_problems

## PRIVATE
Expand All @@ -326,7 +326,7 @@ type Connection
to ensure that default `ResultSet` metadata is used for these columns.
- last_row_only: If set true, only the last row of the query is fetched.
Defaults to false.
read_statement : SQL_Statement -> (Nothing | Vector SQL_Type_Reference) -> Boolean -> Materialized_Table
read_statement : SQL_Statement -> (Nothing | Vector SQL_Type_Reference) -> Boolean -> Table
read_statement self statement column_type_suggestions=Nothing last_row_only=False =
type_overrides = self.dialect.get_type_mapping.prepare_type_overrides column_type_suggestions
statement_setter = self.dialect.get_statement_setter
Expand Down Expand Up @@ -432,7 +432,7 @@ type Connection

Once all references to the table with this name are destroyed, the table
will be marked for removal and dropped at the next maintenance.
internal_allocate_dry_run_table : Text -> Database_Table
internal_allocate_dry_run_table : Text -> DB_Table
internal_allocate_dry_run_table self table_name =
ref = self.hidden_table_registry.make_reference table_name
make_table_for_name self table_name table_name ref
Expand Down Expand Up @@ -473,6 +473,6 @@ make_table_for_name connection name alias internal_temporary_keep_alive_referenc
fix that - so we are good with just attaching the problems as a
warning. We do not want to fail, as we do want to allow the user to
access any table already present in the database.
Database_Table_Module.make_table connection name columns ctx on_problems=Problem_Behavior.Report_Warning
DB_Table_Module.make_table connection name columns ctx on_problems=Problem_Behavior.Report_Warning
result.catch SQL_Error sql_error->
Error.throw (Table_Not_Found.Error name sql_error treated_as_query=False extra_message="")
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Standard.Table.Data.Join_Kind_Cross.Join_Kind_Cross
import Standard.Table.Data.Match_Columns as Match_Columns_Helpers
import Standard.Table.Data.Report_Unmatched.Report_Unmatched
import Standard.Table.Data.Row.Row
import Standard.Table.Data.Table.Table as Materialized_Table
import Standard.Table.Data.Table.Table
import Standard.Table.Data.Type.Value_Type_Helpers
import Standard.Table.Extensions.Table_Ref.Table_Ref
import Standard.Table.Internal.Add_Row_Number
Expand Down Expand Up @@ -2395,7 +2395,7 @@ type DB_Table
If `Nothing`, all available rows are returned.
- warn_if_more_rows: if set to `True`, a warning is attached to the
result if the number of rows returned by the query exceeds `max_rows`.
read : (Integer | Nothing) -> Boolean -> Materialized_Table
read : (Integer | Nothing) -> Boolean -> Table
read self (max_rows : Integer | Nothing = 1000) (warn_if_more_rows:Boolean = True) =
preprocessed = if max_rows.is_nothing then self else
if warn_if_more_rows then self.limit max_rows+1 else self.limit max_rows
Expand Down Expand Up @@ -2445,7 +2445,7 @@ type DB_Table

The table lists all columns, counts of non-null items and value types of
each column.
info : Materialized_Table
info : Table
info self =
cols = self.internal_columns
count_query =
Expand All @@ -2464,7 +2464,7 @@ type DB_Table
type_mapping = self.connection.dialect.get_type_mapping
types = cols.map col->
type_mapping.sql_type_to_value_type col.sql_type_reference.get
Materialized_Table.new [["Column", cols.map .name], ["Items Count", counts], ["Value Type", types]]
Table.new [["Column", cols.map .name], ["Items Count", counts], ["Value Type", types]]

## PRIVATE

Expand Down Expand Up @@ -2768,7 +2768,7 @@ make_table connection table_name columns ctx on_problems =
line will be included that will say how many hidden rows there are.
- format_term: A boolean flag, specifying whether to use ANSI escape codes
for rich formatting in the terminal.
display_dataframe : Materialized_Table -> Integer -> Integer -> Boolean -> Text
display_dataframe : Table -> Integer -> Integer -> Boolean -> Text
display_dataframe df indices_count all_rows_count format_terminal =
cols = Vector.from_polyglot_array df.java_table.getColumns
col_names = cols.map .getName . map normalize_string_for_display
Expand All @@ -2792,12 +2792,12 @@ default_join_condition table join_kind = case join_kind of
_ -> [Join_Condition.Equals table.column_names.first]

## PRIVATE
Materialized_Table.from (that:DB_Table) =
Table.from (that:DB_Table) =
_ = [that]
Error.throw (Illegal_Argument.Error "Currently cross-backend operations are not supported. Materialize the table using `.read` before mixing it with an in-memory Table.")

## PRIVATE
DB_Table.from (that:Materialized_Table) =
DB_Table.from (that:Table) =
_ = [that]
Error.throw (Illegal_Argument.Error "Currently cross-backend operations are not supported. Either materialize the other table using `.read` or upload the table into the database using `.select_into_database_table`.")

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

import Standard.Table.Data.Table.Table as In_Memory_Table
import Standard.Table.Data.Table.Table
import Standard.Table.Internal.Widget_Helpers
from Standard.Table.Errors import all

Expand Down Expand Up @@ -131,8 +131,8 @@ DB_Table.select_into_database_table self connection (table_name : Text) primary_
More expensive checks, like clashing keys or unmatched rows are checked
only on a sample of rows, so errors may still occur when the output action
is enabled.
DB_Table.update_rows : DB_Table | In_Memory_Table -> Update_Action -> Vector Text | Nothing -> Boolean -> Problem_Behavior -> DB_Table ! Table_Not_Found | Unmatched_Columns | Missing_Input_Columns | Column_Type_Mismatch | SQL_Error | Illegal_Argument
DB_Table.update_rows self (source_table : DB_Table | In_Memory_Table) (update_action : Update_Action = Update_Action.Update_Or_Insert) (key_columns : Vector | Nothing = default_key_columns self) (error_on_missing_columns : Boolean = False) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
DB_Table.update_rows : DB_Table | Table -> Update_Action -> Vector Text | Nothing -> Boolean -> Problem_Behavior -> DB_Table ! Table_Not_Found | Unmatched_Columns | Missing_Input_Columns | Column_Type_Mismatch | SQL_Error | Illegal_Argument
DB_Table.update_rows self (source_table : DB_Table | Table) (update_action : Update_Action = Update_Action.Update_Or_Insert) (key_columns : Vector | Nothing = default_key_columns self) (error_on_missing_columns : Boolean = False) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
common_update_table source_table self update_action key_columns error_on_missing_columns on_problems

## GROUP Standard.Base.Output
Expand Down Expand Up @@ -191,6 +191,6 @@ DB_Table.update_rows self (source_table : DB_Table | In_Memory_Table) (update_ac
columns, and returns the count of rows that would have been deleted by this
operation, with a `Dry_Run_Operation` warning attached.
@key_columns Widget_Helpers.make_column_name_vector_selector
DB_Table.delete_rows : DB_Table | In_Memory_Table -> Vector Text | Nothing -> Boolean -> Integer ! Missing_Input_Columns | SQL_Error
DB_Table.delete_rows self (key_values_to_delete : DB_Table | In_Memory_Table) (key_columns : Vector Text | Nothing = default_key_columns_required self) (allow_duplicate_matches : Boolean = False) =
DB_Table.delete_rows : DB_Table | Table -> Vector Text | Nothing -> Boolean -> Integer ! Missing_Input_Columns | SQL_Error
DB_Table.delete_rows self (key_values_to_delete : DB_Table | Table) (key_columns : Vector Text | Nothing = default_key_columns_required self) (allow_duplicate_matches : Boolean = False) =
common_delete_rows self key_values_to_delete key_columns allow_duplicate_matches
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Standard.Table.Internal.Widget_Helpers
from Standard.Table.Errors import all

import project.Connection.Connection.Connection
import project.Data.DB_Table.DB_Table as Database_Table
import project.Data.DB_Table.DB_Table
import project.Data.Update_Action.Update_Action
from project.Errors import all
from project.Internal.Upload_Table import all
Expand Down Expand Up @@ -56,7 +56,7 @@ from project.Internal.Upload_Table import all
More expensive checks, like clashing keys are checked only on the sample of
rows, so errors may still occur when the output action is enabled.
@primary_key Widget_Helpers.make_column_name_vector_selector
Table.select_into_database_table : Connection -> Text -> Vector Text | Nothing -> Boolean -> Problem_Behavior -> Database_Table ! Table_Already_Exists | Inexact_Type_Coercion | Missing_Input_Columns | Non_Unique_Key | SQL_Error | Illegal_Argument
Table.select_into_database_table : Connection -> Text -> Vector Text | Nothing -> Boolean -> Problem_Behavior -> DB_Table ! Table_Already_Exists | Inexact_Type_Coercion | Missing_Input_Columns | Non_Unique_Key | SQL_Error | Illegal_Argument
Table.select_into_database_table self connection (table_name : Text) primary_key=[self.columns.first.name] temporary=False on_problems=Problem_Behavior.Report_Warning =
select_into_table_implementation self connection table_name primary_key temporary on_problems

Expand Down Expand Up @@ -122,8 +122,8 @@ Table.select_into_database_table self connection (table_name : Text) primary_key
More expensive checks, like clashing keys or unmatched rows are checked
only on a sample of rows, so errors may still occur when the output action
is enabled.
Table.update_rows : Database_Table | Table -> Update_Action -> Vector Text | Nothing -> Boolean -> Problem_Behavior -> Database_Table ! Table_Not_Found | Unmatched_Columns | Missing_Input_Columns | Column_Type_Mismatch | SQL_Error | Illegal_Argument
Table.update_rows self (source_table : Database_Table | Table) (update_action : Update_Action = Update_Action.Update_Or_Insert) (key_columns : Vector | Nothing = Nothing) (error_on_missing_columns : Boolean = False) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
Table.update_rows : DB_Table | Table -> Update_Action -> Vector Text | Nothing -> Boolean -> Problem_Behavior -> DB_Table ! Table_Not_Found | Unmatched_Columns | Missing_Input_Columns | Column_Type_Mismatch | SQL_Error | Illegal_Argument
Table.update_rows self (source_table : DB_Table | Table) (update_action : Update_Action = Update_Action.Update_Or_Insert) (key_columns : Vector | Nothing = Nothing) (error_on_missing_columns : Boolean = False) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
_ = [source_table, update_action, key_columns, error_on_missing_columns, on_problems]
Error.throw (Illegal_Argument.Error "Table.update_rows modifies the underlying table, so it is only supported for Database tables - in-memory tables are immutable. Consider using `join` or `merge` for a similar operation that creates a new Table instead.")

Expand Down Expand Up @@ -183,7 +183,7 @@ Table.update_rows self (source_table : Database_Table | Table) (update_action :
columns, and returns the count of rows that would have been deleted by this
operation, with a `Dry_Run_Operation` warning attached.
@key_columns Widget_Helpers.make_column_name_vector_selector
Table.delete_rows : Table | Database_Table -> Vector Text | Nothing -> Boolean -> Integer ! Missing_Input_Columns | SQL_Error
Table.delete_rows self (key_values_to_delete : Table | Database_Table) (key_columns=[]) (allow_duplicate_matches : Boolean = False) =
Table.delete_rows : Table | DB_Table -> Vector Text | Nothing -> Boolean -> Integer ! Missing_Input_Columns | SQL_Error
Table.delete_rows self (key_values_to_delete : Table | DB_Table) (key_columns=[]) (allow_duplicate_matches : Boolean = False) =
_ = [key_values_to_delete, key_columns, allow_duplicate_matches]
Error.throw (Illegal_Argument.Error "Table.delete_rows modifies the underlying table, so it is only supported for Database tables - in-memory tables are immutable. If you want to create a copy of this table with some rows removed based on a key from another table, you can use a Left Exclusive join, e.g. `table.join key_values_to_delete join_kind=Join_Kind.Left_Exclusive on=key_columns`.")
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import Standard.Base.Runtime.Context
import Standard.Base.Runtime.Managed_Resource.Managed_Resource
from Standard.Base.Data.Enso_Cloud.Enso_Secret import as_hideable_value

import Standard.Table.Data.Table.Table as Materialized_Table
import Standard.Table.Data.Table.Table
import Standard.Table.Data.Type.Value_Type.Value_Type

import project.Data.SQL_Statement.SQL_Statement
import project.Data.SQL_Type.SQL_Type
import project.Data.DB_Table.DB_Table as Database_Table
import project.Internal.Column_Fetcher as Column_Fetcher_Module
import project.Internal.In_Transaction.In_Transaction
import project.Internal.SQL_Warning_Helper
Expand Down Expand Up @@ -158,7 +157,7 @@ type JDBC_Connection

It may be used for accessing simple database metadata. It should never be
used for proper queries.
raw_read_statement : Text -> Materialized_Table
raw_read_statement : Text -> Table
raw_read_statement self raw_sql =
self.with_prepared_statement raw_sql Statement_Setter.null stmt->
log_sql_if_enabled self stmt.toString
Expand Down Expand Up @@ -227,7 +226,7 @@ type JDBC_Connection

It is the caller's responsibility to call this method from within a
transaction to ensure consistency.
batch_insert : Text -> Statement_Setter -> Materialized_Table -> Integer -> Vector Value_Type | Nothing -> Integer | Nothing -> Nothing
batch_insert : Text -> Statement_Setter -> Table -> Integer -> Vector Value_Type | Nothing -> Integer | Nothing -> Nothing
batch_insert self insert_template statement_setter table batch_size expected_type_hints=Nothing row_limit=Nothing =
In_Transaction.ensure_in_transaction <| self.with_connection java_connection-> handle_sql_errors related_query=insert_template <|
Managed_Resource.bracket (java_connection.prepareStatement insert_template) .close stmt->
Expand Down
Loading

0 comments on commit 4cbdc9d

Please sign in to comment.