1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package extensions
import (
"fmt"
"reflect"
"slices"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/internal/json"
"github.com/apache/arrow-go/v18/parquet/schema"
)
var jsonSupportedStorageTypes = []arrow.DataType{
arrow.BinaryTypes.String,
arrow.BinaryTypes.LargeString,
arrow.BinaryTypes.StringView,
}
// JSONType represents a UTF-8 encoded JSON string as specified in RFC8259.
type JSONType struct {
arrow.ExtensionBase
}
// ParquetLogicalType implements pqarrow.ExtensionCustomParquetType.
func (b *JSONType) ParquetLogicalType() schema.LogicalType {
return schema.JSONLogicalType{}
}
// NewJSONType creates a new JSONType with the specified storage type.
// storageType must be one of String, LargeString, StringView.
func NewJSONType(storageType arrow.DataType) (*JSONType, error) {
if !slices.Contains(jsonSupportedStorageTypes, storageType) {
return nil, fmt.Errorf("unsupported storage type for JSON extension type: %s", storageType)
}
return &JSONType{ExtensionBase: arrow.ExtensionBase{Storage: storageType}}, nil
}
func (b *JSONType) ArrayType() reflect.Type { return reflect.TypeOf(JSONArray{}) }
func (b *JSONType) Deserialize(storageType arrow.DataType, data string) (arrow.ExtensionType, error) {
if !(data == "" || data == "{}") {
return nil, fmt.Errorf("serialized metadata for JSON extension type must be '' or '{}', found: %s", data)
}
return NewJSONType(storageType)
}
func (b *JSONType) ExtensionEquals(other arrow.ExtensionType) bool {
return b.ExtensionName() == other.ExtensionName() && arrow.TypeEqual(b.Storage, other.StorageType())
}
func (b *JSONType) ExtensionName() string { return "arrow.json" }
func (b *JSONType) Serialize() string { return "" }
func (b *JSONType) String() string {
return fmt.Sprintf("extension<%s[storage_type=%s]>", b.ExtensionName(), b.Storage)
}
// JSONArray is logically an array of UTF-8 encoded JSON strings.
// Its values are unmarshaled to native Go values.
type JSONArray struct {
array.ExtensionArrayBase
}
func (a *JSONArray) String() string {
b, err := a.MarshalJSON()
if err != nil {
panic(fmt.Sprintf("failed marshal JSONArray: %s", err))
}
return string(b)
}
func (a *JSONArray) Value(i int) any {
val := a.ValueBytes(i)
var res any
if err := json.Unmarshal(val, &res); err != nil {
panic(err)
}
return res
}
func (a *JSONArray) ValueStr(i int) string {
return string(a.ValueBytes(i))
}
func (a *JSONArray) ValueBytes(i int) []byte {
// convert to json.RawMessage, set to nil if elem isNull.
val := a.ValueJSON(i)
// simply returns wrapped bytes, or null if val is nil.
b, err := val.MarshalJSON()
if err != nil {
panic(err)
}
return b
}
// ValueJSON wraps the underlying string value as a json.RawMessage,
// or returns nil if the array value is null.
func (a *JSONArray) ValueJSON(i int) json.RawMessage {
var val json.RawMessage
if a.IsValid(i) {
val = json.RawMessage(a.Storage().(array.StringLike).Value(i))
}
return val
}
// MarshalJSON implements json.Marshaler.
// Marshaling json.RawMessage is a no-op, except that nil values will
// be marshaled as a JSON null.
func (a *JSONArray) MarshalJSON() ([]byte, error) {
values := make([]json.RawMessage, a.Len())
for i := 0; i < a.Len(); i++ {
values[i] = a.ValueJSON(i)
}
return json.Marshal(values)
}
// GetOneForMarshal implements arrow.Array.
func (a *JSONArray) GetOneForMarshal(i int) interface{} {
return a.ValueJSON(i)
}
var (
_ arrow.ExtensionType = (*JSONType)(nil)
_ array.ExtensionArray = (*JSONArray)(nil)
)
|