From 52ae3bbf94caf1d5a3408de599ea973cc7ad0ad6 Mon Sep 17 00:00:00 2001
From: David Li
Date: Tue, 26 Jul 2022 10:04:48 -0400
Subject: [PATCH] Fix bug with zero-length values in varbinary vectors
---
.../adapter/jdbc/binder/VarBinaryBinder.java | 2 +-
.../adapter/jdbc/binder/VarCharBinder.java | 2 +-
.../adapter/jdbc/JdbcParameterBinderTest.java | 27 ++++++++++++++++---
3 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java
index 86948f6d2ecde..a94cff6a00496 100644
--- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java
+++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarBinaryBinder.java
@@ -56,7 +56,7 @@ public void bind(PreparedStatement statement, int parameterIndex, int rowIndex)
throw new RuntimeException(message);
}
byte[] binaryData = new byte[(int) element.getLength()];
- element.getBuf().getBytes(0, binaryData);
+ element.getBuf().getBytes(element.getOffset(), binaryData);
statement.setBytes(parameterIndex, binaryData);
}
}
diff --git a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java
index 818d3dcba7f99..73bd55981490b 100644
--- a/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java
+++ b/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/binder/VarCharBinder.java
@@ -57,7 +57,7 @@ public void bind(PreparedStatement statement, int parameterIndex, int rowIndex)
throw new RuntimeException(message);
}
byte[] utf8Bytes = new byte[(int) element.getLength()];
- element.getBuf().getBytes(0, utf8Bytes);
+ element.getBuf().getBytes(element.getOffset(), utf8Bytes);
statement.setString(parameterIndex, new String(utf8Bytes, StandardCharsets.UTF_8));
}
}
diff --git a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java
index 29b9148bbfd66..c8c043f2f0bcc 100644
--- a/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java
+++ b/java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/JdbcParameterBinderTest.java
@@ -422,13 +422,22 @@ void testSimpleType(ArrowType arrowType, int jdbcType
setNull.accept(vector, 0);
setValue.accept(vector, 1, values.get(2));
- root.setRowCount(2);
+ setValue.accept(vector, 2, values.get(0));
+ setValue.accept(vector, 3, values.get(2));
+ setValue.accept(vector, 4, values.get(1));
+ root.setRowCount(5);
assertThat(binder.next()).isTrue();
assertThat(statement.getParamValue(1)).isNull();
assertThat(statement.getParamType(1)).isEqualTo(jdbcType);
assertThat(binder.next()).isTrue();
assertThat(statement.getParamValue(1)).isEqualTo(values.get(2));
+ assertThat(binder.next()).isTrue();
+ assertThat(statement.getParamValue(1)).isEqualTo(values.get(0));
+ assertThat(binder.next()).isTrue();
+ assertThat(statement.getParamValue(1)).isEqualTo(values.get(2));
+ assertThat(binder.next()).isTrue();
+ assertThat(statement.getParamValue(1)).isEqualTo(values.get(1));
assertThat(binder.next()).isFalse();
}
@@ -454,11 +463,23 @@ void testSimpleType(ArrowType arrowType, int jdbcType
binder.reset();
- setValue.accept(vector, 0, values.get(2));
- root.setRowCount(1);
+ setValue.accept(vector, 0, values.get(0));
+ setValue.accept(vector, 1, values.get(2));
+ setValue.accept(vector, 2, values.get(0));
+ setValue.accept(vector, 3, values.get(2));
+ setValue.accept(vector, 4, values.get(1));
+ root.setRowCount(5);
+ assertThat(binder.next()).isTrue();
+ assertThat(statement.getParamValue(1)).isEqualTo(values.get(0));
+ assertThat(binder.next()).isTrue();
+ assertThat(statement.getParamValue(1)).isEqualTo(values.get(2));
+ assertThat(binder.next()).isTrue();
+ assertThat(statement.getParamValue(1)).isEqualTo(values.get(0));
assertThat(binder.next()).isTrue();
assertThat(statement.getParamValue(1)).isEqualTo(values.get(2));
+ assertThat(binder.next()).isTrue();
+ assertThat(statement.getParamValue(1)).isEqualTo(values.get(1));
assertThat(binder.next()).isFalse();
}
}