Skip to content

refactor(pyamber, amber): remove redundant Python code - #9550

Merged
aglinxinyuan merged 3 commits into
apache:mainfrom
aglinxinyuan:refactor/pyamber-redundant-code-cleanup
Nov 25, 2026
Merged

aglinxinyuan merged 3 commits into
apache:mainfrom
aglinxinyuan:refactor/pyamber-redundant-code-cleanup

Conversation

@aglinxinyuan

@aglinxinyuan aglinxinyuan commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

Remove redundant statements, unused imports, unnecessary control-flow branches, and legacy idioms across amber/src/main/python/ and amber/src/main/scala/org/apache/texera/web/resource/aiassistant/type_annotation_visitor.py with no runtime behavior change:

  • Redundant statements and unused imports: Remove redundant pass statements in docstring-only abstract methods and marker classes (InternalMarker, BufferedItemWriter, ReadonlyVirtualDocument, and UDFOperatorV2/UDFSourceOperator/UDFTableOperator/UDFBatchOperator), remove trailing return None in BackpressureHandler, drop unused import core, unnecessary dict unpacking, and intermediate return variables in iceberg_utils and timestamp, and remove the unused enumerate index in TypeAnnotationVisitor.
  • Unnecessary control-flow branches: Remove redundant else/elif blocks following return in detect_attribute_type_from_arrow_field, Table.__eq__, DocumentFactory, and IcebergCatalogInstance.get_instance, and merge identical staticmethod decorator checks in TypeAnnotationVisitor.
  • Idiomatic constructors, formatting, and stdlib equivalents: Replace list()/dict()/bytes() with literal forms ([]/{}/b""), replace super(ProxyServer, self) with super(), replace map(lambda ...) with a generator expression and simplify the payload conditional in ProxyServer, adopt @functools.cache in attribute_type, use !r f-strings in Schema, ProxyServer, and ExpressionEvaluator, and drop the redundant str() in Heartbeat.

Any related issues, documentation, discussions?

Closes #9543.

How was this PR tested?

Ran in amber/ on 2a319878af (Python 3.12.10, ruff 0.14.7, pytest 7.4.0):

ruff check src/main/python src/test/python
ruff format --check src/main/python src/test/python
ruff check src/main/scala/org/apache/texera/web/resource/aiassistant/type_annotation_visitor.py
PYTHONPATH=src/main/scala/org/apache/texera/web/resource/aiassistant \
  pytest src/main/scala/org/apache/texera/web/resource/aiassistant/test_type_annotation_visitor.py -q
pytest \
  src/test/python/core/architecture/handlers/actorcommand/test_actor_handler_base.py \
  src/test/python/core/architecture/managers/test_exception_manager.py \
  src/test/python/core/architecture/rpc/test_async_rpc_client.py \
  src/test/python/core/architecture/sendsemantics/test_partitioners.py \
  src/test/python/core/models/schema/test_arrow_schema_utils.py \
  src/test/python/core/models/schema/test_attribute_type.py \
  src/test/python/core/models/schema/test_schema.py \
  src/test/python/core/models/test_table.py \
  src/test/python/core/proxy/test_proxy_client.py \
  src/test/python/core/proxy/test_proxy_server.py \
  src/test/python/core/runnables/test_console_message.py \
  src/test/python/core/runnables/test_heartbeat.py \
  src/test/python/core/runnables/test_main_loop.py \
  src/test/python/core/storage/iceberg/test_iceberg_catalog_instance.py \
  src/test/python/core/storage/iceberg/test_iceberg_utils_large_binary.py \
  src/test/python/core/storage/model/test_virtual_document.py \
  src/test/python/core/storage/test_document_factory.py \
  src/test/python/pytexera/udf/test_udf_operator.py -q
pytest src/test/python/core/storage/iceberg/test_iceberg_utils_catalog.py \
  -k "not test_signed_filesystems_are_isolated_between_file_ios" -q
pytest src/test/python/core/util/test_expression_evaluator.py \
  -k "not object and not container" -q
  • ruff check and ruff format --check (218 files): passed.
  • test_type_annotation_visitor.py: 5 passed.
  • 18 targeted amber Python unit-test suites: 320 passed, 9 skipped (test_attribute_type.py Windows time.tzset skips).
  • test_iceberg_utils_catalog.py (excluding the 3 s3fs-dependent cases absent locally): 19 passed.
  • test_expression_evaluator.py (excluding the 2 --import-mode=importlib repr cases): 3 passed.
  • After review (38bd50c909, macOS): ruff check and ruff format --check passed; test_attribute_type.py, test_heartbeat.py, test_schema.py, and test_arrow_schema_utils.py: 76 passed; all 135 hand-written Python sources under src/main/python and aiassistant/ compile under Python 3.10.19.

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Antigravity (Gemini 4 Neon)

Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Automated Reviewer Suggestions

