Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-43118: [JS] Add interval for unit MONTH_DAY_NANO #43117

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
6 changes: 2 additions & 4 deletions dev/archery/archery/integration/datagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,11 +1887,9 @@ def _temp_path():

generate_duration_case(),

generate_interval_case()
.skip_tester('JS'),
generate_interval_case(),

generate_month_day_nano_interval_case()
.skip_tester('JS'),
generate_month_day_nano_interval_case(),

generate_map_case(),

Expand Down
58 changes: 58 additions & 0 deletions js/src/util/interval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.

type EncodedIntervalMonthDayNano = {
months: number;
days: number;
nanoseconds: bigint | number;
};

type EncodedIntervalDayTime = {
days: number;
milliseconds: number;
};

export function encodeIntervalDayTime(intervalArray: EncodedIntervalDayTime[]) {
const length = intervalArray.length;
const data = new Int32Array(length * 2);
const intervalLength = intervalArray.length;
for (let i = 0, ptr = 0; i < intervalLength; i++) {
const interval = intervalArray[i];
data[ptr++] = interval.days ?? 0;
data[ptr++] = interval.milliseconds ?? 0;
}
return data;
}

export function encodeIntervalMonthDayNano(
intervalArray: EncodedIntervalMonthDayNano[]
) {
const length = intervalArray.length;
const data = new Int32Array(length * 4);
const intervalLength = intervalArray.length;
for (let i = 0, ptr = 0; i < intervalLength; i++) {
const interval = intervalArray[i];
const nanoseconds = new Int32Array(
new BigInt64Array([BigInt(interval.nanoseconds ?? 0)]).buffer
);
data[ptr++] = interval.months ?? 0;
data[ptr++] = interval.days ?? 0;
data[ptr++] = nanoseconds[0];
data[ptr++] = nanoseconds[1];
}
return data;
}
10 changes: 9 additions & 1 deletion js/src/visitor/vectorloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import { Visitor } from '../visitor.js';
import { packBools } from '../util/bit.js';
import { encodeUtf8 } from '../util/utf8.js';
import { Int64, Int128 } from '../util/int.js';
import { UnionMode, DateUnit, MetadataVersion } from '../enum.js';
import { UnionMode, DateUnit, MetadataVersion, IntervalUnit } from '../enum.js';
import { toArrayBufferView } from '../util/buffer.js';
import { BufferRegion, FieldNode } from '../ipc/metadata/message.js';
import { encodeIntervalDayTime, encodeIntervalMonthDayNano } from '../util/interval.js';

/** @ignore */
export interface VectorLoader extends Visitor {
Expand Down Expand Up @@ -178,6 +179,13 @@ export class JSONVectorLoader extends VectorLoader {
return packBools(sources[offset] as number[]);
} else if (DataType.isUtf8(type) || DataType.isLargeUtf8(type)) {
return encodeUtf8((sources[offset] as string[]).join(''));
} else if (DataType.isInterval(type)) {
if (type.unit === IntervalUnit.MONTH_DAY_NANO) {
return encodeIntervalMonthDayNano(sources[offset]);
}
if (type.unit === IntervalUnit.DAY_TIME) {
return encodeIntervalDayTime(sources[offset]);
}
trxcllnt marked this conversation as resolved.
Show resolved Hide resolved
}
return toArrayBufferView(Uint8Array, toArrayBufferView(type.ArrayType, sources[offset].map((x) => +x)));
}
Expand Down
Loading