You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "siying (via GitHub)" <gi...@apache.org> on 2023/05/31 19:02:41 UTC

[GitHub] [spark] siying opened a new pull request, #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

siying opened a new pull request, #41409:
URL: https://github.com/apache/spark/pull/41409

   ### What changes were proposed in this pull request?
   Add a logical type "custom-decimal" in Avro, which can only be backed by physical type long, and will be convert into decimal type.
   
   ### Why are the changes needed?
   A user would like to represent currency (for money) after loading Avro into SQL type. However, there isn't a good way to represent it in Avro. This custom type will allow them to do that.
   
   
   ### Does this PR introduce _any_ user-facing change?
   No.
   
   
   ### How was this patch tested?
   Added several unit test cases to test the new "custom-decimal" to be loaded successfully and also exception cases.
   


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] sadikovi commented on pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on PR #41409:
URL: https://github.com/apache/spark/pull/41409#issuecomment-1579817132

   Great, thanks a lot @dongjoon-hyun!


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] rangadi commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "rangadi (via GitHub)" <gi...@apache.org>.
rangadi commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1212390633


##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal { val TYPE_NAME = "custom-decimal" }
+class CustomDecimal(schema: Schema) extends LogicalType(CustomDecimal.TYPE_NAME) {
+  val scale : Int = {
+    val obj = schema.getObjectProp("scale")
+    obj match {
+      case null =>
+        throw new IllegalArgumentException("Invalid long-decimal: missing scale");
+      case i : Integer =>
+        i
+      case other =>
+        throw new IllegalArgumentException("Expected int long-decimal:scale")

Review Comment:
   Fix name 'long-decimal' here and other places in the PR.



##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal { val TYPE_NAME = "custom-decimal" }

Review Comment:
   Spread this out. Leave a blank line before next class def.



##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal { val TYPE_NAME = "custom-decimal" }
+class CustomDecimal(schema: Schema) extends LogicalType(CustomDecimal.TYPE_NAME) {

Review Comment:
   Where does scala-doc got? Is this a good place? 



##########
connector/avro/src/main/scala/org/apache/spark/sql/avro/SchemaConverters.scala:
##########
@@ -80,6 +80,8 @@ object SchemaConverters {
       case DOUBLE => SchemaType(DoubleType, nullable = false)
       case FLOAT => SchemaType(FloatType, nullable = false)
       case LONG => avroSchema.getLogicalType match {
+        case d: CustomDecimal =>
+          SchemaType(DecimalType(d.precision, d.scale), nullable = false)

Review Comment:
   Why is it not nullable?  Is it a an Avro thing? Other types seem to be the same.



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] dongjoon-hyun commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1213811989


##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal {
+  val TYPE_NAME = "custom-decimal"
+}
+
+// A customized logical type, which will be registered to Avro. This logical type is similar to
+// Avro's builtin Decimal type, but is meant to be registered for long type. It indicates that
+// the long type should be converted to Spark's Decimal type, with provided precision and scale.
+private class CustomDecimal(schema: Schema) extends LogicalType(CustomDecimal.TYPE_NAME) {
+  val scale : Int = {
+    val obj = schema.getObjectProp("scale")
+    obj match {
+      case null =>
+        throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing scale");
+      case i : Integer =>
+        i
+      case other =>
+        throw new IllegalArgumentException(s"Expected int ${CustomDecimal.TYPE_NAME}:scale")
+    }
+  }
+  val precision : Int = {
+    val obj = schema.getObjectProp("precision")
+    if (obj == null) {
+      throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing precision");
+    }
+    obj.asInstanceOf[Int]
+  }
+  val className : String = schema.getProp("className")
+
+  override def validate(schema: Schema): Unit = {
+    super.validate(schema)
+    if (schema.getType != Schema.Type.LONG) {
+      throw new IllegalArgumentException(
+        s"${CustomDecimal.TYPE_NAME} can only be used with an underlying long type")
+    }
+    if (precision <= 0) {
+      throw new IllegalArgumentException(s"Invalid decimal precision: $precision" +
+        " (must be positive)");
+    } else if (precision > DecimalType.MAX_PRECISION) {
+      throw new IllegalArgumentException(
+        s"cannot store $precision digits (max ${DecimalType.MAX_PRECISION})")
+    }
+    if (scale < 0) {
+      throw new IllegalArgumentException(s"Invalid decimal scale: $scale" +
+        " (must be positive)");
+    } else if (scale > precision) {
+      throw new IllegalArgumentException(s"Invalid decimal scale: $scale (greater than " +
+        s"precision: $precision)");
+    }
+  }
+  override def toString: String =
+    s"${CustomDecimal.TYPE_NAME}<scale: $scale, precision: $precision>"

Review Comment:
   May I ask if where this pattern, `custom-decimal<scale: x, precision: y>`, comes?
   
   1. I'm wondering we can simplify by removing `<scale: ` and `, precision: ` parts because it seems that we omit them previously in Spark decimal type.
   ```scala
   scala> org.apache.spark.sql.types.DecimalType(9,3).toString
   res13: String = DecimalType(9,3)
   
   scala> sql("select cast(1.01 as decimal(9,3))").printSchema
   root
    |-- CAST(1.01 AS DECIMAL(9,3)): decimal(9,3) (nullable = false)
   ``` 
   
   2. Also, we show `precision` first.
   ```scala
   scala> org.apache.spark.sql.types.DecimalType(9,3).precision
   res15: Int = 9
   ```



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] sadikovi commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1213813283


##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal {
+  val TYPE_NAME = "custom-decimal"
+}
+
+// A customized logical type, which will be registered to Avro. This logical type is similar to
+// Avro's builtin Decimal type, but is meant to be registered for long type. It indicates that
+// the long type should be converted to Spark's Decimal type, with provided precision and scale.
+private class CustomDecimal(schema: Schema) extends LogicalType(CustomDecimal.TYPE_NAME) {
+  val scale : Int = {
+    val obj = schema.getObjectProp("scale")
+    obj match {
+      case null =>
+        throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing scale");
+      case i : Integer =>
+        i
+      case other =>
+        throw new IllegalArgumentException(s"Expected int ${CustomDecimal.TYPE_NAME}:scale")
+    }
+  }
+  val precision : Int = {
+    val obj = schema.getObjectProp("precision")
+    if (obj == null) {
+      throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing precision");
+    }
+    obj.asInstanceOf[Int]
+  }
+  val className : String = schema.getProp("className")
+
+  override def validate(schema: Schema): Unit = {
+    super.validate(schema)
+    if (schema.getType != Schema.Type.LONG) {
+      throw new IllegalArgumentException(
+        s"${CustomDecimal.TYPE_NAME} can only be used with an underlying long type")
+    }
+    if (precision <= 0) {
+      throw new IllegalArgumentException(s"Invalid decimal precision: $precision" +
+        " (must be positive)");
+    } else if (precision > DecimalType.MAX_PRECISION) {
+      throw new IllegalArgumentException(
+        s"cannot store $precision digits (max ${DecimalType.MAX_PRECISION})")
+    }
+    if (scale < 0) {
+      throw new IllegalArgumentException(s"Invalid decimal scale: $scale" +
+        " (must be positive)");
+    } else if (scale > precision) {
+      throw new IllegalArgumentException(s"Invalid decimal scale: $scale (greater than " +
+        s"precision: $precision)");
+    }
+  }
+  override def toString: String =
+    s"${CustomDecimal.TYPE_NAME}<scale: $scale, precision: $precision>"

Review Comment:
   +1, I missed it: precision should come first in the string representation and all of the method definitions.



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] dongjoon-hyun commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1213812202


##########
connector/avro/src/test/scala/org/apache/spark/sql/avro/AvroLogicalTypeSuite.scala:
##########
@@ -446,6 +446,98 @@ abstract class AvroLogicalTypeSuite extends QueryTest with SharedSparkSession {
       )
     }
   }
+
+  test("SPARK-43901: LogicalType: Custom Decimal for Long Type") {
+    val schema =
+      new Schema.Parser().parse("""{
+        "namespace": "logical",
+        "type": "record",
+        "name": "test",
+        "fields": [
+         {
+           "name": "field1",
+           "type": {"type": "long", "logicalType": "custom-decimal", "scale": 2, "precision": 38}
+         },
+         {
+           "name": "field2",
+           "type": {"type": "long", "logicalType": "custom-decimal", "scale": 9, "precision": 33}
+         },
+         {
+           "name": "field3",
+           "type": "long"
+         }]
+        }""")
+
+    withTempDir { dir =>
+      val datumWriter = new GenericDatumWriter[GenericRecord](schema)
+      val dataFileWriter = new DataFileWriter[GenericRecord](datumWriter)
+      dataFileWriter.create(schema, new File(s"$dir.avro"))
+      val avroRec = new GenericData.Record(schema)
+      avroRec.put("field1", 123456789L)
+      avroRec.put("field2", 123456789L)
+      avroRec.put("field3", 123456789L)
+      dataFileWriter.append(avroRec)
+      dataFileWriter.flush()
+      dataFileWriter.close()
+      val df = spark
+        .read
+        .format("avro")
+        .load(s"$dir.avro")
+      assertResult(DecimalType(38, 2))(df.schema.head.dataType)
+      val firstRow = df.take(1)(0)
+      assertResult(java.math.BigDecimal.valueOf(123456789L, 2))(firstRow.getAs("field1"))
+      assertResult(java.math.BigDecimal.valueOf(123456789L, 9))(firstRow.getAs("field2"))
+      assertResult(123456789L)(firstRow.getAs("field3"))
+    }
+  }
+
+  test("SPARK-43901:LogicalType: Decimal for Long Type Exception Cases") {

Review Comment:
   nit. `SPARK-43901:LogicalType` -> `SPARK-43901: LogicalType`



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] siying commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "siying (via GitHub)" <gi...@apache.org>.
siying commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1213910062


##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal {
+  val TYPE_NAME = "custom-decimal"
+}
+
+// A customized logical type, which will be registered to Avro. This logical type is similar to
+// Avro's builtin Decimal type, but is meant to be registered for long type. It indicates that
+// the long type should be converted to Spark's Decimal type, with provided precision and scale.
+private class CustomDecimal(schema: Schema) extends LogicalType(CustomDecimal.TYPE_NAME) {
+  val scale : Int = {
+    val obj = schema.getObjectProp("scale")
+    obj match {
+      case null =>
+        throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing scale");
+      case i : Integer =>
+        i
+      case other =>
+        throw new IllegalArgumentException(s"Expected int ${CustomDecimal.TYPE_NAME}:scale")
+    }
+  }
+  val precision : Int = {
+    val obj = schema.getObjectProp("precision")
+    if (obj == null) {
+      throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing precision");
+    }
+    obj.asInstanceOf[Int]
+  }
+  val className : String = schema.getProp("className")
+
+  override def validate(schema: Schema): Unit = {
+    super.validate(schema)
+    if (schema.getType != Schema.Type.LONG) {
+      throw new IllegalArgumentException(
+        s"${CustomDecimal.TYPE_NAME} can only be used with an underlying long type")
+    }
+    if (precision <= 0) {
+      throw new IllegalArgumentException(s"Invalid decimal precision: $precision" +
+        " (must be positive)");
+    } else if (precision > DecimalType.MAX_PRECISION) {
+      throw new IllegalArgumentException(
+        s"cannot store $precision digits (max ${DecimalType.MAX_PRECISION})")
+    }
+    if (scale < 0) {
+      throw new IllegalArgumentException(s"Invalid decimal scale: $scale" +
+        " (must be positive)");
+    } else if (scale > precision) {
+      throw new IllegalArgumentException(s"Invalid decimal scale: $scale (greater than " +
+        s"precision: $precision)");
+    }
+  }
+  override def toString: String =
+    s"${CustomDecimal.TYPE_NAME}<scale: $scale, precision: $precision>"

Review Comment:
   It appears that we don't need to overwrite toString(). Let me just try to remove it.



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] sadikovi commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1213766622


