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
[SPARK-28698][SQL] Support user-specified output schema in to_avro
#25419
Changes from 1 commit
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)) | ||
} | ||
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 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) | ||
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. 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,31 @@ 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()) | ||
intercept[SparkException] { | ||
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. what's the error message? |
||
AvroDataToCatalyst( | ||
CatalystDataToAvro( | ||
data, | ||
None), | ||
jsonFormatSchema, | ||
options = Map.empty).eval() | ||
} | ||
} | ||
} |
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 .