-
Notifications
You must be signed in to change notification settings - Fork 98
GH-853: Allow ISO String Binding for Date, Time, and Timestamp #852
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * 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 static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.time.LocalDate; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.DateDayVector; | ||
| import org.apache.arrow.vector.DateMilliVector; | ||
| import org.apache.arrow.vector.IntVector; | ||
| import org.apache.arrow.vector.types.DateUnit; | ||
| import org.apache.arrow.vector.types.pojo.ArrowType; | ||
| import org.apache.calcite.avatica.ColumnMetaData; | ||
| import org.apache.calcite.avatica.remote.TypedValue; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class DateAvaticaParameterConverterTest { | ||
| private RootAllocator allocator; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| allocator = new RootAllocator(Long.MAX_VALUE); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| allocator.close(); | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithStringDateMilliVector() { | ||
| try (DateMilliVector vector = new DateMilliVector("date", allocator)) { | ||
| DateAvaticaParameterConverter converter = | ||
| new DateAvaticaParameterConverter(new ArrowType.Date(DateUnit.DAY)); | ||
| String dateStr = "2023-09-15"; | ||
| TypedValue typedValue = TypedValue.create(ColumnMetaData.Rep.STRING.toString(), dateStr); | ||
| boolean result = converter.bindParameter(vector, typedValue, 0); | ||
| assertTrue(result); | ||
| assertEquals((int) LocalDate.parse(dateStr).toEpochDay(), vector.get(0)); | ||
|
||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithStringDateDayVector() { | ||
| try (DateDayVector vector = new DateDayVector("date", allocator)) { | ||
| DateAvaticaParameterConverter converter = | ||
| new DateAvaticaParameterConverter(new ArrowType.Date(DateUnit.DAY)); | ||
| String dateStr = "2023-09-15"; | ||
| TypedValue typedValue = TypedValue.create(ColumnMetaData.Rep.STRING.toString(), dateStr); | ||
| boolean result = converter.bindParameter(vector, typedValue, 0); | ||
| assertTrue(result); | ||
| assertEquals((int) LocalDate.parse(dateStr).toEpochDay(), vector.get(0)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithEpochDays() { | ||
| try (DateDayVector vector = new DateDayVector("date", allocator)) { | ||
| DateAvaticaParameterConverter converter = | ||
| new DateAvaticaParameterConverter(new ArrowType.Date(DateUnit.DAY)); | ||
| int days = 19500; | ||
| TypedValue typedValue = TypedValue.create(ColumnMetaData.Rep.INTEGER.toString(), days); | ||
| boolean result = converter.bindParameter(vector, typedValue, 0); | ||
| assertTrue(result); | ||
| assertEquals(days, vector.get(0)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithUnsupportedVector() { | ||
| try (IntVector vector = new IntVector("int", allocator)) { | ||
| DateAvaticaParameterConverter converter = | ||
| new DateAvaticaParameterConverter(new ArrowType.Date(DateUnit.DAY)); | ||
| TypedValue typedValue = TypedValue.create(ColumnMetaData.Rep.STRING.toString(), "2023-09-15"); | ||
| boolean result = converter.bindParameter(vector, typedValue, 0); | ||
| assertFalse(result); | ||
| } | ||
| } | ||
| } | ||
lidavidm marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * 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 static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.TimeMicroVector; | ||
| import org.apache.arrow.vector.TimeMilliVector; | ||
| import org.apache.arrow.vector.TimeNanoVector; | ||
| import org.apache.arrow.vector.TimeSecVector; | ||
| import org.apache.arrow.vector.types.TimeUnit; | ||
| import org.apache.arrow.vector.types.pojo.ArrowType; | ||
| import org.apache.calcite.avatica.ColumnMetaData; | ||
| import org.apache.calcite.avatica.remote.TypedValue; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TimeAvaticaParameterConverterTest { | ||
|
|
||
| @Test | ||
| void testBindParameterWithIsoStringMilli() { | ||
| BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); | ||
| TimeMilliVector vector = new TimeMilliVector("t", allocator); | ||
| vector.allocateNew(1); | ||
| TimeAvaticaParameterConverter converter = | ||
| new TimeAvaticaParameterConverter(new ArrowType.Time(TimeUnit.MILLISECOND, 32)); | ||
| boolean result = | ||
| converter.bindParameter( | ||
| vector, TypedValue.create(ColumnMetaData.Rep.STRING.toString(), "21:39:50"), 0); | ||
| assertTrue(result); | ||
| assertEquals(77990000, vector.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithIsoStringMicro() { | ||
| BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); | ||
| TimeMicroVector vector = new TimeMicroVector("t", allocator); | ||
| vector.allocateNew(1); | ||
| TimeAvaticaParameterConverter converter = | ||
| new TimeAvaticaParameterConverter(new ArrowType.Time(TimeUnit.MICROSECOND, 32)); | ||
| boolean result = | ||
| converter.bindParameter( | ||
| vector, TypedValue.create(ColumnMetaData.Rep.STRING.toString(), "21:39:50.123456"), 0); | ||
| assertTrue(result); | ||
| assertEquals(77990123456L, (long) vector.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithIsoStringNano() { | ||
| BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); | ||
| TimeNanoVector vector = new TimeNanoVector("t", allocator); | ||
| vector.allocateNew(1); | ||
| TimeAvaticaParameterConverter converter = | ||
| new TimeAvaticaParameterConverter(new ArrowType.Time(TimeUnit.NANOSECOND, 64)); | ||
| boolean result = | ||
| converter.bindParameter( | ||
| vector, | ||
| TypedValue.create(ColumnMetaData.Rep.STRING.toString(), "21:39:50.123456789"), | ||
| 0); | ||
| assertTrue(result); | ||
| assertEquals(77990123456789L, (long) vector.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithIsoStringSec() { | ||
| BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); | ||
| TimeSecVector vector = new TimeSecVector("t", allocator); | ||
| vector.allocateNew(1); | ||
| TimeAvaticaParameterConverter converter = | ||
| new TimeAvaticaParameterConverter(new ArrowType.Time(TimeUnit.SECOND, 32)); | ||
| boolean result = | ||
| converter.bindParameter( | ||
| vector, TypedValue.create(ColumnMetaData.Rep.STRING.toString(), "21:39:50"), 0); | ||
| assertTrue(result); | ||
| assertEquals(77990, vector.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithIntValueMilli() { | ||
| BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); | ||
| TimeMilliVector vector = new TimeMilliVector("t", allocator); | ||
| vector.allocateNew(1); | ||
| TimeAvaticaParameterConverter converter = | ||
| new TimeAvaticaParameterConverter(new ArrowType.Time(TimeUnit.MILLISECOND, 32)); | ||
| boolean result = | ||
| converter.bindParameter( | ||
| vector, TypedValue.create(ColumnMetaData.Rep.INTEGER.toString(), 123456), 0); | ||
| assertTrue(result); | ||
| assertEquals(123456, vector.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| void testBindParameterWithIntValueSec() { | ||
| BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); | ||
| TimeSecVector vector = new TimeSecVector("t", allocator); | ||
| vector.allocateNew(1); | ||
| TimeAvaticaParameterConverter converter = | ||
| new TimeAvaticaParameterConverter(new ArrowType.Time(TimeUnit.SECOND, 32)); | ||
| boolean result = | ||
| converter.bindParameter( | ||
| vector, TypedValue.create(ColumnMetaData.Rep.INTEGER.toString(), 42), 0); | ||
| assertTrue(result); | ||
| assertEquals(42, vector.get(0)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, so we only allow timestamps with the
Zsuffix - but we can bind to naive timestamp vectors too. And we don't allow timestamps without the suffix. (We could always relax that later, though.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct. Currently, we only accept timestamps with the Z suffix, but we can easily relax this restriction later to support naive timestamps if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we shouldn't allow binding timestamps with timezones to columns that don't have timezones in principle. Although what happens the Z suffix is probably unambiguous in this case I think if we ever allow other timezones then it becomes a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbonofre or @aiguofer any opinions on the behavior here?