##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal { val TYPE_NAME = "custom-decimal" }

Review Comment:
   I would also suggest writing this as 
   ```scala
   object CustomDecimal { 
     val TYPE_NAME = "custom-decimal" 
   }
   ```



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] sadikovi commented on pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on PR #41409:
URL: https://github.com/apache/spark/pull/41409#issuecomment-1572908647

   @dongjoon-hyun Could you also review this PR? Thank you.


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] siying commented on pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "siying (via GitHub)" <gi...@apache.org>.
siying commented on PR #41409:
URL: https://github.com/apache/spark/pull/41409#issuecomment-1581441603

   @dongjoon-hyun thank you for your help!


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] dongjoon-hyun commented on pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #41409:
URL: https://github.com/apache/spark/pull/41409#issuecomment-1572979757

   Also, cc @gengliangwang , too


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] dongjoon-hyun closed pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long
URL: https://github.com/apache/spark/pull/41409


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] dongjoon-hyun commented on pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #41409:
URL: https://github.com/apache/spark/pull/41409#issuecomment-1575421708

   Could you resolve the conflict, @siying ?


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] siying commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "siying (via GitHub)" <gi...@apache.org>.
siying commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1213545399


##########
connector/avro/src/main/scala/org/apache/spark/sql/avro/SchemaConverters.scala:
##########
@@ -80,6 +80,8 @@ object SchemaConverters {
       case DOUBLE => SchemaType(DoubleType, nullable = false)
       case FLOAT => SchemaType(FloatType, nullable = false)
       case LONG => avroSchema.getLogicalType match {
+        case d: CustomDecimal =>
+          SchemaType(DecimalType(d.precision, d.scale), nullable = false)

Review Comment:
   I have no idea. Decimal for bytes or fixed has the same nullable value, and we have no reason to be inconstant with Decimal. 



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] rangadi commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "rangadi (via GitHub)" <gi...@apache.org>.
rangadi commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1212391568


##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal { val TYPE_NAME = "custom-decimal" }
+class CustomDecimal(schema: Schema) extends LogicalType(CustomDecimal.TYPE_NAME) {

Review Comment:
   Where does scala-doc go? Is this a good place? 



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] siying commented on pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "siying (via GitHub)" <gi...@apache.org>.
siying commented on PR #41409:
URL: https://github.com/apache/spark/pull/41409#issuecomment-1575861442

   @dongjoon-hyun 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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] dongjoon-hyun commented on a diff in pull request #41409: [SPARK-43901][SQL] Avro to Support custom decimal type backed by Long

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #41409:
URL: https://github.com/apache/spark/pull/41409#discussion_r1213805784


