-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arrow batches structured types (#1879)
- Loading branch information
1 parent
d11a12b
commit 8c209d2
Showing
17 changed files
with
921 additions
and
182 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
23 changes: 23 additions & 0 deletions
23
...ava/net/snowflake/client/core/arrow/fullvectorconverters/AbstractFullVectorConverter.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,23 @@ | ||
package net.snowflake.client.core.arrow.fullvectorconverters; | ||
|
||
import net.snowflake.client.core.SFException; | ||
import net.snowflake.client.jdbc.SnowflakeSQLException; | ||
import org.apache.arrow.vector.FieldVector; | ||
|
||
public abstract class AbstractFullVectorConverter implements ArrowFullVectorConverter { | ||
private boolean converted; | ||
|
||
protected abstract FieldVector convertVector() | ||
throws SFException, SnowflakeSQLException, SFArrowException; | ||
|
||
@Override | ||
public FieldVector convert() throws SFException, SnowflakeSQLException, SFArrowException { | ||
if (converted) { | ||
throw new SFArrowException( | ||
ArrowErrorCode.VECTOR_ALREADY_CONVERTED, "Convert has already been called"); | ||
} else { | ||
converted = true; | ||
return convertVector(); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/net/snowflake/client/core/arrow/fullvectorconverters/ArrowErrorCode.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,7 @@ | ||
package net.snowflake.client.core.arrow.fullvectorconverters; | ||
|
||
public enum ArrowErrorCode { | ||
VECTOR_ALREADY_CONVERTED, | ||
CONVERT_FAILED, | ||
CHUNK_FETCH_FAILED, | ||
} |
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
70 changes: 70 additions & 0 deletions
70
...va/net/snowflake/client/core/arrow/fullvectorconverters/FixedSizeListVectorConverter.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,70 @@ | ||
package net.snowflake.client.core.arrow.fullvectorconverters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.TimeZone; | ||
import net.snowflake.client.core.DataConversionContext; | ||
import net.snowflake.client.core.SFBaseSession; | ||
import net.snowflake.client.core.SFException; | ||
import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
import net.snowflake.client.jdbc.SnowflakeSQLException; | ||
import org.apache.arrow.memory.ArrowBuf; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.apache.arrow.vector.FieldVector; | ||
import org.apache.arrow.vector.ValueVector; | ||
import org.apache.arrow.vector.complex.FixedSizeListVector; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
|
||
@SnowflakeJdbcInternalApi | ||
public class FixedSizeListVectorConverter extends AbstractFullVectorConverter { | ||
protected RootAllocator allocator; | ||
protected ValueVector vector; | ||
protected DataConversionContext context; | ||
protected SFBaseSession session; | ||
protected int idx; | ||
protected Object valueTargetType; | ||
private TimeZone timeZoneToUse; | ||
|
||
FixedSizeListVectorConverter( | ||
RootAllocator allocator, | ||
ValueVector vector, | ||
DataConversionContext context, | ||
SFBaseSession session, | ||
TimeZone timeZoneToUse, | ||
int idx, | ||
Object valueTargetType) { | ||
this.allocator = allocator; | ||
this.vector = vector; | ||
this.context = context; | ||
this.session = session; | ||
this.timeZoneToUse = timeZoneToUse; | ||
this.idx = idx; | ||
this.valueTargetType = valueTargetType; | ||
} | ||
|
||
@Override | ||
protected FieldVector convertVector() | ||
throws SFException, SnowflakeSQLException, SFArrowException { | ||
try { | ||
FixedSizeListVector listVector = (FixedSizeListVector) vector; | ||
FieldVector dataVector = listVector.getDataVector(); | ||
FieldVector convertedDataVector = | ||
ArrowFullVectorConverterUtil.convert( | ||
allocator, dataVector, context, session, timeZoneToUse, 0, valueTargetType); | ||
FixedSizeListVector convertedListVector = | ||
FixedSizeListVector.empty(listVector.getName(), listVector.getListSize(), allocator); | ||
ArrayList<Field> fields = new ArrayList<>(); | ||
fields.add(convertedDataVector.getField()); | ||
convertedListVector.initializeChildrenFromFields(fields); | ||
convertedListVector.allocateNew(); | ||
convertedListVector.setValueCount(listVector.getValueCount()); | ||
ArrowBuf validityBuffer = listVector.getValidityBuffer(); | ||
convertedListVector | ||
.getValidityBuffer() | ||
.setBytes(0L, validityBuffer, 0L, validityBuffer.capacity()); | ||
convertedDataVector.makeTransferPair(convertedListVector.getDataVector()).transfer(); | ||
return convertedListVector; | ||
} finally { | ||
vector.close(); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/net/snowflake/client/core/arrow/fullvectorconverters/ListVectorConverter.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,77 @@ | ||
package net.snowflake.client.core.arrow.fullvectorconverters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.TimeZone; | ||
import net.snowflake.client.core.DataConversionContext; | ||
import net.snowflake.client.core.SFBaseSession; | ||
import net.snowflake.client.core.SFException; | ||
import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
import net.snowflake.client.jdbc.SnowflakeSQLException; | ||
import org.apache.arrow.memory.ArrowBuf; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.apache.arrow.vector.FieldVector; | ||
import org.apache.arrow.vector.ValueVector; | ||
import org.apache.arrow.vector.complex.ListVector; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
|
||
@SnowflakeJdbcInternalApi | ||
public class ListVectorConverter extends AbstractFullVectorConverter { | ||
protected RootAllocator allocator; | ||
protected ValueVector vector; | ||
protected DataConversionContext context; | ||
protected SFBaseSession session; | ||
protected int idx; | ||
protected Object valueTargetType; | ||
private TimeZone timeZoneToUse; | ||
|
||
ListVectorConverter( | ||
RootAllocator allocator, | ||
ValueVector vector, | ||
DataConversionContext context, | ||
SFBaseSession session, | ||
TimeZone timeZoneToUse, | ||
int idx, | ||
Object valueTargetType) { | ||
this.allocator = allocator; | ||
this.vector = vector; | ||
this.context = context; | ||
this.session = session; | ||
this.timeZoneToUse = timeZoneToUse; | ||
this.idx = idx; | ||
this.valueTargetType = valueTargetType; | ||
} | ||
|
||
protected ListVector initVector(String name, Field field) { | ||
ListVector convertedListVector = ListVector.empty(name, allocator); | ||
ArrayList<Field> fields = new ArrayList<>(); | ||
fields.add(field); | ||
convertedListVector.initializeChildrenFromFields(fields); | ||
return convertedListVector; | ||
} | ||
|
||
@Override | ||
protected FieldVector convertVector() | ||
throws SFException, SnowflakeSQLException, SFArrowException { | ||
try { | ||
ListVector listVector = (ListVector) vector; | ||
FieldVector dataVector = listVector.getDataVector(); | ||
FieldVector convertedDataVector = | ||
ArrowFullVectorConverterUtil.convert( | ||
allocator, dataVector, context, session, timeZoneToUse, 0, valueTargetType); | ||
// TODO: change to convertedDataVector and make all necessary changes to make it work | ||
ListVector convertedListVector = initVector(vector.getName(), dataVector.getField()); | ||
convertedListVector.allocateNew(); | ||
convertedListVector.setValueCount(listVector.getValueCount()); | ||
convertedListVector.getOffsetBuffer().setBytes(0, listVector.getOffsetBuffer()); | ||
ArrowBuf validityBuffer = listVector.getValidityBuffer(); | ||
convertedListVector | ||
.getValidityBuffer() | ||
.setBytes(0L, validityBuffer, 0L, validityBuffer.capacity()); | ||
convertedListVector.setLastSet(listVector.getLastSet()); | ||
convertedDataVector.makeTransferPair(convertedListVector.getDataVector()).transfer(); | ||
return convertedListVector; | ||
} finally { | ||
vector.close(); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/net/snowflake/client/core/arrow/fullvectorconverters/MapVectorConverter.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,36 @@ | ||
package net.snowflake.client.core.arrow.fullvectorconverters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.TimeZone; | ||
import net.snowflake.client.core.DataConversionContext; | ||
import net.snowflake.client.core.SFBaseSession; | ||
import net.snowflake.client.core.SnowflakeJdbcInternalApi; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.apache.arrow.vector.ValueVector; | ||
import org.apache.arrow.vector.complex.ListVector; | ||
import org.apache.arrow.vector.complex.MapVector; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
|
||
@SnowflakeJdbcInternalApi | ||
public class MapVectorConverter extends ListVectorConverter { | ||
|
||
MapVectorConverter( | ||
RootAllocator allocator, | ||
ValueVector vector, | ||
DataConversionContext context, | ||
SFBaseSession session, | ||
TimeZone timeZoneToUse, | ||
int idx, | ||
Object valueTargetType) { | ||
super(allocator, vector, context, session, timeZoneToUse, idx, valueTargetType); | ||
} | ||
|
||
@Override | ||
protected ListVector initVector(String name, Field field) { | ||
MapVector convertedMapVector = MapVector.empty(name, allocator, false); | ||
ArrayList<Field> fields = new ArrayList<>(); | ||
fields.add(field); | ||
convertedMapVector.initializeChildrenFromFields(fields); | ||
return convertedMapVector; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/net/snowflake/client/core/arrow/fullvectorconverters/SFArrowException.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,23 @@ | ||
package net.snowflake.client.core.arrow.fullvectorconverters; | ||
|
||
public class SFArrowException extends Exception { | ||
private final ArrowErrorCode errorCode; | ||
|
||
public SFArrowException(ArrowErrorCode errorCode, String message) { | ||
this(errorCode, message, null); | ||
} | ||
|
||
public SFArrowException(ArrowErrorCode errorCode, String message, Throwable cause) { | ||
super(message, cause); | ||
this.errorCode = errorCode; | ||
} | ||
|
||
public ArrowErrorCode getErrorCode() { | ||
return errorCode; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString() + (getErrorCode() != null ? ", errorCode = " + getErrorCode() : ""); | ||
} | ||
} |
Oops, something went wrong.