Skip to content

Commit 22162ce

Browse files
committed
format float with scientific notation
1 parent 441743e commit 22162ce

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

ibis-server/app/util.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def _to_datetime_and_format(series: pd.Series) -> pd.Series:
2929

3030

3131
def _to_json_obj(df: pd.DataFrame) -> dict:
32-
data = df.to_dict(orient="split", index=False)
32+
data = df.map(lambda x: f"{x:.9f}" if isinstance(x, float) else x).to_dict(
33+
orient="split", index=False
34+
)
3335

3436
def default(obj):
3537
if pd.isna(obj):

ibis-server/tests/routers/v2/connector/test_postgres.py

+81
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,87 @@ async def test_query_with_dot_all(client, manifest_str, postgres: PostgresContai
208208
assert result["dtypes"] is not None
209209

210210

211+
async def test_format_floating(client, manifest_str, postgres):
212+
connection_info = _to_connection_info(postgres)
213+
response = await client.post(
214+
url=f"{base_url}/query",
215+
json={
216+
"connectionInfo": connection_info,
217+
"manifestStr": manifest_str,
218+
"sql": """
219+
SELECT
220+
0.0123e-5 AS case_scientific_original,
221+
1.23e+4 AS case_scientific_positive,
222+
-4.56e-3 AS case_scientific_negative,
223+
7.89e0 AS case_scientific_zero_exponent,
224+
0e0 AS case_scientific_zero,
225+
226+
123.456 AS case_decimal_positive,
227+
-123.456 AS case_decimal_negative,
228+
0.0000123 AS case_decimal_small,
229+
123.0000 AS case_decimal_trailing_zeros,
230+
0.0 AS case_decimal_zero,
231+
232+
0 AS case_integer_zero,
233+
0e-9 AS case_integer_zero_scientific,
234+
-1 AS case_integer_negative,
235+
9999999999 AS case_integer_large,
236+
237+
1.7976931348623157E+308 AS case_float_max,
238+
2.2250738585072014E-308 AS case_float_min,
239+
-1.7976931348623157E+308 AS case_float_min_negative,
240+
241+
1.23e4 + 4.56 AS case_mixed_addition,
242+
-1.23e-4 - 123.45 AS case_mixed_subtraction,
243+
0.0123e-5 * 1000 AS case_mixed_multiplication,
244+
123.45 / 1.23e2 AS case_mixed_division,
245+
246+
CAST('NaN' AS FLOAT) AS case_special_nan,
247+
CAST('Infinity' AS FLOAT) AS case_special_infinity,
248+
CAST('-Infinity' AS FLOAT) AS case_special_negative_infinity,
249+
NULL AS case_special_null,
250+
251+
CAST(123.456 AS FLOAT) AS case_cast_float,
252+
CAST(1.23e4 AS DECIMAL(10,5)) AS case_cast_decimal
253+
""",
254+
},
255+
)
256+
assert response.status_code == 200
257+
result = response.json()
258+
259+
assert result["data"] == [
260+
[
261+
"1.23E-7",
262+
"1.23E+4",
263+
"-0.00456",
264+
"7.89",
265+
"0",
266+
"123.456",
267+
"-123.456",
268+
"0.0000123",
269+
"123",
270+
"0",
271+
0,
272+
"0",
273+
-1,
274+
9999999999,
275+
"1.7976931348623157E+308",
276+
"2.2250738585072014E-308",
277+
"-1.7976931348623157E+308",
278+
"12304.56",
279+
"-123.450123",
280+
"0.000123",
281+
"1.0036585365853659",
282+
"nan",
283+
"inf",
284+
"-inf",
285+
None,
286+
"123.456001282",
287+
"12300.00000",
288+
]
289+
]
290+
291+
211292
async def test_dry_run_with_connection_url_and_password_with_bracket_should_not_raise_value_error(
212293
client, manifest_str, postgres: PostgresContainer
213294
):

0 commit comments

Comments
 (0)