Skip to content

Commit

Permalink
[Feature] refactor date/datetime with higher precision data type
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel39 committed May 23, 2022
1 parent d97e2b1 commit bf18dd9
Show file tree
Hide file tree
Showing 59 changed files with 3,216 additions and 1,040 deletions.
10 changes: 7 additions & 3 deletions fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -4599,11 +4599,15 @@ type ::=
| KW_DOUBLE
{: RESULT = Type.DOUBLE; :}
| KW_DATE
{: RESULT = Type.DATE; :}
{: RESULT = ScalarType.createDateType(); :}
| KW_DATETIME LPAREN INTEGER_LITERAL:precision RPAREN
{: RESULT = ScalarType.createDatetimeV2Type(precision.intValue()); :}
| KW_DATETIME
{: RESULT = Type.DATETIME; :}
{: RESULT = ScalarType.createDatetimeType(); :}
| KW_TIME LPAREN INTEGER_LITERAL:precision RPAREN
{: RESULT = ScalarType.createTimeV2Type(precision.intValue()); :}
| KW_TIME
{: RESULT = Type.TIME; :}
{: RESULT = ScalarType.createTimeType(); :}
| KW_BITMAP
{: RESULT = Type.BITMAP; :}
| KW_QUANTILE_STATE
Expand Down
11 changes: 9 additions & 2 deletions fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.doris.catalog.Type;
import org.apache.doris.catalog.View;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.Config;
import org.apache.doris.common.DdlException;
import org.apache.doris.common.MetaNotFoundException;
import org.apache.doris.common.UserException;
Expand Down Expand Up @@ -677,12 +678,18 @@ public void modifyPartitionsProperty(Database db,
// 4.2 combine the old properties with new ones
Map<String, String> newProperties = new HashMap<>();
newProperties.put(PropertyAnalyzer.PROPERTIES_STORAGE_MEDIUM, dataProperty.getStorageMedium().name());
Type datetimeType;
if (Config.use_date_v2_by_default) {
datetimeType = Type.DATETIMEV2;
} else {
datetimeType = Type.DATETIME;
}
DateLiteral dateLiteral = new DateLiteral(dataProperty.getCooldownTimeMs(),
TimeUtils.getTimeZone(), Type.DATETIME);
TimeUtils.getTimeZone(), datetimeType);
newProperties.put(PropertyAnalyzer.PROPERTIES_STORAGE_COOLDOWN_TIME, dateLiteral.getStringValue());
newProperties.put(PropertyAnalyzer.PROPERTIES_REMOTE_STORAGE_RESOURCE, dataProperty.getRemoteStorageResourceName());
DateLiteral dateLiteral1 = new DateLiteral(dataProperty.getRemoteCooldownTimeMs(),
TimeUtils.getTimeZone(), Type.DATETIME);
TimeUtils.getTimeZone(), datetimeType);
newProperties.put(PropertyAnalyzer.PROPERTIES_REMOTE_STORAGE_COOLDOWN_TIME, dateLiteral1.getStringValue());
newProperties.putAll(properties);
// 4.3 analyze new properties
Expand Down
31 changes: 16 additions & 15 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ public TypeDef getTargetTypeDef() {
private static boolean disableRegisterCastingFunction(Type fromType, Type toType) {
// Disable casting from boolean to decimal or datetime or date
if (fromType.isBoolean() &&
(toType.equals(Type.DECIMALV2) ||
toType.equals(Type.DATETIME) || toType.equals(Type.DATE))) {
(toType.equals(Type.DECIMALV2) || toType.isDateType())) {
return true;
}

Expand All @@ -166,14 +165,16 @@ public static void initBuiltins(FunctionSet functionSet) {
beClass = "TimeOperators";
}
String typeName = Function.getUdfTypeName(toType.getPrimitiveType());
if (toType.getPrimitiveType() == PrimitiveType.DATE) {
// only refactor date/datetime for vectorized engine.
if (toType.getPrimitiveType() == PrimitiveType.DATE
|| toType.getPrimitiveType() == PrimitiveType.DATEV2) {
typeName = "date_val";
}
String beSymbol = "doris::" + beClass + "::cast_to_"
+ typeName;
functionSet.addBuiltinBothScalaAndVectorized(ScalarFunction.createBuiltin(getFnName(toType),
toType, TYPE_NULLABLE_MODE.get(new Pair<>(fromType, toType)),
Lists.newArrayList(fromType), false ,
Lists.newArrayList(fromType), false,
beSymbol, null, null, true));
}
}
Expand Down Expand Up @@ -207,11 +208,11 @@ public String toSqlImpl() {

@Override
public String toDigestImpl() {
boolean isVerbose = ConnectContext.get() != null &&
ConnectContext.get().getExecutor() != null &&
ConnectContext.get().getExecutor().getParsedStmt() != null &&
ConnectContext.get().getExecutor().getParsedStmt().getExplainOptions() != null &&
ConnectContext.get().getExecutor().getParsedStmt().getExplainOptions().isVerbose();
boolean isVerbose = ConnectContext.get() != null
&& ConnectContext.get().getExecutor() != null
&& ConnectContext.get().getExecutor().getParsedStmt() != null
&& ConnectContext.get().getExecutor().getParsedStmt().getExplainOptions() != null
&& ConnectContext.get().getExecutor().getParsedStmt().getExplainOptions().isVerbose();
if (isImplicit && !isVerbose) {
return getChild(0).toDigest();
}
Expand Down Expand Up @@ -278,10 +279,10 @@ public void analyze() throws AnalysisException {
fn = Catalog.getCurrentCatalog().getFunction(
searchDesc, Function.CompareMode.IS_IDENTICAL);
}
} else if (type.isArrayType()){
} else if (type.isArrayType()) {
fn = ScalarFunction.createBuiltin(getFnName(Type.ARRAY),
type, Function.NullableMode.ALWAYS_NULLABLE,
Lists.newArrayList(Type.VARCHAR), false ,
Lists.newArrayList(Type.VARCHAR), false,
"doris::CastFunctions::cast_to_array_val", null, null, true);
}

Expand Down Expand Up @@ -361,7 +362,7 @@ public Expr getResultValue() throws AnalysisException {
}
Expr targetExpr;
try {
targetExpr = castTo((LiteralExpr)value);
targetExpr = castTo((LiteralExpr) value);
} catch (AnalysisException ae) {
targetExpr = this;
} catch (NumberFormatException nfe) {
Expand Down Expand Up @@ -501,8 +502,8 @@ private int getDigital(String desc, List<String> parameters, List<Expr> inputPar

@Override
public boolean isNullable() {
return children.get(0).isNullable() ||
(children.get(0).getType().isStringType() && !getType().isStringType()) ||
(!children.get(0).getType().isDateType() && getType().isDateType());
return children.get(0).isNullable()
|| (children.get(0).getType().isStringType() && !getType().isStringType())
|| (!children.get(0).getType().isDateType() && getType().isDateType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ public static void validateDefaultValue(Type type, String defaultValue) throws A
case SMALLINT:
case INT:
case BIGINT:
IntLiteral intLiteral = new IntLiteral(defaultValue, type);
new IntLiteral(defaultValue, type);
break;
case LARGEINT:
LargeIntLiteral largeIntLiteral = new LargeIntLiteral(defaultValue);
new LargeIntLiteral(defaultValue);
break;
case FLOAT:
FloatLiteral floatLiteral = new FloatLiteral(defaultValue);
Expand All @@ -282,15 +282,17 @@ public static void validateDefaultValue(Type type, String defaultValue) throws A
}
break;
case DOUBLE:
FloatLiteral doubleLiteral = new FloatLiteral(defaultValue);
new FloatLiteral(defaultValue);
break;
case DECIMALV2:
DecimalLiteral decimalLiteral = new DecimalLiteral(defaultValue);
decimalLiteral.checkPrecisionAndScale(scalarType.getScalarPrecision(), scalarType.getScalarScale());
break;
case DATE:
case DATETIME:
DateLiteral dateLiteral = new DateLiteral(defaultValue, type);
case DATEV2:
case DATETIMEV2:
new DateLiteral(defaultValue, DateLiteral.getDefaultDateType(type));
break;
case CHAR:
case VARCHAR:
Expand All @@ -301,15 +303,12 @@ public static void validateDefaultValue(Type type, String defaultValue) throws A
}
break;
case BITMAP:
break;
case ARRAY:
break;
case MAP:
break;
case STRUCT:
break;
case BOOLEAN:
BoolLiteral boolLiteral = new BoolLiteral(defaultValue);
new BoolLiteral(defaultValue);
break;
default:
throw new AnalysisException("Unsupported type: " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,13 @@ private Types.PGenericType convertToPParameterType(Type arg) throws AnalysisExce
typeBuilder.setId(Types.PGenericType.TypeId.BITMAP);
break;
case DATE:
case DATEV2:
typeBuilder.setId(Types.PGenericType.TypeId.DATE);
break;
case DATETIME:
case DATETIMEV2:
case TIME:
case TIMEV2:
typeBuilder.setId(Types.PGenericType.TypeId.DATETIME);
break;
case DECIMALV2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ private static void validateHllHash(List<String> args, Map<String, String> colum
}

private static void validateNowFunction(Column mappingColumn) throws AnalysisException {
if (!mappingColumn.getOriginType().equals(Type.DATE) && !mappingColumn.getOriginType().equals(Type.DATETIME)) {
if (!mappingColumn.getOriginType().isDateType()) {
throw new AnalysisException("Now() function is only support for DATE/DATETIME column");
}
}
Expand Down
Loading

0 comments on commit bf18dd9

Please sign in to comment.