This is an automated email from the ASF dual-hosted git repository.

HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git


The following commit(s) were added to refs/heads/master by this push:
     new 2623777f [SPARK-61616][FOLLOWUP][CONNECT][PYTHON] Run the native ML 
live tests on the native classes only
2623777f is described below

commit 2623777faafdc8df4c4e537d431f98b4bf00ee27
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Fri Nov 13 20:31:06 2026 +0900

    [SPARK-61616][FOLLOWUP][CONNECT][PYTHON] Run the native ML live tests on 
the native classes only
    
    ### What changes were proposed in this pull request?
    
    A follow-up to #616 from its post-merge review, split out of the closed 
#634. The ML live tests picked their class with helpers that fell back to the 
official `pyspark.ml` class when the native import failed. So a build that 
lacked a native class silently tested the official class instead. They now pick 
it with one helper, `python/tests/_native_classes.py`. In this client it 
returns the native class or fails. Official PySpark, which has no 
`pyspark._pyspark`, still runs the tests with [...]
    
    ### Why are the changes needed?
    
    So that the live tests exercise the native implementations, as the review 
of #634 asked.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No. Tests only.
    
    ### How was this patch tested?
    
    - The eight ML live test files that use the helper: 159 passed against a 
Spark 4.3.0 Connect server, and the ML subset of `test_dropin_e2e.py`: 10 
passed.
    - Two of them run on the official `pyspark-client` 4.3.0, which uses its 
own classes: 44 passed.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    This pull request and its description were written by Isaac.
    
    Closes #638 from HyukjinKwon/SPARK-61616-followup-native-test-classes.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 python/tests/_native_classes.py                    | 33 ++++++++++++++++++++++
 python/tests/test_bisecting_kmeans_e2e.py          |  7 ++---
 python/tests/test_dropin_e2e.py                    | 14 ++-------
 python/tests/test_gaussian_mixture_e2e.py          |  7 ++---
 python/tests/test_gbt_classifier_e2e.py            |  7 ++---
 python/tests/test_gbt_regressor_e2e.py             |  7 ++---
 .../test_generalized_linear_regression_e2e.py      |  7 ++---
 python/tests/test_naive_bayes_e2e.py               |  7 ++---
 python/tests/test_remaining_classifiers_e2e.py     |  9 ++----
 python/tests/test_remaining_regressors_e2e.py      | 11 ++------
 10 files changed, 51 insertions(+), 58 deletions(-)

diff --git a/python/tests/_native_classes.py b/python/tests/_native_classes.py
new file mode 100644
index 00000000..804f7ca9
--- /dev/null
+++ b/python/tests/_native_classes.py
@@ -0,0 +1,33 @@
+#
+# 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.
+#
+"""The ML class that a live test runs: native in this client, the reference's 
in official PySpark."""
+
+import importlib
+
+
+def native_class(module, name, reference_module):
+    """The native class `name` of `module` in this client. A class that this 
client lacks fails the
+    test, rather than running it with the vendored pyspark.ml class. Official 
PySpark, which has no
+    pyspark._pyspark, runs the test with its JVM-backed class of 
`reference_module`, as the
+    reference."""
+    try:
+        import pyspark._pyspark  # noqa: F401
+    except ImportError:
+        return getattr(importlib.import_module(reference_module), name)
+    native = getattr(importlib.import_module(module), name)
+    assert native.__module__.startswith("pyspark.ml.connect"), native
+    return native
diff --git a/python/tests/test_bisecting_kmeans_e2e.py 
b/python/tests/test_bisecting_kmeans_e2e.py
index b2db90c8..e1fbf85f 100644
--- a/python/tests/test_bisecting_kmeans_e2e.py
+++ b/python/tests/test_bisecting_kmeans_e2e.py
@@ -20,6 +20,7 @@ import gc
 import os
 
 import pytest
+from _native_classes import native_class
 from pyspark.ml.connect.functions import array_to_vector
 from pyspark.ml.linalg import Vectors
 from pyspark.sql import SparkSession
@@ -39,11 +40,7 @@ def session():
 
 
 def _estimator():
-    try:
-        from pyspark.ml.connect.clustering import BisectingKMeans
-    except ImportError:
-        from pyspark.ml.clustering import BisectingKMeans
-    return BisectingKMeans
+    return native_class("pyspark.ml.connect.clustering", "BisectingKMeans", 
"pyspark.ml.clustering")
 
 
 def _vectors(session, sql, name="features"):
