dttung2905 commented on code in PR #414: URL: https://github.com/apache/iceberg-go/pull/414#discussion_r2076094123
########## catalog/sql/sql.go: ########## @@ -844,3 +871,235 @@ func (c *Catalog) UpdateNamespaceProperties(ctx context.Context, namespace table func (c *Catalog) CheckNamespaceExists(ctx context.Context, namespace table.Identifier) (bool, error) { return c.namespaceExists(ctx, strings.Join(namespace, ".")) } + +// CreateView creates a new view in the catalog. +func (c *Catalog) CreateView(ctx context.Context, identifier table.Identifier, schema *iceberg.Schema, viewSQL string, props iceberg.Properties) error { + nsIdent := catalog.NamespaceFromIdent(identifier) + viewIdent := catalog.TableNameFromIdent(identifier) + ns := strings.Join(nsIdent, ".") + + exists, err := c.namespaceExists(ctx, ns) + if err != nil { + return err + } + if !exists { + return fmt.Errorf("%w: %s", catalog.ErrNoSuchNamespace, ns) + } + + exists, err = c.CheckViewExists(ctx, identifier) + if err != nil { + return err + } + if exists { + return fmt.Errorf("%w: %s", catalog.ErrViewAlreadyExists, identifier) + } + + viewVersion := struct { + VersionID int64 `json:"version-id"` + TimestampMs int64 `json:"timestamp-ms"` + SchemaID int `json:"schema-id"` + Summary map[string]string `json:"summary"` + Representations []struct { + Type string `json:"type"` + SQL string `json:"sql"` + Dialect string `json:"dialect"` + } `json:"representations"` + DefaultCatalog string `json:"default-catalog"` + DefaultNamespace []string `json:"default-namespace"` + }{ + VersionID: 1, + TimestampMs: time.Now().UnixMilli(), + SchemaID: schema.ID, + Summary: map[string]string{"sql": viewSQL}, + Representations: []struct { + Type string `json:"type"` + SQL string `json:"sql"` + Dialect string `json:"dialect"` + }{ + {Type: "sql", SQL: viewSQL, Dialect: "default"}, + }, + DefaultCatalog: c.name, + DefaultNamespace: nsIdent, + } + + viewVersionBytes, err := json.Marshal(viewVersion) + if err != nil { + return fmt.Errorf("failed to marshal view version: %w", err) + } + + if props == nil { + props = iceberg.Properties{} + } + props["view-version"] = string(viewVersionBytes) + props["view-format"] = "iceberg" + props["view-sql"] = viewSQL + + schemaBytes, err := json.Marshal(schema) + if err != nil { + return fmt.Errorf("failed to marshal schema: %w", err) + } + + // Create metadata location (even though we're not actually writing a metadata file) + metadataLocation := fmt.Sprintf("virtual://%s/%s/%s.view.json", c.name, ns, viewIdent) Review Comment: @zeroshade I'm not entirely sure what to put under metadaLocation so I'm putting a dummy one. Please let me know if its okay -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org