Search in sources :

Example 1 with AliasExpression

use of org.jpmml.sparkml.AliasExpression in project jpmml-sparkml by jpmml.

the class SQLTransformerConverter method encodeLogicalPlan.

public static List<Field<?>> encodeLogicalPlan(SparkMLEncoder encoder, LogicalPlan logicalPlan) {
    List<Field<?>> result = new ArrayList<>();
    List<LogicalPlan> children = JavaConversions.seqAsJavaList(logicalPlan.children());
    for (LogicalPlan child : children) {
        encodeLogicalPlan(encoder, child);
    }
    List<Expression> expressions = JavaConversions.seqAsJavaList(logicalPlan.expressions());
    for (Expression expression : expressions) {
        org.dmg.pmml.Expression pmmlExpression = ExpressionTranslator.translate(encoder, expression);
        if (pmmlExpression instanceof FieldRef) {
            FieldRef fieldRef = (FieldRef) pmmlExpression;
            Field<?> field = ensureField(encoder, fieldRef.getField());
            if (field != null) {
                result.add(field);
                continue;
            }
        }
        FieldName name = null;
        if (pmmlExpression instanceof AliasExpression) {
            AliasExpression aliasExpression = (AliasExpression) pmmlExpression;
            name = FieldName.create(aliasExpression.getName());
        } else {
            name = FieldNameUtil.create("sql", ExpressionUtil.format(expression));
        }
        DataType dataType = DatasetUtil.translateDataType(expression.dataType());
        OpType opType = ExpressionUtil.getOpType(dataType);
        pmmlExpression = AliasExpression.unwrap(pmmlExpression);
        Visitor visitor = new AbstractVisitor() {

            @Override
            public VisitorAction visit(FieldRef fieldRef) {
                ensureField(encoder, fieldRef.getField());
                return super.visit(fieldRef);
            }
        };
        visitor.applyTo(pmmlExpression);
        DerivedField derivedField = encoder.createDerivedField(name, opType, dataType, pmmlExpression);
        result.add(derivedField);
    }
    return result;
}
Also used : AbstractVisitor(org.jpmml.model.visitors.AbstractVisitor) FieldRef(org.dmg.pmml.FieldRef) Visitor(org.dmg.pmml.Visitor) AbstractVisitor(org.jpmml.model.visitors.AbstractVisitor) ArrayList(java.util.ArrayList) Field(org.dmg.pmml.Field) DerivedField(org.dmg.pmml.DerivedField) AliasExpression(org.jpmml.sparkml.AliasExpression) Expression(org.apache.spark.sql.catalyst.expressions.Expression) AliasExpression(org.jpmml.sparkml.AliasExpression) DataType(org.dmg.pmml.DataType) LogicalPlan(org.apache.spark.sql.catalyst.plans.logical.LogicalPlan) OpType(org.dmg.pmml.OpType) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Aggregations

ArrayList (java.util.ArrayList)1 Expression (org.apache.spark.sql.catalyst.expressions.Expression)1 LogicalPlan (org.apache.spark.sql.catalyst.plans.logical.LogicalPlan)1 DataType (org.dmg.pmml.DataType)1 DerivedField (org.dmg.pmml.DerivedField)1 Field (org.dmg.pmml.Field)1 FieldName (org.dmg.pmml.FieldName)1 FieldRef (org.dmg.pmml.FieldRef)1 OpType (org.dmg.pmml.OpType)1 Visitor (org.dmg.pmml.Visitor)1 AbstractVisitor (org.jpmml.model.visitors.AbstractVisitor)1 AliasExpression (org.jpmml.sparkml.AliasExpression)1