]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/js/src/visitor/bytewidth.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / visitor / bytewidth.ts
CommitLineData
1d09f67e
TL
1/* istanbul ignore file */
2
3// Licensed to the Apache Software Foundation (ASF) under one
4// or more contributor license agreements. See the NOTICE file
5// distributed with this work for additional information
6// regarding copyright ownership. The ASF licenses this file
7// to you under the Apache License, Version 2.0 (the
8// "License"); you may not use this file except in compliance
9// with the License. You may obtain a copy of the License at
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
13// Unless required by applicable law or agreed to in writing,
14// software distributed under the License is distributed on an
15// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16// KIND, either express or implied. See the License for the
17// specific language governing permissions and limitations
18// under the License.
19
20import { Data } from '../data';
21import { Visitor } from '../visitor';
22import { VectorType } from '../interfaces';
23import { Type, TimeUnit } from '../enum';
24import { Schema, Field } from '../schema';
25import {
26 DataType, Dictionary,
27 Float, Int, Date_, Interval, Time, Timestamp,
28 Bool, Null, Utf8, Binary, Decimal, FixedSizeBinary,
29 List, FixedSizeList, Map_, Struct, Union,
30} from '../type';
31
32/** @ignore */ const sum = (x: number, y: number) => x + y;
33/** @ignore */ const variableWidthColumnErrorMessage = (type: DataType) => `Cannot compute the byte width of variable-width column ${type}`;
34
35/** @ignore */
36export interface ByteWidthVisitor extends Visitor {
37 visit<T extends DataType>(node: T): number;
38 visitMany<T extends DataType>(nodes: T[]): number[];
39 getVisitFn<T extends Type> (node: T): (type: DataType<T>) => number;
40 getVisitFn<T extends DataType>(node: VectorType<T> | Data<T> | T): (type: T) => number;
41}
42
43/** @ignore */
44export class ByteWidthVisitor extends Visitor {
45 public visitNull (____: Null ) { return 0; }
46 public visitInt (type: Int ) { return type.bitWidth / 8; }
47 public visitFloat (type: Float ) { return type.ArrayType.BYTES_PER_ELEMENT; }
48 public visitBinary (type: Binary ) { throw new Error(variableWidthColumnErrorMessage(type)); }
49 public visitUtf8 (type: Utf8 ) { throw new Error(variableWidthColumnErrorMessage(type)); }
50 public visitBool (____: Bool ) { return 1 / 8; }
51 public visitDecimal (____: Decimal ) { return 16; }
52 public visitDate (type: Date_ ) { return (type.unit + 1) * 4; }
53 public visitTime (type: Time ) { return type.bitWidth / 8; }
54 public visitTimestamp (type: Timestamp ) { return type.unit === TimeUnit.SECOND ? 4 : 8; }
55 public visitInterval (type: Interval ) { return (type.unit + 1) * 4; }
56 public visitList (type: List ) { throw new Error(variableWidthColumnErrorMessage(type)); }
57 public visitStruct (type: Struct ) { return this.visitFields(type.children).reduce(sum, 0); }
58 public visitUnion (type: Union ) { return this.visitFields(type.children).reduce(sum, 0); }
59 public visitFixedSizeBinary (type: FixedSizeBinary ) { return type.byteWidth; }
60 public visitFixedSizeList (type: FixedSizeList ) { return type.listSize * this.visitFields(type.children).reduce(sum, 0); }
61 public visitMap (type: Map_ ) { return this.visitFields(type.children).reduce(sum, 0); }
62 public visitDictionary (type: Dictionary ) { return this.visit(type.indices); }
63 public visitFields (fields: Field[] ) { return (fields || []).map((field) => this.visit(field.type)); }
64 public visitSchema (schema: Schema ) { return this.visitFields(schema.fields).reduce(sum, 0); }
65}
66
67/** @ignore */
68export const instance = new ByteWidthVisitor();