@@ -9,12 +9,7 @@ import {
99 DEFAULT_BUFFER_SIZE ,
1010 DEFAULT_MAX_BUFFER_SIZE ,
1111} from "./index" ;
12- import {
13- isInteger ,
14- timestampToMicros ,
15- timestampToNanos ,
16- TimestampUnit ,
17- } from "../utils" ;
12+ import { isInteger , TimestampUnit } from "../utils" ;
1813
1914// Default maximum length for table and column names.
2015const DEFAULT_MAX_NAME_LENGTH = 127 ;
@@ -269,6 +264,12 @@ abstract class SenderBufferBase implements SenderBuffer {
269264 return this ;
270265 }
271266
267+ protected abstract writeTimestamp (
268+ timestamp : number | bigint ,
269+ unit : TimestampUnit ,
270+ designated : boolean ,
271+ ) : void ;
272+
272273 /**
273274 * Writes a timestamp column with its value into the buffer. <br>
274275 * Use it to insert into TIMESTAMP columns.
@@ -284,15 +285,18 @@ abstract class SenderBufferBase implements SenderBuffer {
284285 unit : TimestampUnit = "us" ,
285286 ) : SenderBuffer {
286287 if ( typeof value !== "bigint" && ! Number . isInteger ( value ) ) {
287- throw new Error ( `Value must be an integer or BigInt, received ${ value } ` ) ;
288+ throw new Error (
289+ `Timestamp value must be an integer or BigInt, received ${ value } ` ,
290+ ) ;
288291 }
289- this . writeColumn ( name , value , ( ) => {
290- const valueMicros = timestampToMicros ( BigInt ( value ) , unit ) ;
291- const valueStr = valueMicros . toString ( ) ;
292- this . checkCapacity ( [ valueStr ] , 1 ) ;
293- this . write ( valueStr ) ;
294- this . write ( "t" ) ;
295- } ) ;
292+ if ( unit == "ns" && typeof value !== "bigint" ) {
293+ throw new Error (
294+ `Timestamp value must be a BigInt if it is set in nanoseconds` ,
295+ ) ;
296+ }
297+ this . writeColumn ( name , value , ( ) =>
298+ this . writeTimestamp ( value , unit , false ) ,
299+ ) ;
296300 return this ;
297301 }
298302
@@ -313,11 +317,14 @@ abstract class SenderBufferBase implements SenderBuffer {
313317 `Designated timestamp must be an integer or BigInt, received ${ timestamp } ` ,
314318 ) ;
315319 }
316- const timestampNanos = timestampToNanos ( BigInt ( timestamp ) , unit ) ;
317- const timestampStr = timestampNanos . toString ( ) ;
318- this . checkCapacity ( [ timestampStr ] , 2 ) ;
320+ if ( unit == "ns" && typeof timestamp !== "bigint" ) {
321+ throw new Error (
322+ `Designated timestamp must be a BigInt if it is set in nanoseconds` ,
323+ ) ;
324+ }
325+ this . checkCapacity ( [ ] , 1 ) ;
319326 this . write ( " " ) ;
320- this . write ( timestampStr ) ;
327+ this . writeTimestamp ( timestamp , unit , true ) ;
321328 this . write ( "\n" ) ;
322329 this . startNewRow ( ) ;
323330 }
0 commit comments