// 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. import { Data } from '../data.js'; import { Vector } from '../vector.js'; import { Visitor } from '../visitor.js'; import { Schema, Field } from '../schema.js'; import { DataType, TypeMap, Dictionary, Bool, Null, Utf8, LargeUtf8, Binary, LargeBinary, Decimal, FixedSizeBinary, List, FixedSizeList, Map_, Struct, Float, Float16, Float32, Float64, Int, Uint8, Uint16, Uint32, Uint64, Int8, Int16, Int32, Int64, Date_, DateDay, DateMillisecond, Interval, IntervalDayTime, IntervalYearMonth, Time, TimeSecond, TimeMillisecond, TimeMicrosecond, TimeNanosecond, Timestamp, TimestampSecond, TimestampMillisecond, TimestampMicrosecond, TimestampNanosecond, Duration, DurationSecond, DurationMillisecond, DurationMicrosecond, DurationNanosecond, Union, DenseUnion, SparseUnion, IntervalMonthDayNano, } from '../type.js'; /** @ignore */ export interface TypeComparator extends Visitor { visit(type: T, other?: DataType | null): other is T; visitMany(nodes: T[], others?: DataType[] | null): boolean[]; getVisitFn(node: Vector | Data | T): (other?: DataType | null) => other is T; visitNull(type: T, other?: DataType | null): other is T; visitBool(type: T, other?: DataType | null): other is T; visitInt(type: T, other?: DataType | null): other is T; visitInt8(type: T, other?: DataType | null): other is T; visitInt16(type: T, other?: DataType | null): other is T; visitInt32(type: T, other?: DataType | null): other is T; visitInt64(type: T, other?: DataType | null): other is T; visitUint8(type: T, other?: DataType | null): other is T; visitUint16(type: T, other?: DataType | null): other is T; visitUint32(type: T, other?: DataType | null): other is T; visitUint64(type: T, other?: DataType | null): other is T; visitFloat(type: T, other?: DataType | null): other is T; visitFloat16(type: T, other?: DataType | null): other is T; visitFloat32(type: T, other?: DataType | null): other is T; visitFloat64(type: T, other?: DataType | null): other is T; visitUtf8(type: T, other?: DataType | null): other is T; visitLargeUtf8(type: T, other?: DataType | null): other is T; visitBinary(type: T, other?: DataType | null): other is T; visitLargeBinary(type: T, other?: DataType | null): other is T; visitFixedSizeBinary(type: T, other?: DataType | null): other is T; visitDate(type: T, other?: DataType | null): other is T; visitDateDay(type: T, other?: DataType | null): other is T; visitDateMillisecond(type: T, other?: DataType | null): other is T; visitTimestamp(type: T, other?: DataType | null): other is T; visitTimestampSecond(type: T, other?: DataType | null): other is T; visitTimestampMillisecond(type: T, other?: DataType | null): other is T; visitTimestampMicrosecond(type: T, other?: DataType | null): other is T; visitTimestampNanosecond(type: T, other?: DataType | null): other is T; visitTime(type: T, other?: DataType | null): other is T; visitTimeSecond(type: T, other?: DataType | null): other is T; visitTimeMillisecond(type: T, other?: DataType | null): other is T; visitTimeMicrosecond(type: T, other?: DataType | null): other is T; visitTimeNanosecond(type: T, other?: DataType | null): other is T; visitDecimal(type: T, other?: DataType | null): other is T; visitList(type: T, other?: DataType | null): other is T; visitStruct(type: T, other?: DataType | null): other is T; visitUnion(type: T, other?: DataType | null): other is T; visitDenseUnion(type: T, other?: DataType | null): other is T; visitSparseUnion(type: T, other?: DataType | null): other is T; visitDictionary(type: T, other?: DataType | null): other is T; visitInterval(type: T, other?: DataType | null): other is T; visitIntervalDayTime(type: T, other?: DataType | null): other is T; visitIntervalYearMonth(type: T, other?: DataType | null): other is T; visitIntervalMonthDayNano(type: T, other?: DataType | null): other is T; visitDuration(type: T, other?: DataType | null): other is T; visitDurationSecond(type: T, other?: DataType | null): other is T; visitDurationMillisecond(type: T, other?: DataType | null): other is T; visitDurationMicrosecond(type: T, other?: DataType | null): other is T; visitDurationNanosecond(type: T, other?: DataType | null): other is T; visitFixedSizeList(type: T, other?: DataType | null): other is T; visitMap(type: T, other?: DataType | null): other is T; } /** @ignore */ export class TypeComparator extends Visitor { compareSchemas(schema: Schema, other?: Schema | null): other is Schema { return (schema === other) || ( other instanceof schema.constructor && this.compareManyFields(schema.fields, other.fields) ); } compareManyFields(fields: Field[], others?: Field[] | null): others is Field[] { return (fields === others) || ( Array.isArray(fields) && Array.isArray(others) && fields.length === others.length && fields.every((f, i) => this.compareFields(f, others[i])) ); } compareFields(field: Field, other?: Field | null): other is Field { return (field === other) || ( other instanceof field.constructor && field.name === other.name && field.nullable === other.nullable && this.visit(field.type, other.type) ); } } function compareConstructor(type: T, other?: DataType | null): other is T { return other instanceof type.constructor; } function compareAny(type: T, other?: DataType | null): other is T { return (type === other) || compareConstructor(type, other); } function compareInt(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.bitWidth === other.bitWidth && type.isSigned === other.isSigned ); } function compareFloat(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.precision === other.precision ); } function compareFixedSizeBinary(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.byteWidth === other.byteWidth ); } function compareDate(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.unit === other.unit ); } function compareTimestamp(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.unit === other.unit && type.timezone === other.timezone ); } function compareTime(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.unit === other.unit && type.bitWidth === other.bitWidth ); } function compareList(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.children.length === other.children.length && instance.compareManyFields(type.children, other.children) ); } function compareStruct(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.children.length === other.children.length && instance.compareManyFields(type.children, other.children) ); } function compareUnion(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.mode === other.mode && type.typeIds.every((x, i) => x === other.typeIds[i]) && instance.compareManyFields(type.children, other.children) ); } function compareDictionary(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.id === other.id && type.isOrdered === other.isOrdered && instance.visit(type.indices, other.indices) && instance.visit(type.dictionary, other.dictionary) ); } function compareInterval(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.unit === other.unit ); } function compareDuration(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.unit === other.unit ); } function compareFixedSizeList(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.listSize === other.listSize && type.children.length === other.children.length && instance.compareManyFields(type.children, other.children) ); } function compareMap(type: T, other?: DataType | null): other is T { return (type === other) || ( compareConstructor(type, other) && type.keysSorted === other.keysSorted && type.children.length === other.children.length && instance.compareManyFields(type.children, other.children) ); } TypeComparator.prototype.visitNull = compareAny; TypeComparator.prototype.visitBool = compareAny; TypeComparator.prototype.visitInt = compareInt; TypeComparator.prototype.visitInt8 = compareInt; TypeComparator.prototype.visitInt16 = compareInt; TypeComparator.prototype.visitInt32 = compareInt; TypeComparator.prototype.visitInt64 = compareInt; TypeComparator.prototype.visitUint8 = compareInt; TypeComparator.prototype.visitUint16 = compareInt; TypeComparator.prototype.visitUint32 = compareInt; TypeComparator.prototype.visitUint64 = compareInt; TypeComparator.prototype.visitFloat = compareFloat; TypeComparator.prototype.visitFloat16 = compareFloat; TypeComparator.prototype.visitFloat32 = compareFloat; TypeComparator.prototype.visitFloat64 = compareFloat; TypeComparator.prototype.visitUtf8 = compareAny; TypeComparator.prototype.visitLargeUtf8 = compareAny; TypeComparator.prototype.visitBinary = compareAny; TypeComparator.prototype.visitLargeBinary = compareAny; TypeComparator.prototype.visitFixedSizeBinary = compareFixedSizeBinary; TypeComparator.prototype.visitDate = compareDate; TypeComparator.prototype.visitDateDay = compareDate; TypeComparator.prototype.visitDateMillisecond = compareDate; TypeComparator.prototype.visitTimestamp = compareTimestamp; TypeComparator.prototype.visitTimestampSecond = compareTimestamp; TypeComparator.prototype.visitTimestampMillisecond = compareTimestamp; TypeComparator.prototype.visitTimestampMicrosecond = compareTimestamp; TypeComparator.prototype.visitTimestampNanosecond = compareTimestamp; TypeComparator.prototype.visitTime = compareTime; TypeComparator.prototype.visitTimeSecond = compareTime; TypeComparator.prototype.visitTimeMillisecond = compareTime; TypeComparator.prototype.visitTimeMicrosecond = compareTime; TypeComparator.prototype.visitTimeNanosecond = compareTime; TypeComparator.prototype.visitDecimal = compareAny; TypeComparator.prototype.visitList = compareList; TypeComparator.prototype.visitStruct = compareStruct; TypeComparator.prototype.visitUnion = compareUnion; TypeComparator.prototype.visitDenseUnion = compareUnion; TypeComparator.prototype.visitSparseUnion = compareUnion; TypeComparator.prototype.visitDictionary = compareDictionary; TypeComparator.prototype.visitInterval = compareInterval; TypeComparator.prototype.visitIntervalDayTime = compareInterval; TypeComparator.prototype.visitIntervalYearMonth = compareInterval; TypeComparator.prototype.visitIntervalMonthDayNano = compareInterval; TypeComparator.prototype.visitDuration = compareDuration; TypeComparator.prototype.visitDurationSecond = compareDuration; TypeComparator.prototype.visitDurationMillisecond = compareDuration; TypeComparator.prototype.visitDurationMicrosecond = compareDuration; TypeComparator.prototype.visitDurationNanosecond = compareDuration; TypeComparator.prototype.visitFixedSizeList = compareFixedSizeList; TypeComparator.prototype.visitMap = compareMap; /** @ignore */ export const instance = new TypeComparator(); export function compareSchemas(schema: Schema, other?: Schema | null): other is Schema { return instance.compareSchemas(schema, other); } export function compareFields(field: Field, other?: Field | null): other is Field { return instance.compareFields(field, other); } export function compareTypes(type: A, other?: DataType): other is A { return instance.visit(type, other); }