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 // date
public static final DataType DATETIME = new DataType("DATETIME", "date", Long.BYTES, false, false, true); public static final DataType DATETIME = new DataType("DATETIME", "date", Long.BYTES, false, false, true);
// ip // 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 // binary
public static final DataType BINARY = new DataType("binary", Integer.MAX_VALUE, false, false, true); public static final DataType BINARY = new DataType("binary", Integer.MAX_VALUE, false, false, true);
// complex types // complex types

View File

@ -87,9 +87,9 @@ public abstract class SqlProtocolTestCase extends ESRestTestCase {
} }
public void testIPs() throws IOException { 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)", 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 { public void testDateTimeIntervals() throws IOException {

View File

@ -87,37 +87,37 @@ integTest |null |test_alias |salary |4 |I
sysColumnsAllTables sysColumnsAllTables
SYS COLUMNS TABLE LIKE '%'; 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 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 |@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_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 |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 |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 |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 |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 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
integTest |null |test_emp |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO integTest |null |test_emp |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |4 |YES |null |null |null |null |NO |NO integTest |null |test_emp |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |4 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |5 |YES |null |null |null |null |NO |NO integTest |null |test_emp |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |5 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO integTest |null |test_emp |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |7 |YES |null |null |null |null |NO |NO integTest |null |test_emp |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |7 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |8 |YES |null |null |null |null |NO |NO integTest |null |test_emp |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |8 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO integTest |null |test_emp |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO integTest |null |test_emp |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO
integTest |null |test_emp |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |11 |YES |null |null |null |null |NO |NO integTest |null |test_emp |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |11 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |birth_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |1 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |emp_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |3 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |extra.info.gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |extra.info.gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |6 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |extra_gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |7 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |extra_gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |7 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |extra_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |8 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |extra_no |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |8 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |first_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |9 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |first_name.keyword|12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |10 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |11 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |gender |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |11 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |12 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |hire_date |93 |DATETIME |29 |8 |null |null |1 |null |null |9 |3 |null |12 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |13 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |languages |-6 |BYTE |5 |1 |null |10 |1 |null |null |-6 |0 |null |13 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |14 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |last_name |12 |TEXT |2147483647 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |14 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |15 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |last_name.keyword |12 |KEYWORD |32766 |2147483647 |null |null |1 |null |null |12 |0 |2147483647 |15 |YES |null |null |null |null |NO |NO
integTest |null |test_emp_copy |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |16 |YES |null |null |null |null |NO |NO integTest |null |test_emp_copy |salary |4 |INTEGER |11 |4 |null |10 |1 |null |null |4 |0 |null |16 |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.JDBCType;
import java.sql.SQLType; import java.sql.SQLType;
import java.time.OffsetTime; import java.time.OffsetTime;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
@ -137,7 +136,7 @@ public class SqlDataTypes {
ODBC_TO_ES.put("SQL_INTERVAL_MINUTE_TO_SECOND", INTERVAL_MINUTE_TO_SECOND); 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, DATE,
TIME, TIME,
INTERVAL_YEAR, INTERVAL_YEAR,
@ -155,8 +154,7 @@ public class SqlDataTypes {
INTERVAL_MINUTE_TO_SECOND, INTERVAL_MINUTE_TO_SECOND,
GEO_SHAPE, GEO_SHAPE,
GEO_POINT, GEO_POINT,
SHAPE) SHAPE))
.stream())
.sorted(Comparator.comparing(DataType::typeName)) .sorted(Comparator.comparing(DataType::typeName))
.collect(toList())); .collect(toList()));
@ -421,13 +419,13 @@ public class SqlDataTypes {
*/ */
public static int defaultPrecision(DataType dataType) { public static int defaultPrecision(DataType dataType) {
if (dataType == UNSUPPORTED) { if (dataType == UNSUPPORTED) {
return 0; return dataType.size();
} }
if (dataType == NULL) { if (dataType == NULL) {
return 0; return dataType.size();
} }
if (dataType == BOOLEAN) { if (dataType == BOOLEAN) {
return 1; return dataType.size();
} }
if (dataType == BYTE) { if (dataType == BYTE) {
return 3; return 3;
@ -463,16 +461,16 @@ public class SqlDataTypes {
return 3; return 3;
} }
if (dataType == IP) { if (dataType == IP) {
return 39; return dataType.size();
} }
if (dataType == BINARY) { if (dataType == BINARY) {
return Integer.MAX_VALUE; return dataType.size();
} }
if (dataType == OBJECT) { if (dataType == OBJECT) {
return 0; return dataType.size();
} }
if (dataType == NESTED) { if (dataType == NESTED) {
return 0; return dataType.size();
} }
// //
// SQL specific // SQL specific
@ -488,13 +486,13 @@ public class SqlDataTypes {
} }
if (dataType == GEO_SHAPE) { if (dataType == GEO_SHAPE) {
return Integer.MAX_VALUE; return dataType.size();
} }
if (dataType == GEO_POINT) { if (dataType == GEO_POINT) {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
if (dataType == SHAPE) { if (dataType == SHAPE) {
return Integer.MAX_VALUE; return dataType.size();
} }
if (dataType == INTERVAL_YEAR) { if (dataType == INTERVAL_YEAR) {
return 7; return 7;
@ -541,13 +539,13 @@ public class SqlDataTypes {
public static int displaySize(DataType dataType) { public static int displaySize(DataType dataType) {
if (dataType == UNSUPPORTED) { if (dataType == UNSUPPORTED) {
return 0; return dataType.size();
} }
if (dataType == NULL) { if (dataType == NULL) {
return 0; return dataType.size();
} }
if (dataType == BOOLEAN) { if (dataType == BOOLEAN) {
return 1; return dataType.size();
} }
if (dataType == BYTE) { if (dataType == BYTE) {
return 5; return 5;
@ -577,22 +575,22 @@ public class SqlDataTypes {
return 32766; return 32766;
} }
if (dataType == TEXT) { if (dataType == TEXT) {
return Integer.MAX_VALUE; return dataType.size();
} }
if (dataType == DATETIME) { if (dataType == DATETIME) {
return 29; return 29;
} }
if (dataType == IP) { if (dataType == IP) {
return 0; return dataType.size();
} }
if (dataType == BINARY) { if (dataType == BINARY) {
return Integer.MAX_VALUE; return dataType.size();
} }
if (dataType == OBJECT) { if (dataType == OBJECT) {
return 0; return dataType.size();
} }
if (dataType == NESTED) { if (dataType == NESTED) {
return 0; return dataType.size();
} }
// //
// SQL specific // SQL specific
@ -604,53 +602,17 @@ public class SqlDataTypes {
return 18; return 18;
} }
if (dataType == GEO_SHAPE) { if (dataType == GEO_SHAPE) {
return Integer.MAX_VALUE; return dataType.size();
} }
if (dataType == GEO_POINT) { if (dataType == GEO_POINT) {
//2 doubles + len("POINT( )") //2 doubles + len("POINT( )")
return 25 * 2 + 8; return 25 * 2 + 8;
} }
if (dataType == SHAPE) { if (dataType == SHAPE) {
return Integer.MAX_VALUE; return dataType.size();
} }
if (dataType == INTERVAL_YEAR) { if (SqlDataTypes.isInterval(dataType)) {
return 7; return defaultPrecision(dataType);
}
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;
} }
return 0; return 0;