Java Code Examples for org.apache.calcite.sql.type.SqlTypeName#DECIMAL

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#DECIMAL . 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: DrillAggregateRel.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
  for (AggregateCall aggCall : getAggCallList()) {
    String name = aggCall.getAggregation().getName();
    // For avg, stddev_pop, stddev_samp, var_pop and var_samp, the ReduceAggregatesRule is supposed
    // to convert them to use sum and count. Here, we make the cost of the original functions high
    // enough such that the planner does not choose them and instead chooses the rewritten functions.
    // Except when AVG, STDDEV_POP, STDDEV_SAMP, VAR_POP and VAR_SAMP are used with DECIMAL type.
    if ((name.equals(SqlKind.AVG.name())
          || name.equals(SqlKind.STDDEV_POP.name())
          || name.equals(SqlKind.STDDEV_SAMP.name())
          || name.equals(SqlKind.VAR_POP.name())
          || name.equals(SqlKind.VAR_SAMP.name()))
        && aggCall.getType().getSqlTypeName() != SqlTypeName.DECIMAL) {
      return planner.getCostFactory().makeHugeCost();
    }
  }

  return computeLogicalAggCost(planner, mq);
}
 
Example 2
Source File: FlinkReturnTypes.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	final RelDataType numType = opBinding.getOperandType(0);
	if (numType.getSqlTypeName() != SqlTypeName.DECIMAL) {
		return numType;
	}
	final BigDecimal lenVal;
	if (opBinding.getOperandCount() == 1) {
		lenVal = BigDecimal.ZERO;
	} else if (opBinding.getOperandCount() == 2) {
		lenVal = getArg1Literal(opBinding); // may return null
	} else {
		throw new AssertionError();
	}
	if (lenVal == null) {
		return numType; //
	}
	// ROUND( decimal(p,s), r )
	final int p = numType.getPrecision();
	final int s = numType.getScale();
	final int r = lenVal.intValueExact();
	DecimalType dt = FlinkTypeSystem.inferRoundType(p, s, r);
	return opBinding.getTypeFactory().createSqlType(
		SqlTypeName.DECIMAL, dt.getPrecision(), dt.getScale());
}
 
Example 3
Source File: SqlHandlerUtil.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Create arrow field from sql column declaration
 *
 * @param config
 * @param column
 * @return
 */
public static Field fieldFromSqlColDeclaration(SqlHandlerConfig config, SqlColumnDeclaration column, String sql) {
  if (SqlTypeName.get(column.getDataType().getTypeName().getSimple()) == null) {
    throw SqlExceptionHelper.parseError(String.format("Invalid column type [%s] specified for column [%s].",
        column.getDataType(), column.getName().getSimple()),
        sql, column.getParserPosition()).buildSilently();
  }

  if (SqlTypeName.get(column.getDataType().getTypeName().getSimple()) == SqlTypeName.DECIMAL &&
      column.getDataType().getPrecision() > RelDataTypeSystemImpl.MAX_NUMERIC_PRECISION) {
    throw SqlExceptionHelper.parseError(String.format("Precision larger than %s is not supported.",
        RelDataTypeSystemImpl.MAX_NUMERIC_PRECISION), sql, column.getParserPosition()).buildSilently();
  }

  if (SqlTypeName.get(column.getDataType().getTypeName().getSimple()) == SqlTypeName.DECIMAL &&
      column.getDataType().getScale() > RelDataTypeSystemImpl.MAX_NUMERIC_SCALE) {
    throw SqlExceptionHelper.parseError(String.format("Scale larger than %s is not supported.",
        RelDataTypeSystemImpl.MAX_NUMERIC_SCALE), sql, column.getParserPosition()).buildSilently();
  }

  return CalciteArrowHelper.fieldFromCalciteRowType(column.getName().getSimple(), column.getDataType()
      .deriveType(config.getConverter().getTypeFactory())).orElseThrow(
      () -> SqlExceptionHelper.parseError(String.format("Invalid type [%s] specified for column [%s].",
          column.getDataType(), column.getName().getSimple()), sql, column.getParserPosition()).buildSilently());
}
 
Example 4
Source File: TypeCastRules.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static boolean isCastSafeFromDataTruncation(RelDataType type1, RelDataType type2) {
  switch ((type1.getSqlTypeName())) {
    case TINYINT:
    case SMALLINT:
    case INTEGER:
    case FLOAT:
    case BIGINT:
    case DOUBLE:
      // cast to decimal is allowed if target type has enough digits to the left of decimal point
      return (type2.getSqlTypeName() != SqlTypeName.DECIMAL) ||
        (type2.getPrecision() - type2.getScale() >= getMaxPrecision(type1.getSqlTypeName()));
    case DECIMAL:
      switch (type2.getSqlTypeName()) {
        case DECIMAL:
          return ( (type2.getScale() >= type1.getScale()) &&
            (type2.getPrecision() - type2.getScale() >= type1.getPrecision() - type1.getScale()));
        case FLOAT:
        case DOUBLE:
          return true;
        default:
          return false;
      }
    default:
      return true;
  }
}
 
