Support conversion for postgres numeric without precision and scale

This commit is contained in:
kssenii 2021-02-18 12:16:58 +00:00
parent 80e6ad7aae
commit 2aad067e7c
2 changed files with 27 additions and 16 deletions

View File

@ -54,19 +54,30 @@ static DataTypePtr convertPostgreSQLDataType(std::string & type, bool is_nullabl
res = std::make_shared<DataTypeDate>();
else if (type.starts_with("numeric"))
{
/// Numeric and decimal will both end up here as numeric.
res = DataTypeFactory::instance().get(type);
uint32_t precision = getDecimalPrecision(*res);
uint32_t scale = getDecimalScale(*res);
/// Numeric and decimal will both end up here as numeric. If it has type and precision,
/// there will be Numeric(x, y), otherwise just Numeric
uint32_t precision, scale;
if (type.ends_with(")"))
{
res = DataTypeFactory::instance().get(type);
precision = getDecimalPrecision(*res);
scale = getDecimalScale(*res);
if (precision <= DecimalUtils::maxPrecision<Decimal32>())
res = std::make_shared<DataTypeDecimal<Decimal32>>(precision, scale);
else if (precision <= DecimalUtils::maxPrecision<Decimal64>())
res = std::make_shared<DataTypeDecimal<Decimal64>>(precision, scale);
else if (precision <= DecimalUtils::maxPrecision<Decimal128>())
res = std::make_shared<DataTypeDecimal<Decimal128>>(precision, scale);
else if (precision <= DecimalUtils::maxPrecision<Decimal256>())
res = std::make_shared<DataTypeDecimal<Decimal256>>(precision, scale);
}
else
{
precision = DecimalUtils::maxPrecision<Decimal128>();
res = std::make_shared<DataTypeDecimal<Decimal128>>(precision, precision);
}
if (precision <= DecimalUtils::maxPrecision<Decimal32>())
res = std::make_shared<DataTypeDecimal<Decimal32>>(precision, scale);
else if (precision <= DecimalUtils::maxPrecision<Decimal64>())
res = std::make_shared<DataTypeDecimal<Decimal64>>(precision, scale);
else if (precision <= DecimalUtils::maxPrecision<Decimal128>())
res = std::make_shared<DataTypeDecimal<Decimal128>>(precision, scale);
else if (precision <= DecimalUtils::maxPrecision<Decimal256>())
res = std::make_shared<DataTypeDecimal<Decimal256>>(precision, scale);
}
if (!res)

View File

@ -63,13 +63,13 @@ def test_postgres_conversions(started_cluster):
cursor.execute(
'''CREATE TABLE IF NOT EXISTS test_types (
a smallint, b integer, c bigint, d real, e double precision, f serial, g bigserial,
h timestamp, i date, j numeric(5, 5), k decimal(5, 5))''')
h timestamp, i date, j decimal(5, 5), k numeric)''')
node1.query('''
INSERT INTO TABLE FUNCTION postgresql('postgres1:5432', 'clickhouse', 'test_types', 'postgres', 'mysecretpassword') VALUES
(-32768, -2147483648, -9223372036854775808, 1.12345, 1.1234567890, 2147483647, 9223372036854775807, '2000-05-12 12:12:12', '2000-05-12', 0.2, 0.2)''')
(-32768, -2147483648, -9223372036854775808, 1.12345, 1.1234567890, 2147483647, 9223372036854775807, '2000-05-12 12:12:12', '2000-05-12', 0.22222, 0.22222)''')
result = node1.query('''
SELECT * FROM postgresql('postgres1:5432', 'clickhouse', 'test_types', 'postgres', 'mysecretpassword')''')
assert(result == '-32768\t-2147483648\t-9223372036854775808\t1.12345\t1.123456789\t2147483647\t9223372036854775807\t2000-05-12 12:12:12\t2000-05-12\t0.20000\t0.20000\n')
SELECT a, b, c, d, e, f, g, h, i, j, toDecimal32(k, 5) FROM postgresql('postgres1:5432', 'clickhouse', 'test_types', 'postgres', 'mysecretpassword')''')
assert(result == '-32768\t-2147483648\t-9223372036854775808\t1.12345\t1.123456789\t2147483647\t9223372036854775807\t2000-05-12 12:12:12\t2000-05-12\t0.22222\t0.22222\n')
cursor.execute(
'''CREATE TABLE IF NOT EXISTS test_array_dimensions