org.apache.flink.table.types.logical.DecimalType Java Examples

The following examples show how to use org.apache.flink.table.types.logical.DecimalType. 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: 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 #2
Source File: AvroRowDataDeserializationSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
private static DeserializationRuntimeConverter createDecimalConverter(DecimalType decimalType) {
	final int precision = decimalType.getPrecision();
	final int scale = decimalType.getScale();
	return avroObject -> {
		final byte[] bytes;
		if (avroObject instanceof GenericFixed) {
			bytes = ((GenericFixed) avroObject).bytes();
		} else if (avroObject instanceof ByteBuffer) {
			ByteBuffer byteBuffer = (ByteBuffer) avroObject;
			bytes = new byte[byteBuffer.remaining()];
			byteBuffer.get(bytes);
		} else {
			bytes = (byte[]) avroObject;
		}
		return DecimalData.fromUnscaledBytes(bytes, precision, scale);
	};
}
 
Example #3
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 #4
Source File: KuduTableUtils.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
public static List<ColumnSchema> toKuduConnectorColumns(List<Tuple2<String, DataType>> columns, Collection<String> keyColumns) {
    return columns.stream()
            .map(t -> {
                        ColumnSchema.ColumnSchemaBuilder builder = new ColumnSchema
                                .ColumnSchemaBuilder(t.f0, KuduTypeUtils.toKuduType(t.f1))
                                .key(keyColumns.contains(t.f0))
                                .nullable(!keyColumns.contains(t.f0) && t.f1.getLogicalType().isNullable());
                        if(t.f1.getLogicalType() instanceof DecimalType) {
                            DecimalType decimalType = ((DecimalType) t.f1.getLogicalType());
                            builder.typeAttributes(new ColumnTypeAttributes.ColumnTypeAttributesBuilder()
                                .precision(decimalType.getPrecision())
                                .scale(decimalType.getScale())
                                .build());
                        }
                        return builder.build();
                    }
            ).collect(Collectors.toList());
}
 
Example #5
Source File: FlinkCalciteSqlValidator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void validateLiteral(SqlLiteral literal) {
	if (literal.getTypeName() == DECIMAL) {
		final BigDecimal decimal = literal.getValueAs(BigDecimal.class);
		if (decimal.precision() > DecimalType.MAX_PRECISION) {
			throw newValidationError(
				literal,
				Static.RESOURCE.numberLiteralOutOfRange(decimal.toString()));
		}
	}
	super.validateLiteral(literal);
}
 
Example #6
Source File: LogicalTypeGeneralization.java    From flink with Apache License 2.0 5 votes vote down vote up
private static LogicalType createCommonExactNumericType(LogicalType resultType, LogicalType type) {
	// same EXACT_NUMERIC types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();

	// no DECIMAL types involved
	if (resultTypeRoot != DECIMAL && typeRoot != DECIMAL) {
		// type root contains order of precision
		if (getPrecision(type) > getPrecision(resultType)) {
			return type;
		}
		return resultType;
	}

	// determine DECIMAL with precision (p), scale (s) and number of whole digits (d):
	// d = max(p1 - s1, p2 - s2)
	// s <= max(s1, s2)
	// p = s + d
	final int p1 = getPrecision(resultType);
	final int p2 = getPrecision(type);
	final int s1 = getScale(resultType);
	final int s2 = getScale(type);
	final int maxPrecision = DecimalType.MAX_PRECISION;

	int d = Math.max(p1 - s1, p2 - s2);
	d = Math.min(d, maxPrecision);

	int s = Math.max(s1, s2);
	s = Math.min(s, maxPrecision - d);

	final int p = d + s;

	return new DecimalType(p, s);
}
 
Example #7
Source File: CsvRowDataSerializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private RowFieldConverter createDecimalRowFieldConverter(DecimalType decimalType) {
	final int precision = decimalType.getPrecision();
	final int scale = decimalType.getScale();
	return (csvMapper, container, row, pos) -> {
		DecimalData decimal = row.getDecimal(pos, precision, scale);
		return convertDecimal(decimal, container);
	};
}
 
