SQL: Fix column size for IP data type (#53056)

Set size/displaySize to 45 which is the maximum string for
an IP (v6), since IPs are returned as strings.

Fixes: #52762

(cherry picked from commit 815f01747a4d54a274ca248af6fc08e5ea0728c1)
This commit is contained in:
Marios Trivyzas 2020-03-04 10:09:48 +01:00
parent 4b528d97ad
commit 1d5c842700
4 changed files with 61 additions and 99 deletions

View File

@ -41,7 +41,7 @@ public final class DataTypes {
// date
public static final DataType DATETIME = new DataType("DATETIME", "date", Long.BYTES, false, false, true);
// ip
public static final DataType IP = new DataType("ip", 39, false, false, true);
public static final DataType IP = new DataType("ip", 45, false, false, true);
// binary
public static final DataType BINARY = new DataType("binary", Integer.MAX_VALUE, false, false, true);
// complex types

View File

@ -87,9 +87,9 @@ public abstract class SqlProtocolTestCase extends ESRestTestCase {
}
public void testIPs() throws IOException {
assertQuery("SELECT CAST('12.13.14.15' AS IP)", "CAST('12.13.14.15' AS IP)", "ip", "12.13.14.15", 0);
assertQuery("SELECT CAST('12.13.14.15' AS IP)", "CAST('12.13.14.15' AS IP)", "ip", "12.13.14.15", 45);
assertQuery("SELECT CAST('2001:0db8:0000:0000:0000:ff00:0042:8329' AS IP)", "CAST('2001:0db8:0000:0000:0000:ff00:0042:8329' AS IP)",
"ip", "2001:0db8:0000:0000:0000:ff00:0042:8329", 0);
"ip", "2001:0db8:0000:0000:0000:ff00:0042:8329", 45);
}
public void testDateTimeIntervals() throws IOException {

View File

@ -88,13 +88,13 @@ sysColumnsAllTables
SYS COLUMNS TABLE LIKE '%';
TABLE_CAT:s | TABLE_SCHEM:s| TABLE_NAME:s | COLUMN_NAME:s | DATA_TYPE:i | TYPE_NAME:s | COLUMN_SIZE:i| BUFFER_LENGTH:i|DECIMAL_DIGITS:i|NUM_PREC_RADIX:i | NULLABLE:i| REMARKS:s | COLUMN_DEF:s |SQL_DATA_TYPE:i|SQL_DATETIME_SUB:i|CHAR_OCTET_LENGTH:i|ORDINAL_POSITION:i|IS_NULLABLE:s|SCOPE_CATALOG:s|SCOPE_SCHEMA:s|SCOPE_TABLE:s|SOURCE_DATA_TYPE:sh|IS_AUTOINCREMENT:s|IS_GENERATEDCOLUMN:s
---------------+---------------+---------------+--------------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+----------------+-----------------+----------------+---------------+---------------+---------------+---------------+----------------+----------------+------------------
---------------+---------------+---------------+------------------+---------------+---------------+---------------+----------------+----------------+-----------------+-----------+---------------+---------------+---------------+------------------+-------------------+------------------+-------------+---------------+---------------+---------------+----------------+------------------+------------------
integTest |null |logs |@timestamp |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO
integTest |null |logs |bytes_in |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |2 |YES |null |null |null |null |NO |NO
integTest |null |logs |bytes_out |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO
integTest |null |logs |client_ip |12 |IP |0 |39 |null |null |1 |null |null |12 |0 |null |4 |YES |null |null |null |null |NO |NO
integTest |null |logs |client_ip |12 |IP |45 |45 |null |null |1 |null |null |12 |0 |null |4 |YES |null |null |null |null |NO |NO
integTest |null |logs |client_port |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |5 |YES |null |null |null |null |NO |NO
integTest |null |logs |dest_ip |12 |IP |0 |39 |null |null |1 |null |null |12 |0 |null |6 |YES |null |null |null |null |NO |NO
integTest |null |logs |dest_ip |12 |IP |45 |45 |null |null |1 |null |null |12 |0 |null |6 |YES |null |null |null |null |NO |NO
integTest |null |logs |id |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |7 |YES |null |null |null |null |NO |NO
integTest |null |logs |status |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |8 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO

View File

@ -15,7 +15,6 @@ import org.elasticsearch.xpack.sql.expression.literal.interval.Intervals;
import java.sql.JDBCType;
import java.sql.SQLType;
import java.time.OffsetTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
@ -137,7 +136,7 @@ public class SqlDataTypes {
ODBC_TO_ES.put("SQL_INTERVAL_MINUTE_TO_SECOND", INTERVAL_MINUTE_TO_SECOND);
}
private static final Collection<DataType> TYPES = unmodifiableList(Stream.concat(DataTypes.types().stream(), Arrays.asList(
private static final Collection<DataType> TYPES = unmodifiableList(Stream.concat(DataTypes.types().stream(), Stream.of(
DATE,
TIME,
INTERVAL_YEAR,
@ -155,8 +154,7 @@ public class SqlDataTypes {
INTERVAL_MINUTE_TO_SECOND,
GEO_SHAPE,
GEO_POINT,
SHAPE)
.stream())
SHAPE))
.sorted(Comparator.comparing(DataType::typeName))
.collect(toList()));
@ -421,13 +419,13 @@ public class SqlDataTypes {
*/
public static int defaultPrecision(DataType dataType) {
if (dataType == UNSUPPORTED) {
return 0;
return dataType.size();
}
if (dataType == NULL) {
return 0;
return dataType.size();
}
if (dataType == BOOLEAN) {
return 1;
return dataType.size();
}
if (dataType == BYTE) {
return 3;
@ -463,16 +461,16 @@ public class SqlDataTypes {
return 3;
}
if (dataType == IP) {
return 39;
return dataType.size();
}
if (dataType == BINARY) {
return Integer.MAX_VALUE;
return dataType.size();
}
if (dataType == OBJECT) {
return 0;
return dataType.size();
}
if (dataType == NESTED) {
return 0;
return dataType.size();
}
//
// SQL specific
@ -488,13 +486,13 @@ public class SqlDataTypes {
}
if (dataType == GEO_SHAPE) {
return Integer.MAX_VALUE;
return dataType.size();
}
if (dataType == GEO_POINT) {
return Integer.MAX_VALUE;
}
if (dataType == SHAPE) {
return Integer.MAX_VALUE;
return dataType.size();
}
if (dataType == INTERVAL_YEAR) {
return 7;
@ -541,13 +539,13 @@ public class SqlDataTypes {
public static int displaySize(DataType dataType) {
if (dataType == UNSUPPORTED) {
return 0;
return dataType.size();
}
if (dataType == NULL) {
return 0;
return dataType.size();
}
if (dataType == BOOLEAN) {
return 1;
return dataType.size();
}
if (dataType == BYTE) {
return 5;
@ -577,22 +575,22 @@ public class SqlDataTypes {
return 32766;
}
if (dataType == TEXT) {
return Integer.MAX_VALUE;
return dataType.size();
}
if (dataType == DATETIME) {
return 29;
}
if (dataType == IP) {
return 0;
return dataType.size();
}
if (dataType == BINARY) {
return Integer.MAX_VALUE;
return dataType.size();
}
if (dataType == OBJECT) {
return 0;
return dataType.size();
}
if (dataType == NESTED) {
return 0;
return dataType.size();
}
//
// SQL specific
@ -604,53 +602,17 @@ public class SqlDataTypes {
return 18;
}
if (dataType == GEO_SHAPE) {
return Integer.MAX_VALUE;
return dataType.size();
}
if (dataType == GEO_POINT) {
//2 doubles + len("POINT( )")
return 25 * 2 + 8;
}
if (dataType == SHAPE) {
return Integer.MAX_VALUE;
return dataType.size();
}
if (dataType == INTERVAL_YEAR) {
return 7;
}
if (dataType == INTERVAL_MONTH) {
return 7;
}
if (dataType == INTERVAL_DAY) {
return 23;
}
if (dataType == INTERVAL_HOUR) {
return 23;
}
if (dataType == INTERVAL_MINUTE) {
return 23;
}
if (dataType == INTERVAL_SECOND) {
return 23;
}
if (dataType == INTERVAL_YEAR_TO_MONTH) {
return 7;
}
if (dataType == INTERVAL_DAY_TO_HOUR) {
return 23;
}
if (dataType == INTERVAL_DAY_TO_MINUTE) {
return 23;
}
if (dataType == INTERVAL_DAY_TO_SECOND) {
return 23;
}
if (dataType == INTERVAL_HOUR_TO_MINUTE) {
return 23;
}
if (dataType == INTERVAL_HOUR_TO_SECOND) {
return 23;
}
if (dataType == INTERVAL_MINUTE_TO_SECOND) {
return 23;
if (SqlDataTypes.isInterval(dataType)) {
return defaultPrecision(dataType);
}
return 0;