Skip to content

[Python] Fix create_python_function for Decimal and timedelta types in the OSS client - #1988

Merged
yili-db merged 2 commits into
unitycatalog:mainfrom
maharanay22:fix-oss-decimal-timedelta-types
Oct 20, 2026
Merged

yili-db merged 2 commits into
unitycatalog:mainfrom
maharanay22:fix-oss-decimal-timedelta-types

Conversation

@maharanay22

Copy link
Copy Markdown
Contributor

Fixes #1986

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description 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

…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]>
@yili-db

yili-db commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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]>
@yili-db

yili-db commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db
yili-db merged commit 484acea into unitycatalog:main Oct 20, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] create_python_function fails for Decimal and timedelta types in the OSS client

2 participants