cloud-fan commented on a change in pull request #35494: URL: https://github.com/apache/spark/pull/35494#discussion_r811217042
########## File path: sql/core/src/main/scala/org/apache/spark/sql/catalyst/util/V2ExpressionBuilder.scala ########## @@ -0,0 +1,124 @@ +/* + * 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. + */ + +package org.apache.spark.sql.catalyst.util + +import org.apache.spark.sql.catalyst.expressions.{Attribute, BinaryOperator, CaseWhen, EqualTo, Expression, IsNotNull, IsNull, Literal, Not} +import org.apache.spark.sql.connector.expressions.{Expression => V2Expression, FieldReference, GeneralScalarExpression, LiteralValue} + +/** + * The builder to generate V2 expressions from catalyst expressions. + */ +class V2ExpressionBuilder(e: Expression) { + + def build(): Option[V2Expression] = { + val expression = generateExpression(e) + expression.foreach { + case generated: GeneralScalarExpression => + generateSQL(e).foreach(sql => generated.setSql(sql)) + case _ => + } + expression + } + + private def generateExpression(expr: Expression): Option[V2Expression] = expr match { + case Literal(value, dataType) => Some(LiteralValue(value, dataType)) + case attr: Attribute => Some(FieldReference.column(quoteIfNeeded(attr.name))) + case IsNull(col) => generateExpression(col) + .map(c => new GeneralScalarExpression("IS NULL", Array[V2Expression](c))) + case IsNotNull(col) => generateExpression(col) + .map(c => new GeneralScalarExpression("IS NOT NULL", Array[V2Expression](c))) + case b: BinaryOperator => + val left = generateExpression(b.left) + val right = generateExpression(b.right) + if (left.isDefined && right.isDefined) { + Some(new GeneralScalarExpression(b.sqlOperator, Array[V2Expression](left.get, right.get))) + } else { + None + } + case Not(eq: EqualTo) => + val left = generateExpression(eq.left) + val right = generateExpression(eq.right) + if (left.isDefined && right.isDefined) { + Some(new GeneralScalarExpression("!=", Array[V2Expression](left.get, right.get))) + } else { + None + } + case Not(child) => generateExpression(child) + .map(v => new GeneralScalarExpression("NOT", Array[V2Expression](v))) + case CaseWhen(branches, elseValue) => + val conditions = branches.map(_._1).flatMap(generateExpression) + val values = branches.map(_._2).flatMap(generateExpression) + if (conditions.length == branches.length && values.length == branches.length) { + val branchExpressions = conditions.zip(values).map { case (c, v) => + new GeneralScalarExpression(Array[V2Expression](c, v)) + } + val branchExpression = new GeneralScalarExpression(branchExpressions.toArray[V2Expression]) + if (elseValue.isDefined) { + elseValue.flatMap(generateExpression).map { v => + new GeneralScalarExpression("CASE WHEN", Array[V2Expression](branchExpression, v)) + } + } else { + Some(new GeneralScalarExpression("CASE WHEN", Array[V2Expression](branchExpression))) + } + } else { + None + } + // TODO supports other expressions + case _ => None + } + + private def generateSQL(expr: Expression): Option[String] = expr match { Review comment: I don't think this helps. What we need is a tool that can help source implementations to easily implement generating SQL string from v2 expressions, and can customize for special cases. I'd like to see something like this ``` class V2ExpressionSQLBuilder { pubic String build(expr: V2Expression) { if (expr instanceof LiteralValue) { return visitLiteral((LiteralValue) expr); } else if ... } else if (expr instanceof GeneralScalarExpression) { GeneralScalarExpression e = (GeneralScalarExpression) expr; String name = e.name; if ("IS_NULL" == name) { return visitIsNull(build(e.children()[0])); } else if ... } else { return visitUnexpectedExpr(expr); } } protected String visitLiteral... protected String visitIsNull(String child)... ... } ``` Then source implementations can use it directly, or extend it and overwrite some `visitXXX` methods for customization. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
