-
Notifications
You must be signed in to change notification settings - Fork 593
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
feat(source): support Avro Union type #17485
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f8093db
refine current union code
xxchan 51c1803
implement union
xxchan 1ba9154
fix [null, record]
xxchan 66939b2
fix debezium
xxchan f508df8
Update src/connector/codec/src/decoder/avro/schema.rs
xxchan 9d08add
resolve style comments
xxchan a305c1c
ban logical type
xxchan bf445cf
test named type
xxchan 6a8c351
add e2e test, ban named type
xxchan 04112f2
fix
xxchan 7f17f2b
fix
xxchan a8fc09f
add a simpler demo
xxchan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
# Register a schema to schema registry | ||
# | ||
# Usage: sr_register <subject> <schema> | ||
# | ||
# https://docs.confluent.io/platform/current/schema-registry/develop/api.html#post--subjects-(string-%20subject)-versions | ||
|
||
# Validate arguments | ||
if [[ $# -ne 2 ]]; then | ||
echo "Usage: sr_register <subject> <schema>" | ||
exit 1 | ||
fi | ||
|
||
subject="$1" | ||
schema="$2" | ||
|
||
|
||
if [[ -z $subject || -z $schema ]]; then | ||
echo "Error: Arguments cannot be empty" | ||
exit 1 | ||
fi | ||
|
||
echo "$schema" | jq '{"schema": tojson}' \ | ||
| curl -X POST -H 'content-type:application/vnd.schemaregistry.v1+json' -d @- "${RISEDEV_SCHEMA_REGISTRY_URL}/subjects/${subject}/versions" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
control substitution on | ||
|
||
system ok | ||
rpk topic delete 'avro-union' || true; \ | ||
(rpk sr subject delete 'avro-union-value' && rpk sr subject delete 'avro-union-value' --permanent) || true; | ||
rpk topic create avro-union | ||
|
||
system ok | ||
sr_register avro-union-value ' | ||
{ | ||
"type": "record", | ||
"name": "Root", | ||
"fields": [ | ||
{ | ||
"name": "unionType", | ||
"type": ["int", "string"] | ||
}, | ||
{ | ||
"name": "unionTypeComplex", | ||
"type": [ | ||
"null", | ||
{"type": "record", "name": "Email","fields": [{"name":"inner","type":"string"}]}, | ||
{"type": "record", "name": "Fax","fields": [{"name":"inner","type":"int"}]}, | ||
{"type": "record", "name": "Sms","fields": [{"name":"inner","type":"int"}]} | ||
] | ||
}, | ||
{ | ||
"name": "enumField", | ||
"type": ["null", "int", { | ||
"type": "enum", | ||
"name": "myEnum", | ||
"namespace": "my.namespace", | ||
"symbols": ["A", "B", "C", "D"] | ||
}], | ||
"default": null | ||
} | ||
] | ||
} | ||
' | ||
|
||
system ok | ||
cat<<EOF | rpk topic produce avro-union --schema-id=topic | ||
{"unionType": {"int":1}, "unionTypeComplex": {"Sms": {"inner":6}}, "enumField": {"my.namespace.myEnum": "A"}} | ||
{"unionType": {"string":"2"}, "unionTypeComplex": {"Fax": {"inner":6}}} | ||
{"unionType": {"int":3}, "unionTypeComplex": {"Email": {"inner":"[email protected]"}}, "enumField": {"int":66}} | ||
EOF | ||
|
||
statement error | ||
create source avro_union | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro-union' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
---- | ||
db error: ERROR: Failed to run the query | ||
|
||
Caused by these errors (recent errors listed first): | ||
1: connector error | ||
2: failed to convert Avro union to struct | ||
3: Feature is not yet implemented: Avro named type used in Union type: Record(RecordSchema { name: Name { name: "Email", namespace: None }, aliases: None, doc: None, fields: [RecordField { name: "inner", doc: None, aliases: None, default: None, schema: String, order: Ascending, position: 0, custom_attributes: {} }], lookup: {"inner": 0}, attributes: {} }) | ||
Tracking issue: https://github.com/risingwavelabs/risingwave/issues/17632 | ||
|
||
|
||
# FIXME: The following is the current buggy result. | ||
|
||
|
||
# query ? rowsort | ||
# select * from avro_union | ||
# ---- | ||
# ("([email protected])",,) | ||
# (,"(6)",) | ||
# (,"(6)",) | ||
|
||
# # Demonstrate how to access union variants (struct fields) below: | ||
# # Note that we need to use quotes. | ||
|
||
# query ? rowsort | ||
# select ("enumField")."my.namespace.myEnum" from avro_union; | ||
# ---- | ||
# A | ||
# NULL | ||
# NULL | ||
|
||
# # To output the union’s tag (i.e. case in protobuf), a case-when can be used. | ||
# query ? rowsort | ||
# select | ||
# case | ||
# when ("unionTypeComplex")."Sms" is not null then 'Sms' | ||
# when ("unionTypeComplex")."Fax" is not null then 'Fax' | ||
# when ("unionTypeComplex")."Email" is not null then 'Email' | ||
# else null -- optional | ||
# end | ||
# from avro_union; | ||
# ---- | ||
# Fax | ||
# Fax | ||
|
||
|
||
|
||
system ok | ||
rpk topic delete 'avro-union-simple' || true; \ | ||
(rpk sr subject delete 'avro-union-simple-value' && rpk sr subject delete 'avro-union-simple-value' --permanent) || true; | ||
rpk topic create avro-union-simple | ||
|
||
system ok | ||
sr_register avro-union-simple-value ' | ||
{ | ||
"type": "record", | ||
"name": "Root", | ||
"fields": [ | ||
{ | ||
"name": "unionType", | ||
"type": ["int", "string", "null", "boolean"] | ||
} | ||
] | ||
} | ||
' | ||
|
||
system ok | ||
cat<<EOF | rpk topic produce avro-union-simple --schema-id=topic | ||
{"unionType": {"int":1}} | ||
{"unionType": {"string":"2"}} | ||
{"unionType": {"boolean": true}} | ||
{"unionType": null} | ||
EOF | ||
|
||
statement ok | ||
create source avro_union | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro-union-simple' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
|
||
|
||
query ? rowsort | ||
select * from avro_union | ||
---- | ||
(,,t) | ||
(,2,) | ||
(1,,) | ||
NULL | ||
|
||
# Demonstrate how to access union variants (struct fields) below: | ||
# Note that we need to use quotes. | ||
|
||
query ? rowsort | ||
select ("unionType")."string" from avro_union; | ||
---- | ||
2 | ||
NULL | ||
NULL | ||
NULL | ||
|
||
# To output the union’s tag (i.e. case in protobuf), a case-when can be used. | ||
query ? rowsort | ||
select | ||
case | ||
when ("unionType")."int" is not null then 'int' | ||
when ("unionType")."string" is not null then 'string' | ||
when ("unionType")."boolean" is not null then 'boolean' | ||
else null -- optional | ||
end | ||
from avro_union; | ||
---- | ||
NULL | ||
boolean | ||
int | ||
string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Demonstrate with a simpler example that is working now?