Skip to content

Instantly share code, notes, and snippets.

@skonto
Created January 22, 2020 11:51
Show Gist options
  • Save skonto/ede89e51313569f2954f92e365b1d7dd to your computer and use it in GitHub Desktop.
Save skonto/ede89e51313569f2954f92e365b1d7dd to your computer and use it in GitHub Desktop.
Replace https://github.com/apache/spark/blob/f2d71f5838f4a611e3ebd60f49ee80521d772524/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/JavaTypeInference.scala#L138-L153
with
case other =>
if (seenTypeSet.contains(other)) {
logInfo("SeenTypeSet:")
seenTypeSet.foreach{ x => logInfo(s"class: $x")}
throw new UnsupportedOperationException(
"Cannot have circular references in bean class, but got the circular reference " +
s"of class $other")
}
// TODO: we should only collect properties that have getter and setter. However, some tests
// pass in scala case class as java bean class which doesn't have getter and setter.
val properties = getJavaBeanReadableProperties(other)
val fields = properties.map { property =>
val returnType = typeToken.method(property.getReadMethod).getReturnType
logInfo(s"typetoken: $typeToken")
logInfo(s"method: ${typeToken.method(property.getReadMethod)}")
logInfo(s"returnType: ${typeToken.method(property.getReadMethod).getReturnType}")
logInfo(s"other: $other")
val (dataType, nullable) = inferDataType(returnType, seenTypeSet + other)
logInfo("-------------")
new StructField(property.getName, dataType, nullable)
}
(new StructType(fields), true)
20/01/21 19:52:58 INFO JavaTypeInference: typetoken: example.avro.User
20/01/21 19:52:58 INFO JavaTypeInference: method: public java.lang.String example.avro.User.getFavoriteColor()
20/01/21 19:52:58 INFO JavaTypeInference: returnType: java.lang.String
20/01/21 19:52:58 INFO JavaTypeInference: other: class example.avro.User
20/01/21 19:52:58 INFO JavaTypeInference: -------------
20/01/21 19:52:58 INFO JavaTypeInference: typetoken: example.avro.User
20/01/21 19:52:58 INFO JavaTypeInference: method: public java.lang.String example.avro.User.getName()
20/01/21 19:52:58 INFO JavaTypeInference: returnType: java.lang.String
20/01/21 19:52:58 INFO JavaTypeInference: other: class example.avro.User
20/01/21 19:52:58 INFO JavaTypeInference: -------------
20/01/21 19:52:58 INFO JavaTypeInference: typetoken: example.avro.User
20/01/21 19:52:58 INFO JavaTypeInference: method: public org.apache.avro.Schema example.avro.User.getSchema()
20/01/21 19:52:58 INFO JavaTypeInference: returnType: org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: other: class example.avro.User
20/01/21 19:52:58 INFO JavaTypeInference: typetoken: org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: method: public java.util.Set org.apache.avro.Schema.getAliases()
20/01/21 19:52:58 INFO JavaTypeInference: returnType: java.util.Set<java.lang.String>
20/01/21 19:52:58 INFO JavaTypeInference: other: class org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: -------------
20/01/21 19:52:58 INFO JavaTypeInference: typetoken: org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: method: public java.lang.String org.apache.avro.Schema.getDoc()
20/01/21 19:52:58 INFO JavaTypeInference: returnType: java.lang.String
20/01/21 19:52:58 INFO JavaTypeInference: other: class org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: -------------
20/01/21 19:52:58 INFO JavaTypeInference: typetoken: org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: method: public org.apache.avro.Schema org.apache.avro.Schema.getElementType()
20/01/21 19:52:58 INFO JavaTypeInference: returnType: org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: other: class org.apache.avro.Schema
20/01/21 19:52:58 INFO JavaTypeInference: SeenTypeSet:
20/01/21 19:52:58 INFO JavaTypeInference: class: class example.avro.User
20/01/21 19:52:58 INFO JavaTypeInference: class: class org.apache.avro.Schema
User avro objectL
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package example.avro;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class User extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = 3662299783880045548L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"example.avro\",\"fields\":[{\"name\":\"name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"favorite_color\",\"type\":[{\"type\":\"string\",\"avro.java.string\":\"String\"},\"null\"]}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<User> ENCODER =
new BinaryMessageEncoder<User>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<User> DECODER =
new BinaryMessageDecoder<User>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageDecoder instance used by this class.
*/
public static BinaryMessageDecoder<User> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
* @param resolver a {@link SchemaStore} used to find schemas by fingerprint
*/
public static BinaryMessageDecoder<User> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<User>(MODEL$, SCHEMA$, resolver);
}
/** Serializes this User to a ByteBuffer. */
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/** Deserializes a User from a ByteBuffer. */
public static User fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
@Deprecated public java.lang.String name;
@Deprecated public java.lang.String favorite_color;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public User() {}
/**
* All-args constructor.
* @param name The new value for name
* @param favorite_color The new value for favorite_color
*/
public User(java.lang.String name, java.lang.String favorite_color) {
this.name = name;
this.favorite_color = favorite_color;
}
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return name;
case 1: return favorite_color;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
// Used by DatumReader. Applications should not call.
@SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: name = (java.lang.String)value$; break;
case 1: favorite_color = (java.lang.String)value$; break;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
/**
* Gets the value of the 'name' field.
* @return The value of the 'name' field.
*/
public java.lang.String getName() {
return name;
}
/**
* Sets the value of the 'name' field.
* @param value the value to set.
*/
public void setName(java.lang.String value) {
this.name = value;
}
/**
* Gets the value of the 'favorite_color' field.
* @return The value of the 'favorite_color' field.
*/
public java.lang.String getFavoriteColor() {
return favorite_color;
}
/**
* Sets the value of the 'favorite_color' field.
* @param value the value to set.
*/
public void setFavoriteColor(java.lang.String value) {
this.favorite_color = value;
}
/**
* Creates a new User RecordBuilder.
* @return A new User RecordBuilder
*/
public static example.avro.User.Builder newBuilder() {
return new example.avro.User.Builder();
}
/**
* Creates a new User RecordBuilder by copying an existing Builder.
* @param other The existing builder to copy.
* @return A new User RecordBuilder
*/
public static example.avro.User.Builder newBuilder(example.avro.User.Builder other) {
return new example.avro.User.Builder(other);
}
/**
* Creates a new User RecordBuilder by copying an existing User instance.
* @param other The existing instance to copy.
* @return A new User RecordBuilder
*/
public static example.avro.User.Builder newBuilder(example.avro.User other) {
return new example.avro.User.Builder(other);
}
/**
* RecordBuilder for User instances.
*/
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<User>
implements org.apache.avro.data.RecordBuilder<User> {
private java.lang.String name;
private java.lang.String favorite_color;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* @param other The existing Builder to copy.
*/
private Builder(example.avro.User.Builder other) {
super(other);
if (isValidValue(fields()[0], other.name)) {
this.name = data().deepCopy(fields()[0].schema(), other.name);
fieldSetFlags()[0] = true;
}
if (isValidValue(fields()[1], other.favorite_color)) {
this.favorite_color = data().deepCopy(fields()[1].schema(), other.favorite_color);
fieldSetFlags()[1] = true;
}
}
/**
* Creates a Builder by copying an existing User instance
* @param other The existing instance to copy.
*/
private Builder(example.avro.User other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other.name)) {
this.name = data().deepCopy(fields()[0].schema(), other.name);
fieldSetFlags()[0] = true;
}
if (isValidValue(fields()[1], other.favorite_color)) {
this.favorite_color = data().deepCopy(fields()[1].schema(), other.favorite_color);
fieldSetFlags()[1] = true;
}
}
/**
* Gets the value of the 'name' field.
* @return The value.
*/
public java.lang.String getName() {
return name;
}
/**
* Sets the value of the 'name' field.
* @param value The value of 'name'.
* @return This builder.
*/
public example.avro.User.Builder setName(java.lang.String value) {
validate(fields()[0], value);
this.name = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the 'name' field has been set.
* @return True if the 'name' field has been set, false otherwise.
*/
public boolean hasName() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the 'name' field.
* @return This builder.
*/
public example.avro.User.Builder clearName() {
name = null;
fieldSetFlags()[0] = false;
return this;
}
/**
* Gets the value of the 'favorite_color' field.
* @return The value.
*/
public java.lang.String getFavoriteColor() {
return favorite_color;
}
/**
* Sets the value of the 'favorite_color' field.
* @param value The value of 'favorite_color'.
* @return This builder.
*/
public example.avro.User.Builder setFavoriteColor(java.lang.String value) {
validate(fields()[1], value);
this.favorite_color = value;
fieldSetFlags()[1] = true;
return this;
}
/**
* Checks whether the 'favorite_color' field has been set.
* @return True if the 'favorite_color' field has been set, false otherwise.
*/
public boolean hasFavoriteColor() {
return fieldSetFlags()[1];
}
/**
* Clears the value of the 'favorite_color' field.
* @return This builder.
*/
public example.avro.User.Builder clearFavoriteColor() {
favorite_color = null;
fieldSetFlags()[1] = false;
return this;
}
@Override
@SuppressWarnings("unchecked")
public User build() {
try {
User record = new User();
record.name = fieldSetFlags()[0] ? this.name : (java.lang.String) defaultValue(fields()[0]);
record.favorite_color = fieldSetFlags()[1] ? this.favorite_color : (java.lang.String) defaultValue(fields()[1]);
return record;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<User>
WRITER$ = (org.apache.avro.io.DatumWriter<User>)MODEL$.createDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<User>
READER$ = (org.apache.avro.io.DatumReader<User>)MODEL$.createDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment