Java Code Examples for org.apache.calcite.rel.type.RelDataTypeFactory#createTypeWithNullability()

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactory#createTypeWithNullability() . 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: RelDataTypeSystemImpl.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType deriveSumType(
    RelDataTypeFactory typeFactory, RelDataType argumentType) {
  SqlTypeName sqlTypeName = argumentType.getSqlTypeName();
  switch (sqlTypeName) {
    case TINYINT:
    case SMALLINT:
    case INTEGER:
    case BIGINT:
      return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BIGINT), argumentType.isNullable());
    case FLOAT:
    case DOUBLE:
      return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.DOUBLE), argumentType.isNullable());
    case DECIMAL:
      return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName
          .DECIMAL, MAX_NUMERIC_PRECISION, argumentType.getScale()), argumentType.isNullable());
  }
  return argumentType;
}
 
Example 2
Source File: JoinNamespace.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected RelDataType validateImpl(RelDataType targetRowType) {
  RelDataType leftType =
      validator.getNamespace(join.getLeft()).getRowType();
  RelDataType rightType =
      validator.getNamespace(join.getRight()).getRowType();
  final RelDataTypeFactory typeFactory = validator.getTypeFactory();
  switch (join.getJoinType()) {
  case LEFT:
    rightType = typeFactory.createTypeWithNullability(rightType, true);
    break;
  case RIGHT:
    leftType = typeFactory.createTypeWithNullability(leftType, true);
    break;
  case FULL:
    leftType = typeFactory.createTypeWithNullability(leftType, true);
    rightType = typeFactory.createTypeWithNullability(rightType, true);
    break;
  }
  return typeFactory.createJoinType(leftType, rightType);
}
 
Example 3
Source File: SqlTypeFactoryTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2464">[CALCITE-2464]
 * Allow to set nullability for columns of structured types</a>. */
@Test void createStructTypeWithNullability() {
  SqlTypeFixture f = new SqlTypeFixture();
  RelDataTypeFactory typeFactory = f.typeFactory;
  List<RelDataTypeField> fields = new ArrayList<>();
  RelDataTypeField field0 = new RelDataTypeFieldImpl(
          "i", 0, typeFactory.createSqlType(SqlTypeName.INTEGER));
  RelDataTypeField field1 = new RelDataTypeFieldImpl(
          "s", 1, typeFactory.createSqlType(SqlTypeName.VARCHAR));
  fields.add(field0);
  fields.add(field1);
  final RelDataType recordType = new RelRecordType(fields); // nullable false by default
  final RelDataType copyRecordType = typeFactory.createTypeWithNullability(recordType, true);
  assertFalse(recordType.isNullable());
  assertTrue(copyRecordType.isNullable());
}
 
Example 4
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static RelDataType createMapType(
    RelDataTypeFactory typeFactory,
    RelDataType keyType,
    RelDataType valueType,
    boolean nullable) {
  RelDataType ret = typeFactory.createMapType(keyType, valueType);
  return typeFactory.createTypeWithNullability(ret, nullable);
}
 
Example 5
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding
      .getOperandType(0)
      .isNullable();

  RelDataType ret = factory.createTypeWithNullability(
      opBinding.getOperandType(1),
      isNullable);
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding callBinding = (SqlCallBinding) opBinding;
    SqlNode operand0 = callBinding.operand(0);

    // dynamic parameters and null constants need their types assigned
    // to them using the type they are casted to.
    if(((operand0 instanceof SqlLiteral)
        && (((SqlLiteral) operand0).getValue() == null))
            || (operand0 instanceof SqlDynamicParam)) {
      callBinding.getValidator().setValidatedNodeType(
          operand0,
          ret);
    }
  }

  return ret;
}
 
Example 6
Source File: CalciteCatalogReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static RelDataType toSql(RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example 7
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  return factory.createTypeWithNullability(
    factory.createSqlType(SqlTypeName.ANY),
    true);
}
 
Example 8
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a RelDataType using specified RelDataTypeFactory which corresponds to specified TypeProtos.MajorType.
 *
 * @param typeFactory RelDataTypeFactory used to create the RelDataType
 * @param drillType   the given TypeProtos.MajorType
 * @param isNullable  nullability of the resulting type
 * @return RelDataType which corresponds to specified TypeProtos.MajorType
 */
