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

The following examples show how to use org.apache.calcite.linq4j.tree.Primitive#flavor() . 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: RexToLixTranslator.java    From calcite with Apache License 2.0 5 votes vote down vote up
Expression checkNull(Expression expr) {
  if (Primitive.flavor(expr.getType())
      == Primitive.Flavor.PRIMITIVE) {
    return RexImpTable.FALSE_EXPR;
  }
  return Expressions.equal(expr, RexImpTable.NULL_EXPR);
}
 
Example 2
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 5 votes vote down vote up
Expression checkNotNull(Expression expr) {
  if (Primitive.flavor(expr.getType())
      == Primitive.Flavor.PRIMITIVE) {
    return RexImpTable.TRUE_EXPR;
  }
  return Expressions.notEqual(expr, RexImpTable.NULL_EXPR);
}
 
Example 3
Source File: ReflectiveSchemaTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static boolean isNumeric(Class type) {
  switch (Primitive.flavor(type)) {
  case BOX:
    return Primitive.ofBox(type).isNumeric();
  case PRIMITIVE:
    return Primitive.of(type).isNumeric();
  default:
    return Number.class.isAssignableFrom(type); // e.g. BigDecimal
  }
}
 
Example 4
Source File: JavaTypeFactoryImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataType createType(Type type) {
  if (type instanceof RelDataType) {
    return (RelDataType) type;
  }
  if (type instanceof SyntheticRecordType) {
    final SyntheticRecordType syntheticRecordType =
        (SyntheticRecordType) type;
    return syntheticRecordType.relType;
  }
  if (type instanceof Types.ArrayType) {
    final Types.ArrayType arrayType = (Types.ArrayType) type;
    final RelDataType componentRelType =
        createType(arrayType.getComponentType());
    return createArrayType(
        createTypeWithNullability(componentRelType,
            arrayType.componentIsNullable()), arrayType.maximumCardinality());
  }
  if (type instanceof Types.MapType) {
    final Types.MapType mapType = (Types.MapType) type;
    final RelDataType keyRelType = createType(mapType.getKeyType());
    final RelDataType valueRelType = createType(mapType.getValueType());
    return createMapType(
        createTypeWithNullability(keyRelType, mapType.keyIsNullable()),
        createTypeWithNullability(valueRelType, mapType.valueIsNullable()));
  }
  if (!(type instanceof Class)) {
    throw new UnsupportedOperationException("TODO: implement " + type);
  }
  final Class clazz = (Class) type;
  switch (Primitive.flavor(clazz)) {
  case PRIMITIVE:
    return createJavaType(clazz);
  case BOX:
    return createJavaType(Primitive.ofBox(clazz).boxClass);
  }
  if (JavaToSqlTypeConversionRules.instance().lookup(clazz) != null) {
    return createJavaType(clazz);
  } else if (clazz.isArray()) {
    return createMultisetType(
        createType(clazz.getComponentType()), -1);
  } else if (List.class.isAssignableFrom(clazz)) {
    return createArrayType(
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true), -1);
  } else if (Map.class.isAssignableFrom(clazz)) {
    return createMapType(
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true),
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true));
  } else {
    return createStructType(clazz);
  }
}
 
Example 5
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 6
Source File: RexImpTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Adapts an expression with "normal" result to one that adheres to
 * this particular policy. */
public Expression handle(Expression x) {
  switch (Primitive.flavor(x.getType())) {
  case PRIMITIVE:
    // Expression cannot be null. We can skip any runtime checks.
    switch (this) {
    case NULL:
    case NOT_POSSIBLE:
    case FALSE:
    case TRUE:
      return x;
    case IS_NULL:
      return FALSE_EXPR;
    case IS_NOT_NULL:
      return TRUE_EXPR;
    default:
      throw new AssertionError();
    }
  case BOX:
    switch (this) {
    case NOT_POSSIBLE:
      return EnumUtils.convert(x,
          Primitive.ofBox(x.getType()).primitiveClass);
    }
    // fall through
  }
  switch (this) {
  case NULL:
  case NOT_POSSIBLE:
    return x;
  case FALSE:
    return Expressions.call(BuiltInMethod.IS_TRUE.method, x);
  case TRUE:
    return Expressions.call(BuiltInMethod.IS_NOT_FALSE.method, x);
  case IS_NULL:
    return Expressions.equal(x, NULL_EXPR);
  case IS_NOT_NULL:
    return Expressions.notEqual(x, NULL_EXPR);
  default:
    throw new AssertionError();
  }
}