Repository navigation
[Python] Fix create_python_function for Decimal and timedelta types in the OSS client - #1988
Conversation
…n the OSS client
UnitycatalogFunctionClient.create_python_function failed for any
decimal.Decimal or datetime.timedelta parameter or return type. The base
type name was taken by splitting the SQL type on "<" only, so
"DECIMAL(38, 18)" and "INTERVAL DAY TO SECOND" were sent as the type name
and rejected. Split on "(" and spaces too, so they become DECIMAL and
INTERVAL
Executing a function with a timedelta argument then failed because the
interval type text check was case sensitive while the stored type text is
upper case, so compare it case insensitively
Signed-off-by: Maha Rana Yadavalli <[email protected]>
|
/review |
yili-db
left a comment
There was a problem hiding this comment.
Thanks for the fix! The split change and the case-insensitive interval check look right to me, and nested types like ARRAY<DECIMAL(38, 18)> still resolve to ARRAY. A few comments inline, none blocking.
| raise ValueError(error_message) from e | ||
|
|
||
| base_type_name = sql_type.split("<", 1)[0] | ||
| base_type_name = re.split(r"[<( ]", sql_type, maxsplit=1)[0] |
There was a problem hiding this comment.
This also changes type_json for these two types: create_parameter_info in callable_utils_oss.py builds type_json["type"] from base_type_name.lower(), so it becomes "decimal" / "interval" and the precision is only kept in type_text. The existing test data in test_oss_client.py puts the full type there ("type":"interval day to second"). Should the scalar type keep the full type text, e.g. param_info["sql_type"].lower()? Nothing reads it back today, so a follow-up issue is fine too.
There was a problem hiding this comment.
Agreed it should keep the full type. #2013 already changes this same line to build type_json from the SQL type, so with both merged it becomes "decimal(38, 18)" and "interval day to second". I checked the two branches merge cleanly, so I left this line alone here to avoid a conflict (ai assisted)
| sql_return_type = validate_return_type(func_name, type_hints) | ||
|
|
||
| base_return_type_name = sql_return_type.split("<", 1)[0] | ||
| base_return_type_name = re.split(r"[<( ]", sql_return_type, maxsplit=1)[0] |
There was a problem hiding this comment.
nit: the same expression is repeated at line 755, and validate_input_parameter in client.py already derives the base name for DECIMAL/INTERVAL a different way. Can we extract a small _base_type_name(sql_type) helper so the two call sites here don't drift?
There was a problem hiding this comment.
Done, both call sites now use a small _base_type_name helper. I left validate_input_parameter as is since it works on the server's type_name and changing it felt out of scope for this PR (ai assisted)
| column_type == "INTERVAL" | ||
| and isinstance(param, datetime.timedelta) | ||
| and param_type_text != "interval day to second" | ||
| and param_type_text.lower() != "interval day to second" |
There was a problem hiding this comment.
Can we add a unit test for this next to test_validate_param_invalid_interval? Right now only the shift_time server round trip covers the upper-case path. Something like:
@pytest.mark.parametrize("text", ["interval day to second", "INTERVAL DAY TO SECOND"])
def test_validate_param_interval_case_insensitive(text):
validate_param(datetime.timedelta(days=1), "INTERVAL", text)There was a problem hiding this comment.
Added test_validate_param_interval_type_text_is_case_insensitive next to the invalid interval test, parametrized over both cases. It fails on the upper case input without the .lower() (ai assisted)
| (shift_time, {"delta": datetime.timedelta(minutes=90)}, "2024-01-01T01:30:00"), | ||
| (to_duration, {"minutes": 45}, datetime.timedelta(minutes=45)), | ||
| ] | ||
| for func, parameters, expected in cases: |
There was a problem hiding this comment.
nit: if an early case fails, the later ones never run and the report doesn't say which function broke. Can we use @pytest.mark.parametrize like the other tests in this file?
There was a problem hiding this comment.
Switched it to @pytest.mark.parametrize with one id per function, so each case runs and reports on its own (ai assisted)
…rval tests extract_function_metadata derived the base type name with the same regex in two places, so it now goes through a small _base_type_name helper. The OSS client Decimal and timedelta round trip is parametrized, so each function is reported separately and an early failure no longer hides the later cases. validate_param also gets a unit test that the day-to-second interval type text is accepted in both lower and upper case. Signed-off-by: Maha Rana Yadavalli <[email protected]>
|
/review |
Fixes #1986
PR Checklist
docsis updatedDescription of changes
create_python_function failed for any function that uses decimal.Decimal or datetime.timedelta as a parameter or return type. The base type name was taken by splitting the SQL type only on "<", so DECIMAL(38, 18) and INTERVAL DAY TO SECOND were used as type names, which are not valid. Now the split also stops at "(" and spaces, so the names become DECIMAL and INTERVAL
After that, calling a timedelta function still failed because the interval check compared the type text in lower case only, while functions created from Python store it in upper case. That comparison now ignores case
I added a unit test for the type names and a test that creates and runs Decimal and timedelta functions against a local server. Both fail before this change and pass with it
I used an AI assistant to help investigate and test this, and I reviewed every change myself