Based on the git blame history of the changed files, we recommend the following reviewers:

  • Contributors with relevant context: @mengw15, @Yicong-Huang, @kz930
    You can notify them by mentioning @mengw15, @Yicong-Huang, @kz930 in a comment.

@codecov-commenter

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.68%. Comparing base (c15f724) to head (6e01760).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
amber/src/main/python/core/proxy/proxy_server.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9550      +/-   ##
============================================
+ Coverage     91.67%   91.68%   +0.01%     
+ Complexity     5134     5028     -106     
============================================
  Files          1304     1300       -4     
  Lines         58107    57155     -952     
  Branches       7584     7502      -82     
============================================
- Hits          53272    52405     -867     
+ Misses         2923     2858      -65     
+ Partials       1912     1892      -20     
Flag Coverage Δ *Carryforward flag
access-control-service 77.44% <ø> (ø) Carriedforward from 4db10cf
agent-service 99.56% <ø> (ø) Carriedforward from 4db10cf
amber 85.51% <ø> (-0.26%) ⬇️ Carriedforward from 4db10cf
computing-unit-managing-service 64.27% <ø> (+0.43%) ⬆️ Carriedforward from 4db10cf
config-service 87.50% <ø> (ø) Carriedforward from 4db10cf
file-service 82.16% <ø> (ø) Carriedforward from 4db10cf
frontend 96.44% <ø> (-0.01%) ⬇️ Carriedforward from 4db10cf
notebook-migration-service 83.77% <ø> (ø) Carriedforward from 4db10cf
pyamber 98.58% <95.23%> (+0.19%) ⬆️
result-service 85.71% <ø> (-0.78%) ⬇️ Carriedforward from 4db10cf
workflow-compiling-service 78.30% <ø> (ø) Carriedforward from 4db10cf

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Benchmark changes need a look

🟢 0 better · 🔴 5 worse · ⚪ 10 noise (<±5%) · 0 without baseline

Compared against main c15f724 benchmarked on this same runner, so the delta is largely free of cross-runner hardware noise. The "7d avg" column still reflects the gh-pages dashboard. Treat <±5% as noise unless repeated.

Dashboard · Run

config throughput MB/s latency max Δ latest / 7d
🔴 bs=10 sw=10 sl=64 588 0.359 16,251/24,465/24,465 us 🔴 +16.4% / 🔴 +82.7%
🔴 bs=100 sw=10 sl=64 1,297 0.792 77,877/87,433/87,433 us 🔴 +6.9% / 🟢 -7.3%
⚪ bs=1000 sw=10 sl=64 1,454 0.888 685,640/754,225/754,225 us ⚪ within ±5% / 🟢 -12.2%
Baseline details

Latest main c15f724 from same runner

config metric PR latest main 7d avg Δ latest Δ 7d
bs=10 sw=10 sl=64 throughput 588 tuples/sec 641 tuples/sec 993.83 tuples/sec -8.3% -40.8%
bs=10 sw=10 sl=64 MB/s 0.359 MB/s 0.391 MB/s 0.607 MB/s -8.2% -40.8%
bs=10 sw=10 sl=64 p50 16,251 us 15,584 us 10,119 us +4.3% +60.6%
bs=10 sw=10 sl=64 p95 24,465 us 21,020 us 13,394 us +16.4% +82.7%
bs=10 sw=10 sl=64 p99 24,465 us 21,020 us 16,114 us +16.4% +51.8%
bs=100 sw=10 sl=64 throughput 1,297 tuples/sec 1,325 tuples/sec 1,327 tuples/sec -2.1% -2.3%
bs=100 sw=10 sl=64 MB/s 0.792 MB/s 0.808 MB/s 0.81 MB/s -2.0% -2.2%
bs=100 sw=10 sl=64 p50 77,877 us 72,866 us 79,097 us +6.9% -1.5%
bs=100 sw=10 sl=64 p95 87,433 us 90,617 us 86,335 us -3.5% +1.3%
bs=100 sw=10 sl=64 p99 87,433 us 90,617 us 94,338 us -3.5% -7.3%
bs=1000 sw=10 sl=64 throughput 1,454 tuples/sec 1,437 tuples/sec 1,363 tuples/sec +1.2% +6.7%
bs=1000 sw=10 sl=64 MB/s 0.888 MB/s 0.877 MB/s 0.832 MB/s +1.3% +6.7%
bs=1000 sw=10 sl=64 p50 685,640 us 699,177 us 774,633 us -1.9% -11.5%
bs=1000 sw=10 sl=64 p95 754,225 us 730,549 us 814,584 us +3.2% -7.4%
bs=1000 sw=10 sl=64 p99 754,225 us 730,549 us 858,914 us +3.2% -12.2%
Raw CSV
config_idx,batch_size,schema_width,string_len,num_batches,total_ms,total_tuples,total_bytes,tuples_per_sec,mb_per_sec,lat_p50_us,lat_p95_us,lat_p99_us
0,10,10,64,20,339.95,200,128000,588,0.359,16250.94,24464.57,24464.57
1,100,10,64,20,1542.12,2000,1280000,1297,0.792,77876.76,87433.34,87433.34
2,1000,10,64,20,13753.76,20000,12800000,1454,0.888,685640.09,754224.85,754224.85

