Skip to content

feat: Add initial support for create table CLI through json schema - #424

Merged
zeroshade merged 6 commits into
apache:mainfrom
lliangyu-lin:create-table-cli
May 10, 2025
Merged

zeroshade merged 6 commits into
apache:mainfrom
lliangyu-lin:create-table-cli

Conversation

@lliangyu-lin

@lliangyu-lin lliangyu-lin commented May 7, 2025 •

Copy link
Copy Markdown
Contributor

Description

  • Adds support for creating table through CLI
  • The schema needs to be provided in json form
    • An alternative idea is to use SQL like form like id bigint NOT NULL COMMENT 'unique id', but the parsing may get complicated with nestings.

Testings

go run . create table lliangyu_test_db.test_tbl \                         
  --catalog glue \
  --schema '[{"name":"id","type":"int","required":false},{"name":"name","type":"string","required":true}]' \
--location-uri s3://xxxxx/xxxxx

Result metadata file

{"last-sequence-number":0,"format-version":2,"table-uuid":"bc6fd1b7-6855-428b-a5f3-65b8fa742e84","location":"s3://xxxxx/xxxxx","last-updated-ms":1746826023185,"last-column-id":2,"schemas":[{"type":"struct","fields":[{"type":"int","id":1,"name":"id","required":false},{"type":"string","id":2,"name":"name","required":true}],"schema-id":0,"identifier-field-ids":[]}],"current-schema-id":0,"partition-specs":[{"spec-id":0,"fields":[]}],"default-spec-id":0,"last-partition-id":999,"sort-orders":[{"order-id":0,"fields":[]}],"default-sort-order-id":0}

@zeroshade

Copy link
Copy Markdown
Member

An alternative idea is to use SQL like form like id bigint NOT NULL COMMENT 'unique id', but the parsing may get complicated with nestings.

Yea, let's not attempt to parse SQL ourselves haha. Providing it via JSON should be fine.

Comment thread cmd/iceberg/main.go Resolved
Comment thread schema.go Outdated
Comment on lines +54 to +63
// NewSchemaFromJson constructs a new schema with the provided ID and a string in json form
func NewSchemaFromJson(id int, jsonStr string) (*Schema, error) {
var fields []NestedField
err := json.Unmarshal([]byte(jsonStr), &fields)
if err != nil {
return nil, fmt.Errorf("failed to parse schema JSON: %w", err)
}

return NewSchema(id, fields...), nil
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually need this function instead of just letting people use json.Unmarshal(bytes, &schema) ?

@lliangyu-lin lliangyu-lin May 8, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zeroshade Thanks for the review! I think for directly parse to schema means user also needs to provide the other fields in the actual schema structure in https://github.com/apache/iceberg-go/blob/main/schema.go#L36-L39
Ex:

--schema '{
  "schema-id": 1,
  "identifier-field-ids": [1],
  "fields": [
    {"id":1,"name":"id","type":"int","required":true},
    {"id":2,"name":"name","type":"string","required":false}
  ]
}'

I'm not sure if it's good idea to ask user to specify the schema-id and identifier-field-ids.
But I agree that a new function may not be needed, we can simply parse in main whether it's entire schema or just the fields, but my concern is we also then export the init() in order to be called in main.

What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay that's fair enough, we should probably rename the argument to be --fields then instead 😄

