forked from rapidsai/cudf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3200f01
commit 022963b
Showing
36 changed files
with
3,930 additions
and
159 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
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
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
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
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
24 changes: 24 additions & 0 deletions
24
java/src/main/java/ai/rapids/cudf/schema/SchemaVisitor.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,24 @@ | ||
package ai.rapids.cudf.schema; | ||
|
||
import ai.rapids.cudf.Schema; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* Interface for visiting a schema in post order. | ||
*/ | ||
public interface SchemaVisitor<T, R> { | ||
R visitTopSchema(Schema schema, List<T> children); | ||
|
||
T visitStruct(Schema structType, List<T> children); | ||
|
||
T preVisitList(Schema listType); | ||
|
||
T visitList(Schema listType, T preVisitResult, T childResult); | ||
|
||
T visit(Schema primitiveType); | ||
|
||
|
||
} |
17 changes: 17 additions & 0 deletions
17
java/src/main/java/ai/rapids/cudf/schema/SchemaWithColumnsVisitor.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,17 @@ | ||
package ai.rapids.cudf.schema; | ||
|
||
import ai.rapids.cudf.HostColumnVectorCore; | ||
import ai.rapids.cudf.Schema; | ||
|
||
import java.util.List; | ||
|
||
public interface SchemaWithColumnsVisitor<T, R> { | ||
R visitTopSchema(Schema schema, List<T> children); | ||
|
||
T visitStruct(Schema structType, HostColumnVectorCore col, List<T> children); | ||
|
||
T preVisitList(Schema listType, HostColumnVectorCore col); | ||
T visitList(Schema listType, HostColumnVectorCore col, T preVisitResult, T childResult); | ||
|
||
T visit(Schema primitiveType, HostColumnVectorCore col); | ||
} |
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 ai.rapids.cudf.schema; | ||
|
||
import ai.rapids.cudf.HostColumnVector; | ||
import ai.rapids.cudf.HostColumnVectorCore; | ||
import ai.rapids.cudf.Schema; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Visitors { | ||
public static <T, R> R visitSchema(Schema schema, SchemaVisitor<T, R> visitor) { | ||
Objects.requireNonNull(schema, "schema cannot be null"); | ||
Objects.requireNonNull(visitor, "visitor cannot be null"); | ||
|
||
List<T> childrenResult = IntStream.range(0, schema.getNumChildren()) | ||
.mapToObj(i -> visitSchemaInner(schema.getChild(i), visitor)) | ||
.collect(Collectors.toList()); | ||
|
||
return visitor.visitTopSchema(schema, childrenResult); | ||
} | ||
|
||
private static <T, R> T visitSchemaInner(Schema schema, SchemaVisitor<T, R> visitor) { | ||
switch (schema.getType().getTypeId()) { | ||
case STRUCT: | ||
List<T> children = IntStream.range(0, schema.getNumChildren()) | ||
.mapToObj(childIdx -> visitSchemaInner(schema.getChild(childIdx), visitor)) | ||
.collect(Collectors.toList()); | ||
return visitor.visitStruct(schema, children); | ||
case LIST: | ||
T preVisitResult = visitor.preVisitList(schema); | ||
T childResult = visitSchemaInner(schema.getChild(0), visitor); | ||
return visitor.visitList(schema, preVisitResult, childResult); | ||
default: | ||
return visitor.visit(schema); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Entry point for visiting a schema with columns. | ||
*/ | ||
public static <T, R> R visitSchemaWithColumns(Schema schema, List<HostColumnVector> cols, | ||
SchemaWithColumnsVisitor<T, R> visitor) { | ||
Objects.requireNonNull(schema, "schema cannot be null"); | ||
Objects.requireNonNull(cols, "cols cannot be null"); | ||
Objects.requireNonNull(visitor, "visitor cannot be null"); | ||
|
||
if (schema.getNumChildren() != cols.size()) { | ||
throw new IllegalArgumentException("Schema children num: " + schema.getNumChildren() + | ||
" is not same as columns num: " + cols.size()); | ||
} | ||
|
||
List<T> childrenResult = IntStream.range(0, schema.getNumChildren()) | ||
.mapToObj(i -> visitSchema(schema.getChild(i), cols.get(i), visitor)) | ||
.collect(Collectors.toList()); | ||
|
||
return visitor.visitTopSchema(schema, childrenResult); | ||
} | ||
|
||
private static <T, R> T visitSchema(Schema schema, HostColumnVectorCore col, SchemaWithColumnsVisitor<T, R> visitor) { | ||
switch (schema.getType().getTypeId()) { | ||
case STRUCT: | ||
List<T> children = IntStream.range(0, schema.getNumChildren()) | ||
.mapToObj(childIdx -> visitSchema(schema.getChild(childIdx), col.getChildColumnView(childIdx), visitor)) | ||
.collect(Collectors.toList()); | ||
return visitor.visitStruct(schema, col, children); | ||
case LIST: | ||
T preVisitResult = visitor.preVisitList(schema, col); | ||
T childResult = visitSchema(schema.getChild(0), col.getChildColumnView(0), visitor); | ||
return visitor.visitList(schema, col, preVisitResult, childResult); | ||
default: | ||
return visitor.visit(schema, col); | ||
} | ||
} | ||
} |
Oops, something went wrong.