Search in sources :

Example 1 with RexWindowBound

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);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) BinaryExpression(org.apache.calcite.linq4j.tree.BinaryExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexWindowBound(org.apache.calcite.rex.RexWindowBound) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with RexWindowBound

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);
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Method(java.lang.reflect.Method) NlsString(org.apache.calcite.util.NlsString) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) InvocationTargetException(java.lang.reflect.InvocationTargetException) RexWindowBound(org.apache.calcite.rex.RexWindowBound) ArrayList(java.util.ArrayList) List(java.util.List) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with RexWindowBound

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;
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlCall(org.apache.calcite.sql.SqlCall) RexWindowBound(org.apache.calcite.rex.RexWindowBound) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with RexWindowBound

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));
}
Also used : FlinkContextImpl(org.apache.flink.table.planner.calcite.FlinkContextImpl) RexWindowBound(org.apache.calcite.rex.RexWindowBound) ObjectWriter(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter) RexBuilder(org.apache.calcite.rex.RexBuilder) ObjectReader(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader) ModuleManager(org.apache.flink.table.module.ModuleManager) Test(org.junit.Test)

Example 5 with RexWindowBound

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);
}
Also used : RexWindowBound(org.apache.calcite.rex.RexWindowBound) BoundarySpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.BoundarySpec) WindowFrameSpec(org.apache.hadoop.hive.ql.parse.WindowingSpec.WindowFrameSpec)

Aggregations

RexWindowBound (org.apache.calcite.rex.RexWindowBound)7 RexNode (org.apache.calcite.rex.RexNode)5 RelDataType (org.apache.calcite.rel.type.RelDataType)4 ArrayList (java.util.ArrayList)3 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)3 List (java.util.List)2 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)2 RexFieldCollation (org.apache.calcite.rex.RexFieldCollation)2 SqlNode (org.apache.calcite.sql.SqlNode)2 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)2 ImmutableList (com.google.common.collect.ImmutableList)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)1