|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.sql.connect |
| 18 | + |
| 19 | +import scala.jdk.CollectionConverters._ |
| 20 | + |
| 21 | +import org.apache.spark.SparkException |
| 22 | +import org.apache.spark.connect.proto |
| 23 | +import org.apache.spark.connect.proto.Expression.SortOrder.NullOrdering.{SORT_NULLS_FIRST, SORT_NULLS_LAST} |
| 24 | +import org.apache.spark.connect.proto.Expression.SortOrder.SortDirection.{SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING} |
| 25 | +import org.apache.spark.connect.proto.Expression.Window.WindowFrame.{FrameBoundary, FrameType} |
| 26 | +import org.apache.spark.sql.catalyst.trees.{CurrentOrigin, Origin} |
| 27 | +import org.apache.spark.sql.connect.common.DataTypeProtoConverter |
| 28 | +import org.apache.spark.sql.connect.common.LiteralValueProtoConverter.toLiteralProtoBuilder |
| 29 | +import org.apache.spark.sql.expressions.ScalaUserDefinedFunction |
| 30 | +import org.apache.spark.sql.internal._ |
| 31 | + |
| 32 | +/** |
| 33 | + * Converter for [[ColumnNode]] to [[proto.Expression]] conversions. |
| 34 | + */ |
| 35 | +object ColumnNodeToProtoConverter extends (ColumnNode => proto.Expression) { |
| 36 | + override def apply(node: ColumnNode): proto.Expression = { |
| 37 | + val builder = proto.Expression.newBuilder() |
| 38 | + // TODO(SPARK-49273) support Origin in Connect Scala Client. |
| 39 | + node match { |
| 40 | + case Literal(value, None, _) => |
| 41 | + builder.setLiteral(toLiteralProtoBuilder(value)) |
| 42 | + |
| 43 | + case Literal(value, Some(dataType), _) => |
| 44 | + builder.setLiteral(toLiteralProtoBuilder(value, dataType)) |
| 45 | + |
| 46 | + case UnresolvedAttribute(unparsedIdentifier, planId, isMetadataColumn, _) => |
| 47 | + val b = builder.getUnresolvedAttributeBuilder |
| 48 | + .setUnparsedIdentifier(unparsedIdentifier) |
| 49 | + .setIsMetadataColumn(isMetadataColumn) |
| 50 | + planId.foreach(b.setPlanId) |
| 51 | + |
| 52 | + case UnresolvedStar(unparsedTarget, planId, _) => |
| 53 | + val b = builder.getUnresolvedStarBuilder |
| 54 | + unparsedTarget.foreach(b.setUnparsedTarget) |
| 55 | + planId.foreach(b.setPlanId) |
| 56 | + |
| 57 | + case UnresolvedRegex(regex, planId, _) => |
| 58 | + val b = builder.getUnresolvedRegexBuilder |
| 59 | + .setColName(regex) |
| 60 | + planId.foreach(b.setPlanId) |
| 61 | + |
| 62 | + case UnresolvedFunction(functionName, arguments, isDistinct, isUserDefinedFunction, _, _) => |
| 63 | + // TODO(SPARK-49087) use internal namespace. |
| 64 | + builder.getUnresolvedFunctionBuilder |
| 65 | + .setFunctionName(functionName) |
| 66 | + .setIsUserDefinedFunction(isUserDefinedFunction) |
| 67 | + .setIsDistinct(isDistinct) |
| 68 | + .addAllArguments(arguments.map(apply).asJava) |
| 69 | + |
| 70 | + case Alias(child, name, metadata, _) => |
| 71 | + val b = builder.getAliasBuilder.setExpr(apply(child)) |
| 72 | + name.foreach(b.addName) |
| 73 | + metadata.foreach(m => b.setMetadata(m.json)) |
| 74 | + |
| 75 | + case Cast(child, dataType, evalMode, _) => |
| 76 | + val b = builder.getCastBuilder |
| 77 | + .setExpr(apply(child)) |
| 78 | + .setType(DataTypeProtoConverter.toConnectProtoType(dataType)) |
| 79 | + evalMode.foreach { mode => |
| 80 | + val convertedMode = mode match { |
| 81 | + case Cast.Try => proto.Expression.Cast.EvalMode.EVAL_MODE_TRY |
| 82 | + case Cast.Ansi => proto.Expression.Cast.EvalMode.EVAL_MODE_ANSI |
| 83 | + case Cast.Legacy => proto.Expression.Cast.EvalMode.EVAL_MODE_LEGACY |
| 84 | + } |
| 85 | + b.setEvalMode(convertedMode) |
| 86 | + } |
| 87 | + |
| 88 | + case SqlExpression(expression, _) => |
| 89 | + builder.getExpressionStringBuilder.setExpression(expression) |
| 90 | + |
| 91 | + case s: SortOrder => |
| 92 | + builder.setSortOrder(convertSortOrder(s)) |
| 93 | + |
| 94 | + case Window(windowFunction, windowSpec, _) => |
| 95 | + val b = builder.getWindowBuilder |
| 96 | + .setWindowFunction(apply(windowFunction)) |
| 97 | + .addAllPartitionSpec(windowSpec.partitionColumns.map(apply).asJava) |
| 98 | + .addAllOrderSpec(windowSpec.sortColumns.map(convertSortOrder).asJava) |
| 99 | + windowSpec.frame.foreach { frame => |
| 100 | + b.getFrameSpecBuilder |
| 101 | + .setFrameType(frame.frameType match { |
| 102 | + case WindowFrame.Row => FrameType.FRAME_TYPE_ROW |
| 103 | + case WindowFrame.Range => FrameType.FRAME_TYPE_RANGE |
| 104 | + }) |
| 105 | + .setLower(convertFrameBoundary(frame.lower)) |
| 106 | + .setUpper(convertFrameBoundary(frame.upper)) |
| 107 | + } |
| 108 | + |
| 109 | + case UnresolvedExtractValue(child, extraction, _) => |
| 110 | + builder.getUnresolvedExtractValueBuilder |
| 111 | + .setChild(apply(child)) |
| 112 | + .setExtraction(apply(extraction)) |
| 113 | + |
| 114 | + case UpdateFields(structExpression, fieldName, valueExpression, _) => |
| 115 | + val b = builder.getUpdateFieldsBuilder |
| 116 | + .setStructExpression(apply(structExpression)) |
| 117 | + .setFieldName(fieldName) |
| 118 | + valueExpression.foreach(v => b.setValueExpression(apply(v))) |
| 119 | + |
| 120 | + case v: UnresolvedNamedLambdaVariable => |
| 121 | + builder.setUnresolvedNamedLambdaVariable(convertNamedLambdaVariable(v)) |
| 122 | + |
| 123 | + case LambdaFunction(function, arguments, _) => |
| 124 | + builder.getLambdaFunctionBuilder |
| 125 | + .setFunction(apply(function)) |
| 126 | + .addAllArguments(arguments.map(convertNamedLambdaVariable).asJava) |
| 127 | + |
| 128 | + case InvokeInlineUserDefinedFunction(udf: ScalaUserDefinedFunction, arguments, false, _) => |
| 129 | + val b = builder.getCommonInlineUserDefinedFunctionBuilder |
| 130 | + .setScalarScalaUdf(udf.udf) |
| 131 | + .setDeterministic(udf.deterministic) |
| 132 | + .addAllArguments(arguments.map(apply).asJava) |
| 133 | + udf.givenName.foreach(b.setFunctionName) |
| 134 | + |
| 135 | + case CaseWhenOtherwise(branches, otherwise, _) => |
| 136 | + val b = builder.getUnresolvedFunctionBuilder |
| 137 | + .setFunctionName("when") |
| 138 | + branches.foreach { case (condition, value) => |
| 139 | + b.addArguments(apply(condition)) |
| 140 | + b.addArguments(apply(value)) |
| 141 | + } |
| 142 | + otherwise.foreach { value => |
| 143 | + b.addArguments(apply(value)) |
| 144 | + } |
| 145 | + |
| 146 | + case ProtoColumnNode(e, _) => |
| 147 | + return e |
| 148 | + |
| 149 | + case node => |
| 150 | + throw SparkException.internalError("Unsupported ColumnNode: " + node) |
| 151 | + } |
| 152 | + builder.build() |
| 153 | + } |
| 154 | + |
| 155 | + private def convertSortOrder(s: SortOrder): proto.Expression.SortOrder = { |
| 156 | + proto.Expression.SortOrder |
| 157 | + .newBuilder() |
| 158 | + .setChild(apply(s.child)) |
| 159 | + .setDirection(s.sortDirection match { |
| 160 | + case SortOrder.Ascending => SORT_DIRECTION_ASCENDING |
| 161 | + case SortOrder.Descending => SORT_DIRECTION_DESCENDING |
| 162 | + }) |
| 163 | + .setNullOrdering(s.nullOrdering match { |
| 164 | + case SortOrder.NullsFirst => SORT_NULLS_FIRST |
| 165 | + case SortOrder.NullsLast => SORT_NULLS_LAST |
| 166 | + }) |
| 167 | + .build() |
| 168 | + } |
| 169 | + |
| 170 | + private def convertFrameBoundary(boundary: WindowFrame.FrameBoundary): FrameBoundary = { |
| 171 | + val builder = FrameBoundary.newBuilder() |
| 172 | + boundary match { |
| 173 | + case WindowFrame.UnboundedPreceding => builder.setUnbounded(true) |
| 174 | + case WindowFrame.UnboundedFollowing => builder.setUnbounded(true) |
| 175 | + case WindowFrame.CurrentRow => builder.setCurrentRow(true) |
| 176 | + case WindowFrame.Value(value) => builder.setValue(apply(value)) |
| 177 | + } |
| 178 | + builder.build() |
| 179 | + } |
| 180 | + |
| 181 | + private def convertNamedLambdaVariable( |
| 182 | + v: UnresolvedNamedLambdaVariable): proto.Expression.UnresolvedNamedLambdaVariable = { |
| 183 | + proto.Expression.UnresolvedNamedLambdaVariable.newBuilder().addNameParts(v.name).build() |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +case class ProtoColumnNode( |
| 188 | + expr: proto.Expression, |
| 189 | + override val origin: Origin = CurrentOrigin.get) |
| 190 | + extends ColumnNode { |
| 191 | + override def sql: String = expr.toString |
| 192 | +} |
0 commit comments