-
Notifications
You must be signed in to change notification settings - Fork 58
GH-759: Get length of byte[] in TryCopyLastError #760
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
Open
hnwyllmm
wants to merge
8
commits into
apache:main
Choose a base branch
from
hnwyllmm:fix-copyerror
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c3ad74d
fix get length of byte[]
hnwyllmm a4614c5
add a unittest
hnwyllmm 8d2c4d5
test copy-error
hnwyllmm 4f158ee
test for copyerror
hnwyllmm ee860b1
remove unused file
hnwyllmm 88ed36b
shorten exception message
hnwyllmm 3f40711
assert throws
hnwyllmm cd9cd5b
simplify message test
hnwyllmm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,157 @@ | ||
/* | ||
* 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.c; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchThrowableOfType; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import org.apache.arrow.c.jni.CDataJniException; | ||
import org.apache.arrow.memory.BufferAllocator; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.apache.arrow.vector.IntVector; | ||
import org.apache.arrow.vector.VectorLoader; | ||
import org.apache.arrow.vector.VectorSchemaRoot; | ||
import org.apache.arrow.vector.VectorUnloader; | ||
import org.apache.arrow.vector.dictionary.Dictionary; | ||
import org.apache.arrow.vector.dictionary.DictionaryProvider; | ||
import org.apache.arrow.vector.ipc.ArrowReader; | ||
import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; | ||
import org.apache.arrow.vector.types.pojo.ArrowType; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.junit.jupiter.api.Test; | ||
|
||
// Regression test for https://github.com/apache/arrow-java/issues/759 | ||
final class ExceptionTest { | ||
@Test | ||
public void testException() throws IOException { | ||
final Schema schema = | ||
new Schema(Collections.singletonList(Field.nullable("ints", new ArrowType.Int(32, true)))); | ||
final List<Object> batches = new ArrayList<>(); | ||
|
||
try (BufferAllocator allocator = new RootAllocator(); | ||
VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { | ||
|
||
final String exceptionMessage = "This is a message for testing exception."; | ||
|
||
RuntimeException exToThrow = new RuntimeException(exceptionMessage); | ||
batches.add(exToThrow); | ||
|
||
StringWriter sw = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(sw); | ||
exToThrow.printStackTrace(pw); | ||
final String expectExceptionMessage = sw.toString(); | ||
|
||
ArrowReader source = new ExceptionMemoryArrowReader(allocator, schema, batches); | ||
|
||
try (final ArrowArrayStream stream = ArrowArrayStream.allocateNew(allocator); | ||
final VectorSchemaRoot importRoot = VectorSchemaRoot.create(schema, allocator)) { | ||
final VectorLoader loader = new VectorLoader(importRoot); | ||
Data.exportArrayStream(allocator, source, stream); | ||
|
||
try (final ArrowReader reader = Data.importArrayStream(allocator, stream)) { | ||
IOException jniException = catchThrowableOfType(IOException.class, reader::loadNextBatch); | ||
final String jniMessage = jniException.getMessage(); | ||
assertThat(jniMessage.endsWith(expectExceptionMessage + "}")); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to assert that we did indeed get an exception. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DONE |
||
} | ||
|
||
static class ExceptionMemoryArrowReader extends ArrowReader { | ||
private final Schema schema; | ||
private final List<Object> batches; // set ArrowRecordBatch or Exception | ||
private final DictionaryProvider provider; | ||
private int nextBatch; | ||
|
||
ExceptionMemoryArrowReader( | ||
BufferAllocator allocator, | ||
Schema schema, | ||
List<Object> batches) { | ||
super(allocator); | ||
this.schema = schema; | ||
this.batches = batches; | ||
this.provider = new CDataDictionaryProvider(); | ||
this.nextBatch = 0; | ||
} | ||
|
||
@Override | ||
public Dictionary lookup(long id) { | ||
return provider.lookup(id); | ||
} | ||
|
||
@Override | ||
public Set<Long> getDictionaryIds() { | ||
return provider.getDictionaryIds(); | ||
} | ||
|
||
@Override | ||
public Map<Long, Dictionary> getDictionaryVectors() { | ||
return getDictionaryIds().stream() | ||
.collect(Collectors.toMap(Function.identity(), this::lookup)); | ||
} | ||
|
||
@Override | ||
public boolean loadNextBatch() throws IOException { | ||
if (nextBatch < batches.size()) { | ||
Object object = batches.get(nextBatch++); | ||
if (object instanceof RuntimeException) { | ||
throw (RuntimeException) object; | ||
} | ||
VectorLoader loader = new VectorLoader(getVectorSchemaRoot()); | ||
loader.load((ArrowRecordBatch) object); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public long bytesRead() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
protected void closeReadSource() throws IOException { | ||
try { | ||
for (Object object : batches) { | ||
if (object instanceof ArrowRecordBatch) { | ||
ArrowRecordBatch batch = (ArrowRecordBatch) object; | ||
batch.close(); | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected Schema readSchema() { | ||
return schema; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.