-
Notifications
You must be signed in to change notification settings - Fork 16.8k
feat(serialization): implement truncation logic for rendered values #61878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+324
−30
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
0efa378
feat(serialization): implement truncation logic for rendered values i…
imrichardwu 4663edc
Merge branch 'main' into truncation
imrichardwu ffd891a
fix(test): update assertion for rendered fields in task runner tests
imrichardwu 4081048
refactor(tests): update large string and object assertions to use ser…
imrichardwu a2c086f
Merge branch 'main' into truncation
imrichardwu 6295023
Merge branch 'main' into truncation
imrichardwu 5a702ea
Merge branch 'main' into truncation
imrichardwu 37ca60e
refactor: simplify _truncate_rendered_value function and remove redun…
imrichardwu 7910bcd
fix: correct expected output for quoted string in test case
imrichardwu e1a555e
refactor: move _truncate_rendered_value function to utils.helpers and…
imrichardwu cae896a
Merge branch 'main' into truncation
imrichardwu f1ab760
refactor: improve _truncate_rendered_value logic for better readabili…
imrichardwu c54d2c3
fix: adjust available space calculation in _truncate_rendered_value f…
imrichardwu 825e107
fix: adjust available space calculation in _truncate_rendered_value f…
imrichardwu d5feb36
Merge branch 'main' into truncation
imrichardwu 2593f85
Clean up comments
imrichardwu 135930a
Merge branch 'main' into truncation
imrichardwu 85e7097
refactor: rename _truncate_rendered_value to truncate_rendered_value …
imrichardwu 4911fb5
Merge branch 'main' into truncation
imrichardwu f08a874
feat: add shared template rendering module with truncate_rendered_val…
imrichardwu 86a3aed
refactor: cleanup
imrichardwu cd02e70
feat: enhance truncation functionality with configurable constants an…
imrichardwu 752043c
remove dups
imrichardwu 6baa73e
Merge branch 'main' into truncation
imrichardwu b6ae5ef
test: update truncation tests with dynamic boundary calculations
imrichardwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../../../shared/template_rendering/src/airflow_shared/template_rendering |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def test_serialize_template_field_with_very_small_max_length(monkeypatch): | ||
| """Test that truncation message is prioritized even for very small max_length.""" | ||
| monkeypatch.setenv("AIRFLOW__CORE__MAX_TEMPLATED_FIELD_LENGTH", "1") | ||
|
|
||
| from airflow.serialization.helpers import serialize_template_field | ||
|
|
||
| result = serialize_template_field("This is a long string", "test") | ||
|
|
||
| # The truncation message should be shown even if it exceeds max_length | ||
| # This ensures users always see why content is truncated | ||
| assert result | ||
| assert "Truncated. You can change this behaviour" in result | ||
imrichardwu marked this conversation as resolved.
Show resolved
Hide resolved
imrichardwu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| [project] | ||
| name = "apache-airflow-shared-template-rendering" | ||
| description = "Shared template rendering utilities for Airflow distributions" | ||
| version = "0.0" | ||
| classifiers = [ | ||
| "Private :: Do Not Upload", | ||
| ] | ||
|
|
||
| dependencies = [] | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "apache-airflow-devel-common", | ||
| ] | ||
|
|
||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["src/airflow_shared"] | ||
|
|
||
| [tool.ruff] | ||
| extend = "../../pyproject.toml" | ||
| src = ["src"] | ||
|
|
||
| [tool.ruff.lint.per-file-ignores] | ||
| # Ignore Doc rules et al for anything outside of tests | ||
| "!src/*" = ["D", "S101", "TRY002"] |
86 changes: 86 additions & 0 deletions
86
shared/template_rendering/src/airflow_shared/template_rendering/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| # Public truncation configuration used by ``truncate_rendered_value``. | ||
| # Exposed as module-level constants to avoid duplicating literals in callers/tests. | ||
| TRUNCATE_MIN_CONTENT_LENGTH = 7 | ||
| TRUNCATE_PREFIX = "Truncated. You can change this behaviour in [core]max_templated_field_length. " | ||
| TRUNCATE_SUFFIX = "..." | ||
|
|
||
|
|
||
| def truncate_rendered_value(rendered: str, max_length: int) -> str: | ||
| """ | ||
| Truncate a rendered template value to approximately ``max_length`` characters. | ||
|
|
||
| Behavior: | ||
|
|
||
| * If ``max_length <= 0``, an empty string is returned. | ||
| * A fixed prefix (``TRUNCATE_PREFIX``) and suffix (``TRUNCATE_SUFFIX``) are always | ||
| included when truncation occurs. The minimal truncation-only message is:: | ||
|
|
||
| f"{TRUNCATE_PREFIX}{TRUNCATE_SUFFIX}" | ||
|
|
||
| * If ``max_length`` is smaller than the length of this truncation-only message, that | ||
| message is returned in full, even though its length may exceed ``max_length``. | ||
| * Otherwise, space remaining after the prefix and suffix is allocated to the original | ||
| ``rendered`` content. Content is only appended if at least | ||
| ``TRUNCATE_MIN_CONTENT_LENGTH`` characters are available; if fewer are available, | ||
| the truncation-only message is returned instead. | ||
|
|
||
| Note: this function is best-effort — the return value is intended to be no longer than | ||
| ``max_length``, but when ``max_length < len(TRUNCATE_PREFIX + TRUNCATE_SUFFIX)`` it | ||
| intentionally returns a longer string to preserve the full truncation message. | ||
| """ | ||
imrichardwu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if max_length <= 0: | ||
| return "" | ||
|
|
||
| trunc_only = f"{TRUNCATE_PREFIX}{TRUNCATE_SUFFIX}" | ||
|
|
||
| if max_length < len(trunc_only): | ||
| return trunc_only | ||
|
|
||
| # Compute available space for content | ||
| overhead = len(TRUNCATE_PREFIX) + len(TRUNCATE_SUFFIX) | ||
| available = max_length - overhead | ||
|
|
||
| if available < TRUNCATE_MIN_CONTENT_LENGTH: | ||
| return trunc_only | ||
|
|
||
| # Slice content to fit and construct final string | ||
| content = rendered[:available].rstrip() | ||
imrichardwu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| result = f"{TRUNCATE_PREFIX}{content}{TRUNCATE_SUFFIX}" | ||
|
|
||
| if len(result) > max_length: | ||
| log.warning( | ||
| "Truncated value still exceeds max_length=%d; this should not happen.", | ||
| max_length, | ||
| ) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "TRUNCATE_MIN_CONTENT_LENGTH", | ||
| "TRUNCATE_PREFIX", | ||
| "TRUNCATE_SUFFIX", | ||
| "truncate_rendered_value", | ||
| ] | ||
16 changes: 16 additions & 0 deletions
16
shared/template_rendering/tests/template_rendering/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. |
110 changes: 110 additions & 0 deletions
110
shared/template_rendering/tests/template_rendering/test_truncate_rendered_value.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| from airflow_shared.template_rendering import ( | ||
| TRUNCATE_MIN_CONTENT_LENGTH, | ||
| TRUNCATE_PREFIX, | ||
| TRUNCATE_SUFFIX, | ||
| truncate_rendered_value, | ||
| ) | ||
|
|
||
|
|
||
| def test_truncate_rendered_value_prioritizes_message(): | ||
| """Test that truncation message is always shown first, content only if space allows.""" | ||
| trunc_only = f"{TRUNCATE_PREFIX}{TRUNCATE_SUFFIX}" | ||
| trunc_only_len = len(trunc_only) | ||
| overhead = len(TRUNCATE_PREFIX) + len(TRUNCATE_SUFFIX) | ||
| min_length_for_content = overhead + TRUNCATE_MIN_CONTENT_LENGTH | ||
|
|
||
| test_cases = [ | ||
| (1, "test", "Minimum value"), | ||
| (3, "test", "At ellipsis length"), | ||
| (5, "test", "Very small"), | ||
| (10, "password123", "Small"), | ||
| (20, "secret_value", "Small with content"), | ||
| (50, "This is a test string", "Medium"), | ||
| (overhead + 1, "Hello World", "At prefix+suffix boundary v1"), | ||
| (overhead + 2, "Hello World", "Just above boundary v1"), | ||
| (min_length_for_content - 3, "Hello World", "At overhead boundary v2"), | ||
| (90, "short", "Normal case - short string"), | ||
| (100, "This is a longer string", "Normal case"), | ||
| ( | ||
| 150, | ||
| "x" * 200, | ||
| "Large max_length, long string", | ||
| ), | ||
| (100, "None", "String 'None'"), | ||
| (100, "True", "String 'True'"), | ||
| (100, "{'key': 'value'}", "Dict-like string"), | ||
| (100, "test's", "String with apostrophe"), | ||
| (90, '"quoted"', "String with quotes"), | ||
| ] | ||
|
|
||
| for max_length, rendered, description in test_cases: | ||
| result = truncate_rendered_value(rendered, max_length) | ||
|
|
||
| assert result.startswith(TRUNCATE_PREFIX), ( | ||
| f"Failed for {description}: result should start with prefix" | ||
| ) | ||
|
|
||
| if max_length < trunc_only_len or max_length < min_length_for_content: | ||
| assert result == trunc_only, ( | ||
| f"Failed for {description}: max_length={max_length}, expected message only, got: {result}" | ||
| ) | ||
| else: | ||
| assert result.endswith(TRUNCATE_SUFFIX), ( | ||
| f"Failed for {description}: result should end with suffix" | ||
| ) | ||
| assert len(result) <= max_length, ( | ||
| f"Failed for {description}: result length {len(result)} > max_length {max_length}" | ||
| ) | ||
|
|
||
|
|
||
| def test_truncate_rendered_value_exact_expected_output(): | ||
| """Test that truncation produces exact expected output: message first, then content when space allows.""" | ||
| trunc_only = TRUNCATE_PREFIX + TRUNCATE_SUFFIX | ||
| overhead = len(TRUNCATE_PREFIX) + len(TRUNCATE_SUFFIX) | ||
| min_length_for_content = overhead + TRUNCATE_MIN_CONTENT_LENGTH | ||
|
|
||
| test_cases = [ | ||
| (1, "test", trunc_only), | ||
| (3, "test", trunc_only), | ||
| (5, "test", trunc_only), | ||
| (10, "password123", trunc_only), | ||
| (20, "secret_value", trunc_only), | ||
| (50, "This is a test string", trunc_only), | ||
| (overhead + 1, "Hello World", trunc_only), | ||
| (overhead + 2, "Hello World", trunc_only), | ||
| (min_length_for_content - 3, "Hello World", trunc_only), | ||
| (90, "short", TRUNCATE_PREFIX + "short" + TRUNCATE_SUFFIX), | ||
| (100, "This is a longer string", TRUNCATE_PREFIX + "This is a longer st" + TRUNCATE_SUFFIX), | ||
| (150, "x" * 200, TRUNCATE_PREFIX + "x" * 69 + TRUNCATE_SUFFIX), | ||
| (100, "None", TRUNCATE_PREFIX + "None" + TRUNCATE_SUFFIX), | ||
| (100, "True", TRUNCATE_PREFIX + "True" + TRUNCATE_SUFFIX), | ||
| (100, "{'key': 'value'}", TRUNCATE_PREFIX + "{'key': 'value'}" + TRUNCATE_SUFFIX), | ||
| (100, "test's", TRUNCATE_PREFIX + "test's" + TRUNCATE_SUFFIX), | ||
| (90, '"quoted"', TRUNCATE_PREFIX + '"quoted"' + TRUNCATE_SUFFIX), | ||
| ] | ||
|
|
||
| for max_length, rendered, expected in test_cases: | ||
| result = truncate_rendered_value(rendered, max_length) | ||
| assert result == expected, ( | ||
| f"max_length={max_length}, rendered={rendered!r}:\n" | ||
| f" expected: {expected!r}\n" | ||
| f" got: {result!r}" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../../../../shared/template_rendering/src/airflow_shared/template_rendering |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.