##########
connector/avro/src/main/java/org/apache/spark/sql/avro/CustomDecimal.scala:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.avro
+
+import org.apache.avro.LogicalType
+import org.apache.avro.Schema
+
+import org.apache.spark.sql.types.DecimalType
+
+object CustomDecimal {
+  val TYPE_NAME = "custom-decimal"
+}
+
+// A customized logical type, which will be registered to Avro. This logical type is similar to
+// Avro's builtin Decimal type, but is meant to be registered for long type. It indicates that
+// the long type should be converted to Spark's Decimal type, with provided precision and scale.
+private class CustomDecimal(schema: Schema) extends LogicalType(CustomDecimal.TYPE_NAME) {
+  val scale : Int = {
+    val obj = schema.getObjectProp("scale")
+    obj match {
+      case null =>
+        throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing scale");
+      case i : Integer =>
+        i
+      case other =>
+        throw new IllegalArgumentException(s"Expected int ${CustomDecimal.TYPE_NAME}:scale")
+    }
+  }
+  val precision : Int = {
+    val obj = schema.getObjectProp("precision")
+    if (obj == null) {
+      throw new IllegalArgumentException(s"Invalid ${CustomDecimal.TYPE_NAME}: missing precision");
+    }
+    obj.asInstanceOf[Int]

Review Comment:
   Like `scale`, do we need to check if `Integer` type or not?



-- 
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-unsubscribe@spark.apache.org

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


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