Example 5
Source File: FlinkReturnTypes.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	final RelDataType numType = opBinding.getOperandType(0);
	if (numType.getSqlTypeName() != SqlTypeName.DECIMAL) {
		return numType;
	}
	final BigDecimal lenVal;
	if (opBinding.getOperandCount() == 1) {
		lenVal = BigDecimal.ZERO;
	} else if (opBinding.getOperandCount() == 2) {
		lenVal = getArg1Literal(opBinding); // may return null
	} else {
		throw new AssertionError();
	}
	if (lenVal == null) {
		return numType; //
	}
	// ROUND( decimal(p,s), r )
	final int p = numType.getPrecision();
	final int s = numType.getScale();
	final int r = lenVal.intValueExact();
	DecimalType dt = LogicalTypeMerging.findRoundDecimalType(p, s, r);
	return opBinding.getTypeFactory().createSqlType(
		SqlTypeName.DECIMAL, dt.getPrecision(), dt.getScale());
}
 
Example 6
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 7
Source File: FindLimit0Visitor.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * If all field types of the given node are {@link #TYPES recognized types} and honored by execution, then this
 * method returns the tree: DrillDirectScanRel(field types). Otherwise, the method returns null.
 *
 * @param rel calcite logical rel tree
 * @return drill logical rel tree
 */
public static DrillRel getDirectScanRelIfFullySchemaed(RelNode rel) {
  final List<RelDataTypeField> fieldList = rel.getRowType().getFieldList();
  final List<TypeProtos.MajorType> columnTypes = new ArrayList<>();

  for (final RelDataTypeField field : fieldList) {
    final SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
    if (!TYPES.contains(sqlTypeName)) {
      return null;
    } else {
      final TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder()
          .setMode(field.getType().isNullable() ? TypeProtos.DataMode.OPTIONAL : TypeProtos.DataMode.REQUIRED)
          .setMinorType(TypeInferenceUtils.getDrillTypeFromCalciteType(sqlTypeName));

      if (sqlTypeName == SqlTypeName.DECIMAL) {
        builder.setScale(field.getType().getScale());
        builder.setPrecision(field.getType().getPrecision());
      } else if (TypeInferenceUtils.isScalarStringType(sqlTypeName)) {
        builder.setPrecision(field.getType().getPrecision());
      }

      columnTypes.add(builder.build());
    }
  }
  final RelTraitSet traits = rel.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
  final RelDataTypeReader reader = new RelDataTypeReader(rel.getRowType().getFieldNames(), columnTypes);
  return new DrillDirectScanRel(rel.getCluster(), traits,
      new DirectGroupScan(reader, ScanStats.ZERO_RECORD_TABLE), rel.getRowType());
}
 
Example 8
Source File: SqlConverter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a call to the CAST operator, expanding if possible, and optionally
 * also preserving nullability.
 *
 * <p>Tries to expand the cast, and therefore the result may be something
 * other than a {@link org.apache.calcite.rex.RexCall} to the CAST operator, such as a
 * {@link RexLiteral} if {@code matchNullability} is false.
 *
 * @param type             Type to cast to
 * @param exp              Expression being cast
 * @param matchNullability Whether to ensure the result has the same
 *                         nullability as {@code type}
 * @return Call to CAST operator
 */
@Override
public RexNode makeCast(RelDataType type, RexNode exp, boolean matchNullability) {
  if (matchNullability) {
    return makeAbstractCast(type, exp);
  }
  // for the case when BigDecimal literal has a scale or precision
  // that differs from the value from specified RelDataType, cast cannot be removed
  // TODO: remove this code when CALCITE-1468 is fixed
  if (type.getSqlTypeName() == SqlTypeName.DECIMAL && exp instanceof RexLiteral) {
    if (type.getPrecision() < 1) {
      throw UserException.validationError()
          .message("Expected precision greater than 0, but was %s.", type.getPrecision())
          .build(logger);
    }
    if (type.getScale() > type.getPrecision()) {
      throw UserException.validationError()
          .message("Expected scale less than or equal to precision, " +
              "but was scale %s and precision %s.", type.getScale(), type.getPrecision())
          .build(logger);
    }
    RexLiteral literal = (RexLiteral) exp;
    Comparable value = literal.getValueAs(Comparable.class);
    if (value instanceof BigDecimal) {
      BigDecimal bigDecimal = (BigDecimal) value;
      DecimalUtility.checkValueOverflow(bigDecimal, type.getPrecision(), type.getScale());
      if (bigDecimal.scale() != type.getScale() || bigDecimal.precision() != type.getPrecision()) {
        return makeAbstractCast(type, exp);
      }
    }
  }
  return super.makeCast(type, exp, false);
}
 
Example 9
Source File: Schema.java    From kareldb with Apache License 2.0 5 votes vote down vote up
private static ColumnDef toColumnDef(RelDataType fieldType) {
    SqlTypeName sqlTypeName = fieldType.getSqlTypeName();
    ColumnType columnType = ColumnType.of(sqlTypeName);
    return sqlTypeName == SqlTypeName.DECIMAL
        ? new ColumnDef(columnType, fieldType.getPrecision(), fieldType.getScale())
        : new ColumnDef(columnType);
}
 
Example 10
Source File: SqlNumericLiteral.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected SqlNumericLiteral(
    BigDecimal value,
    Integer prec,
    Integer scale,
    boolean isExact,
    SqlParserPos pos) {
  super(
      value,
      isExact ? SqlTypeName.DECIMAL : SqlTypeName.DOUBLE,
      pos);
  this.prec = prec;
  this.scale = scale;
  this.isExact = isExact;
}
 
Example 11
Source File: SqlNumericLiteral.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected SqlNumericLiteral(
    BigDecimal value,
    Integer prec,
    Integer scale,
    boolean isExact,
    SqlParserPos pos) {
  super(
      value,
      isExact ? SqlTypeName.DECIMAL : SqlTypeName.DOUBLE,
      pos);
  this.prec = prec;
  this.scale = scale;
  this.isExact = isExact;
}
 
Example 12
Source File: MaterializationExpander.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static boolean isSumAggOutput(RelDataType type1, RelDataType type2) {
  if (type1.getSqlTypeName() == SqlTypeName
    .DECIMAL && type2.getSqlTypeName() == SqlTypeName.DECIMAL) {
    // output of sum aggregation is always 38,inputScale
    return type1.getPrecision() == 38 && type1.getScale() == type2.getScale();
  }
  return false;
}
 
Example 13
Source File: IcebergCatalog.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private String sqlTypeNameWithPrecisionAndScale(Type type) {
  CompleteType completeType = SchemaConverter.fromIcebergType(type);
  SqlTypeName calciteTypeFromMinorType = CalciteArrowHelper.getCalciteTypeFromMinorType(completeType.toMinorType());
  if (calciteTypeFromMinorType == SqlTypeName.DECIMAL) {
    return calciteTypeFromMinorType + "(" + completeType.getPrecision() + ", " + completeType.getScale() + ")";
  }
  return calciteTypeFromMinorType.toString();
}
 
Example 14
Source File: SQLExpressionCompiler.java    From herddb with Apache License 2.0 5 votes vote down vote up
private static Object safeValue(Object value3, RelDataType relDataType, SqlTypeName sqlTypeName) {
    if (value3 instanceof BigDecimal) {
        if (relDataType instanceof BasicSqlType) {
            sqlTypeName = relDataType.getSqlTypeName();
        }
        if (sqlTypeName == SqlTypeName.DECIMAL) {
            return ((BigDecimal) value3).doubleValue();
        }
        return ((BigDecimal) value3).longValue();
    }
    return value3;
}
 
Example 15
Source File: RexUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns whether a value of {@code type2} can be assigned to a variable
 * of {@code type1}.
 *
 * <p>For example:
 * <ul>
 *   <li>{@code canAssignFrom(BIGINT, TINYINT)} returns {@code true}</li>
 *   <li>{@code canAssignFrom(TINYINT, BIGINT)} returns {@code false}</li>
 *   <li>{@code canAssignFrom(BIGINT, VARCHAR)} returns {@code false}</li>
 * </ul>
 */
private static boolean canAssignFrom(RelDataType type1, RelDataType type2,
    RelDataTypeFactory typeFactory) {
  final SqlTypeName name1 = type1.getSqlTypeName();
  final SqlTypeName name2 = type2.getSqlTypeName();
  if (name1.getFamily() == name2.getFamily()) {
    switch (name1.getFamily()) {
    case NUMERIC:
      if (SqlTypeUtil.isExactNumeric(type1)
          && SqlTypeUtil.isExactNumeric(type2)) {
        int precision1;
        int scale1;
        if (name1 == SqlTypeName.DECIMAL) {
          type1 = typeFactory.decimalOf(type1);
          precision1 = type1.getPrecision();
          scale1 = type1.getScale();
        } else {
          precision1 = typeFactory.getTypeSystem().getMaxPrecision(name1);
          scale1 = typeFactory.getTypeSystem().getMaxScale(name1);
        }
        int precision2;
        int scale2;
        if (name2 == SqlTypeName.DECIMAL) {
          type2 = typeFactory.decimalOf(type2);
          precision2 = type2.getPrecision();
          scale2 = type2.getScale();
        } else {
          precision2 = typeFactory.getTypeSystem().getMaxPrecision(name2);
          scale2 = typeFactory.getTypeSystem().getMaxScale(name2);
        }
        return precision1 >= precision2
            && scale1 >= scale2;
      } else if (SqlTypeUtil.isApproximateNumeric(type1)
          && SqlTypeUtil.isApproximateNumeric(type2)) {
        return type1.getPrecision() >= type2.getPrecision()
            && type1.getScale() >= type2.getScale();
      }
      break;
    default:
      // getPrecision() will return:
      // - number of decimal digits for fractional seconds for datetime types
      // - length in characters for character types
      // - length in bytes for binary types
      // - RelDataType.PRECISION_NOT_SPECIFIED (-1) if not applicable for this type
      return type1.getPrecision() >= type2.getPrecision();
    }
  }
  return false;
}
 
Example 16
Source File: MaterializationExpander.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Compare row types ignoring field names, nullability, ANY and CHAR/VARCHAR types
 */
@VisibleForTesting
static boolean areRowTypesEqual(RelDataType rowType1, RelDataType rowType2) {
    if (rowType1 == rowType2) {
      return true;
    }

    if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
      return false;
    }

    final List<RelDataTypeField> f1 = rowType1.getFieldList(); // materialized field
    final List<RelDataTypeField> f2 = rowType2.getFieldList(); // original materialization query field
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
      // remove nullability
      final RelDataType type1 = JavaTypeFactoryImpl.INSTANCE.createTypeWithNullability(pair.left.getType(), false);
      final RelDataType type2 = JavaTypeFactoryImpl.INSTANCE.createTypeWithNullability(pair.right.getType(), false);

      // are types equal ?
      if (type1.equals(type2)) {
        continue;
      }

      // ignore ANY types
      if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) {
        continue;
      }

      // are both types from the CHARACTER family ?
      if (type1.getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER &&
          type2.getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
        continue;
      }

      // safely ignore when materialized field is DOUBLE instead of DECIMAL
      if (type1.getSqlTypeName() == SqlTypeName.DOUBLE && type2.getSqlTypeName() == SqlTypeName
        .DECIMAL || isSumAggOutput(type1, type2)) {
        continue;
      }

      return false;
    }

    return true;
}
 
