Skip to content
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

Make TextualFromNative output deterministic #222

Open
lebaptiste opened this issue Jan 20, 2021 · 3 comments
Open

Make TextualFromNative output deterministic #222

lebaptiste opened this issue Jan 20, 2021 · 3 comments

Comments

@lebaptiste
Copy link

TextualFromNative output is not deterministic due to the underlying map type used for the data struct.
Ideally the method should be deterministic similarly to what the json.Marshal already does by sorting the map keys.

Current behaviour:

        codec, err := goavro.NewCodec(`
	{
		"type": "record",
		"name": "Schema",
		"fields" : [
		  {"name": "first", "type": "string"},
		  {"name": "second", "type": "string"}
		]
	}`)
	if err != nil {
		fmt.Println(err)
	}

	val := map[string]interface{}{"first":"foo", "second":"bar"}
	for i:=0; i<20;i++ {
		b, _ := codec.TextualFromNative(nil, val)
		fmt.Println(string(b))
	}

Result:

{"first":"foo","second":"bar"}
{"second":"bar","first":"foo"}
{"first":"foo","second":"bar"}
{"second":"bar","first":"foo"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"second":"bar","first":"foo"}
{"first":"foo","second":"bar"}
{"second":"bar","first":"foo"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
{"first":"foo","second":"bar"}
@galion96
Copy link

galion96 commented Aug 2, 2022

Hello, I am also experiencing this issue.
Is there any plan to make a fix for this?

@xmcqueen
Copy link
Contributor

checking

@ckarmann
Copy link

ckarmann commented Nov 1, 2022

Another way to make it deterministic would be to keep the order of the fields in the records in the schema. It would be consistent with what produce Avro library for Java and also with avro-tools.

In general the question of being deterministic is not just to have a consistent JSON representation: the result of going Binary -> Native -> Textual -> Native -> Binary should get you back where you started. And this should be the case regardless of which platform created the first binary. A lot of Avro data around here has been created in Hadoop by Java processes so I think this library should be consistent with it.

For the same reason, maps should try to keep the ordering all along. This means it can not simply use Go's map implementation. An equivalent of Java's LinkedHashMap should be used instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants