Skip to content
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

Add Java bindings to join gather map APIs [skip ci] #7751

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions java/src/main/java/ai/rapids/cudf/ColumnView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,49 @@ public static ColumnView makeStructView(ColumnView... columns) {
return makeStructView(columns[0].rows, columns);
}

/**
* Create a new column view from a raw device buffer. Note that this will NOT copy
* the contents of the buffer but only creates a view. The view MUST NOT outlive
* the underlying device buffer. The column view will be created without a validity
* vector, so it is not possible to create a view containing null elements. Additionally
* only fixed-width primitive types are supported.
*
* @param buffer device memory that will back the column view
* @param startOffset byte offset into the device buffer where the column data starts
* @param type type of data in the column view
* @param rows number of data elements in the column view
* @return new column view instance that must not outlive the backing device buffer
*/
public static ColumnView fromDeviceBuffer(BaseDeviceMemoryBuffer buffer,
long startOffset,
DType type,
int rows) {
if (buffer == null) {
throw new NullPointerException("buffer is null");
}
int typeSize = type.getSizeInBytes();
if (typeSize <= 0) {
throw new IllegalArgumentException("Unsupported type: " + type);
}
if (startOffset < 0) {
throw new IllegalArgumentException("Invalid start offset: " + startOffset);
}
if (rows < 0) {
throw new IllegalArgumentException("Invalid row count: " + rows);
}
long dataSize = typeSize * rows;
if (startOffset + dataSize > buffer.length) {
throw new IllegalArgumentException("View extends beyond buffer range");
}
long dataAddress = buffer.getAddress() + startOffset;
jlowe marked this conversation as resolved.
Show resolved Hide resolved
revans2 marked this conversation as resolved.
Show resolved Hide resolved
if (dataAddress % typeSize != 0) {
throw new IllegalArgumentException("Data address " + Long.toHexString(dataAddress) +
" is misaligned relative to type size of " + typeSize + " bytes");
}
return new ColumnView(makeCudfColumnView(type.typeId.getNativeId(), type.getScale(),
dataAddress, dataSize, 0, 0, 0, rows, null));
}

