You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/10/06 20:07:51 UTC

[GitHub] [arrow-nanoarrow] paleolimbot commented on a diff in pull request #57: [C][R] Stringify schemas

paleolimbot commented on code in PR #57:
URL: https://github.com/apache/arrow-nanoarrow/pull/57#discussion_r989439837


##########
src/nanoarrow/schema.c:
##########
@@ -1088,6 +1088,149 @@ ArrowErrorCode ArrowSchemaViewInit(struct ArrowSchemaView* schema_view,
   return NANOARROW_OK;
 }
 
+static int64_t ArrowSchemaFormatInternal(struct ArrowSchemaView* schema_view,
+                                         enum ArrowType data_type, char* out, int64_t n) {
+  const char* type_string = ArrowTypeString(data_type);
+  switch (data_type) {
+    case NANOARROW_TYPE_DECIMAL128:
+    case NANOARROW_TYPE_DECIMAL256:
+      return snprintf(out, n, "%s(%d, %d)", type_string,
+                      (int)schema_view->decimal_precision,
+                      (int)schema_view->decimal_scale);
+    case NANOARROW_TYPE_TIMESTAMP:
+      return snprintf(out, n, "%s('%s', '%.*s')", type_string,
+                      ArrowTimeUnitString(schema_view->time_unit),
+                      (int)schema_view->timezone.n_bytes, schema_view->timezone.data);
+    case NANOARROW_TYPE_TIME32:
+    case NANOARROW_TYPE_TIME64:
+    case NANOARROW_TYPE_DURATION:
+      return snprintf(out, n, "%s('%s')", type_string,
+                      ArrowTimeUnitString(schema_view->time_unit));
+    case NANOARROW_TYPE_FIXED_SIZE_BINARY:
+    case NANOARROW_TYPE_FIXED_SIZE_LIST:
+      return snprintf(out, n, "%s(%ld)", type_string, (long)schema_view->fixed_size);
+    case NANOARROW_TYPE_SPARSE_UNION:
+    case NANOARROW_TYPE_DENSE_UNION:
+      return snprintf(out, n, "%s([%.*s])", type_string,
+                      (int)schema_view->union_type_ids.n_bytes,
+                      schema_view->union_type_ids.data);
+    default:
+      return snprintf(out, n, "%s", type_string);
+  }
+}
+
+int64_t ArrowSchemaFormat(struct ArrowSchema* schema, char* out, int64_t n,
+                          char recursive) {
+  if (schema == NULL) {
+    return snprintf(out, n, "[invalid: pointer is null]");
+  }
+
+  if (schema->release == NULL) {
+    return snprintf(out, n, "[invalid: schema is released]");
+  }
+
+  struct ArrowSchemaView schema_view;
+  struct ArrowError error;
+
+  if (ArrowSchemaViewInit(&schema_view, schema, &error) != NANOARROW_OK) {

Review Comment:
   So, I changed it back 😬 ...it turns out it takes *more* code to do it the other way. Happy to update this if it turns out to be super annoying! (Or to expose a SchemaView version if that's helpful later on)



-- 
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: github-unsubscribe@arrow.apache.org

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