Java Code Examples for org.apache.calcite.linq4j.tree.Primitive#box()

The following examples show how to use org.apache.calcite.linq4j.tree.Primitive#box() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RelDataTypeFactoryImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelDataType copySimpleType(
    RelDataType type,
    boolean nullable) {
  if (type instanceof JavaType) {
    JavaType javaType = (JavaType) type;
    if (SqlTypeUtil.inCharFamily(javaType)) {
      return new JavaType(
          javaType.clazz,
          nullable,
          javaType.charset,
          javaType.collation);
    } else {
      return new JavaType(
          nullable
              ? Primitive.box(javaType.clazz)
              : Primitive.unbox(javaType.clazz),
          nullable);
    }
  } else {
    // REVIEW: RelCrossType if it stays around; otherwise get rid of
    // this comment
    return type;
  }
}
 
Example 2
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public PhysType makeNullable(boolean nullable) {
  if (!nullable) {
    return this;
  }
  return new PhysTypeImpl(typeFactory,
      typeFactory.createTypeWithNullability(rowType, true),
      Primitive.box(javaRowClass), format);
}
 
Example 3
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Expression generateComparator(RelCollation collation) {
  // int c;
  // c = Utilities.compare(v0, v1);
  // if (c != 0) return c; // or -c if descending
  // ...
  // return 0;
  BlockBuilder body = new BlockBuilder();
  final Type javaRowClass = Primitive.box(this.javaRowClass);
  final ParameterExpression parameterV0 =
      Expressions.parameter(javaRowClass, "v0");
  final ParameterExpression parameterV1 =
      Expressions.parameter(javaRowClass, "v1");
  final ParameterExpression parameterC =
      Expressions.parameter(int.class, "c");
  final int mod =
      collation.getFieldCollations().size() == 1 ? Modifier.FINAL : 0;
  body.add(Expressions.declare(mod, parameterC, null));
  for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
    final int index = fieldCollation.getFieldIndex();
    final RelDataType fieldType = rowType.getFieldList().get(index).getType();
    final Expression fieldComparator = generateCollatorExpression(fieldType.getCollation());
    Expression arg0 = fieldReference(parameterV0, index);
    Expression arg1 = fieldReference(parameterV1, index);
    switch (Primitive.flavor(fieldClass(index))) {
    case OBJECT:
      arg0 = EnumUtils.convert(arg0, Comparable.class);
      arg1 = EnumUtils.convert(arg1, Comparable.class);
    }
    final boolean nullsFirst =
        fieldCollation.nullDirection
            == RelFieldCollation.NullDirection.FIRST;
    final boolean descending =
        fieldCollation.getDirection()
            == RelFieldCollation.Direction.DESCENDING;
    body.add(
        Expressions.statement(
            Expressions.assign(
                parameterC,
                Expressions.call(
                    Utilities.class,
                    fieldNullable(index)
                        ? (nullsFirst != descending
                        ? "compareNullsFirst"
                        : "compareNullsLast")
                        : "compare",
                    Expressions.list(
                        arg0,
                        arg1)
                        .appendIfNotNull(fieldComparator)))));
    body.add(
        Expressions.ifThen(
            Expressions.notEqual(
                parameterC, Expressions.constant(0)),
            Expressions.return_(
                null,
                descending
                    ? Expressions.negate(parameterC)
                    : parameterC)));
  }
  body.add(
      Expressions.return_(null, Expressions.constant(0)));

  final List<MemberDeclaration> memberDeclarations =
      Expressions.list(
          Expressions.methodDecl(
              Modifier.PUBLIC,
              int.class,
              "compare",
              ImmutableList.of(parameterV0, parameterV1),
              body.toBlock()));

  if (EnumerableRules.BRIDGE_METHODS) {
    final ParameterExpression parameterO0 =
        Expressions.parameter(Object.class, "o0");
    final ParameterExpression parameterO1 =
        Expressions.parameter(Object.class, "o1");
    BlockBuilder bridgeBody = new BlockBuilder();
    bridgeBody.add(
        Expressions.return_(
            null,
            Expressions.call(
                Expressions.parameter(
                    Comparable.class, "this"),
                BuiltInMethod.COMPARATOR_COMPARE.method,
                Expressions.convert_(
                    parameterO0,
                    javaRowClass),
                Expressions.convert_(
                    parameterO1,
                    javaRowClass))));
    memberDeclarations.add(
        overridingMethodDecl(
            BuiltInMethod.COMPARATOR_COMPARE.method,
            ImmutableList.of(parameterO0, parameterO1),
            bridgeBody.toBlock()));
  }
  return Expressions.new_(
      Comparator.class,
      ImmutableList.of(),
      memberDeclarations);
}
 
Example 4
Source File: EnumerableWindow.java    From calcite with Apache License 2.0 4 votes vote down vote up
private void declareAndResetState(final JavaTypeFactory typeFactory,
    BlockBuilder builder, final Result result, int windowIdx,
    List<AggImpState> aggs, PhysType outputPhysType,
    List<Expression> outputRow) {
  for (final AggImpState agg : aggs) {
    agg.context =
        new WinAggContext() {
          public SqlAggFunction aggregation() {
            return agg.call.getAggregation();
          }

          public RelDataType returnRelType() {
            return agg.call.type;
          }

          public Type returnType() {
            return EnumUtils.javaClass(typeFactory, returnRelType());
          }

          public List<? extends Type> parameterTypes() {
            return EnumUtils.fieldTypes(typeFactory,
                parameterRelTypes());
          }

          public List<? extends RelDataType> parameterRelTypes() {
            return EnumUtils.fieldRowTypes(result.physType.getRowType(),
                constants, agg.call.getArgList());
          }

          public List<ImmutableBitSet> groupSets() {
            throw new UnsupportedOperationException();
          }

          public List<Integer> keyOrdinals() {
            throw new UnsupportedOperationException();
          }

          public List<? extends RelDataType> keyRelTypes() {
            throw new UnsupportedOperationException();
          }

          public List<? extends Type> keyTypes() {
            throw new UnsupportedOperationException();
          }
        };
    String aggName = "a" + agg.aggIdx;
    if (CalciteSystemProperty.DEBUG.value()) {
      aggName = Util.toJavaId(agg.call.getAggregation().getName(), 0)
          .substring("ID$0$".length()) + aggName;
    }
    List<Type> state = agg.implementor.getStateType(agg.context);
    final List<Expression> decls = new ArrayList<>(state.size());
    for (int i = 0; i < state.size(); i++) {
      Type type = state.get(i);
      ParameterExpression pe =
          Expressions.parameter(type,
              builder.newName(aggName
                  + "s" + i + "w" + windowIdx));
      builder.add(Expressions.declare(0, pe, null));
      decls.add(pe);
    }
    agg.state = decls;
    Type aggHolderType = agg.context.returnType();
    Type aggStorageType =
        outputPhysType.getJavaFieldType(outputRow.size());
    if (Primitive.is(aggHolderType) && !Primitive.is(aggStorageType)) {
      aggHolderType = Primitive.box(aggHolderType);
    }
    ParameterExpression aggRes = Expressions.parameter(0,
        aggHolderType,
        builder.newName(aggName + "w" + windowIdx));

    builder.add(
        Expressions.declare(0, aggRes,
            Expressions.constant(Primitive.is(aggRes.getType())
                ? Primitive.of(aggRes.getType()).defaultValue
                : null,
                aggRes.getType())));
    agg.result = aggRes;
    outputRow.add(aggRes);
    agg.implementor.implementReset(agg.context,
        new WinAggResetContextImpl(builder, agg.state,
            null, null, null, null, null, null));
  }
}