forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DX-100503: new arrow type and vector TimestampWithPrecision
- Loading branch information
Showing
23 changed files
with
665 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/TimestampWithPrecisionBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.arrow.adapter.jdbc.binder; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Timestamp; | ||
import java.sql.Types; | ||
import java.util.Calendar; | ||
import org.apache.arrow.vector.TimeStampVector; | ||
import org.apache.arrow.vector.TimeStampWithPrecisionVector; | ||
import org.apache.arrow.vector.types.pojo.ArrowType; | ||
|
||
/** A column binder for timestamps. */ | ||
public class TimestampWithPrecisionBinder extends BaseColumnBinder<TimeStampVector> { | ||
private final Calendar calendar; | ||
private final long unitsPerSecond; | ||
private final long nanosPerUnit; | ||
|
||
/** Create a binder for a timestamp vector using the default JDBC type code. */ | ||
public TimestampWithPrecisionBinder(TimeStampWithPrecisionVector vector, Calendar calendar) { | ||
this( | ||
vector, | ||
calendar, | ||
isZoned(vector.getField().getType()) ? Types.TIMESTAMP_WITH_TIMEZONE : Types.TIMESTAMP); | ||
} | ||
|
||
/** | ||
* Create a binder for a timestamp vector. | ||
* | ||
* @param vector The vector to pull values from. | ||
* @param calendar Optionally, the calendar to pass to JDBC. | ||
* @param jdbcType The JDBC type code to use for null values. | ||
*/ | ||
public TimestampWithPrecisionBinder( | ||
TimeStampWithPrecisionVector vector, Calendar calendar, int jdbcType) { | ||
super(vector, jdbcType); | ||
this.calendar = calendar; | ||
|
||
final ArrowType.TimestampWithPrecision type = | ||
(ArrowType.TimestampWithPrecision) vector.getField().getType(); | ||
switch (type.getPrecision()) { | ||
case 0: | ||
this.unitsPerSecond = 1; | ||
this.nanosPerUnit = 1_000_000_000; | ||
break; | ||
case 1: | ||
this.unitsPerSecond = 10; | ||
this.nanosPerUnit = 1_000_000_00; | ||
break; | ||
case 2: | ||
this.unitsPerSecond = 100; | ||
this.nanosPerUnit = 1_000_000_0; | ||
break; | ||
case 3: | ||
this.unitsPerSecond = 1_000; | ||
this.nanosPerUnit = 1_000_000; | ||
break; | ||
case 4: | ||
this.unitsPerSecond = 10_000; | ||
this.nanosPerUnit = 1_000_00; | ||
break; | ||
case 5: | ||
this.unitsPerSecond = 100_000; | ||
this.nanosPerUnit = 1_000_0; | ||
break; | ||
case 6: | ||
this.unitsPerSecond = 1_000_000; | ||
this.nanosPerUnit = 1_000; | ||
break; | ||
case 7: | ||
this.unitsPerSecond = 10_000_000; | ||
this.nanosPerUnit = 1_00; | ||
break; | ||
case 8: | ||
this.unitsPerSecond = 100_000_000; | ||
this.nanosPerUnit = 1_0; | ||
break; | ||
case 9: | ||
this.unitsPerSecond = 1_000_000_000; | ||
this.nanosPerUnit = 1; | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Invalid time unit in " + type); | ||
} | ||
} | ||
|
||
@Override | ||
public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) | ||
throws SQLException { | ||
// TODO: option to throw on truncation (vendor Guava IntMath#multiply) or overflow | ||
final long rawValue = | ||
vector.getDataBuffer().getLong((long) rowIndex * TimeStampWithPrecisionVector.TYPE_WIDTH); | ||
final long seconds = rawValue / unitsPerSecond; | ||
final int nanos = (int) ((rawValue - (seconds * unitsPerSecond)) * nanosPerUnit); | ||
final Timestamp value = new Timestamp(seconds * 1_000); | ||
value.setNanos(nanos); | ||
if (calendar != null) { | ||
// Timestamp == Date == UTC timestamp (confusingly). Arrow's timestamp with timezone is a UTC | ||
// value with a | ||
// zone offset, so we don't need to do any conversion. | ||
statement.setTimestamp(parameterIndex, value, calendar); | ||
} else { | ||
// Arrow timestamp without timezone isn't strictly convertible to any timezone. So this is | ||
// technically wrong, | ||
// but there is no 'correct' interpretation here. The application should provide a calendar. | ||
statement.setTimestamp(parameterIndex, value); | ||
} | ||
} | ||
|
||
private static boolean isZoned(ArrowType type) { | ||
final String timezone = ((ArrowType.TimestampWithPrecision) type).getTimezone(); | ||
return timezone != null && !timezone.isEmpty(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
.../apache/arrow/driver/jdbc/converter/impl/TimestampPrecisionAvaticaParameterConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.arrow.driver.jdbc.converter.impl; | ||
|
||
import org.apache.arrow.vector.FieldVector; | ||
import org.apache.arrow.vector.TimeStampWithPrecisionVector; | ||
import org.apache.arrow.vector.types.pojo.ArrowType; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
import org.apache.calcite.avatica.AvaticaParameter; | ||
import org.apache.calcite.avatica.remote.TypedValue; | ||
|
||
/** AvaticaParameterConverter for Timestamp Arrow types. */ | ||
public class TimestampPrecisionAvaticaParameterConverter extends BaseAvaticaParameterConverter { | ||
|
||
public TimestampPrecisionAvaticaParameterConverter(ArrowType.TimestampWithPrecision type) {} | ||
|
||
@Override | ||
public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { | ||
long value = (long) typedValue.toLocal(); | ||
if (vector instanceof TimeStampWithPrecisionVector) { | ||
((TimeStampWithPrecisionVector) vector).setSafe(index, value); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public AvaticaParameter createParameter(Field field) { | ||
return createParameter(field, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
java/format/src/main/java/org/apache/arrow/flatbuf/TimestampWithPrecision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.arrow.flatbuf; | ||
|
||
import com.google.flatbuffers.BaseVector; | ||
import com.google.flatbuffers.Constants; | ||
import com.google.flatbuffers.FlatBufferBuilder; | ||
import com.google.flatbuffers.Table; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
public final class TimestampWithPrecision extends Table { | ||
public static void ValidateVersion() { Constants.FLATBUFFERS_24_3_25(); } | ||
public static TimestampWithPrecision getRootAsTimestampWithPrecision(ByteBuffer _bb) { | ||
return getRootAsTimestampWithPrecision(_bb, new TimestampWithPrecision()); | ||
} | ||
public static TimestampWithPrecision getRootAsTimestampWithPrecision(ByteBuffer _bb, TimestampWithPrecision obj) { | ||
_bb.order(ByteOrder.LITTLE_ENDIAN); | ||
return obj.__init(_bb.getInt(_bb.position()) + _bb.position(), _bb); | ||
} | ||
|
||
public TimestampWithPrecision __init(int _i, ByteBuffer _bb) { | ||
this.bb_pos = _i; | ||
this.bb = _bb; | ||
return this; | ||
} | ||
public TimestampWithPrecision __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } | ||
|
||
public int precision() { | ||
int o = this.__offset(4); | ||
return o != 0 ? this.bb.getInt(o + this.bb_pos) : 0; | ||
} | ||
public String timezone() { | ||
int o = this.__offset(6); | ||
return o != 0 ? this.__string(o + this.bb_pos) : null; | ||
} | ||
public ByteBuffer timezoneAsByteBuffer() { | ||
return this.__vector_as_bytebuffer(6, 1); | ||
} | ||
public ByteBuffer timezoneInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 6, 1); } | ||
|
||
public static int createTimestampWithPrecision(FlatBufferBuilder builder, int precision, int timezone) { | ||
builder.startTable(2); | ||
TimestampWithPrecision.addTimezone(builder, timezone); | ||
TimestampWithPrecision.addPrecision(builder, precision); | ||
return endTimestampWithPrecision(builder); | ||
} | ||
public static void startTimestampWithPrecision(FlatBufferBuilder builder) { | ||
builder.startTable(2); | ||
} | ||
public static void addPrecision(FlatBufferBuilder builder, int precision) { | ||
builder.addInt(0, precision, 0); | ||
} | ||
public static void addTimezone(FlatBufferBuilder builder, int timezoneOffset) { | ||
builder.addOffset(1, timezoneOffset, 0); | ||
} | ||
public static int endTimestampWithPrecision(FlatBufferBuilder builder) { | ||
int o = builder.endTable(); | ||
return o; | ||
} | ||
|
||
public static final class Vector extends BaseVector { | ||
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; } | ||
|
||
public TimestampWithPrecision get(int j) { return get( new TimestampWithPrecision(), j); } | ||
public TimestampWithPrecision get(TimestampWithPrecision obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.