Skip to content

Commit

Permalink
add comments and static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mchades committed Nov 15, 2023
1 parent 4b807be commit edc2a82
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ static FuncExpressionImpl of(String functionName, Expression... arguments) {
return new FuncExpressionImpl(functionName, arguments);
}

static FuncExpressionImpl of(String functionName) {
return of(functionName, Expression.EMPTY_EXPRESSION);
}

/** Returns the transform function name. */
String functionName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.datastrato.gravitino.rel.expressions;

import io.substrait.type.Type;
import io.substrait.type.TypeCreator;
import java.util.Objects;

/**
Expand All @@ -44,6 +45,14 @@ static <T> LiteralImpl<T> of(T value, Type dataType) {
return new LiteralImpl<>(value, dataType);
}

static LiteralImpl<Integer> ofInteger(Integer value) {
return of(value, TypeCreator.REQUIRED.I32);
}

static LiteralImpl<String> ofString(String value) {
return of(value, TypeCreator.REQUIRED.STRING);
}

final class LiteralImpl<T> implements Literal<T> {
private final T value;
private final Type dataType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public static Distribution of(Strategy strategy, int number, Expression... expre

/**
* Create a distribution on columns. Like distribute by (a) or (a, b), for complex like
* distributing by (func(a), b) or (func(a), func(b)), please use {@link Builder} to create.
* distributing by (func(a), b) or (func(a), func(b)), please use {@link DistributionImpl.Builder}
* to create.
*
* <pre>
* NOTE: a, b, c are column names.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

import java.util.Arrays;

/**
* An enum that defines the distribution strategy.
*
* <p>The following strategies are supported:
*
* <ul>
* <li>Hash: Uses the hash value of the expression to distribute data.
* <li>Range: Uses the range of the expression specified to distribute data.
* <li>Even: Distributes data evenly across partitions.
* </ul>
*/
public enum Strategy {
HASH,
RANGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

/** Helper methods to create SortOrders to pass into Gravitino. */
public class SortOrders {

public static SortImpl of(Expression expression) {
return of(expression, SortDirection.ASCENDING);
}

public static SortImpl of(Expression expression, SortDirection direction) {
return of(expression, direction, direction.defaultNullOrdering());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.datastrato.gravitino.rel.expressions.Literal;
import com.datastrato.gravitino.rel.expressions.NamedReference;
import com.google.common.collect.ObjectArrays;
import io.substrait.type.TypeCreator;
import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -67,7 +66,7 @@ private static HourTransform hour(NamedReference ref) {

public static BucketTransform bucket(int numBuckets, String[]... fieldNames) {
return new BucketTransform(
Literal.of(numBuckets, TypeCreator.REQUIRED.I32),
Literal.ofInteger(numBuckets),
Arrays.stream(fieldNames).map(NamedReference::field).toArray(NamedReference[]::new));
}

Expand All @@ -81,8 +80,7 @@ public static RangeTransform range(String[] fieldName) {
}

public static TruncateTransform truncate(int width, String... fieldName) {
return new TruncateTransform(
Literal.of(width, TypeCreator.REQUIRED.I32), NamedReference.field(fieldName));
return new TruncateTransform(Literal.ofInteger(width), NamedReference.field(fieldName));
}

public static ApplyTransform apply(String name, Expression[] arguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ void testDistribution() {
Assertions.assertArrayEquals(distributionArg1, bucket.expressions());

Expression[] distributionArg2 =
new Expression[] {
field("field1"), FunctionExpression.of("now", Expression.EMPTY_EXPRESSION)
};
new Expression[] {field("field1"), FunctionExpression.of("now")};
bucket = Distributions.ofEVEN(11111, distributionArg2);

Assertions.assertEquals(Strategy.EVEN, bucket.strategy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public String comment() {
}
};
// partition by foo(col_1, 'bar')
NamedReference.FieldReference arg1 = field(new String[] {column.name()});
Literal.LiteralImpl<String> arg2 = Literal.of("bar", TypeCreator.REQUIRED.STRING);
NamedReference.FieldReference arg1 = field(column.name());
Literal.LiteralImpl<String> arg2 = Literal.ofString("bar");
Transform applyTransform = apply("foo", new Expression[] {arg1, arg2});
Assertions.assertEquals("foo", applyTransform.name());
Assertions.assertEquals(2, applyTransform.arguments().length);
Expand Down

0 comments on commit edc2a82

Please sign in to comment.