diff --git a/python/tests/test_dropin_e2e.py b/python/tests/test_dropin_e2e.py
index 87da5ed0..6dfa86eb 100644
--- a/python/tests/test_dropin_e2e.py
+++ b/python/tests/test_dropin_e2e.py
@@ -28,6 +28,7 @@ from decimal import Decimal
 
 import pyarrow as pa
 import pytest
+from _native_classes import native_class
 
 # Keep interval round trips in the live suite selected by CI.
 from interval_input_cases import IntervalInputLiveTests  # noqa: F401
@@ -5176,18 +5177,7 @@ def 
test_linear_regression_weights_and_iterative_losses(session):
 
 
 def _native_ml(name, reference_module):
-    # The native pyspark.ml.connect class of this client, which these cases 
are of: one that is
-    # missing fails them, rather than running them with the vendored 
pyspark.ml class. The
-    # reference client, which has no pyspark._pyspark, runs them with its 
JVM-backed class.
-    import importlib
-
-    try:
-        import pyspark._pyspark  # noqa: F401
-    except ImportError:
-        return getattr(importlib.import_module(reference_module), name)
-    native = getattr(importlib.import_module("pyspark.ml.connect"), name)
-    assert native.__module__.startswith("pyspark.ml.connect"), native
-    return native
+    return native_class("pyspark.ml.connect", name, reference_module)
 
 
 def _kmeans():
diff --git a/python/tests/test_gaussian_mixture_e2e.py 
b/python/tests/test_gaussian_mixture_e2e.py
index b21fd536..9d25dbc3 100644
--- a/python/tests/test_gaussian_mixture_e2e.py
+++ b/python/tests/test_gaussian_mixture_e2e.py
@@ -21,6 +21,7 @@ import math
 import os
 
 import pytest
+from _native_classes import native_class
 from pyspark.ml.connect.functions import array_to_vector, vector_to_array
 from pyspark.ml.linalg import Vectors
 from pyspark.sql import SparkSession
@@ -40,11 +41,7 @@ def session():
 
 
 def _estimator():
-    try:
-        from pyspark.ml.connect.clustering import GaussianMixture
-    except ImportError:
-        from pyspark.ml.clustering import GaussianMixture
-    return GaussianMixture
+    return native_class("pyspark.ml.connect.clustering", "GaussianMixture", 
"pyspark.ml.clustering")
 
 
 def _vectors(session, sql, name="features"):
diff --git a/python/tests/test_gbt_classifier_e2e.py 
b/python/tests/test_gbt_classifier_e2e.py
index 87d42597..4ed62411 100644
--- a/python/tests/test_gbt_classifier_e2e.py
+++ b/python/tests/test_gbt_classifier_e2e.py
@@ -21,6 +21,7 @@ import math
 import os
 
 import pytest
+from _native_classes import native_class
 from pyspark.ml.connect.functions import array_to_vector, vector_to_array
 from pyspark.sql import SparkSession
 from pyspark.sql import functions as F
@@ -39,11 +40,7 @@ def session():
 
 
 def _classifier():
-    try:
-        from pyspark.ml.connect.classification import GBTClassifier
-    except ImportError:
-        from pyspark.ml.classification import GBTClassifier
-    return GBTClassifier
+    return native_class("pyspark.ml.connect.classification", "GBTClassifier", 
"pyspark.ml.classification")
 
 
 def _vectors(session, sql, name="features"):
diff --git a/python/tests/test_gbt_regressor_e2e.py 
b/python/tests/test_gbt_regressor_e2e.py
index 7e8d18fd..f8686df5 100644
--- a/python/tests/test_gbt_regressor_e2e.py
+++ b/python/tests/test_gbt_regressor_e2e.py
@@ -21,6 +21,7 @@ import math
 import os
 
 import pytest
+from _native_classes import native_class
 from pyspark.ml.connect.functions import array_to_vector, vector_to_array
 from pyspark.sql import SparkSession
 from pyspark.sql import functions as F
@@ -39,11 +40,7 @@ def session():
 
 
 def _regressor():