@kunwp1 kunwp1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left some comments

datetime.UTC only exists from Python 3.11, but the computing-unit
images run the python3 that Ubuntu 22.04 ships (3.10), which
PythonUtils falls back to when udf.python.path is unset. An int in an
f-string is already formatted with str(), so {child.pid} needs no !s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aglinxinyuan
aglinxinyuan requested a review from kunwp1 November 23, 2026 22:41

@kunwp1 kunwp1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@aglinxinyuan
aglinxinyuan added this pull request to the merge queue Nov 25, 2026
Merged via the queue into apache:main with commit 461c695 Nov 25, 2026
25 checks passed
@aglinxinyuan
aglinxinyuan deleted the refactor/pyamber-redundant-code-cleanup branch November 25, 2026 01:24
renovate-bot pushed a commit to renovate-bot/apache-_-texera that referenced this pull request Nov 25, 2026
### What changes were proposed in this PR?

**Root cause.** The computing-unit images run Ubuntu 22.04's `python3`,
which is 3.10, and Python workers use it whenever `python.path` in
`udf.conf` (`UDF_PYTHON_PATH`) is unset. pyamber CI only tests 3.11 to
3.13 (ruff targets `py311`, AGENTS.md uses 3.12), so a 3.11+ API can
pass CI and then fail only in deployed workers. One already does: since
apache#8768, a UDF that fails to load gets a hint through
`BaseException.add_note`. 3.10 has no `add_note`, so a deployed worker
raises `AttributeError: 'NameError' object has no attribute 'add_note'`
in place of the user's error and the hint.

```
Before:  CI tests 3.11-3.13  ->  image runs 3.10  ->  UDF load error -> AttributeError: ... 'add_note'
After:   CI tests 3.11-3.13  ->  image runs 3.12  ->  UDF load error -> user's error + hint
```

**This also fixes the images' LICENSE.** On 3.10, pip installs three
packages that only Pythons older than 3.11 need: `async-timeout` (for
`aiohttp`), `exceptiongroup` (for `anyio`) and, since huggingface-hub
1.32, `tomli`. `amber/LICENSE-binary-python`, the Python part of the
images' `/texera/LICENSE`, lists none of them. CI didn't catch this
because only its 3.12 leg checks licenses. On 3.12 none of the three is
installed.

This PR changes the runtime stages of `computing-unit-master` and
`computing-unit-worker`:

| | Before | After |
| --- | --- | --- |
| Base | `eclipse-temurin:17-{jdk,jre}-jammy` (Ubuntu 22.04) |
`eclipse-temurin:17-{jdk,jre}-noble` (Ubuntu 24.04), same Temurin
release (17.0.20.1+1), pinned by digest |
| `python3` | 3.10.12, the system interpreter | 3.12.3, from a venv at
`/opt/venv` that leads `PATH` |
| Python packages | `/usr/local/lib/python3.10` | `/opt/venv` |

- **Why a venv.** 24.04 marks its system Python as externally managed
(PEP 668), and overriding that doesn't help: Ubuntu's own pip has no
`RECORD`, so `install.sh`'s pip bootstrap fails with `Cannot uninstall
pip 24.0, RECORD file not found. Hint: The package was installed by
debian.` Because the venv's `bin/` comes first on `PATH`, every place
that resolves `python3` by name now gets 3.12: `PythonUtils`' fallback
for Python workers, PVE creation (`python3 -m venv`, which still bases
each PVE on `/usr/bin/python3.12`), and `install.sh`. No code or config
changes.
- **Reproducibility.** The venv is created in the same layer as the pip
installs. The pip that `venv` seeds is byte-compiled differently on
every build; the `install.sh` pip upgrade in the same layer replaces it,
so the layer stays reproducible.
- **Packages.** The apt package lists are unchanged, except that the
worker also installs `python3-venv` to create the venv. `python3-pip`'s
recommended packages still install gcc, g++ and make for PVE installs
that need to compile.
- **Compatibility.** Images built `FROM` these (curated computing-unit
images) now get 3.12. In those images, `pip install` lands in the venv,
and Python packages installed with apt are no longer visible to
`python3`.
- The build stages and every other image are unchanged; none of them
runs pyamber.

### Any related issues, documentation, discussions?

Closes apache#9654. Raised in review of apache#9550, which had to keep
`datetime.timezone.utc` rather than 3.11's `datetime.UTC` for the same
reason.

### How was this PR tested?

**Both runtime stages, built from their Dockerfiles** on linux/arm64
(Docker 29.9.3, BuildKit v0.34.2). The sbt `build` stage, which this PR
doesn't change, was swapped for a stub build context. The stub holds the
files the runtime stage copies: both requirements files,
`amber/src/main/python` with generated protos, resources, license files,
and a stub `bin/computing-unit-*`.

```bash
docker buildx build --builder desktop-linux --load --build-context build=<stub> --target runtime \
  --build-arg PACKAGE_SNAPSHOT=2026-11-23 --build-arg SOURCE_DATE_EPOCH="$(git log -1 --format=%ct origin/main)" \
  -f bin/dockerfiles/computing-unit-worker.dockerfile -t texera-computing-unit-worker:py312-test .
