浏览代码

SQL: Polish parsing of CAST expression (#40428)

Costin Leau 6 年之前
父节点
当前提交
9d291aa300

+ 11 - 19
x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java

@@ -387,11 +387,7 @@ abstract class ExpressionBuilder extends IdentifierBuilder {
 
     @Override
     public DataType visitPrimitiveDataType(PrimitiveDataTypeContext ctx) {
-        String type = visitIdentifier(ctx.identifier());
-        DataType dataType = DataType.fromSqlOrEsType(type);
-        if (dataType == null) {
-            throw new ParsingException(source(ctx), "Does not recognize type [{}]", type);        }
-        return dataType;
+        return dataType(source(ctx), visitIdentifier(ctx.identifier()));
     }
 
     //
@@ -404,24 +400,20 @@ abstract class ExpressionBuilder extends IdentifierBuilder {
             return new Cast(source(castTc), expression(castTc.expression()), typedParsing(castTc.dataType(), DataType.class));
         } else {
             ConvertTemplateContext convertTc = ctx.convertTemplate();
-            String convertDataType = convertTc.dataType().getText().toUpperCase(Locale.ROOT);
-            DataType dataType;
-            if (convertDataType.startsWith("SQL_")) {
-                dataType = DataType.fromOdbcType(convertDataType);
-                if (dataType == null) {
-                    throw new ParsingException(source(convertTc.dataType()), "Invalid data type [{}] provided", convertDataType);
-                }
-            } else {
-                try {
-                    dataType = DataType.valueOf(convertDataType);
-                } catch (IllegalArgumentException e) {
-                    throw new ParsingException(source(convertTc.dataType()), "Invalid data type [{}] provided", convertDataType);
-                }
-            }
+            DataType dataType = dataType(source(convertTc.dataType()), convertTc.dataType().getText());
             return new Cast(source(convertTc), expression(convertTc.expression()), dataType);
         }
     }
 
+    private static DataType dataType(Source ctx, String string) {
+        String type = string.toUpperCase(Locale.ROOT);
+        DataType dataType = type.startsWith("SQL_") ? DataType.fromOdbcType(type) : DataType.fromSqlOrEsType(type);
+        if (dataType == null) {
+            throw new ParsingException(ctx, "Does not recognize type [{}]", string);
+        }
+        return dataType;
+    }
+
     @Override
     public Object visitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx) {
         return new Cast(source(ctx), expression(ctx.valueExpression()), typedParsing(ctx.dataType(), DataType.class));

+ 2 - 2
x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java

@@ -361,12 +361,12 @@ public class ExpressionTests extends ESTestCase {
 
     public void testConvertWithInvalidODBCDataType() {
         ParsingException ex = expectThrows(ParsingException.class, () -> parser.createExpression("CONVERT(1, SQL_INVALID)"));
-        assertEquals("line 1:13: Invalid data type [SQL_INVALID] provided", ex.getMessage());
+        assertEquals("line 1:13: Does not recognize type [SQL_INVALID]", ex.getMessage());
     }
 
     public void testConvertWithInvalidESDataType() {
         ParsingException ex = expectThrows(ParsingException.class, () -> parser.createExpression("CONVERT(1, INVALID)"));
-        assertEquals("line 1:13: Invalid data type [INVALID] provided", ex.getMessage());
+        assertEquals("line 1:13: Does not recognize type [INVALID]", ex.getMessage());
     }
 
     public void testCurrentDate() {