Skip to content
Merged
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
11 changes: 2 additions & 9 deletions backend/apps/datasource/api/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ..crud.table import get_tables_by_ds_id
from ..models.datasource import CoreDatasource, CreateDatasource, TableObj, CoreTable, CoreField, FieldObj, \
TableSchemaResponse, ColumnSchemaResponse, PreviewResponse, ImportRequest
from ..utils.excel import parse_excel_preview, USER_TYPE_TO_PANDAS
from ..utils.excel import parse_excel_preview, read_import_dataframe

router = APIRouter(tags=["Datasource"], prefix="/datasource")
path = settings.EXCEL_PATH
Expand Down Expand Up @@ -576,17 +576,10 @@ def inner():
fields = sheet_info.fields

field_mapping = {f.fieldName: f.fieldType for f in fields}
dtype_dict = {
col: USER_TYPE_TO_PANDAS.get(field_mapping.get(col, 'string'), 'string')
for col in field_mapping.keys()
}

try:
df = read_import_dataframe(save_path, sheet_name, field_mapping)
if save_path.endswith(".csv"):
df = pd.read_csv(save_path, engine='c', dtype=dtype_dict)
sheet_name = "Sheet1"
else:
df = pd.read_excel(save_path, sheet_name=sheet_name, engine='calamine', dtype=dtype_dict)
except Exception as e:
raise HTTPException(500, f"{trans('i18n_ds_upload_error')}: {str(e)}")

Expand Down
41 changes: 41 additions & 0 deletions backend/apps/datasource/utils/excel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP

import pandas as pd

FIELD_TYPE_MAP = {
Expand Down Expand Up @@ -25,6 +27,45 @@ def infer_field_type(dtype) -> str:
return FIELD_TYPE_MAP.get(dtype_str, 'string')


def _round_import_integer(value):
"""Round explicitly selected integer values without a float intermediate."""
if pd.isna(value) or (isinstance(value, str) and not value.strip()):
return pd.NA
Comment on lines +32 to +33
if isinstance(value, bool):
return int(value)
try:
number = Decimal(str(value))
except InvalidOperation as exc:
raise ValueError('Invalid numeric value for integer field') from exc
if not number.is_finite():
raise ValueError('Integer fields require finite numeric values')
rounded = number.to_integral_value(rounding=ROUND_HALF_UP)
if rounded < -(2 ** 63) or rounded > 2 ** 63 - 1:
raise ValueError('Rounded value is outside the signed 64-bit integer range')
return int(rounded)


def read_import_dataframe(save_path: str, sheet_name: str, field_mapping: dict):
"""Read stored cell values; round only columns explicitly selected as integers."""
dtype_dict = {
column: 'object' if field_type == 'int' else USER_TYPE_TO_PANDAS.get(field_type, 'string')
for column, field_type in field_mapping.items()
}
if save_path.endswith('.csv'):
df = pd.read_csv(save_path, engine='c', dtype=dtype_dict)
else:
df = pd.read_excel(save_path, sheet_name=sheet_name, engine='calamine', dtype=dtype_dict)
for column, field_type in field_mapping.items():
if field_type == 'int' and column in df.columns:
try:
# Build a nullable integer array directly: Series.map can coerce
# large integers plus missing values to lossy float64 values.
df[column] = pd.array([_round_import_integer(value) for value in df[column]], dtype='Int64')
except ValueError as exc:
raise ValueError(f"Column '{column}': {exc}") from exc
return df


def parse_excel_preview(save_path: str, max_rows: int = 10):
sheets_data = []
if save_path.endswith(".csv"):
Expand Down
Loading