-
Notifications
You must be signed in to change notification settings - Fork 603
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(frontend): allow specifying (partial) schema when creating table with derived schema #20203
Open
BugenZhao
wants to merge
12
commits into
main
Choose a base branch
from
bz/purify-sql-part-7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a438d62
feat(frontend): allow specifying (partial) schema when creating table…
BugenZhao dcd23e8
allow alter add|drop column on table with schema registry
BugenZhao ca96712
no need to check whether there's schema registry for deciding strateg…
BugenZhao fbb5db7
allow no wildcard
BugenZhao c6c301c
rearrange 2 ifs
BugenZhao ec6fa50
correctly use cols from sql
BugenZhao 9c31748
add tests
BugenZhao 3b88e4a
refine docs on strategies
BugenZhao 1af4501
fix clippy
BugenZhao d9b6a6b
remove alter table with schema registry error testing
BugenZhao 272ebcc
add retry to flaky test
BugenZhao d4ff462
use unchecked for drop column
BugenZhao 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
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,199 @@ | ||
control substitution on | ||
|
||
# cleanup | ||
system ok | ||
rpk topic delete 'avro_partial_schema_test' || true; \ | ||
(rpk sr subject delete 'avro_partial_schema_test-value' && rpk sr subject delete 'avro_partial_schema_test-value' --permanent) || true; | ||
|
||
# create topic and sr subject | ||
system ok | ||
rpk topic create 'avro_partial_schema_test' | ||
|
||
# create a schema | ||
system ok | ||
sr_register avro_partial_schema_test-value AVRO <<< '{"type":"record","name":"Root","fields":[{"name":"bar","type":"int","default":0},{"name":"foo","type":"string"}]}' | ||
|
||
# Specify schema | ||
statement ok | ||
create table t1 (foo varchar, bar int) | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro_partial_schema_test' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
|
||
# Specify partial schema | ||
statement ok | ||
create table t2 (bar int, gen_col int as bar + 1) | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro_partial_schema_test' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
|
||
# Specify incorrect schema | ||
statement error | ||
create table t (bar int, foo varchar, baz int) | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro_partial_schema_test' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
---- | ||
db error: ERROR: Failed to run the query | ||
|
||
Caused by: | ||
Protocol error: Column "baz" is defined in SQL but not found in the source | ||
|
||
|
||
statement error | ||
create table t (bar double) | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro_partial_schema_test' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
---- | ||
db error: ERROR: Failed to run the query | ||
|
||
Caused by: | ||
Protocol error: Data type mismatch for column "bar". Defined in SQL as "double precision", but found in the source as "integer" | ||
|
||
|
||
# Resolve schema | ||
statement ok | ||
create table tstar (*, gen_col int as bar + 1) | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro_partial_schema_test' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
|
||
# No wildcard will be interpreted as `*` for syntax backward compatibility | ||
statement ok | ||
create table tstar2 (gen_col int as bar + 1) | ||
WITH ( | ||
${RISEDEV_KAFKA_WITH_OPTIONS_COMMON}, | ||
topic = 'avro_partial_schema_test' | ||
) | ||
FORMAT PLAIN ENCODE AVRO ( | ||
schema.registry = '${RISEDEV_SCHEMA_REGISTRY_URL}' | ||
); | ||
|
||
|
||
# Demonstrate purified definition | ||
query TT rowsort | ||
SELECT name, SUBSTRING(definition, 1, POSITION(' WITH' IN definition) - 1) FROM rw_tables WHERE name LIKE 't%'; | ||
---- | ||
t1 CREATE TABLE t1 (foo CHARACTER VARYING, bar INT) | ||
t2 CREATE TABLE t2 (bar INT, gen_col INT AS bar + 1) | ||
tstar CREATE TABLE tstar (bar INT, foo CHARACTER VARYING, gen_col INT AS bar + 1) | ||
tstar2 CREATE TABLE tstar2 (bar INT, foo CHARACTER VARYING, gen_col INT AS bar + 1) | ||
|
||
# create a new schema | ||
system ok | ||
sr_register avro_partial_schema_test-value AVRO <<< '{"type":"record","name":"Root","fields":[{"name":"bar","type":"int","default":0},{"name":"foo","type":"string"}, {"name":"baz", "type":"double", "default":0}]}' | ||
|
||
# Can perform `[ADD | DROP] COLUMN` no matter whether the schema is from resolved | ||
# However, the schema will be checked against the resolved schema | ||
statement ok | ||
alter table t1 drop column foo; | ||
|
||
statement error | ||
alter table t2 add column baz int; | ||
---- | ||
db error: ERROR: Failed to run the query | ||
|
||
Caused by: | ||
Protocol error: Data type mismatch for column "baz". Defined in SQL as "integer", but found in the source as "double precision" | ||
Comment on lines
+112
to
+118
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. does it mean we always keep track of the latest version in the schema registry and prevent possible future name/type collide? 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. Yes |
||
|
||
|
||
statement ok | ||
alter table t2 add column baz double; | ||
|
||
statement error | ||
alter table t2 add column bbaazz double; | ||
---- | ||
db error: ERROR: Failed to run the query | ||
|
||
Caused by: | ||
Protocol error: Column "bbaazz" is defined in SQL but not found in the source | ||
|
||
|
||
statement ok | ||
alter table tstar drop column foo; | ||
|
||
statement error | ||
alter table tstar add column baz int; | ||
---- | ||
db error: ERROR: Failed to run the query | ||
|
||
Caused by: | ||
Protocol error: Data type mismatch for column "baz". Defined in SQL as "integer", but found in the source as "double precision" | ||
|
||
|
||
statement ok | ||
alter table tstar add column baz double; | ||
|
||
statement error | ||
alter table tstar add column bbaazz double; | ||
---- | ||
db error: ERROR: Failed to run the query | ||
|
||
Caused by: | ||
Protocol error: Column "bbaazz" is defined in SQL but not found in the source | ||
|
||
|
||
# Demonstrate purified definition | ||
query TT rowsort | ||
SELECT name, SUBSTRING(definition, 1, POSITION(' WITH' IN definition) - 1) FROM rw_tables WHERE name LIKE 't%'; | ||
---- | ||
t1 CREATE TABLE t1 (bar INT) | ||
t2 CREATE TABLE t2 (bar INT, gen_col INT AS bar + 1, baz DOUBLE) | ||
tstar CREATE TABLE tstar (bar INT, gen_col INT AS bar + 1, baz DOUBLE) | ||
tstar2 CREATE TABLE tstar2 (bar INT, foo CHARACTER VARYING, gen_col INT AS bar + 1) | ||
|
||
# Can refresh schema no matter whether the schema is from resolved | ||
statement ok | ||
alter table t1 refresh schema; | ||
|
||
statement ok | ||
alter table t2 refresh schema; | ||
|
||
statement ok | ||
alter table tstar refresh schema; | ||
|
||
statement ok | ||
alter table tstar2 refresh schema; | ||
|
||
# Demonstrate purified definition | ||
query TT rowsort | ||
SELECT name, SUBSTRING(definition, 1, POSITION(' WITH' IN definition) - 1) FROM rw_tables WHERE name LIKE 't%'; | ||
---- | ||
t1 CREATE TABLE t1 (bar INT, foo CHARACTER VARYING, baz DOUBLE) | ||
t2 CREATE TABLE t2 (bar INT, foo CHARACTER VARYING, baz DOUBLE, gen_col INT AS bar + 1) | ||
tstar CREATE TABLE tstar (bar INT, foo CHARACTER VARYING, baz DOUBLE, gen_col INT AS bar + 1) | ||
tstar2 CREATE TABLE tstar2 (bar INT, foo CHARACTER VARYING, baz DOUBLE, gen_col INT AS bar + 1) | ||
|
||
# Cleanup | ||
statement ok | ||
DROP TABLE t1; | ||
|
||
statement ok | ||
DROP TABLE t2; | ||
|
||
statement ok | ||
DROP TABLE tstar; | ||
|
||
statement ok | ||
DROP TABLE tstar2; |
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
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.
Can you add one more test
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.
Step 3 will not affect the purified SQL because we don't access (then test against) the external schema unless we are going to perform schema change.
However, this is still an interesting example, as I'm not sure about the behavior if we call
REFRESH
orADD COLUMN
afterwards. I'm afraid this has long been undefined behavior since we allow the definition of a generated column on a source. Will test it.