[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-06 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1126386137


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,145 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.connect.common.DataTypeProtoConverter._
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+
.setElementType(toConnectProtoType(toDataType(array.getClass.getComponentType)))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =

Review Comment:
  

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-06 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1126383844


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,145 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.connect.common.DataTypeProtoConverter._
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+
.setElementType(toConnectProtoType(toDataType(array.getClass.getComponentType)))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =

Review Comment:
  

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-05 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125854357


##
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##
@@ -189,6 +190,11 @@ message Expression {
   int32 days = 2;
   int64 microseconds = 3;
 }
+
+message Array {
+  DataType elementType = 1;
+  repeated Literal element = 2;

Review Comment:
   Thanks for your confirmation
   
   



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-05 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125852404


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,297 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-05 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125852404


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,297 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-05 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125852404


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,297 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-05 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125837371


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,297 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-05 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125837371


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,297 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-04 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125600149


##
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/LiteralValueProtoConverter.scala:
##
@@ -130,4 +138,61 @@ object LiteralValueProtoConverter {
   case o => throw new Exception(s"Unsupported value type: $o")
 }
   }
+
+  private def toArrayData(array: proto.Expression.Literal.Array): Any = {
+def makeArrayData[T](converter: proto.Expression.Literal => T)(implicit
+tag: ClassTag[T]): Array[T] = {

Review Comment:
   like this way?



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-04 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125595959


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,157 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.connect.common.DataTypeProtoConverter._
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-04 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125595959


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,157 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.connect.common.DataTypeProtoConverter._
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-04 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125595915


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,157 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.connect.common.DataTypeProtoConverter._
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-03 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125340340


##
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/LiteralValueProtoConverter.scala:
##
@@ -130,4 +135,117 @@ object LiteralValueProtoConverter {
   case o => throw new Exception(s"Unsupported value type: $o")
 }
   }
+
+  private def toArrayData(array: proto.Expression.Literal.Array): Any = {
+def makeArrayData[T](
+initFunc: Int => Array[T],
+converter: proto.Expression.Literal => T): Array[T] = {
+  val elementList = array.getElementList
+  val data = initFunc(elementList.size())
+  var idx = 0
+  val iter = elementList.iterator()
+  while (iter.hasNext) {
+data(idx) = converter(iter.next())
+idx += 1
+  }
+  data
+}
+
+val elementType = array.getElementType
+if (elementType.hasShort) {
+  makeArrayData(size => new Array[Short](size), v => v.getShort.toShort)
+} else if (elementType.hasInteger) {
+  makeArrayData(size => new Array[Int](size), v => v.getInteger)
+} else if (elementType.hasLong) {
+  makeArrayData(size => new Array[Long](size), v => v.getLong)
+} else if (elementType.hasDouble) {
+  makeArrayData(size => new Array[Double](size), v => v.getDouble)
+} else if (elementType.hasByte) {
+  makeArrayData(size => new Array[Byte](size), v => v.getByte.toByte)
+} else if (elementType.hasFloat) {
+  makeArrayData(size => new Array[Float](size), v => v.getFloat)
+} else if (elementType.hasBoolean) {
+  makeArrayData(size => new Array[Boolean](size), v => v.getBoolean)
+} else if (elementType.hasString) {
+  makeArrayData(size => new Array[String](size), v => v.getString)
+} else if (elementType.hasBinary) {
+  makeArrayData(size => new Array[Array[Byte]](size), v => 
v.getBinary.toByteArray)
+} else if (elementType.hasDate) {
+  makeArrayData(
+size => new Array[java.sql.Date](size),
+v => DateTimeUtils.toJavaDate(v.getDate))
+} else if (elementType.hasTimestamp) {
+  makeArrayData(
+size => new Array[java.sql.Timestamp](size),
+v => DateTimeUtils.toJavaTimestamp(v.getTimestamp))
+} else if (elementType.hasTimestampNtz) {
+  makeArrayData(
+size => new Array[java.time.LocalDateTime](size),
+v => DateTimeUtils.microsToLocalDateTime(v.getTimestampNtz))
+} else if (elementType.hasDayTimeInterval) {
+  makeArrayData(
+size => new Array[java.time.Duration](size),
+v => IntervalUtils.microsToDuration(v.getDayTimeInterval))
+} else if (elementType.hasYearMonthInterval) {
+  makeArrayData(
+size => new Array[java.time.Period](size),
+v => IntervalUtils.monthsToPeriod(v.getYearMonthInterval))
+} else if (elementType.hasDecimal) {
+  makeArrayData(size => new Array[Decimal](size), v => 
Decimal(v.getDecimal.getValue))
+} else if (elementType.hasCalendarInterval) {
+  makeArrayData(
+size => new Array[CalendarInterval](size),
+v => {
+  val interval = v.getCalendarInterval
+  new CalendarInterval(interval.getMonths, interval.getDays, 
interval.getMicroseconds)
+})
+} else if (elementType.hasArray) {
+  makeArrayData(size => new Array[Any](size), v => toArrayData(v.getArray))
+} else {
+  throw InvalidPlanInput(s"Unsupported Literal Type: $elementType)")
+}
+  }
+
+  private def toArrayType(elementType: proto.DataType): ArrayType = {

Review Comment:
   done



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-03 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125246264


##
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/LiteralValueProtoConverter.scala:
##
@@ -130,4 +135,117 @@ object LiteralValueProtoConverter {
   case o => throw new Exception(s"Unsupported value type: $o")
 }
   }
+
+  private def toArrayData(array: proto.Expression.Literal.Array): Any = {

Review Comment:
   Let me try
   
   



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-03 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125245356


##
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/LiteralValueProtoConverter.scala:
##
@@ -130,4 +135,117 @@ object LiteralValueProtoConverter {
   case o => throw new Exception(s"Unsupported value type: $o")
 }
   }
+
+  private def toArrayData(array: proto.Expression.Literal.Array): Any = {
+def makeArrayData[T](
+initFunc: Int => Array[T],
+converter: proto.Expression.Literal => T): Array[T] = {
+  val elementList = array.getElementList
+  val data = initFunc(elementList.size())
+  var idx = 0
+  val iter = elementList.iterator()
+  while (iter.hasNext) {
+data(idx) = converter(iter.next())
+idx += 1
+  }
+  data
+}
+
+val elementType = array.getElementType
+if (elementType.hasShort) {
+  makeArrayData(size => new Array[Short](size), v => v.getShort.toShort)
+} else if (elementType.hasInteger) {
+  makeArrayData(size => new Array[Int](size), v => v.getInteger)
+} else if (elementType.hasLong) {
+  makeArrayData(size => new Array[Long](size), v => v.getLong)
+} else if (elementType.hasDouble) {
+  makeArrayData(size => new Array[Double](size), v => v.getDouble)
+} else if (elementType.hasByte) {
+  makeArrayData(size => new Array[Byte](size), v => v.getByte.toByte)
+} else if (elementType.hasFloat) {
+  makeArrayData(size => new Array[Float](size), v => v.getFloat)
+} else if (elementType.hasBoolean) {
+  makeArrayData(size => new Array[Boolean](size), v => v.getBoolean)
+} else if (elementType.hasString) {
+  makeArrayData(size => new Array[String](size), v => v.getString)
+} else if (elementType.hasBinary) {
+  makeArrayData(size => new Array[Array[Byte]](size), v => 
v.getBinary.toByteArray)
+} else if (elementType.hasDate) {
+  makeArrayData(
+size => new Array[java.sql.Date](size),
+v => DateTimeUtils.toJavaDate(v.getDate))
+} else if (elementType.hasTimestamp) {
+  makeArrayData(
+size => new Array[java.sql.Timestamp](size),
+v => DateTimeUtils.toJavaTimestamp(v.getTimestamp))
+} else if (elementType.hasTimestampNtz) {
+  makeArrayData(
+size => new Array[java.time.LocalDateTime](size),
+v => DateTimeUtils.microsToLocalDateTime(v.getTimestampNtz))
+} else if (elementType.hasDayTimeInterval) {
+  makeArrayData(
+size => new Array[java.time.Duration](size),
+v => IntervalUtils.microsToDuration(v.getDayTimeInterval))
+} else if (elementType.hasYearMonthInterval) {
+  makeArrayData(
+size => new Array[java.time.Period](size),
+v => IntervalUtils.monthsToPeriod(v.getYearMonthInterval))
+} else if (elementType.hasDecimal) {
+  makeArrayData(size => new Array[Decimal](size), v => 
Decimal(v.getDecimal.getValue))
+} else if (elementType.hasCalendarInterval) {
+  makeArrayData(
+size => new Array[CalendarInterval](size),
+v => {
+  val interval = v.getCalendarInterval
+  new CalendarInterval(interval.getMonths, interval.getDays, 
interval.getMicroseconds)
+})
+} else if (elementType.hasArray) {
+  makeArrayData(size => new Array[Any](size), v => toArrayData(v.getArray))
+} else {
+  throw InvalidPlanInput(s"Unsupported Literal Type: $elementType)")
+}
+  }
+
+  private def toArrayType(elementType: proto.DataType): ArrayType = {

Review Comment:
   ok



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-03 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1125243878


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,297 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JBoolean, Byte => JByte, Character => JChar, 
Double => JDouble, Float => JFloat, Integer => JInteger, Long => JLong, Short 
=> JShort}
+import java.math.{BigDecimal => JBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return
+   *   proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {
+val builder = proto.Expression.Literal.newBuilder()
+
+def decimalBuilder(precision: Int, scale: Int, value: String) = {
+  
builder.getDecimalBuilder.setPrecision(precision).setScale(scale).setValue(value)
+}
+
+def calendarIntervalBuilder(months: Int, days: Int, microseconds: Long) = {
+  builder.getCalendarIntervalBuilder
+.setMonths(months)
+.setDays(days)
+.setMicroseconds(microseconds)
+}
+
+def arrayBuilder(array: Array[_]) = {
+  val ab = builder.getArrayBuilder
+.setElementType(componentTypeToProto(array.getClass.getComponentType))
+  array.foreach(x => ab.addElement(toLiteralProto(x)))
+  ab
+}
+
+literal match {
+  case v: Boolean => builder.setBoolean(v)
+  case v: Byte => builder.setByte(v)
+  case v: Short => builder.setShort(v)
+  case v: Int => builder.setInteger(v)
+  case v: Long => builder.setLong(v)
+  case v: Float => builder.setFloat(v)
+  case v: Double => builder.setDouble(v)
+  case v: BigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: JBigDecimal =>
+builder.setDecimal(decimalBuilder(v.precision, v.scale, v.toString))
+  case v: String => builder.setString(v)
+  case v: Char => builder.setString(v.toString)
+  case v: Array[Char] => builder.setString(String.valueOf(v))
+  case v: Array[Byte] => builder.setBinary(ByteString.copyFrom(v))
+  case v: collection.mutable.WrappedArray[_] => 
toLiteralProtoBuilder(v.array)
+  case v: LocalDate => builder.setDate(v.toEpochDay.toInt)
+  case v: Decimal =>
+builder.setDecimal(decimalBuilder(Math.max(v.precision, v.scale), 
v.scale, v.toString))
+  case v: Instant => builder.setTimestamp(DateTimeUtils.instantToMicros(v))
+  case v: Timestamp => 
builder.setTimestamp(DateTimeUtils.fromJavaTimestamp(v))
+  case v: LocalDateTime => 
builder.setTimestampNtz(DateTimeUtils.localDateTimeToMicros(v))
+  case v: Date => builder.setDate(DateTimeUtils.fromJavaDate(v))
+  case v: Duration => 
builder.setDayTimeInterval(IntervalUtils.durationToMicros(v))
+  case v: Period => 
builder.setYearMonthInterval(IntervalUtils.periodToMonths(v))
+  case v: Array[_] => builder.setArray(arrayBuilder(v))
+  case v: CalendarInterval =>
+builder.setCalendarInterval(calendarIntervalBuilder(v.months, v.days, 
v.microseconds))
+  case null => builder.setNull(nullType)
+  case _ => unsupported(s"literal $literal not supported (yet).")
+}
+  }
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal`.
+   *
+   * @return
+   *   proto.Expression.Literal
+   */
+  def toLiteralProto(literal: Any): proto.Expression.Literal =
+

[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-02 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1123020720


##
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##
@@ -189,6 +190,11 @@ message Expression {
   int32 days = 2;
   int64 microseconds = 3;
 }
+
+message Array {
+  DataType elementType = 1;
+  repeated Literal element = 2;

Review Comment:
   should be `element` or `elements`?



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-01 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1121347931


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,289 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JavaBoolean, Byte => JavaByte, Character => 
JavaChar, Double => JavaDouble, Float => JavaFloat, Integer => JavaInteger, 
Long => JavaLong, Short => JavaShort}

Review Comment:
   done



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-01 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1121343776


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,289 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JavaBoolean, Byte => JavaByte, Character => 
JavaChar, Double => JavaDouble, Float => JavaFloat, Integer => JavaInteger, 
Long => JavaLong, Short => JavaShort}

Review Comment:
   > searched on the codebase, Spark used `import java.lang.{Boolean => 
JBoolean, Integer => JInteger, Long => JLong}` in other places.
   
   but let me follow this



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-03-01 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1121342916


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,289 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JavaBoolean, Byte => JavaByte, Character => 
JavaChar, Double => JavaDouble, Float => JavaFloat, Integer => JavaInteger, 
Long => JavaLong, Short => JavaShort}

Review Comment:
   not all
   
   
https://github.com/apache/spark/blob/d7d8af0dbb47e152b280226a7afcf0771b5a5ae8/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala#L20-L31



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org



[GitHub] [spark] LuciferYang commented on a diff in pull request #40218: [SPARK-42579][CONNECT] Part-1: `function.lit` support `Array[_]` dataType

2023-02-28 Thread via GitHub


LuciferYang commented on code in PR #40218:
URL: https://github.com/apache/spark/pull/40218#discussion_r1121260378


##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,289 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JavaBoolean, Byte => JavaByte, Character => 
JavaChar, Double => JavaDouble, Float => JavaFloat, Integer => JavaInteger, 
Long => JavaLong, Short => JavaShort}
+import java.math.{BigDecimal => JavaBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {

Review Comment:
   The purpose of refactoring is to avoid get `proto.Expression.Literal` 
through `lit(x).expr.getLiteral`



##
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/LiteralProtoConverter.scala:
##
@@ -0,0 +1,289 @@
+/*
+ * 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.expressions
+
+import java.lang.{Boolean => JavaBoolean, Byte => JavaByte, Character => 
JavaChar, Double => JavaDouble, Float => JavaFloat, Integer => JavaInteger, 
Long => JavaLong, Short => JavaShort}
+import java.math.{BigDecimal => JavaBigDecimal}
+import java.sql.{Date, Timestamp}
+import java.time._
+
+import com.google.protobuf.ByteString
+
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.client.unsupported
+import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
YearMonthIntervalType}
+import org.apache.spark.unsafe.types.CalendarInterval
+
+object LiteralProtoConverter {
+
+  private lazy val nullType =
+
proto.DataType.newBuilder().setNull(proto.DataType.NULL.getDefaultInstance).build()
+
+  /**
+   * Transforms literal value to the `proto.Expression.Literal.Builder`.
+   *
+   * @return proto.Expression.Literal.Builder
+   */
+  @scala.annotation.tailrec
+  def toLiteralProtoBuilder(literal: Any): proto.Expression.Literal.Builder = {

Review Comment:
   The purpose of this refactoring is to avoid get `proto.Expression.Literal` 
through `lit(x).expr.getLiteral`



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For