SQL: Return proper NUM_PREC_RADIX for non-numeric types (elastic/x-pack-elasticsearch#3086)

NUM_PREC_RADIX should be 10 for integer types, 2 for rational types and null for everything else.

relates elastic/x-pack-elasticsearch#3085

Original commit: elastic/x-pack-elasticsearch@81d5ee04b3
This commit is contained in:
Igor Motov 2017-11-29 08:21:42 -05:00 committed by GitHub
parent 0cc153f6d3
commit d5525f38f3
4 changed files with 30 additions and 7 deletions

View File

@ -76,6 +76,7 @@ public class DatabaseMetaDataTestCase extends JdbcIntegrationTestCase {
index("test2", body -> {
body.field("number", 7);
body.field("date", "2017-01-01T01:01:01Z");
body.field("float", 42.0);
});
try (Connection h2 = LocalH2.anonymousDb();

View File

@ -23,21 +23,23 @@ CREATE TABLE mock (
IS_AUTOINCREMENT VARCHAR,
IS_GENERATEDCOLUMN VARCHAR
) AS
SELECT '', 'test1', 'name', 12, 'VARCHAR', 2147483647, null, null,
10, -- TODO 10 seem wrong to hard code for non-numbers see https://github.com/elastic/x-pack-elasticsearch/issues/3085
SELECT '', 'test1', 'name', 12, 'VARCHAR', 2147483647, null, null, null,
1, -- columnNullable
null, null, null, null, null, 1, 'YES', null, null, null, null, '', ''
FROM DUAL
UNION ALL
SELECT '', 'test2', 'date', 93, 'TIMESTAMP', 19, null, null,
10,
SELECT '', 'test2', 'date', 93, 'TIMESTAMP', 19, null, null, null,
1, -- columnNullable
null, null, null, null, null, 1, 'YES', null, null, null, null, '', ''
FROM DUAL
UNION ALL
SELECT '', 'test2', 'number', -5, 'BIGINT', 19, null, null,
10,
SELECT '', 'test2', 'float', 7, 'REAL', 7, null, null, 2,
1, -- columnNullable
null, null, null, null, null, 2, 'YES', null, null, null, null, '', ''
FROM DUAL
UNION ALL
SELECT '', 'test2', 'number', -5, 'BIGINT', 19, null, null, 10,
1, -- columnNullable
null, null, null, null, null, 3, 'YES', null, null, null, null, '', ''
FROM DUAL
;

View File

@ -22,6 +22,8 @@ import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.List;
import static org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcUtils.numericPrecisionRadix;
/**
* Implementation of {@link DatabaseMetaData} for Elasticsearch. Draws inspiration
* from <a href="https://www.postgresql.org/docs/9.0/static/information-schema.html">
@ -822,7 +824,7 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper {
row[ 6] = col.size;
row[ 7] = null;
row[ 8] = null;
row[ 9] = 10;
row[ 9] = numericPrecisionRadix(col.type.getVendorTypeNumber());
row[10] = columnNullable;
row[11] = null;
row[12] = null;

View File

@ -198,4 +198,22 @@ public abstract class JdbcUtils {
static JDBCType type(int jdbcType) {
return JDBCType.valueOf(jdbcType);
}
static Integer numericPrecisionRadix(int type) {
switch (type) {
case TINYINT:
case SMALLINT:
case INTEGER:
case BIGINT:
return 10;
case REAL:
case DOUBLE:
case FLOAT:
case DECIMAL:
case NUMERIC:
return 2;
default:
return null;
}
}
}