SQL: Polish parsing of CAST expression (#40428)
(cherry picked from commit 9d291aa300bbb827eeae606e7d3e55eeef7cce00)
This commit is contained in:
parent
fe05a4d511
commit
33737b6b21
|
@ -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));
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue