
我正在学习 apache arrow,想要了解有关如何创建模式和箭头记录的更多信息。为此,我引用了一些材料,但到目前为止,所有这些材料都只是使用原始类型来构建如下所示的模式:`
schema := arrow.NewSchema(
[]arrow.Field{
{Name: "f1-i32", Type: arrow.PrimitiveTypes.Int32},
{Name: "f2-f64", Type: arrow.PrimitiveTypes.Float64},
},
nil,
)
我想要使用的 primitivetypes 中不存在一些数据类型。例如,我想使用bool或decimal128。我正在查看 golang 箭头库,发现文件 datatype.go ,其中包含我想要使用的所有可能的数据类型。
但这里的类型不是构建模式时所需的 datatype 类型。
所以,我有以下三个问题:
- 如果可能的话,如何使用
datatype.go中的这些数据类型来构建我的架构? - 如果我想使用小数类型,如何指定精度和小数位数?
- 使用扩展类型的示例。
正确答案
在 datatype.go 中定义的这些数据类型命名常量已用于创建您想要的新类型的一部分。其中一些是 type decimal128type struct 和 type booleantype struct 如果您检查这些结构的 id 方法的源代码,它们返回在 datatype.go 中定义的常量,其名称与结构的名称相似。这些结构已经实现了 datatype 接口,这意味着您可以将它们分配给 arrow.field.type 因为该字段的类型是 datatype。
我对他们的意思是:bool 中定义的常量 datatype.go 在 datatype_fixedwidth.go 中用作 type booleantype struct 的 id 方法的返回值。func (t *booleantype) id() 类型 { return bool }
同样的事情也适用于 type decimal128type struct 。func (*decimal128type) id() 类型 { return decimal128 }.
这些结构之一的方法显示它们正在实现 datatype 接口:
func (*decimal128type) bitwidth() int func (t *decimal128type) fingerprint() string func (*decimal128type) id() type func (*decimal128type) name() string func (t *decimal128type) string() string
这些方法适用于 type decimal128type struct。
以及datatype接口的定义:
type datatype interface {
id() type
// name is name of the data type.
name() string
fingerprint() string
}
type booleantype struct 也实现了它。
立即学习“go语言免费学习笔记(深入)”;
因此,您可以将它们用于 type 字段:
type field struct {
name string // field name
type datatype // the field's data type
nullable bool // fields can be nullable
metadata metadata // the field's metadata, if any
}
示范性示例:
package main
import (
"fmt"
"github.com/apache/arrow/go/arrow"
)
func main() {
booltype := &arrow.booleantype{}
decimal128type := &arrow.decimal128type{precision: 1, scale: 1}
schema := arrow.newschema(
[]arrow.field{
{name: "f1-bool", type: booltype},
{name: "f2-decimal128", type: decimal128type},
},
nil,
)
fmt.println(schema)
}
输出:
schema:
fields: 2
- f1-bool: type=bool
- f2-decimal128: type=decimal(1, 1)
您可以在 文档。
还有一些与扩展类型相关的东西。
但我不熟悉扩展类型,因此我无法展示它的示例。但如果你熟悉它,你就可以轻松解决它。