public static RelDataType convertToCalciteType(RelDataTypeFactory typeFactory,
                                               TypeProtos.MajorType drillType, boolean isNullable) {
  SqlTypeName sqlTypeName = getCalciteTypeFromDrillType(drillType.getMinorType());
  if (sqlTypeName == SqlTypeName.DECIMAL) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(sqlTypeName, drillType.getPrecision(),
            drillType.getScale()), isNullable);
  }
  return createCalciteTypeWithNullability(typeFactory, sqlTypeName, isNullable);
}
 
Example 9
Source File: SqlJsonObjectFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlJsonObjectFunction() {
  super("JSON_OBJECT", SqlKind.OTHER_FUNCTION, ReturnTypes.VARCHAR_2000,
      (callBinding, returnType, operandTypes) -> {
        RelDataTypeFactory typeFactory = callBinding.getTypeFactory();
        for (int i = 0; i < operandTypes.length; i++) {
          if (i % 2 == 0) {
            operandTypes[i] = typeFactory.createSqlType(SqlTypeName.VARCHAR);
            continue;
          }
          operandTypes[i] = typeFactory.createTypeWithNullability(
              typeFactory.createSqlType(SqlTypeName.ANY), true);
        }
      }, null, SqlFunctionCategory.SYSTEM);
}
 
Example 10
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  return factory.createTypeWithNullability(
      factory.createSqlType(SqlTypeName.ANY),
      true);
}
 
Example 11
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelDataType toSql(RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example 12
Source File: View.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RelDataType getType(Field field, RelDataTypeFactory factory) {
  RelDataType type;
  final SqlTypeName typeName = field.getType();
  final Integer precision = field.getPrecision();
  final Integer scale = field.getScale();

  if (field.isInterval()) {
    type = factory.createSqlIntervalType(field.getIntervalQualifier());
  } else if (precision != null) {
    type = scale != null
        ? factory.createSqlType(typeName, precision, scale)
        : factory.createSqlType(typeName, precision);
  } else if (typeName == SqlTypeName.MAP) {
    if (field.isMapTypesPresent()) {
      type = factory.createMapType(getType(field.getKeyType(), factory), getType(field.getValueType(), factory));
    } else {
       /*
          For older views that doesn't have info about map key and value types,
          chosen type is ANY. Because use of raw MAP type causes creation of
          MAP cast expression that can't be serialized by ExpressionStringBuilder's
          visitCastExpression(CastExpression e, StringBuilder sb) method.
          See DRILL-6944 for more details.
       */
      type = factory.createSqlType(SqlTypeName.ANY);
    }
  } else {
    type = factory.createSqlType(field.getType());
  }

  return field.getIsNullable() ? factory.createTypeWithNullability(type, true) : type;
}
 
Example 13
Source File: SqlJsonObjectAggAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SqlJsonObjectAggAggFunction. */
public SqlJsonObjectAggAggFunction(SqlKind kind,
    SqlJsonConstructorNullClause nullClause) {
  super(kind + "_" + nullClause.name(), null, kind, ReturnTypes.VARCHAR_2000,
      (callBinding, returnType, operandTypes) -> {
        RelDataTypeFactory typeFactory = callBinding.getTypeFactory();
        operandTypes[0] = typeFactory.createSqlType(SqlTypeName.VARCHAR);
        operandTypes[1] = typeFactory.createTypeWithNullability(
            typeFactory.createSqlType(SqlTypeName.ANY), true);
      }, OperandTypes.family(SqlTypeFamily.CHARACTER,
          SqlTypeFamily.ANY),
      SqlFunctionCategory.SYSTEM, false, false, Optionality.FORBIDDEN);
  this.nullClause = Objects.requireNonNull(nullClause);
}
 
Example 14
Source File: CalciteArrowHelper.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public RelDataType toCalciteType(RelDataTypeFactory typeFactory) {
      final MinorType type = completeType.toMinorType();
      if (completeType.isList()) {
//        RelDataType childType = convertFieldToRelDataType(field.getChildren().iterator().next(), typeFactory);
//        return typeFactory.createArrayType(childType, -1);
        return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
      }
      if (completeType.isStruct()) {
//        return convertFieldsToStruct(field.getChildren(), typeFactory);
        return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
      }

      final SqlTypeName sqlTypeName = getCalciteTypeFromMinorType(type);

      if(completeType.isVariableWidthScalar()){
        return typeFactory.createTypeWithNullability(typeFactory.createSqlType(sqlTypeName, 1 << 16), true);
      }

      if(completeType.isDecimal()){
        return typeFactory.createTypeWithNullability(typeFactory.createSqlType(sqlTypeName, completeType.getPrecision(), completeType.getScale()), true);
      }

      if (completeType.getType().getTypeID() == ArrowTypeID.Timestamp ||
          completeType.getType().getTypeID() == ArrowTypeID.Time) {
        return typeFactory.createTypeWithNullability(typeFactory.createSqlType(sqlTypeName, completeType.getPrecision()), true);
      }

      return typeFactory.createTypeWithNullability(typeFactory.createSqlType(sqlTypeName), true);
    }
 
Example 15
Source File: RelDataTypeSystemImpl.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType deriveCovarType(RelDataTypeFactory typeFactory, RelDataType arg0Type, RelDataType arg1Type) {
  return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.DOUBLE), true);
}
 