Comment thread cmd/iceberg/main.go Outdated
--description TEXT specify a description for the namespace
--location-uri TEXT specify a location URI for the namespace`
--location-uri TEXT specify a location URI for the namespace
--fields TEXT specify table schema fields in json (for create table use only)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fields or schema?

@lliangyu-lin lliangyu-lin May 9, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kevinjqliu, thank you for looking into this! I think schema and fields was also discussed above with @zeroshade
#424 (comment)

The concern about specifying a full schema json is that user will also be asked to provide the json field for "schema-id", "identifier-field-ids", which are more like internal metadata that user don't need to worry about. And usually create table only requires specifying the table fields and their type, so it's just the fields. Although I feel like the word schema is more understandable compare to fields.

Ex:

--schema '{
  "schema-id": 1,
  "identifier-field-ids": [1],
  "fields": [
    {"id":1,"name":"id","type":"int","required":true},
    {"id":2,"name":"name","type":"string","required":false}
  ]
}'

and

--fields '{[
    {"id":1,"name":"id","type":"int","required":true},
    {"id":2,"name":"name","type":"string","required":false}
]}'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh thanks for pointing me to that, i didnt see it initially.

i like

--schema '{[
    {"name":"id","type":"int","required":true},
    {"name":"name","type":"string","required":false}
]}'

we dont necessary need schema-id, theres a default value. we can also provide --schema-id to override it.
Similarly for identifier-field-ids, its default to empty list and we can use --identifier-field-ids to override that param too.
the id inside the fields can also be auto assigned.

For me, the --schema represents the schema of the table itself, without any of the details about iceberg tables.

go run . create table lliangyu_test_db.test_tbl \                         
  --catalog glue \
  --schema '[{"name":"id","type":"int","required":false},{"name":"name","type":"string","required":true}]' \
--location-uri s3://xxxxx/xxxxx

should be sufficient

WDYT? This is definitely a nit comment :)

@lliangyu-lin lliangyu-lin May 9, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree. I like --schema '[{"name":"id","type":"int","required":false},{"name":"name","type":"string","required":true}]' as well. Put out an update on this.
I'm thinking we could support schema-id, identifier-field-ids in separate PRs.

@kevinjqliu kevinjqliu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

I dont think we have integration tests yet but it'll be good to run this against a catalog :)

Comment thread cmd/iceberg/main.go Outdated
Comment on lines +83 to +84
--schema TEXT specify table schema in json (for create table use only)
Ex: [{"name":"id","type":"int","required":false,"doc":"unique id"}]`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be TEXT or JSON?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also does the Ex:... part show up in usage? all the other options have 1 line only

@lliangyu-lin lliangyu-lin May 9, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be TEXT or JSON?

Goot point, I think JSON make more sense. Let me update that

also does the Ex:... part show up in usage?

Yes, this is what shows up when I ran go run . -h. Definitely feel like the formatting could be improved, but I think could be fixed later.

Options:
  -h --help          show this help messages and exit
  --catalog TEXT     specify the catalog type [default: rest]
  --uri TEXT         specify the catalog URI
  --output TYPE      output type (json/text) [default: text]
  --credential TEXT  specify credentials for the catalog
  --warehouse TEXT   specify the warehouse to use
  --config TEXT      specify the path to the configuration file
  --description TEXT    specify a description for the namespace
  --location-uri TEXT   specify a location URI for the namespace
  --schema TEXT        specify table schema in json (for create table use only)
                       Ex: [{"name":"id","type":"int","required":false,"doc":"unique id"}]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for checking! I just wanted to make sure its not truncated :)

@kevinjqliu
kevinjqliu requested a review from zeroshade May 9, 2025 23:53

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

@zeroshade
zeroshade merged commit f32578d into apache:main May 10, 2025
@kevinjqliu

Copy link
Copy Markdown
Contributor

Thanks for the contribution @lliangyu-lin!

I just tested this locally against pyiceberg's integration test's IRC.

go run ./cmd/iceberg --uri http://0.0.0.0:8181 create table default.test  --schema '[{"name":"id","type":"int","required":false},{"name":"name","type":"string","required":true}]'

did not output any messages, even though it ran successfully

Another run of the same command outputted:

2025/05/10 18:08:42 failed to create table: AlreadyExistsException: Table already exists: default.test
exit status 1

I also noticed that when running go run ./cmd/iceberg -h, the create command doesn't show up in the Commands: section.

I think we can address these in a followup PR :)

@lliangyu-lin
lliangyu-lin deleted the create-table-cli branch May 12, 2025 04:57
zeroshade pushed a commit that referenced this pull request May 13, 2025
…it table API and improve references (#429)

### Description
* Close the gap in properties CLI using the commit table implementation
* Update references and loggings as mentioned in #424

### Testing
```
go run . properties get table lliangyu_test_db.tbl1 --catalog glue                  
┌──────────────┐
| Key  | Value |
| ------------ |
| key1 | val1  |
└──────────────┘

go run . properties set table lliangyu_test_db.tbl1 write.object-storage.enabled true --catalog glue
Setting write.object-storage.enabled=true on lliangyu_test_db.tbl1
Updated write.object-storage.enabled on lliangyu_test_db.tbl1

go run . properties get table lliangyu_test_db.tbl1 --catalog glue                                  
┌──────────────────────────────────────┐
| Key                          | Value |
| ------------------------------------ |
| key1                         | val1  |
| write.object-storage.enabled | true  |
└──────────────────────────────────────┘
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

Successfully merging this pull request may close these issues.

3 participants