use of org.apache.calcite.rex.RexWindowBound in project calcite by apache.
the class EnumerableWindow method translateBound.
private Expression translateBound(RexToLixTranslator translator, ParameterExpression i_, Expression row_, Expression min_, Expression max_, Expression rows_, Group group, boolean lower, PhysType physType, Expression rowComparator, Expression keySelector, Expression keyComparator) {
RexWindowBound bound = lower ? group.lowerBound : group.upperBound;
if (bound.isUnbounded()) {
return bound.isPreceding() ? min_ : max_;
}
if (group.isRows) {
if (bound.isCurrentRow()) {
return i_;
}
RexNode node = bound.getOffset();
Expression offs = translator.translate(node);
// Floating offset does not make sense since we refer to array index.
// Nulls do not make sense as well.
offs = RexToLixTranslator.convert(offs, int.class);
Expression b = i_;
if (bound.isFollowing()) {
b = Expressions.add(b, offs);
} else {
b = Expressions.subtract(b, offs);
}
return b;
}
Expression searchLower = min_;
Expression searchUpper = max_;
if (bound.isCurrentRow()) {
if (lower) {
searchUpper = i_;
} else {
searchLower = i_;
}
}
List<RelFieldCollation> fieldCollations = group.collation().getFieldCollations();
if (bound.isCurrentRow() && fieldCollations.size() != 1) {
return Expressions.call((lower ? BuiltInMethod.BINARY_SEARCH5_LOWER : BuiltInMethod.BINARY_SEARCH5_UPPER).method, rows_, row_, searchLower, searchUpper, keySelector, keyComparator);
}
assert fieldCollations.size() == 1 : "When using range window specification, ORDER BY should have" + " exactly one expression." + " Actual collation is " + group.collation();
// isRange
int orderKey = fieldCollations.get(0).getFieldIndex();
RelDataType keyType = physType.getRowType().getFieldList().get(orderKey).getType();
Type desiredKeyType = translator.typeFactory.getJavaClass(keyType);
if (bound.getOffset() == null) {
desiredKeyType = Primitive.box(desiredKeyType);
}
Expression val = translator.translate(new RexInputRef(orderKey, keyType), desiredKeyType);
if (!bound.isCurrentRow()) {
RexNode node = bound.getOffset();
Expression offs = translator.translate(node);
// TODO: support date + interval somehow
if (bound.isFollowing()) {
val = Expressions.add(val, offs);
} else {
val = Expressions.subtract(val, offs);
}
}
return Expressions.call((lower ? BuiltInMethod.BINARY_SEARCH6_LOWER : BuiltInMethod.BINARY_SEARCH6_UPPER).method, rows_, val, searchLower, searchUpper, keySelector, keyComparator);
}
use of org.apache.calcite.rex.RexWindowBound in project flink by apache.
the class HiveParserUtils method makeOver.
/**
* Proxy to {@link RexBuilder#makeOver(RelDataType, SqlAggFunction, List, List,
* com.google.common.collect.ImmutableList, RexWindowBound, RexWindowBound, boolean, boolean,
* boolean, boolean, boolean)}.
*/
public static RexNode makeOver(RexBuilder rexBuilder, RelDataType type, SqlAggFunction operator, List<RexNode> exprs, List<RexNode> partitionKeys, List<RexFieldCollation> orderKeys, RexWindowBound lowerBound, RexWindowBound upperBound, boolean physical, boolean allowPartial, boolean nullWhenCountZero, boolean distinct, boolean ignoreNulls) {
Preconditions.checkState(immutableListClz != null || shadedImmutableListClz != null, "Neither original nor shaded guava class can be found");
Method method = null;
final String methodName = "makeOver";
final int orderKeysIndex = 4;
Class[] argTypes = new Class[] { RelDataType.class, SqlAggFunction.class, List.class, List.class, null, RexWindowBound.class, RexWindowBound.class, boolean.class, boolean.class, boolean.class, boolean.class, boolean.class };
if (immutableListClz != null) {
argTypes[orderKeysIndex] = immutableListClz;
method = HiveReflectionUtils.tryGetMethod(rexBuilder.getClass(), methodName, argTypes);
}
if (method == null) {
Preconditions.checkState(shadedImmutableListClz != null, String.format("Shaded guava class not found, but method %s takes shaded parameter", methodName));
argTypes[orderKeysIndex] = shadedImmutableListClz;
method = HiveReflectionUtils.tryGetMethod(rexBuilder.getClass(), methodName, argTypes);
}
Preconditions.checkState(method != null, "Neither original nor shaded method can be found");
Object orderKeysArg = toImmutableList(orderKeys);
Object[] args = new Object[] { type, operator, exprs, partitionKeys, orderKeysArg, lowerBound, upperBound, physical, allowPartial, nullWhenCountZero, distinct, ignoreNulls };
try {
return (RexNode) method.invoke(rexBuilder, args);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException("Failed to invoke " + methodName, e);
}
}
use of org.apache.calcite.rex.RexWindowBound in project flink by apache.
the class HiveParserBaseSemanticAnalyzer method getBound.
public static RexWindowBound getBound(HiveParserWindowingSpec.BoundarySpec spec, RelOptCluster cluster) {
RexWindowBound res = null;
if (spec != null) {
SqlParserPos dummyPos = new SqlParserPos(1, 1);
SqlNode amt = spec.getAmt() == 0 || spec.getAmt() == HiveParserWindowingSpec.BoundarySpec.UNBOUNDED_AMOUNT ? null : SqlLiteral.createExactNumeric(String.valueOf(spec.getAmt()), new SqlParserPos(2, 2));
RexNode amtLiteral = amt == null ? null : cluster.getRexBuilder().makeLiteral(spec.getAmt(), cluster.getTypeFactory().createSqlType(SqlTypeName.INTEGER), true);
switch(spec.getDirection()) {
case PRECEDING:
if (amt == null) {
res = RexWindowBound.create(SqlWindow.createUnboundedPreceding(dummyPos), null);
} else {
SqlCall call = (SqlCall) SqlWindow.createPreceding(amt, dummyPos);
res = RexWindowBound.create(call, cluster.getRexBuilder().makeCall(call.getOperator(), amtLiteral));
}
break;
case CURRENT:
res = RexWindowBound.create(SqlWindow.createCurrentRow(dummyPos), null);
break;
case FOLLOWING:
if (amt == null) {
res = RexWindowBound.create(SqlWindow.createUnboundedFollowing(dummyPos), null);
} else {
SqlCall call = (SqlCall) SqlWindow.createFollowing(amt, dummyPos);
res = RexWindowBound.create(call, cluster.getRexBuilder().makeCall(call.getOperator(), amtLiteral));
}
break;
}
}
return res;
}
use of org.apache.calcite.rex.RexWindowBound in project flink by apache.
the class RexWindowBoundSerdeTest method testSerde.
@Test
public void testSerde() throws IOException {
SerdeContext serdeCtx = new SerdeContext(null, new FlinkContextImpl(false, TableConfig.getDefault(), new ModuleManager(), null, CatalogManagerMocks.createEmptyCatalogManager(), null), Thread.currentThread().getContextClassLoader(), FlinkTypeFactory.INSTANCE(), FlinkSqlOperatorTable.instance());
ObjectReader objectReader = JsonSerdeUtil.createObjectReader(serdeCtx);
ObjectWriter objectWriter = JsonSerdeUtil.createObjectWriter(serdeCtx);
assertEquals(RexWindowBounds.CURRENT_ROW, objectReader.readValue(objectWriter.writeValueAsString(RexWindowBounds.CURRENT_ROW), RexWindowBound.class));
assertEquals(RexWindowBounds.UNBOUNDED_FOLLOWING, objectReader.readValue(objectWriter.writeValueAsString(RexWindowBounds.UNBOUNDED_FOLLOWING), RexWindowBound.class));
assertEquals(RexWindowBounds.UNBOUNDED_PRECEDING, objectReader.readValue(objectWriter.writeValueAsString(RexWindowBounds.UNBOUNDED_PRECEDING), RexWindowBound.class));
RexBuilder builder = new RexBuilder(FlinkTypeFactory.INSTANCE());
RexWindowBound windowBound = RexWindowBounds.following(builder.makeLiteral("test"));
assertEquals(windowBound, objectReader.readValue(objectWriter.writeValueAsString(windowBound), RexWindowBound.class));
windowBound = RexWindowBounds.preceding(builder.makeLiteral("test"));
assertEquals(windowBound, objectReader.readValue(objectWriter.writeValueAsString(windowBound), RexWindowBound.class));
}
use of org.apache.calcite.rex.RexWindowBound in project hive by apache.
the class ExprNodeConverter method getWindowRange.
private WindowFrameSpec getWindowRange(RexWindow window) {
// NOTE: in Hive AST Rows->Range(Physical) & Range -> Values (logical)
BoundarySpec start = null;
RexWindowBound lb = window.getLowerBound();
if (lb != null) {
start = getWindowBound(lb);
}
BoundarySpec end = null;
RexWindowBound ub = window.getUpperBound();
if (ub != null) {
end = getWindowBound(ub);
}
return new WindowFrameSpec(window.isRows() ? WindowType.ROWS : WindowType.RANGE, start, end);
}
Aggregations