Example #8
Source File: LogicalTypesTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private DistinctType createDistinctType(String typeName) {
	return new DistinctType.Builder(
			ObjectIdentifier.of("cat", "db", typeName),
			new DecimalType(10, 2))
		.setDescription("Money type desc.")
		.build();
}
 
Example #9
Source File: LogicalTypesTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistinctType() {
	testAll(
		createDistinctType("Money"),
		"`cat`.`db`.`Money`",
		"`cat`.`db`.`Money`",
		new Class[]{BigDecimal.class},
		new Class[]{BigDecimal.class},
		new LogicalType[]{new DecimalType(10, 2)},
		createDistinctType("Monetary")
	);
}
 
Example #10
Source File: AbstractOrcColumnVector.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create a orc vector from partition spec value.
 * See hive {@code VectorizedRowBatchCtx#addPartitionColsToBatch}.
 */
private static ColumnVector createHiveVectorFromConstant(
		LogicalType type, Object value, int batchSize) {
	switch (type.getTypeRoot()) {
		case CHAR:
		case VARCHAR:
		case BINARY:
		case VARBINARY:
			return createBytesVector(batchSize, value);
		case BOOLEAN:
			return createLongVector(batchSize, (Boolean) value ? 1 : 0);
		case TINYINT:
		case SMALLINT:
		case INTEGER:
		case BIGINT:
			return createLongVector(batchSize, value);
		case DECIMAL:
			DecimalType decimalType = (DecimalType) type;
			return createDecimalVector(
					batchSize, decimalType.getPrecision(), decimalType.getScale(), value);
		case FLOAT:
		case DOUBLE:
			return createDoubleVector(batchSize, value);
		case DATE:
			if (value instanceof LocalDate) {
				value = Date.valueOf((LocalDate) value);
			}
			return createLongVector(batchSize, dateToInternal((Date) value));
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			return createTimestampVector(batchSize, value);
		default:
			throw new UnsupportedOperationException("Unsupported type: " + type);
	}
}
 
Example #11
Source File: SortCodeGeneratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private Object value3(LogicalType type, Random rnd) {
	switch (type.getTypeRoot()) {
		case BOOLEAN:
			return true;
		case TINYINT:
			return Byte.MAX_VALUE;
		case SMALLINT:
			return Short.MAX_VALUE;
		case INTEGER:
			return Integer.MAX_VALUE;
		case BIGINT:
			return Long.MAX_VALUE;
		case FLOAT:
			return Float.MAX_VALUE;
		case DOUBLE:
			return Double.MAX_VALUE;
		case VARCHAR:
			return BinaryString.fromString(RandomStringUtils.random(100));
		case DECIMAL:
			DecimalType decimalType = (DecimalType) type;
			return Decimal.fromBigDecimal(new BigDecimal(Integer.MAX_VALUE),
					decimalType.getPrecision(), decimalType.getScale());
		case ARRAY:
		case VARBINARY:
			byte[] bytes = new byte[rnd.nextInt(100) + 100];
			rnd.nextBytes(bytes);
			return type instanceof VarBinaryType ? bytes : BinaryArray.fromPrimitiveArray(bytes);
		case ROW:
			RowType rowType = (RowType) type;
			if (rowType.getFields().get(0).getType().getTypeRoot() == INTEGER) {
				return GenericRow.of(rnd.nextInt());
			} else {
				return GenericRow.of(GenericRow.of(rnd.nextInt()));
			}
		case ANY:
			return new BinaryGeneric<>(rnd.nextInt(), IntSerializer.INSTANCE);
		default:
			throw new RuntimeException("Not support!");
	}
}
 
Example #12
Source File: FlinkTypeToType.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("checkstyle:CyclomaticComplexity")
@Override
public Type atomic(AtomicDataType type) {
  LogicalType inner = type.getLogicalType();
  if (inner instanceof VarCharType ||
      inner instanceof CharType) {
    return Types.StringType.get();
  } else if (inner instanceof BooleanType) {
    return Types.BooleanType.get();
  } else if (inner instanceof IntType ||
      inner instanceof SmallIntType ||
      inner instanceof TinyIntType) {
    return Types.IntegerType.get();
  } else if (inner instanceof BigIntType) {
    return Types.LongType.get();
  } else if (inner instanceof VarBinaryType) {
    return Types.BinaryType.get();
  } else if (inner instanceof BinaryType) {
    BinaryType binaryType = (BinaryType) inner;
    return Types.FixedType.ofLength(binaryType.getLength());
  } else if (inner instanceof FloatType) {
    return Types.FloatType.get();
  } else if (inner instanceof DoubleType) {
    return Types.DoubleType.get();
  } else if (inner instanceof DateType) {
    return Types.DateType.get();
  } else if (inner instanceof TimeType) {
    return Types.TimeType.get();
  } else if (inner instanceof TimestampType) {
    return Types.TimestampType.withoutZone();
  } else if (inner instanceof LocalZonedTimestampType) {
    return Types.TimestampType.withZone();
  } else if (inner instanceof DecimalType) {
    DecimalType decimalType = (DecimalType) inner;
    return Types.DecimalType.of(decimalType.getPrecision(), decimalType.getScale());
  } else {
    throw new UnsupportedOperationException("Not a supported type: " + type.toString());
  }
}
 
Example #13
Source File: HBaseSerde.java    From flink with Apache License 2.0 5 votes vote down vote up
private static FieldEncoder createDecimalEncoder(DecimalType decimalType) {
	final int precision = decimalType.getPrecision();
	final int scale = decimalType.getScale();
	return (row, pos) -> {
		BigDecimal decimal = row.getDecimal(pos, precision, scale).toBigDecimal();
		return Bytes.toBytes(decimal);
	};
}
 
Example #14
Source File: LegacyDecimalTypeTransformation.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DataType transform(DataType typeToTransform) {
	LogicalType logicalType = typeToTransform.getLogicalType();
	if (logicalType instanceof LegacyTypeInformationType && logicalType.getTypeRoot() == LogicalTypeRoot.DECIMAL) {
		DataType decimalType = DataTypes
			.DECIMAL(DecimalType.MAX_PRECISION, 18)
			.bridgedTo(typeToTransform.getConversionClass());
		return logicalType.isNullable() ? decimalType : decimalType.notNull();
	}
	return typeToTransform;
}
 
Example #15
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the result type of a decimal division operation.
 */
public static DecimalType findDivisionDecimalType(int precision1, int scale1, int precision2, int scale2) {
	// adopted from https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql
	int scale = Math.max(6, scale1 + precision2 + 1);
	int precision = precision1 - scale1 + scale2 + scale;
	if (precision > DecimalType.MAX_PRECISION) {
		scale = Math.max(6, DecimalType.MAX_PRECISION - (precision - scale));
		precision = DecimalType.MAX_PRECISION;
	}
	return new DecimalType(false, precision, scale);
}
 
Example #16
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the result type of a decimal modulo operation.
 */
public static DecimalType findModuloDecimalType(int precision1, int scale1, int precision2, int scale2) {
	// adopted from Calcite
	final int scale = Math.max(scale1, scale2);
	int precision = Math.min(precision1 - scale1, precision2 - scale2) + Math.max(scale1, scale2);
	precision = Math.min(precision, DecimalType.MAX_PRECISION);
	return new DecimalType(false, precision, scale);
}
 
Example #17
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the result type of a decimal multiplication operation.
 */
public static DecimalType findMultiplicationDecimalType(int precision1, int scale1, int precision2, int scale2) {
	// adopted from Calcite
	int scale = scale1 + scale2;
	scale = Math.min(scale, DecimalType.MAX_PRECISION);
	int precision = precision1 + precision2;
	precision = Math.min(precision, DecimalType.MAX_PRECISION);
	return new DecimalType(false, precision, scale);
}
 
Example #18
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the result type of a decimal addition operation.
 */
public static DecimalType findAdditionDecimalType(int precision1, int scale1, int precision2, int scale2) {
	// adopted from Calcite
	final int scale = Math.max(scale1, scale2);
	int precision = Math.max(precision1 - scale1, precision2 - scale2) + scale + 1;
	precision = Math.min(precision, DecimalType.MAX_PRECISION);
	return new DecimalType(false, precision, scale);
}
 
Example #19
Source File: PythonTypeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public FlinkFnApi.Schema.FieldType visit(DecimalType decimalType) {
	FlinkFnApi.Schema.FieldType.Builder builder =
		FlinkFnApi.Schema.FieldType.newBuilder()
			.setTypeName(FlinkFnApi.Schema.TypeName.DECIMAL)
			.setNullable(decimalType.isNullable());

	FlinkFnApi.Schema.DecimalInfo.Builder decimalInfoBuilder =
		FlinkFnApi.Schema.DecimalInfo.newBuilder()
			.setPrecision(decimalType.getPrecision())
			.setScale(decimalType.getScale());
	builder.setDecimalInfo(decimalInfoBuilder);
	return builder.build();
}
 
Example #20
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the result type of a decimal rounding operation.
 */
public static DecimalType findRoundDecimalType(int precision, int scale, int round) {
	if (round >= scale) {
		return new DecimalType(false, precision, scale);
	}
	if (round < 0) {
		return new DecimalType(false, Math.min(DecimalType.MAX_PRECISION, 1 + precision - scale), 0);
	}
	// 0 <= r < s
	// NOTE: rounding may increase the digits by 1, therefore we need +1 on precisions.
	return new DecimalType(false, 1 + precision - scale + round, round);
}
 
Example #21
Source File: SortCodeGeneratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private Object value2(LogicalType type, Random rnd) {
	switch (type.getTypeRoot()) {
		case BOOLEAN:
			return false;
		case TINYINT:
			return (byte) 0;
		case SMALLINT:
			return (short) 0;
		case INTEGER:
			return 0;
		case BIGINT:
			return 0L;
		case FLOAT:
			return 0f;
		case DOUBLE:
			return 0d;
		case VARCHAR:
			return BinaryString.fromString("0");
		case DECIMAL:
			DecimalType decimalType = (DecimalType) type;
			return Decimal.fromBigDecimal(new BigDecimal(0),
					decimalType.getPrecision(), decimalType.getScale());
		case ARRAY:
		case VARBINARY:
			byte[] bytes = new byte[rnd.nextInt(7) + 10];
			rnd.nextBytes(bytes);
			return type instanceof VarBinaryType ? bytes : BinaryArray.fromPrimitiveArray(bytes);
		case ROW:
			RowType rowType = (RowType) type;
			if (rowType.getFields().get(0).getType().getTypeRoot() == INTEGER) {
				return GenericRow.of(rnd.nextInt());
			} else {
				return GenericRow.of(GenericRow.of(new Object[]{null}));
			}
		case ANY:
			return new BinaryGeneric<>(rnd.nextInt(), IntSerializer.INSTANCE);
		default:
			throw new RuntimeException("Not support!");
	}
}
 
Example #22
Source File: LogicalTypeMerging.java    From flink with Apache License 2.0 5 votes vote down vote up
private static LogicalType createCommonExactNumericType(LogicalType resultType, LogicalType type) {
	// same EXACT_NUMERIC types
	if (type.equals(resultType)) {
		return resultType;
	}

	final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot();
	final LogicalTypeRoot typeRoot = type.getTypeRoot();

	// no DECIMAL types involved
	if (resultTypeRoot != DECIMAL && typeRoot != DECIMAL) {
		// type root contains order of precision
		if (getPrecision(type) > getPrecision(resultType)) {
			return type;
		}
		return resultType;
	}

	// determine DECIMAL with precision (p), scale (s) and number of whole digits (d):
	// d = max(p1 - s1, p2 - s2)
	// s <= max(s1, s2)
	// p = s + d
	final int p1 = getPrecision(resultType);
	final int p2 = getPrecision(type);
	final int s1 = getScale(resultType);
	final int s2 = getScale(type);
	final int maxPrecision = DecimalType.MAX_PRECISION;

	int d = Math.max(p1 - s1, p2 - s2);
	d = Math.min(d, maxPrecision);

	int s = Math.max(s1, s2);
	s = Math.min(s, maxPrecision - d);

	final int p = d + s;

	return new DecimalType(p, s);
}
 
Example #23
Source File: LogicalTypesTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecimalType() {
	testAll(
		new DecimalType(10, 2),
		"DECIMAL(10, 2)",
		"DECIMAL(10, 2)",
		new Class[]{BigDecimal.class},
		new Class[]{BigDecimal.class},
		new LogicalType[]{},
		new DecimalType()
	);
}
 
Example #24
Source File: BinaryWriter.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * @deprecated Use {@link #createValueSetter(LogicalType)} for avoiding logical types during runtime.
 */
@Deprecated
static void write(
		BinaryWriter writer, int pos, Object o, LogicalType type, TypeSerializer<?> serializer) {
	switch (type.getTypeRoot()) {
		case BOOLEAN:
			writer.writeBoolean(pos, (boolean) o);
			break;
		case TINYINT:
			writer.writeByte(pos, (byte) o);
			break;
		case SMALLINT:
			writer.writeShort(pos, (short) o);
			break;
		case INTEGER:
		case DATE:
		case TIME_WITHOUT_TIME_ZONE:
		case INTERVAL_YEAR_MONTH:
			writer.writeInt(pos, (int) o);
			break;
		case BIGINT:
		case INTERVAL_DAY_TIME:
			writer.writeLong(pos, (long) o);
			break;
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			TimestampType timestampType = (TimestampType) type;
			writer.writeTimestamp(pos, (TimestampData) o, timestampType.getPrecision());
			break;
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			LocalZonedTimestampType lzTs = (LocalZonedTimestampType) type;
			writer.writeTimestamp(pos, (TimestampData) o, lzTs.getPrecision());
			break;
		case FLOAT:
			writer.writeFloat(pos, (float) o);
			break;
		case DOUBLE:
			writer.writeDouble(pos, (double) o);
			break;
		case CHAR:
		case VARCHAR:
			writer.writeString(pos, (StringData) o);
			break;
		case DECIMAL:
			DecimalType decimalType = (DecimalType) type;
			writer.writeDecimal(pos, (DecimalData) o, decimalType.getPrecision());
			break;
		case ARRAY:
			writer.writeArray(pos, (ArrayData) o, (ArrayDataSerializer) serializer);
			break;
		case MAP:
		case MULTISET:
			writer.writeMap(pos, (MapData) o, (MapDataSerializer) serializer);
			break;
		case ROW:
		case STRUCTURED_TYPE:
			writer.writeRow(pos, (RowData) o, (RowDataSerializer) serializer);
			break;
		case RAW:
			writer.writeRawValue(pos, (RawValueData<?>) o, (RawValueDataSerializer<?>) serializer);
			break;
		case BINARY:
		case VARBINARY:
			writer.writeBinary(pos, (byte[]) o);
			break;
		default:
			throw new UnsupportedOperationException("Not support type: " + type);
	}
}
 
Example #25
Source File: LogicalTypeChecks.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public Integer visit(DecimalType decimalType) {
	return decimalType.getPrecision();
}
 
Example #26
Source File: RowData.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the field object in the internal row data structure at the given position.
 *
 * @param row the internal row data
 * @param pos position of the field to return
 * @param fieldType the field type
 * @return the field object at the specified position in this row data.
 * @deprecated Use {@link #createFieldGetter(LogicalType, int)} for avoiding logical types during runtime.
 */
static Object get(RowData row, int pos, LogicalType fieldType) {
	if (row.isNullAt(pos)) {
		return null;
	}
	switch (fieldType.getTypeRoot()) {
		case BOOLEAN:
			return row.getBoolean(pos);
		case TINYINT:
			return row.getByte(pos);
		case SMALLINT:
			return row.getShort(pos);
		case INTEGER:
		case DATE:
		case TIME_WITHOUT_TIME_ZONE:
		case INTERVAL_YEAR_MONTH:
			return row.getInt(pos);
		case BIGINT:
		case INTERVAL_DAY_TIME:
			return row.getLong(pos);
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			TimestampType timestampType = (TimestampType) fieldType;
			return row.getTimestamp(pos, timestampType.getPrecision());
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			LocalZonedTimestampType lzTs = (LocalZonedTimestampType) fieldType;
			return row.getTimestamp(pos, lzTs.getPrecision());
		case FLOAT:
			return row.getFloat(pos);
		case DOUBLE:
			return row.getDouble(pos);
		case CHAR:
		case VARCHAR:
			return row.getString(pos);
		case DECIMAL:
			DecimalType decimalType = (DecimalType) fieldType;
			return row.getDecimal(pos, decimalType.getPrecision(), decimalType.getScale());
		case ARRAY:
			return row.getArray(pos);
		case MAP:
		case MULTISET:
			return row.getMap(pos);
		case ROW:
			return row.getRow(pos, ((RowType) fieldType).getFieldCount());
		case STRUCTURED_TYPE:
			// not the most efficient code but ok for a deprecated method
			return row.getRow(pos, getFieldCount(fieldType));
		case BINARY:
		case VARBINARY:
			return row.getBinary(pos);
		case RAW:
			return row.getRawValue(pos);
		default:
			throw new UnsupportedOperationException("Unsupported type: " + fieldType);
	}
}
 
Example #27
Source File: ArrayData.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the element object in the internal array data structure at the given position.
 *
 * @param array the internal array data
 * @param pos position of the element to return
 * @param elementType the element type of the array
 * @return the element object at the specified position in this array data
 * @deprecated Use {@link #createElementGetter(LogicalType)} for avoiding logical types during runtime.
 */
@Deprecated
static Object get(ArrayData array, int pos, LogicalType elementType) {
	if (array.isNullAt(pos)) {
		return null;
	}
	switch (elementType.getTypeRoot()) {
		case BOOLEAN:
			return array.getBoolean(pos);
		case TINYINT:
			return array.getByte(pos);
		case SMALLINT:
			return array.getShort(pos);
		case INTEGER:
		case DATE:
		case TIME_WITHOUT_TIME_ZONE:
		case INTERVAL_YEAR_MONTH:
			return array.getInt(pos);
		case BIGINT:
		case INTERVAL_DAY_TIME:
			return array.getLong(pos);
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			TimestampType timestampType = (TimestampType) elementType;
			return array.getTimestamp(pos, timestampType.getPrecision());
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			LocalZonedTimestampType lzTs = (LocalZonedTimestampType) elementType;
			return array.getTimestamp(pos, lzTs.getPrecision());
		case FLOAT:
			return array.getFloat(pos);
		case DOUBLE:
			return array.getDouble(pos);
		case CHAR:
		case VARCHAR:
			return array.getString(pos);
		case DECIMAL:
			DecimalType decimalType = (DecimalType) elementType;
			return array.getDecimal(pos, decimalType.getPrecision(), decimalType.getScale());
		case ARRAY:
			return array.getArray(pos);
		case MAP:
		case MULTISET:
			return array.getMap(pos);
		case ROW:
			return array.getRow(pos, ((RowType) elementType).getFieldCount());
		case STRUCTURED_TYPE:
			// not the most efficient code but ok for a deprecated method
			return array.getRow(pos, getFieldCount(elementType));
		case BINARY:
		case VARBINARY:
			return array.getBinary(pos);
		case RAW:
			return array.getRawValue(pos);
		default:
			throw new UnsupportedOperationException("Unsupported type: " + elementType);
	}
}
 
Example #28
Source File: PrintUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Try to derive column width based on column types.
 * If result set is not small enough to be stored in java heap memory,
 * we can't determine column widths based on column values.
 */
public static int[] columnWidthsByType(
		List<TableColumn> columns,
		int maxColumnWidth,
		String nullColumn,
		@Nullable String rowKindColumn) {
	// fill width with field names first
	final int[] colWidths = columns.stream()
			.mapToInt(col -> col.getName().length())
			.toArray();

	// determine proper column width based on types
	for (int i = 0; i < columns.size(); ++i) {
		LogicalType type = columns.get(i).getType().getLogicalType();
		int len;
		switch (type.getTypeRoot()) {
			case TINYINT:
				len = TinyIntType.PRECISION + 1; // extra for negative value
				break;
			case SMALLINT:
				len = SmallIntType.PRECISION + 1; // extra for negative value
				break;
			case INTEGER:
				len = IntType.PRECISION + 1; // extra for negative value
				break;
			case BIGINT:
				len = BigIntType.PRECISION + 1; // extra for negative value
				break;
			case DECIMAL:
				len = ((DecimalType) type).getPrecision() + 2; // extra for negative value and decimal point
				break;
			case BOOLEAN:
				len = 5; // "true" or "false"
				break;
			case DATE:
				len = 10; // e.g. 9999-12-31
				break;
			case TIME_WITHOUT_TIME_ZONE:
				int precision = ((TimeType) type).getPrecision();
				len = precision == 0 ? 8 : precision + 9; // 23:59:59[.999999999]
				break;
			case TIMESTAMP_WITHOUT_TIME_ZONE:
				precision = ((TimestampType) type).getPrecision();
				len = timestampTypeColumnWidth(precision);
				break;
			case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
				precision = ((LocalZonedTimestampType) type).getPrecision();
				len = timestampTypeColumnWidth(precision);
				break;
			default:
				len = maxColumnWidth;
		}

		// adjust column width with potential null values
		colWidths[i] = Math.max(colWidths[i], Math.max(len, nullColumn.length()));
	}

	// add an extra column for row kind if necessary
	if (rowKindColumn != null) {
		final int[] ret = new int[columns.size() + 1];
		ret[0] = rowKindColumn.length();
		System.arraycopy(colWidths, 0, ret, 1, columns.size());
		return ret;
	} else {
		return colWidths;
	}
}
 
Example #29
Source File: LogicalTypeDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public DataType visit(DecimalType decimalType) {
	return new AtomicDataType(decimalType);
}
 
Example #30
Source File: ParquetSplitReaderUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
public static ColumnReader createColumnReader(
		boolean utcTimestamp,
		LogicalType fieldType,
		ColumnDescriptor descriptor,
		PageReader pageReader) throws IOException {
	switch (fieldType.getTypeRoot()) {
		case BOOLEAN:
			return new BooleanColumnReader(descriptor, pageReader);
		case TINYINT:
			return new ByteColumnReader(descriptor, pageReader);
		case DOUBLE:
			return new DoubleColumnReader(descriptor, pageReader);
		case FLOAT:
			return new FloatColumnReader(descriptor, pageReader);
		case INTEGER:
		case DATE:
		case TIME_WITHOUT_TIME_ZONE:
			return new IntColumnReader(descriptor, pageReader);
		case BIGINT:
			return new LongColumnReader(descriptor, pageReader);
		case SMALLINT:
			return new ShortColumnReader(descriptor, pageReader);
		case CHAR:
		case VARCHAR:
		case BINARY:
		case VARBINARY:
			return new BytesColumnReader(descriptor, pageReader);
		case TIMESTAMP_WITHOUT_TIME_ZONE:
		case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
			return new TimestampColumnReader(utcTimestamp, descriptor, pageReader);
		case DECIMAL:
			switch (descriptor.getPrimitiveType().getPrimitiveTypeName()) {
				case INT32:
					return new IntColumnReader(descriptor, pageReader);
				case INT64:
					return new LongColumnReader(descriptor, pageReader);
				case BINARY:
					return new BytesColumnReader(descriptor, pageReader);
				case FIXED_LEN_BYTE_ARRAY:
					return new FixedLenBytesColumnReader(
							descriptor, pageReader, ((DecimalType) fieldType).getPrecision());
			}
		default:
			throw new UnsupportedOperationException(fieldType + " is not supported now.");
	}
}