-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-28698][SQL] Support user-specified output schema in to_avro
#25419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,19 @@ object functions { | |
| */ | ||
| @Experimental | ||
| def to_avro(data: Column): Column = { | ||
| new Column(CatalystDataToAvro(data.expr)) | ||
| new Column(CatalystDataToAvro(data.expr, None)) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have the default value
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment in #25419 (comment) |
||
|
|
||
| /** | ||
| * Converts a column into binary of avro format. | ||
| * | ||
| * @param data the data column. | ||
| * @param jsonFormatSchema user-specified output avro schema in JSON string format. | ||
| * | ||
| * @since 3.0.0 | ||
| */ | ||
| @Experimental | ||
| def to_avro(data: Column, jsonFormatSchema: String): Column = { | ||
| new Column(CatalystDataToAvro(data.expr, Some(jsonFormatSchema))) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ package org.apache.spark.sql.avro | |
|
|
||
| import org.apache.avro.Schema | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.{SparkException, SparkFunSuite} | ||
| import org.apache.spark.sql.{RandomDataGenerator, Row} | ||
| import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow} | ||
| import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper, GenericInternalRow, Literal} | ||
|
|
@@ -38,12 +38,12 @@ class AvroCatalystDataConversionSuite extends SparkFunSuite | |
|
|
||
| private def checkResult(data: Literal, schema: String, expected: Any): Unit = { | ||
| checkEvaluation( | ||
| AvroDataToCatalyst(CatalystDataToAvro(data), schema, Map.empty), | ||
| AvroDataToCatalyst(CatalystDataToAvro(data, None), schema, Map.empty), | ||
| prepareExpectedResult(expected)) | ||
| } | ||
|
|
||
| protected def checkUnsupportedRead(data: Literal, schema: String): Unit = { | ||
| val binary = CatalystDataToAvro(data) | ||
| val binary = CatalystDataToAvro(data, None) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, if we have the default value, we don't need to change line 41 and 46.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment in #25419 (comment) |
||
| intercept[Exception] { | ||
| AvroDataToCatalyst(binary, schema, Map("mode" -> "FAILFAST")).eval() | ||
| } | ||
|
|
@@ -209,4 +209,32 @@ class AvroCatalystDataConversionSuite extends SparkFunSuite | |
| checkUnsupportedRead(input, avroSchema) | ||
| } | ||
| } | ||
|
|
||
| test("user-specified schema") { | ||
| val data = Literal("SPADES") | ||
| val jsonFormatSchema = | ||
| """ | ||
| |{ "type": "enum", | ||
| | "name": "Suit", | ||
| | "symbols" : ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"] | ||
| |} | ||
| """.stripMargin | ||
| checkEvaluation( | ||
| AvroDataToCatalyst( | ||
| CatalystDataToAvro( | ||
| data, | ||
| Some(jsonFormatSchema)), | ||
| jsonFormatSchema, | ||
| options = Map.empty), | ||
| data.eval()) | ||
| val message = intercept[SparkException] { | ||
| AvroDataToCatalyst( | ||
| CatalystDataToAvro( | ||
| data, | ||
| None), | ||
| jsonFormatSchema, | ||
| options = Map.empty).eval() | ||
| }.getMessage | ||
| assert(message.contains("Malformed records are detected in record parsing.")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this PR,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this error comes from
How do you think about that, @gengliangwang ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here
I will change the order of the two checks in the case and add a new test case for invalid user-specified schema
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. Thanks, @gengliangwang . |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a default value
None?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I am trying to avoid parameter with a default value. The result is quite different with/without a specified schema.
Also, this is consistent with
CatalystDataToAvro.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless the default value is used a lot in tests, I don't think we should add default value in internal classes. We should force the caller side to specify the parameter when they instantiate the internal class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, @gengliangwang and @cloud-fan .