-    try:
-        from pyspark.ml.connect.regression import GBTRegressor
-    except ImportError:
-        from pyspark.ml.regression import GBTRegressor
-    return GBTRegressor
+    return native_class("pyspark.ml.connect.regression", "GBTRegressor", 
"pyspark.ml.regression")
 
 
 def _vectors(session, sql, name="features"):
diff --git a/python/tests/test_generalized_linear_regression_e2e.py 
b/python/tests/test_generalized_linear_regression_e2e.py
index 7eb1e6af..2c6ac49a 100644
--- a/python/tests/test_generalized_linear_regression_e2e.py
+++ b/python/tests/test_generalized_linear_regression_e2e.py
@@ -21,6 +21,7 @@ import math
 import os
 
 import pytest
+from _native_classes import native_class
 from pyspark.ml.connect.functions import array_to_vector
 from pyspark.ml.linalg import Vectors
 from pyspark.sql import SparkSession
@@ -40,11 +41,7 @@ def session():
 
 
 def _estimator():
-    try:
-        from pyspark.ml.connect.regression import GeneralizedLinearRegression
-    except ImportError:
-        from pyspark.ml.regression import GeneralizedLinearRegression
-    return GeneralizedLinearRegression
+    return native_class("pyspark.ml.connect.regression", 
"GeneralizedLinearRegression", "pyspark.ml.regression")
 
 
 def _vectors(session, sql):
diff --git a/python/tests/test_naive_bayes_e2e.py 
b/python/tests/test_naive_bayes_e2e.py
index a63aa886..69044395 100644
--- a/python/tests/test_naive_bayes_e2e.py
+++ b/python/tests/test_naive_bayes_e2e.py
@@ -21,6 +21,7 @@ import math
 import os
 
 import pytest
+from _native_classes import native_class
 from pyspark.ml.connect.functions import array_to_vector, vector_to_array
 from pyspark.ml.linalg import Vectors
 from pyspark.sql import SparkSession
@@ -40,11 +41,7 @@ def session():
 
 
 def _classifier():
-    try:
-        from pyspark.ml.connect.classification import NaiveBayes
-    except ImportError:
-        from pyspark.ml.classification import NaiveBayes
-    return NaiveBayes
+    return native_class("pyspark.ml.connect.classification", "NaiveBayes", 
"pyspark.ml.classification")
 
 
 def _vectors(session, sql, name="features"):
diff --git a/python/tests/test_remaining_classifiers_e2e.py 
b/python/tests/test_remaining_classifiers_e2e.py
index 04fa3216..19e9ad8f 100644
--- a/python/tests/test_remaining_classifiers_e2e.py
+++ b/python/tests/test_remaining_classifiers_e2e.py
@@ -24,6 +24,7 @@ import math
 import os
 
 import pytest
+from _native_classes import native_class
 from pyspark.ml.linalg import Vectors
 from pyspark.sql import SparkSession
 from pyspark.sql import functions as F
@@ -42,13 +43,7 @@ def session():
 
 
 def _classifier(name):
-    from pyspark.ml.connect import classification as native
-
-    if hasattr(native, "FMClassifier"):
-        return getattr(native, name)
-    from pyspark.ml import classification as official
-
-    return getattr(official, name)
+    return native_class("pyspark.ml.connect.classification", name, 
"pyspark.ml.classification")
 
 
 def _triangles(session, names=("features", "label")):
diff --git a/python/tests/test_remaining_regressors_e2e.py 
b/python/tests/test_remaining_regressors_e2e.py
index 638d60ec..88380dae 100644
--- a/python/tests/test_remaining_regressors_e2e.py
+++ b/python/tests/test_remaining_regressors_e2e.py
@@ -22,6 +22,7 @@ import os
 from pathlib import Path
 
 import pytest
+from _native_classes import native_class
 from pyspark.errors import PySparkException
 from pyspark.ml.linalg import Vectors
 from pyspark.sql import SparkSession
@@ -41,15 +42,7 @@ def session():
 
 
 def _regressor(name):
-    try:
-        from pyspark.ml.connect import regression as native
-    except ImportError:
-        native = None
-    if native is not None and hasattr(native, "AFTSurvivalRegression"):
-        return getattr(native, name)
-    from pyspark.ml import regression as official
-
-    return getattr(official, name)
+    return native_class("pyspark.ml.connect.regression", name, 
"pyspark.ml.regression")
 
 
 def _estimator(name, options, setters):


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to