]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership. The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | import { Data } from './data'; | |
19 | import { Vector } from './vector'; | |
20 | import { Type, Precision, DateUnit, TimeUnit, IntervalUnit, UnionMode } from './enum'; | |
21 | import { DataType, Float, Int, Date_, Interval, Time, Timestamp, Union, } from './type'; | |
22 | ||
23 | export abstract class Visitor { | |
24 | public visitMany(nodes: any[], ...args: any[][]) { | |
25 | return nodes.map((node, i) => this.visit(node, ...args.map((x) => x[i]))); | |
26 | } | |
27 | public visit(...args: any[]) { | |
28 | return this.getVisitFn(args[0], false).apply(this, args); | |
29 | } | |
30 | public getVisitFn(node: any, throwIfNotFound = true) { | |
31 | return getVisitFn(this, node, throwIfNotFound); | |
32 | } | |
33 | public visitNull (_node: any, ..._args: any[]): any { return null; } | |
34 | public visitBool (_node: any, ..._args: any[]): any { return null; } | |
35 | public visitInt (_node: any, ..._args: any[]): any { return null; } | |
36 | public visitFloat (_node: any, ..._args: any[]): any { return null; } | |
37 | public visitUtf8 (_node: any, ..._args: any[]): any { return null; } | |
38 | public visitBinary (_node: any, ..._args: any[]): any { return null; } | |
39 | public visitFixedSizeBinary (_node: any, ..._args: any[]): any { return null; } | |
40 | public visitDate (_node: any, ..._args: any[]): any { return null; } | |
41 | public visitTimestamp (_node: any, ..._args: any[]): any { return null; } | |
42 | public visitTime (_node: any, ..._args: any[]): any { return null; } | |
43 | public visitDecimal (_node: any, ..._args: any[]): any { return null; } | |
44 | public visitList (_node: any, ..._args: any[]): any { return null; } | |
45 | public visitStruct (_node: any, ..._args: any[]): any { return null; } | |
46 | public visitUnion (_node: any, ..._args: any[]): any { return null; } | |
47 | public visitDictionary (_node: any, ..._args: any[]): any { return null; } | |
48 | public visitInterval (_node: any, ..._args: any[]): any { return null; } | |
49 | public visitFixedSizeList (_node: any, ..._args: any[]): any { return null; } | |
50 | public visitMap (_node: any, ..._args: any[]): any { return null; } | |
51 | } | |
52 | ||
53 | /** @ignore */ | |
54 | function getVisitFn<T extends DataType>(visitor: Visitor, node: any, throwIfNotFound = true) { | |
55 | let fn: any = null; | |
56 | let dtype: T['TType'] = Type.NONE; | |
57 | if (node instanceof Data ) dtype = inferDType(node.type as T); | |
58 | else if (node instanceof Vector ) dtype = inferDType(node.type as T); | |
59 | else if (node instanceof DataType) dtype = inferDType(node as T); | |
60 | else if (typeof (dtype = node) !== 'number') dtype = Type[node] as any as T['TType']; | |
61 | ||
62 | switch (dtype) { | |
63 | case Type.Null: fn = visitor.visitNull; break; | |
64 | case Type.Bool: fn = visitor.visitBool; break; | |
65 | case Type.Int: fn = visitor.visitInt; break; | |
66 | case Type.Int8: fn = visitor.visitInt8 || visitor.visitInt; break; | |
67 | case Type.Int16: fn = visitor.visitInt16 || visitor.visitInt; break; | |
68 | case Type.Int32: fn = visitor.visitInt32 || visitor.visitInt; break; | |
69 | case Type.Int64: fn = visitor.visitInt64 || visitor.visitInt; break; | |
70 | case Type.Uint8: fn = visitor.visitUint8 || visitor.visitInt; break; | |
71 | case Type.Uint16: fn = visitor.visitUint16 || visitor.visitInt; break; | |
72 | case Type.Uint32: fn = visitor.visitUint32 || visitor.visitInt; break; | |
73 | case Type.Uint64: fn = visitor.visitUint64 || visitor.visitInt; break; | |
74 | case Type.Float: fn = visitor.visitFloat; break; | |
75 | case Type.Float16: fn = visitor.visitFloat16 || visitor.visitFloat; break; | |
76 | case Type.Float32: fn = visitor.visitFloat32 || visitor.visitFloat; break; | |
77 | case Type.Float64: fn = visitor.visitFloat64 || visitor.visitFloat; break; | |
78 | case Type.Utf8: fn = visitor.visitUtf8; break; | |
79 | case Type.Binary: fn = visitor.visitBinary; break; | |
80 | case Type.FixedSizeBinary: fn = visitor.visitFixedSizeBinary; break; | |
81 | case Type.Date: fn = visitor.visitDate; break; | |
82 | case Type.DateDay: fn = visitor.visitDateDay || visitor.visitDate; break; | |
83 | case Type.DateMillisecond: fn = visitor.visitDateMillisecond || visitor.visitDate; break; | |
84 | case Type.Timestamp: fn = visitor.visitTimestamp; break; | |
85 | case Type.TimestampSecond: fn = visitor.visitTimestampSecond || visitor.visitTimestamp; break; | |
86 | case Type.TimestampMillisecond: fn = visitor.visitTimestampMillisecond || visitor.visitTimestamp; break; | |
87 | case Type.TimestampMicrosecond: fn = visitor.visitTimestampMicrosecond || visitor.visitTimestamp; break; | |
88 | case Type.TimestampNanosecond: fn = visitor.visitTimestampNanosecond || visitor.visitTimestamp; break; | |
89 | case Type.Time: fn = visitor.visitTime; break; | |
90 | case Type.TimeSecond: fn = visitor.visitTimeSecond || visitor.visitTime; break; | |
91 | case Type.TimeMillisecond: fn = visitor.visitTimeMillisecond || visitor.visitTime; break; | |
92 | case Type.TimeMicrosecond: fn = visitor.visitTimeMicrosecond || visitor.visitTime; break; | |
93 | case Type.TimeNanosecond: fn = visitor.visitTimeNanosecond || visitor.visitTime; break; | |
94 | case Type.Decimal: fn = visitor.visitDecimal; break; | |
95 | case Type.List: fn = visitor.visitList; break; | |
96 | case Type.Struct: fn = visitor.visitStruct; break; | |
97 | case Type.Union: fn = visitor.visitUnion; break; | |
98 | case Type.DenseUnion: fn = visitor.visitDenseUnion || visitor.visitUnion; break; | |
99 | case Type.SparseUnion: fn = visitor.visitSparseUnion || visitor.visitUnion; break; | |
100 | case Type.Dictionary: fn = visitor.visitDictionary; break; | |
101 | case Type.Interval: fn = visitor.visitInterval; break; | |
102 | case Type.IntervalDayTime: fn = visitor.visitIntervalDayTime || visitor.visitInterval; break; | |
103 | case Type.IntervalYearMonth: fn = visitor.visitIntervalYearMonth || visitor.visitInterval; break; | |
104 | case Type.FixedSizeList: fn = visitor.visitFixedSizeList; break; | |
105 | case Type.Map: fn = visitor.visitMap; break; | |
106 | } | |
107 | if (typeof fn === 'function') return fn; | |
108 | if (!throwIfNotFound) return () => null; | |
109 | throw new Error(`Unrecognized type '${Type[dtype]}'`); | |
110 | } | |
111 | ||
112 | /** @ignore */ | |
113 | function inferDType<T extends DataType>(type: T): Type { | |
114 | switch (type.typeId) { | |
115 | case Type.Null: return Type.Null; | |
116 | case Type.Int: { | |
117 | const { bitWidth, isSigned } = (type as any as Int); | |
118 | switch (bitWidth) { | |
119 | case 8: return isSigned ? Type.Int8 : Type.Uint8 ; | |
120 | case 16: return isSigned ? Type.Int16 : Type.Uint16; | |
121 | case 32: return isSigned ? Type.Int32 : Type.Uint32; | |
122 | case 64: return isSigned ? Type.Int64 : Type.Uint64; | |
123 | } | |
124 | // @ts-ignore | |
125 | return Type.Int; | |
126 | } | |
127 | case Type.Float: | |
128 | switch((type as any as Float).precision) { | |
129 | case Precision.HALF: return Type.Float16; | |
130 | case Precision.SINGLE: return Type.Float32; | |
131 | case Precision.DOUBLE: return Type.Float64; | |
132 | } | |
133 | // @ts-ignore | |
134 | return Type.Float; | |
135 | case Type.Binary: return Type.Binary; | |
136 | case Type.Utf8: return Type.Utf8; | |
137 | case Type.Bool: return Type.Bool; | |
138 | case Type.Decimal: return Type.Decimal; | |
139 | case Type.Time: | |
140 | switch ((type as any as Time).unit) { | |
141 | case TimeUnit.SECOND: return Type.TimeSecond; | |
142 | case TimeUnit.MILLISECOND: return Type.TimeMillisecond; | |
143 | case TimeUnit.MICROSECOND: return Type.TimeMicrosecond; | |
144 | case TimeUnit.NANOSECOND: return Type.TimeNanosecond; | |
145 | } | |
146 | // @ts-ignore | |
147 | return Type.Time; | |
148 | case Type.Timestamp: | |
149 | switch ((type as any as Timestamp).unit) { | |
150 | case TimeUnit.SECOND: return Type.TimestampSecond; | |
151 | case TimeUnit.MILLISECOND: return Type.TimestampMillisecond; | |
152 | case TimeUnit.MICROSECOND: return Type.TimestampMicrosecond; | |
153 | case TimeUnit.NANOSECOND: return Type.TimestampNanosecond; | |
154 | } | |
155 | // @ts-ignore | |
156 | return Type.Timestamp; | |
157 | case Type.Date: | |
158 | switch ((type as any as Date_).unit) { | |
159 | case DateUnit.DAY: return Type.DateDay; | |
160 | case DateUnit.MILLISECOND: return Type.DateMillisecond; | |
161 | } | |
162 | // @ts-ignore | |
163 | return Type.Date; | |
164 | case Type.Interval: | |
165 | switch ((type as any as Interval).unit) { | |
166 | case IntervalUnit.DAY_TIME: return Type.IntervalDayTime; | |
167 | case IntervalUnit.YEAR_MONTH: return Type.IntervalYearMonth; | |
168 | } | |
169 | // @ts-ignore | |
170 | return Type.Interval; | |
171 | case Type.Map: return Type.Map; | |
172 | case Type.List: return Type.List; | |
173 | case Type.Struct: return Type.Struct; | |
174 | case Type.Union: | |
175 | switch ((type as any as Union).mode) { | |
176 | case UnionMode.Dense: return Type.DenseUnion; | |
177 | case UnionMode.Sparse: return Type.SparseUnion; | |
178 | } | |
179 | // @ts-ignore | |
180 | return Type.Union; | |
181 | case Type.FixedSizeBinary: return Type.FixedSizeBinary; | |
182 | case Type.FixedSizeList: return Type.FixedSizeList; | |
183 | case Type.Dictionary: return Type.Dictionary; | |
184 | } | |
185 | throw new Error(`Unrecognized type '${Type[type.typeId]}'`); | |
186 | } | |
187 | ||
188 | export interface Visitor { | |
189 | visitNull (node: any, ...args: any[]): any; | |
190 | visitBool (node: any, ...args: any[]): any; | |
191 | visitInt (node: any, ...args: any[]): any; | |
192 | visitInt8? (node: any, ...args: any[]): any; | |
193 | visitInt16? (node: any, ...args: any[]): any; | |
194 | visitInt32? (node: any, ...args: any[]): any; | |
195 | visitInt64? (node: any, ...args: any[]): any; | |
196 | visitUint8? (node: any, ...args: any[]): any; | |
197 | visitUint16? (node: any, ...args: any[]): any; | |
198 | visitUint32? (node: any, ...args: any[]): any; | |
199 | visitUint64? (node: any, ...args: any[]): any; | |
200 | visitFloat (node: any, ...args: any[]): any; | |
201 | visitFloat16? (node: any, ...args: any[]): any; | |
202 | visitFloat32? (node: any, ...args: any[]): any; | |
203 | visitFloat64? (node: any, ...args: any[]): any; | |
204 | visitUtf8 (node: any, ...args: any[]): any; | |
205 | visitBinary (node: any, ...args: any[]): any; | |
206 | visitFixedSizeBinary (node: any, ...args: any[]): any; | |
207 | visitDate (node: any, ...args: any[]): any; | |
208 | visitDateDay? (node: any, ...args: any[]): any; | |
209 | visitDateMillisecond? (node: any, ...args: any[]): any; | |
210 | visitTimestamp (node: any, ...args: any[]): any; | |
211 | visitTimestampSecond? (node: any, ...args: any[]): any; | |
212 | visitTimestampMillisecond? (node: any, ...args: any[]): any; | |
213 | visitTimestampMicrosecond? (node: any, ...args: any[]): any; | |
214 | visitTimestampNanosecond? (node: any, ...args: any[]): any; | |
215 | visitTime (node: any, ...args: any[]): any; | |
216 | visitTimeSecond? (node: any, ...args: any[]): any; | |
217 | visitTimeMillisecond? (node: any, ...args: any[]): any; | |
218 | visitTimeMicrosecond? (node: any, ...args: any[]): any; | |
219 | visitTimeNanosecond? (node: any, ...args: any[]): any; | |
220 | visitDecimal (node: any, ...args: any[]): any; | |
221 | visitList (node: any, ...args: any[]): any; | |
222 | visitStruct (node: any, ...args: any[]): any; | |
223 | visitUnion (node: any, ...args: any[]): any; | |
224 | visitDenseUnion? (node: any, ...args: any[]): any; | |
225 | visitSparseUnion? (node: any, ...args: any[]): any; | |
226 | visitDictionary (node: any, ...args: any[]): any; | |
227 | visitInterval (node: any, ...args: any[]): any; | |
228 | visitIntervalDayTime? (node: any, ...args: any[]): any; | |
229 | visitIntervalYearMonth? (node: any, ...args: any[]): any; | |
230 | visitFixedSizeList (node: any, ...args: any[]): any; | |
231 | visitMap (node: any, ...args: any[]): any; | |
232 | } | |
233 | ||
234 | // Add these here so they're picked up by the externs creator | |
235 | // in the build, and closure-compiler doesn't minify them away | |
236 | (Visitor.prototype as any).visitInt8 = null; | |
237 | (Visitor.prototype as any).visitInt16 = null; | |
238 | (Visitor.prototype as any).visitInt32 = null; | |
239 | (Visitor.prototype as any).visitInt64 = null; | |
240 | (Visitor.prototype as any).visitUint8 = null; | |
241 | (Visitor.prototype as any).visitUint16 = null; | |
242 | (Visitor.prototype as any).visitUint32 = null; | |
243 | (Visitor.prototype as any).visitUint64 = null; | |
244 | (Visitor.prototype as any).visitFloat16 = null; | |
245 | (Visitor.prototype as any).visitFloat32 = null; | |
246 | (Visitor.prototype as any).visitFloat64 = null; | |
247 | (Visitor.prototype as any).visitDateDay = null; | |
248 | (Visitor.prototype as any).visitDateMillisecond = null; | |
249 | (Visitor.prototype as any).visitTimestampSecond = null; | |
250 | (Visitor.prototype as any).visitTimestampMillisecond = null; | |
251 | (Visitor.prototype as any).visitTimestampMicrosecond = null; | |
252 | (Visitor.prototype as any).visitTimestampNanosecond = null; | |
253 | (Visitor.prototype as any).visitTimeSecond = null; | |
254 | (Visitor.prototype as any).visitTimeMillisecond = null; | |
255 | (Visitor.prototype as any).visitTimeMicrosecond = null; | |
256 | (Visitor.prototype as any).visitTimeNanosecond = null; | |
257 | (Visitor.prototype as any).visitDenseUnion = null; | |
258 | (Visitor.prototype as any).visitSparseUnion = null; | |
259 | (Visitor.prototype as any).visitIntervalDayTime = null; | |
260 | (Visitor.prototype as any).visitIntervalYearMonth = null; |