Skip to content

Added logic to create table from schema. - #219

Merged
subkanthi merged 9 commits into
masterfrom
218-add-support-for-creating-table-with-schema
Sep 25, 2026
Merged

subkanthi merged 9 commits into
masterfrom
218-add-support-for-creating-table-with-schema

Conversation

@subkanthi

Copy link
Copy Markdown
Collaborator

closes: #218

Add support for creating table by passing schema.

@subkanthi subkanthi linked an issue Sep 15, 2026 that may be closed by this pull request
@subkanthi
subkanthi force-pushed the 218-add-support-for-creating-table-with-schema branch from c8b7049 to 6b6e467 Compare September 15, 2026 21:02
@subkanthi
subkanthi force-pushed the 218-add-support-for-creating-table-with-schema branch from e238b47 to 0144dd4 Compare September 15, 2026 21:55
@subkanthi
subkanthi force-pushed the 218-add-support-for-creating-table-with-schema branch from 4a93449 to 7d50554 Compare September 16, 2026 16:30
@subkanthi subkanthi closed this Sep 16, 2026
@subkanthi subkanthi reopened this Sep 16, 2026
@subkanthi

subkanthi commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator Author
create-table ns1.t1 --format-version=3   --schema '[{"name":"id","type":"int","required":true},{"name":"payload","type":"unknown"}]'
describe -s ns1.t1
2026-09-19 12:44:16 [main/3332010] INFO o.a.i.r.auth.AuthManagers > Loading AuthManager implementation: org.apache.iceberg.rest.auth.NoopAuthManager
---
- kind: Table
  metadata:
    id: ns1.t1
  data:
    schemaRaw: |-
      table {
        1: id: required int
        2: payload: optional unknown
      }
    partitionSpecRaw: "[]"
    sortOrderRaw: "[]"
    formatVersion: 3
    location: s3://bucket1/ns1/t1

}

if (schemaJson != null && !schemaJson.isEmpty()) {
CreateTable.run(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

wanted to not to touch the current flow of CreateTable

@subkanthi
subkanthi requested a review from xieandrew September 21, 2026 15:47

@xieandrew xieandrew left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There's an issue with field id ordering with nested schemas plus some other comments.

Also, not exactly from this PR but I noticed that nested fields don't support the required option, in IcebergTypeParser it always uses optional. Not sure if that belongs in this PR or could be added later.

Comment on lines +75 to +83
int fieldId = nextId.incrementAndGet();
Type type = IcebergTypeParser.parseType(field.type(), nextId);
boolean required = field.required() != null && field.required();
columns.add(
required
? Types.NestedField.required(fieldId, field.name(), type, field.doc())
: Types.NestedField.optional(fieldId, field.name(), type, field.doc()));
}
return new Schema(columns);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The field id ordering from this method doesn't match what the metadata actually creates for nested schemas with struct. For example ice create-table schema.nested --schema '[{"name":"a","type":"struct<x:string,y:long>"},{"name":"b","type":"long"}]' results in "a": 1, "x": 3, "y": 4, "b": 2 in the real table metadata but this method produces "a": 1, "x": 2, "y": 3, "b": 4 which ends up in the table property "schema.name-mapping.default" (mismatched from the real schema). I think the fix is to add TypeUtil.assignIncreasingFreshIds(schema) before the return.

}

private static Type parseType(String typeString, AtomicInteger nextId) {
public static Type parseType(String typeString, AtomicInteger nextId) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Create table with variant type does not work: ice create-table schema.variant --schema '[{"name":"v","type":"variant"}]'

Error:

java.lang.IllegalArgumentException: Cannot parse type string: variant is not a primitive type
        at org.apache.iceberg.types.Types.fromPrimitiveString(Types.java:116)
        at com.altinity.ice.cli.internal.util.IcebergTypeParser.parseType(IcebergTypeParser.java:52)
        at com.altinity.ice.cli.internal.util.IceSchemaParser.toSchema(IceSchemaParser.java:77)
        at com.altinity.ice.cli.internal.util.IceSchemaParser.parse(IceSchemaParser.java:56)
        at com.altinity.ice.cli.Main.createTable(Main.java:403)
        at com.altinity.ice.cli.Main.lambda$main$4(Main.java:1258) [9 skipped]
        at com.altinity.ice.cli.Main.main(Main.java:1265) [1 skipped]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

created #222

formatVersion,
partitions,
sortOrders);
if ((schemaFile == null) == (schemaJson == null || schemaJson.isEmpty())) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think schemaFile should check isEmpty too since --schema-from-parquet "" with empty string would currently be allowed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

Map<Integer, Types.NestedField> byId = TypeUtil.indexById(schema.asStruct());
// a, x, y, b, list element
assertThat(byId).hasSize(5);
assertThat(byId.keySet()).containsExactlyInAnyOrder(1, 2, 3, 4, 5);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should check the exact order not just a set to verify the ordering issue from the comment in IceSchemaParser.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@subkanthi
subkanthi requested a review from xieandrew September 25, 2026 03:41

@xieandrew xieandrew left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me

@subkanthi subkanthi added this to the v0.23.0 milestone Sep 25, 2026
@subkanthi subkanthi added the ice Relates to ice label Sep 25, 2026
@subkanthi
subkanthi merged commit ca476fa into master Sep 25, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ice Relates to ice

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for creating table with schema

2 participants