Example 16
Source File: HiveUDFOperatorWithoutInference.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  RelDataTypeFactory factory = opBinding.getTypeFactory();
  return factory.createTypeWithNullability(factory.createSqlType(SqlTypeName.ANY), true);
}
 
Example 17
Source File: HiveUDFOperatorWithoutInference.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
  RelDataTypeFactory factory = validator.getTypeFactory();
  return factory.createTypeWithNullability(factory.createSqlType(SqlTypeName.ANY), true);
}
 
Example 18
Source File: RelMdColumnUniqueness.java    From Bats with Apache License 2.0 4 votes vote down vote up
public Boolean areColumnsUnique(Project rel, RelMetadataQuery mq,
    ImmutableBitSet columns, boolean ignoreNulls) {
  // LogicalProject maps a set of rows to a different set;
  // Without knowledge of the mapping function(whether it
  // preserves uniqueness), it is only safe to derive uniqueness
  // info from the child of a project when the mapping is f(a) => a.
  //
  // Also need to map the input column set to the corresponding child
  // references

  List<RexNode> projExprs = rel.getProjects();
  ImmutableBitSet.Builder childColumns = ImmutableBitSet.builder();
  for (int bit : columns) {
    RexNode projExpr = projExprs.get(bit);
    if (projExpr instanceof RexInputRef) {
      childColumns.set(((RexInputRef) projExpr).getIndex());
    } else if (projExpr instanceof RexCall && ignoreNulls) {
      // If the expression is a cast such that the types are the same
      // except for the nullability, then if we're ignoring nulls,
      // it doesn't matter whether the underlying column reference
      // is nullable.  Check that the types are the same by making a
      // nullable copy of both types and then comparing them.
      RexCall call = (RexCall) projExpr;
      if (call.getOperator() != SqlStdOperatorTable.CAST) {
        continue;
      }
      RexNode castOperand = call.getOperands().get(0);
      if (!(castOperand instanceof RexInputRef)) {
        continue;
      }
      RelDataTypeFactory typeFactory =
          rel.getCluster().getTypeFactory();
      RelDataType castType =
          typeFactory.createTypeWithNullability(
              projExpr.getType(), true);
      RelDataType origType = typeFactory.createTypeWithNullability(
          castOperand.getType(),
          true);
      if (castType.equals(origType)) {
        childColumns.set(((RexInputRef) castOperand).getIndex());
      }
    } else {
      // If the expression will not influence uniqueness of the
      // projection, then skip it.
      continue;
    }
  }

  // If no columns can affect uniqueness, then return unknown
  if (childColumns.cardinality() == 0) {
    return null;
  }

  return mq.areColumnsUnique(rel.getInput(), childColumns.build(),
      ignoreNulls);
}
 
Example 19
Source File: VarArgSqlOperator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private RelDataType getNullableReturnDataType(final RelDataTypeFactory factory) {
  return factory.createTypeWithNullability(getReturnDataType(factory), true);
}
 
Example 20
Source File: SqlUnresolvedFunction.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}T
 *
 * <p>The operator class for this function isn't resolved to the
 * correct class. This happens in the case of user defined
 * functions. Return the return type to be 'ANY', so we don't
 * fail.
 */
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  return typeFactory.createTypeWithNullability(
      typeFactory.createSqlType(SqlTypeName.ANY), true);
}