Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(perf) change cast from string to int #468

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion backend/gn_module_import/checks/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,32 @@ def check_nomenclatures(imprt, fields):
def set_column_from_referential(imprt, field, reference, error_type, whereclause=None):
source_field = getattr(ImportSyntheseData, field.source_field)
synthese_field = getattr(ImportSyntheseData, field.synthese_field)
cast_cte = (
select(
[
ImportSyntheseData.id_import,
func.try_cast_int(source_field, -1).label("source_field"),
ImportSyntheseData.line_no,
]
)
.where(
ImportSyntheseData.id_import == imprt.id_import,
)
.cte("cast_cte")
)

stmt = (
update(ImportSyntheseData)
.where(ImportSyntheseData.id_import == cast_cte.c.id_import)
.where(ImportSyntheseData.line_no == cast_cte.c.line_no)
.values(
{
synthese_field: reference,
}
)
.where(
sa.and_(
sa.cast(source_field, sa.Integer) == reference,
cast_cte.c.source_field == reference,
ImportSyntheseData.id_import == imprt.id_import,
)
)
Expand All @@ -197,6 +213,18 @@ def set_column_from_referential(imprt, field, reference, error_type, whereclause
synthese_field == None,
),
)
report_erroneous_rows(
imprt,
error_type="INVALID_INTEGER",
error_column=field.name_field,
whereclause=sa.and_(
ImportSyntheseData.id_import == cast_cte.c.id_import,
ImportSyntheseData.line_no == cast_cte.c.line_no,
source_field != None,
source_field != "",
cast_cte.c.source_field == -1,
),
)


def set_cd_nom(imprt, fields):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""try cast function

Revision ID: 74e7c35f22c6
Revises: 8611f7aab8dc
Create Date: 2023-09-05 17:51:22.487702

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "74e7c35f22c6"
down_revision = "8611f7aab8dc"
branch_labels = None
depends_on = None


def upgrade():
op.execute(
"""
CREATE FUNCTION try_cast_int (p_in text, p_default int default null)
RETURNS INTEGER
LANGUAGE plpgsql
AS
$function$
BEGIN
BEGIN
RETURN p_in::INTEGER;
EXCEPTION
WHEN OTHERS THEN
RETURN p_default;
END;
END;
$function$
"""
)


def downgrade():
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop function

Loading