Github user ajaysaini725 commented on a diff in the pull request: https://github.com/apache/spark/pull/18746#discussion_r130775517 --- Diff: python/pyspark/ml/base.py --- @@ -116,3 +121,44 @@ class Model(Transformer): """ __metaclass__ = ABCMeta + + +@inherit_doc +class UnaryTransformer(HasInputCol, HasOutputCol, Transformer): + + @abstractmethod + def createTransformFunc(self): + """ + Creates the transoform function using the given param map. + """ + raise NotImplementedError() + + @abstractmethod + def outputDataType(self): + """ + Returns the data type of the output column as a sql type + """ + raise NotImplementedError() + + @abstractmethod + def validateInputType(self, inputType): + """ + Validates the input type. Throws an exception if it is invalid. + """ + raise NotImplementedError() + + def transformSchema(self, schema): + inputType = schema[self.getInputCol()].dataType + self.validateInputType(inputType) + if self.getOutputCol() in schema.names: + raise ValueError("Output column %s already exists." % self.getOutputCol()) + outputFields = copy.copy(schema.fields) + outputFields.append(StructField(self.getOutputCol(), + self.outputDataType(), + nullable=False)) + return StructType(outputFields) + + def transform(self, dataset, paramMap=None): + transformSchema(dataset.schema()) + transformUDF = udf(self.createTransformFunc(), self.outputDataType()) + dataset.withColumn(self.getOutputCol(), transformUDF(self.getInputCol())) --- End diff -- self.createTransformFunc returns a function which is passed to the udf so in this case I think it is okay
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org