You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "zeroshade (via GitHub)" <gi...@apache.org> on 2025/06/05 19:36:26 UTC

Re: [PR] feat(catalog/sql): Add views related ops [iceberg-go]

zeroshade commented on code in PR #414:
URL: https://github.com/apache/iceberg-go/pull/414#discussion_r2130036445


##########
catalog/sql/sql.go:
##########
@@ -809,3 +823,350 @@ 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)
+	}
+
+	loc, err := internal.ResolveTableLocation(ctx, "", ns, viewIdent, c.props, c.LoadNamespaceProperties)
+	if err != nil {
+		return err
+	}
+
+	timestampMs := time.Now().UnixMilli()
+	versionId := int64(1)
+
+	viewVersion := struct {
+		VersionID       int64             `json:"version-id"`
+		TimestampMs     int64             `json:"timestamp-ms"`
+		SchemaID        int               `json:"schema-id"`
+		Summary         map[string]string `json:"summary"`
+		Operation       string            `json:"operation"`
+		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:   versionId,
+		TimestampMs: timestampMs,
+		SchemaID:    schema.ID,
+		Summary:     map[string]string{"sql": viewSQL},
+		Operation:   "create",
+		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
+
+	metadataLocation := loc + "/metadata/view-" + uuid.New().String() + ".metadata.json"
+
+	viewUUID := uuid.New().String()
+	props["view-uuid"] = viewUUID
+
+	viewMetadata := map[string]interface{}{
+		"view-uuid":          viewUUID,
+		"format-version":     1,
+		"location":           loc,
+		"schema":             schema,
+		"current-version-id": versionId,
+		"versions": map[string]interface{}{
+			"1": viewVersion,
+		},
+		"properties": props,
+		"version-log": []map[string]interface{}{
+			{
+				"timestamp-ms": timestampMs,
+				"version-id":   versionId,
+			},
+		},
+	}
+
+	viewMetadataBytes, err := json.Marshal(viewMetadata)
+	if err != nil {
+		return fmt.Errorf("failed to marshal view metadata: %w", err)
+	}
+
+	fs, err := io.LoadFS(ctx, c.props, metadataLocation)
+	if err != nil {
+		return fmt.Errorf("failed to load filesystem for view metadata: %w", err)
+	}
+
+	wfs, ok := fs.(io.WriteFileIO)
+	if !ok {
+		return errors.New("filesystem IO does not support writing")
+	}
+
+	out, err := wfs.Create(metadataLocation)
+	if err != nil {
+		return fmt.Errorf("failed to create view metadata file: %w", err)
+	}
+	defer out.Close()
+
+	if _, err := out.Write(viewMetadataBytes); err != nil {
+		return fmt.Errorf("failed to write view metadata: %w", err)
+	}

Review Comment:
   This whole section should be pulled out as a helper function so that other catalogs can leverage this part easily to create the view metadata files



-- 
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-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org