|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "cloud.google.com/go/bigquery" |
| 9 | + "github.com/averak/protobq/internal/protobuf/protobq" |
| 10 | + "github.com/huandu/go-sqlbuilder" |
| 11 | + "google.golang.org/protobuf/proto" |
| 12 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 13 | +) |
| 14 | + |
| 15 | +type MaterializedViewOptions struct { |
| 16 | + BaseTable string |
| 17 | + EnableRefresh bool |
| 18 | + RefreshInterval time.Duration |
| 19 | + |
| 20 | + // TODO: クラスタ化列、パーティション列を定義する。 |
| 21 | +} |
| 22 | + |
| 23 | +type MaterializedView interface { |
| 24 | + proto.Message |
| 25 | + |
| 26 | + Name() string |
| 27 | + Options() MaterializedViewOptions |
| 28 | + InsertDTO() InsertDTO |
| 29 | +} |
| 30 | + |
| 31 | +type InsertDTO interface { |
| 32 | + TableName() string |
| 33 | + Value() map[string]any |
| 34 | +} |
| 35 | + |
| 36 | +type BQSchemaConverter struct { |
| 37 | + datasetID string |
| 38 | + raw MaterializedView |
| 39 | +} |
| 40 | + |
| 41 | +func NewBQSchemaConverter(datasetID string, mv MaterializedView) *BQSchemaConverter { |
| 42 | + return &BQSchemaConverter{ |
| 43 | + datasetID: datasetID, |
| 44 | + raw: mv, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (c BQSchemaConverter) ApplyBaseTableSchema(td *bigquery.TableMetadata) error { |
| 49 | + schema := make(bigquery.Schema, 0, c.raw.ProtoReflect().Descriptor().Fields().Len()) |
| 50 | + for i := range c.raw.ProtoReflect().Descriptor().Fields().Len() { |
| 51 | + field := c.raw.ProtoReflect().Descriptor().Fields().Get(i) |
| 52 | + fs, err := c.toBQFieldSchema(field) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + schema = append(schema, fs) |
| 57 | + } |
| 58 | + |
| 59 | + for _, existing := range td.Schema { |
| 60 | + if existing.Name == c.raw.Name() { |
| 61 | + existing.Schema = schema |
| 62 | + return nil |
| 63 | + } |
| 64 | + } |
| 65 | + td.Schema = append(td.Schema, &bigquery.FieldSchema{ |
| 66 | + Name: c.raw.Name(), |
| 67 | + Type: bigquery.RecordFieldType, |
| 68 | + Required: false, |
| 69 | + Schema: schema, |
| 70 | + }) |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func (c BQSchemaConverter) MaterializedViewSchema() (*bigquery.TableMetadata, error) { |
| 75 | + res := &bigquery.TableMetadata{ |
| 76 | + Name: c.raw.Name(), |
| 77 | + MaterializedView: &bigquery.MaterializedViewDefinition{ |
| 78 | + Query: c.MaterializedViewDDL(), |
| 79 | + EnableRefresh: c.raw.Options().EnableRefresh, |
| 80 | + RefreshInterval: c.raw.Options().RefreshInterval, |
| 81 | + }, |
| 82 | + // TODO: #7 TimePartitioning, Clustering 定義する |
| 83 | + TimePartitioning: nil, |
| 84 | + Clustering: nil, |
| 85 | + } |
| 86 | + return res, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (c BQSchemaConverter) MaterializedViewDDL() string { |
| 90 | + sb := sqlbuilder.NewSelectBuilder() |
| 91 | + |
| 92 | + // top-level のフィールドのみ DDL に含めれば良い。 |
| 93 | + cols := make([]string, 0, c.raw.ProtoReflect().Descriptor().Fields().Len()) |
| 94 | + for i := range c.raw.ProtoReflect().Descriptor().Fields().Len() { |
| 95 | + field := c.raw.ProtoReflect().Descriptor().Fields().Get(i) |
| 96 | + ext, _ := proto.GetExtension(field.Options(), protobq.E_MaterializedViewField).(*protobq.MaterializedViewField) |
| 97 | + if len(ext.GetOriginPath()) > 0 { |
| 98 | + cols = append(cols, fmt.Sprintf("%s.%s", c.raw.Name(), strings.Join(ext.GetOriginPath(), "."))) |
| 99 | + } else { |
| 100 | + cols = append(cols, fmt.Sprintf("%s.%s", c.raw.Name(), field.Name())) |
| 101 | + } |
| 102 | + } |
| 103 | + res, _ := sb.Select(cols...). |
| 104 | + From(fmt.Sprintf("%s.%s", c.datasetID, c.raw.Options().BaseTable)). |
| 105 | + Where(sb.IsNotNull(c.raw.Name())). |
| 106 | + Build() |
| 107 | + return res |
| 108 | +} |
| 109 | + |
| 110 | +func (c BQSchemaConverter) toBQFieldSchema(field protoreflect.FieldDescriptor) (*bigquery.FieldSchema, error) { |
| 111 | + dt, err := c.toBQDataType(field) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + res := &bigquery.FieldSchema{ |
| 117 | + Name: string(field.Name()), |
| 118 | + Type: dt, |
| 119 | + Required: field.Cardinality() == protoreflect.Required, |
| 120 | + Repeated: field.Cardinality() == protoreflect.Repeated, |
| 121 | + Schema: nil, |
| 122 | + } |
| 123 | + if dt == bigquery.RecordFieldType { |
| 124 | + for i := range field.Message().Fields().Len() { |
| 125 | + nested, err := c.toBQFieldSchema(field.Message().Fields().Get(i)) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + res.Schema = append(res.Schema, nested) |
| 130 | + } |
| 131 | + } |
| 132 | + return res, nil |
| 133 | +} |
| 134 | + |
| 135 | +func (c BQSchemaConverter) toBQDataType(field protoreflect.FieldDescriptor) (bigquery.FieldType, error) { |
| 136 | + // https://pkg.go.dev/google.golang.org/protobuf/reflect/protoreflect#Value |
| 137 | + var res bigquery.FieldType |
| 138 | + switch field.Kind() { |
| 139 | + case protoreflect.BoolKind: |
| 140 | + res = bigquery.BooleanFieldType |
| 141 | + case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
| 142 | + res = bigquery.IntegerFieldType |
| 143 | + case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
| 144 | + res = bigquery.IntegerFieldType |
| 145 | + case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
| 146 | + res = bigquery.IntegerFieldType |
| 147 | + case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
| 148 | + res = bigquery.IntegerFieldType |
| 149 | + case protoreflect.FloatKind: |
| 150 | + res = bigquery.FloatFieldType |
| 151 | + case protoreflect.DoubleKind: |
| 152 | + res = bigquery.FloatFieldType |
| 153 | + case protoreflect.StringKind: |
| 154 | + res = bigquery.StringFieldType |
| 155 | + case protoreflect.BytesKind: |
| 156 | + res = bigquery.BytesFieldType |
| 157 | + case protoreflect.EnumKind: |
| 158 | + res = bigquery.StringFieldType |
| 159 | + case protoreflect.MessageKind, protoreflect.GroupKind: |
| 160 | + switch field.Message().FullName() { |
| 161 | + case "google.protobuf.Timestamp": |
| 162 | + res = bigquery.TimestampFieldType |
| 163 | + default: |
| 164 | + res = bigquery.RecordFieldType |
| 165 | + } |
| 166 | + default: |
| 167 | + return "", fmt.Errorf("unsupported field type: %s", field.Kind()) |
| 168 | + } |
| 169 | + return res, nil |
| 170 | +} |
0 commit comments