Skip to content

Commit 8c93ec6

Browse files
authored
chore(ibis): format float with scientific notation (#1023)
1 parent fbf56c8 commit 8c93ec6

File tree

6 files changed

+84
-5
lines changed

6 files changed

+84
-5
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:.9g}" 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_bigquery.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async def test_query(client, manifest_str):
9090
1,
9191
370,
9292
"O",
93-
172799.49,
93+
"172799.49",
9494
"1996-01-02",
9595
"1_370",
9696
"2024-01-01 23:59:59.000000",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def test_query(client, manifest_str):
9494
1,
9595
370,
9696
"O",
97-
172799.49,
97+
"172799.49",
9898
"1996-01-02",
9999
"1_370",
100100
"2024-01-01 23:59:59.000000",

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

+77
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,83 @@ 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"][0][0] == "1.23E-7"
260+
assert result["data"][0][1] == "1.23E+4"
261+
assert result["data"][0][2] == "-0.00456"
262+
assert result["data"][0][3] == "7.89"
263+
assert result["data"][0][4] == "0"
264+
assert result["data"][0][5] == "123.456"
265+
assert result["data"][0][6] == "-123.456"
266+
assert result["data"][0][7] == "0.0000123"
267+
assert result["data"][0][8] == "123"
268+
assert result["data"][0][9] == "0"
269+
assert result["data"][0][10] == 0
270+
assert result["data"][0][11] == "0"
271+
assert result["data"][0][12] == -1
272+
assert result["data"][0][13] == 9999999999
273+
assert result["data"][0][14] == "1.7976931348623157E+308"
274+
assert result["data"][0][15] == "2.2250738585072014E-308"
275+
assert result["data"][0][16] == "-1.7976931348623157E+308"
276+
assert result["data"][0][17] == "12304.56"
277+
assert result["data"][0][18] == "-123.450123"
278+
assert result["data"][0][19] == "0.000123"
279+
assert result["data"][0][20] == "1.0036585365853659"
280+
assert result["data"][0][21] == "nan"
281+
assert result["data"][0][22] == "inf"
282+
assert result["data"][0][23] == "-inf"
283+
assert result["data"][0][24] is None
284+
assert result["data"][0][25] == "123.456001"
285+
assert result["data"][0][26] == "12300.00000"
286+
287+
211288
async def test_dry_run_with_connection_url_and_password_with_bracket_should_not_raise_value_error(
212289
client, manifest_str, postgres: PostgresContainer
213290
):

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async def test_query(client, manifest_str, trino: TrinoContainer):
111111
1,
112112
370,
113113
"O",
114-
172799.49,
114+
"172799.49",
115115
"1996-01-02",
116116
"1_370",
117117
"2024-01-01 23:59:59.000000",

ibis-server/tests/routers/v3/connector/postgres/test_query.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async def test_query(client, manifest_str, connection_info):
115115
"2024-01-01 23:59:59.000000 UTC",
116116
"2024-01-16 04:00:00.000000 UTC", # utc-5
117117
"2024-07-16 03:00:00.000000 UTC", # utc-4
118-
172799.49,
118+
"172799.49",
119119
"1_370",
120120
370,
121121
"1996-01-02",

0 commit comments

Comments
 (0)