Example 17
Source File: TypeInferenceUtil.java    From marble with Apache License 2.0 4 votes vote down vote up
public static RelDataType getRelDataType(ObjectInspector oi,
      RelDataTypeFactory factory) {
    if (oi == null) {
      return factory.createTypeWithNullability(
          factory.createSqlType(SqlTypeName.ANY), true);
    }
    ObjectInspector.Category category = oi.getCategory();
    if (!category.equals(PRIMITIVE)) {
      if (category.equals(ObjectInspector.Category.LIST)) {
        StandardListObjectInspector standardListObjectInspector =
            (StandardListObjectInspector) oi;
        return factory.createArrayType(
            getRelDataType(
                standardListObjectInspector.getListElementObjectInspector(),
                factory), -1);
      } else {
        throw new IllegalArgumentException(
            "unsupported ObjectInspector category :" + category);
      }

    }
    PrimitiveObjectInspector primitiveObjectInspector =
        (PrimitiveObjectInspector) oi;
    PrimitiveTypeInfo primitiveTypeInfo =
        primitiveObjectInspector.getTypeInfo();

    SqlTypeName sqlTypeName = HIVE_TYPE_2_CALCITE_SQL_TYPE_INFO.get(
        primitiveTypeInfo);
    //handle DecimalTypeInfo special case
    if (primitiveTypeInfo instanceof DecimalTypeInfo) {
      sqlTypeName = SqlTypeName.DECIMAL;
//      return factory.createTypeWithNullability(
//          factory.createSqlType(
//              sqlTypeName, ((DecimalTypeInfo) primitiveTypeInfo).getPrecision(),
//              ((DecimalTypeInfo) primitiveTypeInfo).getScale()),
//          true);
    }
    if (sqlTypeName == null) {
      throw new RuntimeException(
          "can't get sqlType for hive primitiveTypeInfo:" + primitiveTypeInfo);
    }
    return factory.createTypeWithNullability(
        factory.createSqlType(
            sqlTypeName),
        true);

  }