```

The worker was built from the committed Dockerfile. For the master,
Docker Hub's anonymous pull limit had run out, so its base came from
`mirror.gcr.io` by the same digest; that copy of the Dockerfile differed
only in the registry host. In each image, run as its default `texera`
user:

| Check | `computing-unit-master` | `computing-unit-worker` |
| --- | --- | --- |
| Build | ok, 174 s, 2.66 GB | ok, 171 s, 2.51 GB |
| `command -v python3`, `python3 --version` | `/opt/venv/bin/python3`,
3.12.3 | same |
| `python3 -m pip check` | No broken requirements found. | same |
| Import every top-level module of both requirements files (31) | ok,
torch 2.13.0+cpu | same |
| `datetime.UTC`, `BaseException.add_note` | present | present |
| `python3 -m venv /tmp/pve` (how PveManager creates PVEs) | `home =
/usr/bin`, `version = 3.12.3` | same |
| `java -version` | 17.0.20.1, unchanged | same |
| `bin/licensing/check_binary_deps.py --ignore-transitive-version
python` on the image's `pip-licenses` CSV | not run | `OK: 110 Python
packages match LICENSE-binary.` |

**pyamber's suite on the image's interpreter.** This ran inside the
master image as `texera` on `/opt/venv/bin/python3`, with the tests and
`amber/pyproject.toml` mounted read-only and Postgres 16 loaded with
`sql/iceberg_postgres_catalog.sql`, as the CI pyamber job sets up:

```bash
cd /texera/amber && python3 -m pytest -m "not integration"
# 1523 passed, 59 deselected, 1 xfailed
```

**Before.** These are the old runtime steps on the old `17-jre-jammy`
base (Python 3.10.12) with `amber/requirements.txt`:

```bash
python3 -m pytest -m "not integration" src/test/python/core/architecture/managers/test_executor_manager.py
# 13 failed, 60 passed
```

All 13 failures are UDF load-error hint tests, for example
`AttributeError: 'NameError' object has no attribute 'add_note'` raised
over `NameError: name 'UDFOperatorV2' is not defined`. The same file
passes in the master image.

**The LICENSE gap.** I resolved both requirements files with `pip
install --dry-run --report` (pip 26.3, against PyPI as of today) on
Python 3.10.19 and 3.12.7, macOS arm64. 3.10 resolves 113 packages and
3.12 resolves 110. The three 3.10 adds are `async-timeout` 5.0.1,
`exceptiongroup` 1.3.2 and `tomli` 2.5.0. Each comes in through a
`python_version < "3.11"` marker, in `aiohttp`, `anyio` and
`huggingface-hub` 1.33.0 respectively. huggingface-hub added `tomli` in
1.32.0; 1.26.0, the version `main`'s file lists, through 1.31.0 don't
depend on it. No package resolves only on 3.12.
`amber/LICENSE-binary-python` names none of the three, on either `main`
or `release/v1.3`.

**Reproducibility of the new layer.** I ran that layer's first two
commands twice, in separate containers: `python3 -m venv /opt/venv`,
then `install.sh pip --upgrade setuptools wheel` with `PACKAGE_SNAPSHOT`
set. Hashing every file under `/opt/venv` gives identical results for
the two runs (1793 entries). After `python3 -m venv` alone, the two runs
differ in 495 of 1139 entries, all of them `.pyc` files, which is why
the venv is created in the install layer. On the same base, the old flow
(system interpreter, `PIP_BREAK_SYSTEM_PACKAGES=1`) stops at the pip
bootstrap with the `RECORD` error quoted above.

`bash bin/dockerfiles/snapshot/test_install.sh` passes; `install.sh`
itself is unchanged.

**Not verified:**
- linux/amd64 builds. CI's pyamber 3.12 leg does install both
requirements files on x86-64 Linux.
- The sbt build stage, which this PR doesn't touch.
- A deployment running a Python UDF end to end.

### Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove redundant Python statements, imports, and branches in Amber

4 participants