/**
* Create a column of bool values indicating whether the specified scalar
* is an element of each row of a list column.
Expand Down
85 changes: 85 additions & 0 deletions java/src/main/java/ai/rapids/cudf/GatherMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2021, NVIDIA CORPORATION.
*
* Licensed 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 ai.rapids.cudf;

/**
* This class tracks the data associated with a gather map, a buffer of INT32 elements that index
* a source table and can be passed to a table gather operation.
*/
public class GatherMap implements AutoCloseable {
private DeviceMemoryBuffer buffer;

/**
* Construct a gather map instance from a device buffer. The buffer length must be a multiple of
* the {@link DType#INT32} size, as each row of the gather map is an INT32.
* @param buffer device buffer backing the gather map data
*/
public GatherMap(DeviceMemoryBuffer buffer) {
if (buffer.getLength() % DType.INT32.getSizeInBytes() != 0) {
throw new IllegalArgumentException("buffer length not a multiple of 4");
}
this.buffer = buffer;
}

/** Return the number of rows in the gather map */
public long getRowCount() {
ensureOpen();
return buffer.getLength() / 4;
jlowe marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Create a column view that can be used to perform a gather operation. Note that the resulting
* column view MUST NOT outlive the underlying device buffer within this instance!
* @param startRow row offset where the resulting gather map will start
* @param numRows number of rows in the resulting gather map
* @return column view of gather map data
*/
public ColumnView toColumnView(long startRow, int numRows) {
ensureOpen();
return ColumnView.fromDeviceBuffer(buffer, startRow * 4, DType.INT32, numRows);
jlowe marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Release the underlying device buffer instance. After this is called, closing this instance
* will not close the underlying device buffer. It is the responsibility of the caller to close
* the returned device buffer.
* @return device buffer backing gather map data or null if the buffer has already been released
*/
public DeviceMemoryBuffer releaseBuffer() {
DeviceMemoryBuffer result = buffer;
buffer = null;
return result;
}

/** Close the device buffer backing the gather map data. */
@Override
public void close() {
if (buffer != null) {
buffer.close();
buffer = null;
}
}

private void ensureOpen() {
if (buffer == null) {
throw new IllegalStateException("instance is closed");
}
if (buffer.closed) {
throw new IllegalStateException("buffer is closed");
}
}
}
139 changes: 139 additions & 0 deletions java/src/main/java/ai/rapids/cudf/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,33 @@ private static native long[] merge(long[] tableHandles, int[] sortKeyIndexes,
private static native long[] leftJoin(long leftTable, int[] leftJoinCols, long rightTable,
int[] rightJoinCols, boolean compareNullsEqual) throws CudfException;

private static native long[] leftJoinGatherMaps(long leftKeys, long rightKeys,
boolean compareNullsEqual) throws CudfException;

private static native long[] innerJoin(long leftTable, int[] leftJoinCols, long rightTable,
int[] rightJoinCols, boolean compareNullsEqual) throws CudfException;

private static native long[] innerJoinGatherMaps(long leftKeys, long rightKeys,
boolean compareNullsEqual) throws CudfException;

private static native long[] fullJoin(long leftTable, int[] leftJoinCols, long rightTable,
int[] rightJoinCols, boolean compareNullsEqual) throws CudfException;

private static native long[] fullJoinGatherMaps(long leftKeys, long rightKeys,
boolean compareNullsEqual) throws CudfException;

private static native long[] leftSemiJoin(long leftTable, int[] leftJoinCols, long rightTable,
int[] rightJoinCols, boolean compareNullsEqual) throws CudfException;

private static native long[] leftSemiJoinGatherMap(long leftKeys, long rightKeys,
boolean compareNullsEqual) throws CudfException;

private static native long[] leftAntiJoin(long leftTable, int[] leftJoinCols, long rightTable,
int[] rightJoinCols, boolean compareNullsEqual) throws CudfException;

private static native long[] leftAntiJoinGatherMap(long leftKeys, long rightKeys,
boolean compareNullsEqual) throws CudfException;

private static native long[] crossJoin(long leftTable, long rightTable) throws CudfException;

private static native long[] concatenate(long[] cudfTablePointers) throws CudfException;
Expand Down Expand Up @@ -1925,6 +1940,130 @@ public Table gather(ColumnVector gatherMap, boolean checkBounds) {
return new Table(gather(nativeHandle, gatherMap.getNativeView(), checkBounds));
}

private GatherMap[] buildJoinGatherMaps(long[] gatherMapData) {
long bufferSize = gatherMapData[0];
revans2 marked this conversation as resolved.
Show resolved Hide resolved
long leftAddr = gatherMapData[1];
long leftHandle = gatherMapData[2];
long rightAddr = gatherMapData[3];
long rightHandle = gatherMapData[4];
GatherMap[] maps = new GatherMap[2];
maps[0] = new GatherMap(DeviceMemoryBuffer.fromRmm(leftAddr, bufferSize, leftHandle));
maps[1] = new GatherMap(DeviceMemoryBuffer.fromRmm(rightAddr, bufferSize, rightHandle));
return maps;
}

/**
* Computes the gather maps that can be used to manifest the result of a left equi-join between
* two tables. It is assumed this table instance holds the key columns from the left table, and
* the table argument represents the key columns from the right table. Two {@link GatherMap}
* instances will be returned that can be used to gather the left and right tables,
* respectively, to produce the result of the left join.
* It is the responsibility of the caller to close the resulting gather map instances.
* @param rightKeys join key columns from the right table
* @param compareNullsEqual true if null key values should match otherwise false
* @return left and right table gather maps
*/
public GatherMap[] leftJoinGatherMaps(Table rightKeys, boolean compareNullsEqual) {
if (getNumberOfColumns() != rightKeys.getNumberOfColumns()) {
throw new IllegalArgumentException("column count mismatch, this: " + getNumberOfColumns() +
"rightKeys: " + rightKeys.getNumberOfColumns());
}
long[] gatherMapData =
leftJoinGatherMaps(getNativeView(), rightKeys.getNativeView(), compareNullsEqual);
return buildJoinGatherMaps(gatherMapData);
}

/**
* Computes the gather maps that can be used to manifest the result of an inner equi-join between
* two tables. It is assumed this table instance holds the key columns from the left table, and
* the table argument represents the key columns from the right table. Two {@link GatherMap}
* instances will be returned that can be used to gather the left and right tables,
* respectively, to produce the result of the inner join.
* It is the responsibility of the caller to close the resulting gather map instances.
* @param rightKeys join key columns from the right table
* @param compareNullsEqual true if null key values should match otherwise false
* @return left and right table gather maps
*/
public GatherMap[] innerJoinGatherMaps(Table rightKeys, boolean compareNullsEqual) {
if (getNumberOfColumns() != rightKeys.getNumberOfColumns()) {
throw new IllegalArgumentException("column count mismatch, this: " + getNumberOfColumns() +
"rightKeys: " + rightKeys.getNumberOfColumns());
}
long[] gatherMapData =
innerJoinGatherMaps(getNativeView(), rightKeys.getNativeView(), compareNullsEqual);
return buildJoinGatherMaps(gatherMapData);
}

/**
* Computes the gather maps that can be used to manifest the result of an full equi-join between
* two tables. It is assumed this table instance holds the key columns from the left table, and
* the table argument represents the key columns from the right table. Two {@link GatherMap}
* instances will be returned that can be used to gather the left and right tables,
* respectively, to produce the result of the full join.
* It is the responsibility of the caller to close the resulting gather map instances.
* @param rightKeys join key columns from the right table
* @param compareNullsEqual true if null key values should match otherwise false
* @return left and right table gather maps
*/
public GatherMap[] fullJoinGatherMaps(Table rightKeys, boolean compareNullsEqual) {
if (getNumberOfColumns() != rightKeys.getNumberOfColumns()) {
throw new IllegalArgumentException("column count mismatch, this: " + getNumberOfColumns() +
"rightKeys: " + rightKeys.getNumberOfColumns());
}
long[] gatherMapData =
fullJoinGatherMaps(getNativeView(), rightKeys.getNativeView(), compareNullsEqual);
return buildJoinGatherMaps(gatherMapData);
}

private GatherMap buildSemiJoinGatherMap(long[] gatherMapData) {
long bufferSize = gatherMapData[0];
long leftAddr = gatherMapData[1];
long leftHandle = gatherMapData[2];
return new GatherMap(DeviceMemoryBuffer.fromRmm(leftAddr, bufferSize, leftHandle));
}

/**
* Computes the gather map that can be used to manifest the result of a left semi-join between
* two tables. It is assumed this table instance holds the key columns from the left table, and
* the table argument represents the key columns from the right table. The {@link GatherMap}
* instance returned can be used to gather the left table to produce the result of the
* left semi-join.
* It is the responsibility of the caller to close the resulting gather map instance.
* @param rightKeys join key columns from the right table
* @param compareNullsEqual true if null key values should match otherwise false
* @return left table gather map
*/
public GatherMap leftSemiJoinGatherMap(Table rightKeys, boolean compareNullsEqual) {
if (getNumberOfColumns() != rightKeys.getNumberOfColumns()) {
throw new IllegalArgumentException("column count mismatch, this: " + getNumberOfColumns() +
"rightKeys: " + rightKeys.getNumberOfColumns());
}
long[] gatherMapData =
leftSemiJoinGatherMap(getNativeView(), rightKeys.getNativeView(), compareNullsEqual);
return buildSemiJoinGatherMap(gatherMapData);
}

/**
* Computes the gather map that can be used to manifest the result of a left anti-join between
* two tables. It is assumed this table instance holds the key columns from the left table, and
* the table argument represents the key columns from the right table. The {@link GatherMap}
* instance returned can be used to gather the left table to produce the result of the
* left anti-join.
* It is the responsibility of the caller to close the resulting gather map instance.
* @param rightKeys join key columns from the right table
* @param compareNullsEqual true if null key values should match otherwise false
* @return left table gather map
*/
public GatherMap leftAntiJoinGatherMap(Table rightKeys, boolean compareNullsEqual) {
if (getNumberOfColumns() != rightKeys.getNumberOfColumns()) {
throw new IllegalArgumentException("column count mismatch, this: " + getNumberOfColumns() +
"rightKeys: " + rightKeys.getNumberOfColumns());
}
long[] gatherMapData =
leftAntiJoinGatherMap(getNativeView(), rightKeys.getNativeView(), compareNullsEqual);
return buildSemiJoinGatherMap(gatherMapData);
}

/**
* Convert this table of columns into a row major format that is useful for interacting with other
* systems that do row major processing of the data. Currently only fixed-width column types are
Expand Down
18 changes: 17 additions & 1 deletion java/src/main/native/include/jni_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -243,6 +243,22 @@ template <typename N_TYPE, typename J_ARRAY_TYPE, typename ACCESSOR> class nativ
return data_ptr;
}

const N_TYPE *const begin() const {
return data();
}

N_TYPE *begin() {
return data();
}

const N_TYPE *const end() const {
return data() + size();
}

N_TYPE *end() {
return data() + size();
}

const J_ARRAY_TYPE get_jArray() const { return orig; }

J_ARRAY_TYPE get_jArray() { return orig; }
Expand Down
Loading