Skip to content

Commit 796409d

Browse files
committed
[pyspark] Validate the validation indicator column type.
1 parent 06ed146 commit 796409d

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

python-package/xgboost/spark/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from pyspark.sql.functions import col, countDistinct, pandas_udf, rand, struct
5252
from pyspark.sql.types import (
5353
ArrayType,
54+
BooleanType,
5455
DoubleType,
5556
FloatType,
5657
IntegerType,
@@ -844,6 +845,11 @@ def _prepare_input(self, dataset: DataFrame) -> Tuple[DataFrame, FeatureProp]:
844845
sc = _get_spark_session().sparkContext
845846
max_concurrent_tasks = _get_max_num_concurrent_tasks(sc)
846847

848+
if feature_prop.has_validation_col:
849+
dtype = dataset.schema[alias.valid].dataType
850+
if not isinstance(dtype, BooleanType):
851+
raise TypeError("The validation indicator must be boolean type.")
852+
847853
if num_workers > max_concurrent_tasks:
848854
get_logger(self.__class__.__name__).warning(
849855
"The num_workers %s set for xgboost distributed "

tests/test_distributed/test_with_spark/test_spark_local.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,27 @@ def test_classifier_xgb_summary_with_validation(
13081308
atol=1e-3,
13091309
)
13101310

1311+
def test_valid_type(self, spark: SparkSession) -> None:
1312+
# integer as indicator is invalid
1313+
df_train = spark.createDataFrame(
1314+
[
1315+
(Vectors.dense(1.0, 2.0, 3.0), 0, 0),
1316+
(Vectors.sparse(3, {1: 1.0, 2: 5.5}), 1, 0),
1317+
(Vectors.dense(4.0, 5.0, 6.0), 0, 1),
1318+
(Vectors.sparse(3, {1: 6.0, 2: 7.5}), 1, 1),
1319+
],
1320+
["features", "label", "isVal"],
1321+
)
1322+
reg = SparkXGBRegressor(
1323+
features_col="features",
1324+
label_col="label",
1325+
validation_indicator_col="isVal",
1326+
)
1327+
# Use Exception here since spark might return a py4j error instead of specific
1328+
# exception.
1329+
with pytest.raises(Exception, match="The validation indicator must be boolean"):
1330+
reg.fit(df_train)
1331+
13111332

13121333
class XgboostLocalTest(SparkTestCase):
13131334
def setUp(self):

0 commit comments

Comments
 (0)