You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2025/11/08 13:34:04 UTC
(doris) branch branch-4.0 updated: branch-4.0: [refactor](expr) remove castTo in Catalog's Expr #57560 (#57806)
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.0 by this push:
new 359769cf39c branch-4.0: [refactor](expr) remove castTo in Catalog's Expr #57560 (#57806)
359769cf39c is described below
commit 359769cf39c84ec2acd8d7cffaae6d373795e615
Author: morrySnow <zh...@selectdb.com>
AuthorDate: Sat Nov 8 21:33:58 2025 +0800
branch-4.0: [refactor](expr) remove castTo in Catalog's Expr #57560 (#57806)
picked from #57560
---
.../org/apache/doris/analysis/ArrayLiteral.java | 72 --
.../java/org/apache/doris/analysis/CastExpr.java | 96 ---
.../org/apache/doris/analysis/DateLiteral.java | 62 --
.../org/apache/doris/analysis/DecimalLiteral.java | 31 -
.../apache/doris/analysis/DefaultValueExprDef.java | 30 +-
.../main/java/org/apache/doris/analysis/Expr.java | 144 ----
.../org/apache/doris/analysis/FloatLiteral.java | 28 -
.../apache/doris/analysis/FunctionCallExpr.java | 22 -
.../java/org/apache/doris/analysis/IntLiteral.java | 44 --
.../org/apache/doris/analysis/JsonLiteral.java | 6 -
.../org/apache/doris/analysis/LargeIntLiteral.java | 18 -
.../org/apache/doris/analysis/LiteralExpr.java | 34 -
.../java/org/apache/doris/analysis/MapLiteral.java | 86 ---
.../org/apache/doris/analysis/NullLiteral.java | 13 -
.../org/apache/doris/analysis/StringLiteral.java | 154 ----
.../org/apache/doris/analysis/StructLiteral.java | 32 -
.../org/apache/doris/analysis/TryCastExpr.java | 11 -
.../main/java/org/apache/doris/catalog/Column.java | 22 +-
.../org/apache/doris/datasource/FileScanNode.java | 15 +-
.../nereids/load/NereidsLoadPlanInfoCollector.java | 18 +-
.../nereids/load/NereidsLoadScanProvider.java | 16 +-
.../doris/nereids/rules/analysis/BindSink.java | 36 +-
.../trees/plans/commands/UpdateCommand.java | 3 +-
.../trees/plans/commands/insert/InsertUtils.java | 43 +-
.../java/org/apache/doris/rewrite/FEFunctions.java | 50 --
.../external/elasticsearch/QueryBuildersTest.java | 6 +-
.../org/apache/doris/rewrite/FEFunctionsTest.java | 840 ---------------------
.../doris/rewrite/RewriteDateLiteralRuleTest.java | 255 -------
28 files changed, 64 insertions(+), 2123 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java
index 3c7e659d972..a2d6464face 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ArrayLiteral.java
@@ -26,7 +26,6 @@ import org.apache.doris.common.FormatOptions;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
-import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
@@ -47,46 +46,6 @@ public class ArrayLiteral extends LiteralExpr {
analysisDone();
}
- public ArrayLiteral(LiteralExpr... exprs) throws AnalysisException {
- Type itemType = Type.NULL;
- boolean containsNull = true;
- for (LiteralExpr expr : exprs) {
- if (!ArrayType.ARRAY.supportSubType(expr.getType())) {
- throw new AnalysisException("Invalid item type in Array, not support " + expr.getType());
- }
- if (itemType == Type.NULL) {
- itemType = expr.getType();
- } else {
- itemType = Type.getAssignmentCompatibleType(itemType, expr.getType(), false, false);
- }
-
- if (expr.isNullable()) {
- containsNull = true;
- }
- }
-
- if (itemType == Type.INVALID) {
- throw new AnalysisException("Invalid element type in ARRAY");
- }
-
- type = new ArrayType(itemType, containsNull);
-
- children = new ArrayList<>();
- try {
- for (LiteralExpr expr : exprs) {
- if (expr.getType().equals(itemType)) {
- children.add(expr);
- } else {
- children.add(expr.convertTo(itemType));
- }
- }
- } catch (AnalysisException e) {
- String s = "[" + StringUtils.join(exprs, ',') + "]";
- throw new AnalysisException("Invalid ARRAY " + s + " literal: " + e.getMessage());
- }
- analysisDone();
- }
-
protected ArrayLiteral(ArrayLiteral other) {
super(other);
}
@@ -180,37 +139,6 @@ public class ArrayLiteral extends LiteralExpr {
return new ArrayLiteral(this);
}
- @Override
- public LiteralExpr convertTo(Type targetType) throws AnalysisException {
- Preconditions.checkState(targetType instanceof ArrayType);
- Type itemType = ((ArrayType) targetType).getItemType();
- LiteralExpr[] literals = new LiteralExpr[children.size()];
- for (int i = 0; i < children.size(); i++) {
- literals[i] = (LiteralExpr) (Expr.convertLiteral(children.get(i), itemType));
- }
- return new ArrayLiteral(literals);
- }
-
- @Override
- public Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (!targetType.isArrayType()) {
- return super.uncheckedCastTo(targetType);
- }
- Type itemType = ((ArrayType) targetType).getItemType();
- ArrayLiteral literal = new ArrayLiteral(this);
- for (int i = 0; i < children.size(); ++ i) {
- Expr child = Expr.convertLiteral(children.get(i), itemType);
- // all children should be literal or else it will make be core
- if (!child.isLiteral()) {
- throw new AnalysisException("Unexpected array literal cast failed. from type: "
- + this.type + ", to type: " + targetType);
- }
- literal.children.set(i, child);
- }
- literal.setType(targetType);
- return literal;
- }
-
@Override
public void checkValueValid() throws AnalysisException {
for (Expr e : children) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
index 90b552b1355..dcd012a1df7 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
@@ -20,17 +20,14 @@
package org.apache.doris.analysis;
-import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.Function;
import org.apache.doris.catalog.Function.NullableMode;
import org.apache.doris.catalog.FunctionSet;
-import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.ScalarFunction;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.Type;
-import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.FormatOptions;
import org.apache.doris.common.Pair;
import org.apache.doris.qe.ConnectContext;
@@ -46,7 +43,6 @@ import com.google.gson.annotations.SerializedName;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import java.util.Arrays;
import java.util.Map;
@@ -64,8 +60,6 @@ public class CastExpr extends Expr {
// True if this cast does not change the type.
protected boolean noOp = false;
- protected boolean notFold = false;
-
protected static final Map<Pair<Type, Type>, Function.NullableMode> TYPE_NULLABLE_MODE;
static {
@@ -94,26 +88,6 @@ public class CastExpr extends Expr {
}
- public CastExpr(Type targetType, Expr e) {
- super();
- Preconditions.checkArgument(targetType.isValid());
- Preconditions.checkNotNull(e);
- type = targetType;
- targetTypeDef = null;
- isImplicit = true;
-
- children.add(e);
-
- try {
- analyze();
- } catch (AnalysisException ex) {
- LOG.warn("Implicit casts fail", ex);
- Preconditions.checkState(false,
- "Implicit casts should never throw analysis exception.");
- }
- analysisDone();
- }
-
/**
* Just use for nereids, put analyze() in finalizeImplForNereids
*/
@@ -284,72 +258,6 @@ public class CastExpr extends Expr {
}
}
- public void analyze() throws AnalysisException {
- // do not analyze ALL cast
- if (type == Type.ALL) {
- return;
- }
- // cast was asked for in the query, check for validity of cast
- Type childType = getChild(0).getType();
-
- // this cast may result in loss of precision, but the user requested it
- noOp = Type.matchExactType(childType, type, true);
-
- if (noOp) {
- // For decimalv2, we do not perform an actual cast between different precision/scale. Instead, we just
- // set the target type as the child's type.
- if (type.isDecimalV2() && childType.isDecimalV2()) {
- getChild(0).setType(type);
- }
- return;
- }
-
- // select stmt will make BE coredump when its castExpr is like cast(int as array<>),
- // it is necessary to check if it is castable before creating fn.
- // char type will fail in canCastTo, so for compatibility, only the cast of array type is checked here.
- if (type.isArrayType() || childType.isArrayType()) {
- if (!Type.canCastTo(childType, type)) {
- throw new AnalysisException("Invalid type cast of " + getChild(0).toSql()
- + " from " + childType + " to " + type);
- }
- }
-
- this.opcode = TExprOpcode.CAST;
- FunctionName fnName = new FunctionName(getFnName(type));
- Function searchDesc = new Function(fnName, Arrays.asList(getActualArgTypes(collectChildReturnTypes())),
- Type.INVALID, false);
- if (type.isScalarType()) {
- fn = Env.getCurrentEnv().getFunction(searchDesc, Function.CompareMode.IS_IDENTICAL);
- } else {
- createComplexTypeCastFunction();
- }
-
- if (fn == null) {
- //TODO(xy): check map type
- if ((type.isMapType() || type.isStructType()) && childType.isStringType()) {
- return;
- }
- // same with Type.canCastTo() can be cast to jsonb
- if (childType.isComplexType() && type.isJsonbType()) {
- return;
- }
- if (childType.isNull() && Type.canCastTo(childType, type)) {
- return;
- } else {
- throw new AnalysisException("Invalid type cast of " + getChild(0).toSql()
- + " from " + childType + " to " + type);
- }
- }
-
- if (PrimitiveType.typeWithPrecision.contains(type.getPrimitiveType())) {
- Preconditions.checkState(type.isDecimalV3() == fn.getReturnType().isDecimalV3()
- || type.isDatetimeV2() == fn.getReturnType().isDatetimeV2(),
- type + " != " + fn.getReturnType());
- } else {
- Preconditions.checkState(type.matchesType(fn.getReturnType()), type + " != " + fn.getReturnType());
- }
- }
-
@Override
public int hashCode() {
return super.hashCode();
@@ -390,8 +298,4 @@ public class CastExpr extends Expr {
protected String getStringValueInComplexTypeForQuery(FormatOptions options) {
return children.get(0).getStringValueInComplexTypeForQuery(options);
}
-
- public void setNotFold(boolean notFold) {
- this.notFold = notFold;
- }
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
index 86e08df7f85..ac2e732252b 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
@@ -29,7 +29,6 @@ import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.FormatOptions;
import org.apache.doris.common.InvalidFormatException;
import org.apache.doris.nereids.util.DateUtils;
-import org.apache.doris.qe.SessionVariable;
import org.apache.doris.thrift.TDateLiteral;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
@@ -805,48 +804,6 @@ public class DateLiteral extends LiteralExpr {
}
}
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (targetType.isDateType()) {
- if (type.equals(targetType)) {
- return this;
- }
- if (targetType.equals(Type.DATE) || targetType.equals(Type.DATEV2)) {
- return new DateLiteral(this.year, this.month, this.day, targetType);
- } else if (targetType.equals(Type.DATETIME)) {
- return new DateLiteral(this.year, this.month, this.day, this.hour, this.minute, this.second,
- targetType);
- } else if (targetType.isDatetimeV2()) {
- return new DateLiteral(this.year, this.month, this.day, this.hour, this.minute, this.second,
- this.microsecond, targetType);
- } else {
- throw new AnalysisException("Error date literal type : " + type);
- }
- } else if (targetType.isStringType()) {
- return new StringLiteral(getStringValue());
- } else if (targetType.isBigIntType()) {
- long value = getYear() * 1000 + getMonth() * 100 + getDay();
- return new IntLiteral(value, Type.BIGINT);
- } else {
- if (Type.isImplicitlyCastable(this.type, targetType, true, SessionVariable.getEnableDecimal256())) {
- return new CastExpr(targetType, this);
- }
- }
- Preconditions.checkState(false);
- return this;
- }
-
- public void castToDate() {
- if (this.type.isDateOrDateTime()) {
- this.type = Type.DATE;
- } else {
- this.type = Type.DATEV2;
- }
- hour = 0;
- minute = 0;
- second = 0;
- }
-
private boolean isLeapYear() {
return ((year % 4) == 0) && ((year % 100 != 0) || ((year % 400) == 0 && year > 0));
}
@@ -892,10 +849,6 @@ public class DateLiteral extends LiteralExpr {
return Timestamp.from(zonedDateTime.toInstant());
}
- public long getUnixTimestampWithMillisecond(TimeZone timeZone) {
- return unixTimestamp(timeZone);
- }
-
public long getUnixTimestampWithMicroseconds(TimeZone timeZone) {
Timestamp timestamp = getTimestamp(timeZone);
return timestamp.getTime() * 1000 + timestamp.getNanos() / 1000 % 1000;
@@ -909,21 +862,6 @@ public class DateLiteral extends LiteralExpr {
return format.indexOf(MICRO_SECOND_FORMATTER) != -1;
}
- // Return the date stored in the dateliteral as pattern format.
- // eg : "%Y-%m-%d" or "%Y-%m-%d %H:%i:%s"
- public String dateFormat(String pattern) throws AnalysisException {
- TemporalAccessor accessor;
- if (type.equals(Type.DATE) || type.equals(Type.DATEV2)) {
- accessor = DATE_FORMATTER.parse(getStringValue());
- } else if (type.isDatetimeV2()) {
- accessor = DATE_TIME_FORMATTER_TO_MICRO_SECOND.parse(getStringValue());
- } else {
- accessor = DATE_TIME_FORMATTER.parse(getStringValue());
- }
- DateTimeFormatter toFormatter = formatBuilder(pattern).toFormatter();
- return toFormatter.format(accessor);
- }
-
private static DateTimeFormatterBuilder formatBuilder(String pattern) throws AnalysisException {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
boolean escaped = false;
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java
index dd5d65cf52e..90656236695 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java
@@ -312,37 +312,6 @@ public class DecimalLiteral extends NumericLiteralExpr {
return fracPart.intValue();
}
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (targetType.isDecimalV2() && type.isDecimalV2()) {
- setType(targetType);
- return this;
- } else if ((targetType.isDecimalV3() && type.isDecimalV3()
- && (((ScalarType) targetType).decimalPrecision() >= value.precision())
- && (((ScalarType) targetType).decimalScale() >= value.scale()))
- || (targetType.isDecimalV3() && type.isDecimalV2()
- && (((ScalarType) targetType).decimalScale() >= value.scale()))) {
- // If target type is DECIMALV3, we should set type for literal
- setType(targetType);
- return this;
- } else if (targetType.isFloatingPointType()) {
- return new FloatLiteral(value.doubleValue(), targetType);
- } else if (targetType.isIntegerType()) {
- // If the integer part of BigDecimal is too big to fit into long,
- // longValue() will only return the low-order 64-bit value.
- if (value.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0
- || value.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) {
- throw new AnalysisException("Integer part of " + value + " exceeds storage range of Long Type.");
- }
- return new IntLiteral(value.longValue(), targetType);
- } else if (targetType.isStringType()) {
- return new StringLiteral(value.toString());
- } else if (targetType.isLargeIntType()) {
- return new LargeIntLiteral(value.toBigInteger().toString());
- }
- return super.uncheckedCastTo(targetType);
- }
-
@Override
public int hashCode() {
return 31 * super.hashCode() + Objects.hashCode(value);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/DefaultValueExprDef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/DefaultValueExprDef.java
index 911f72d588b..8a894f6252a 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DefaultValueExprDef.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DefaultValueExprDef.java
@@ -20,18 +20,13 @@
package org.apache.doris.analysis;
-import org.apache.doris.catalog.Type;
-import org.apache.doris.common.AnalysisException;
import org.apache.doris.persist.gson.GsonPostProcessable;
-import org.apache.doris.qe.ConnectContext;
-import com.google.common.collect.Lists;
import com.google.gson.annotations.SerializedName;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.IOException;
-import java.util.List;
/**
* This def used for column which defaultValue is an expression.
@@ -53,26 +48,15 @@ public class DefaultValueExprDef implements GsonPostProcessable {
this.precision = precision;
}
- /**
- * generate a FunctionCallExpr
- * @return FunctionCallExpr of exprName
- */
- public FunctionCallExpr getExpr(Type type) {
- List<Expr> exprs = null;
+ public String getSql() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(exprName);
+ sb.append("(");
if (precision != null && precision != 0) {
- exprs = Lists.newArrayList();
- exprs.add(new IntLiteral(precision));
+ sb.append(precision);
}
- FunctionCallExpr expr = new FunctionCallExpr(exprName, new FunctionParams(exprs));
- try {
- expr.analyzeImplForDefaultValue(type);
- } catch (AnalysisException e) {
- if (ConnectContext.get() != null) {
- ConnectContext.get().getState().reset();
- }
- LOG.warn("analyzeImplForDefaultValue fail: {}", e);
- }
- return expr;
+ sb.append(")");
+ return sb.toString();
}
public String getExprName() {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java
index 1565779e689..4fd005d02d2 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java
@@ -858,150 +858,6 @@ public abstract class Expr extends TreeNode<Expr> implements Cloneable, ExprStat
return true;
}
- /**
- * Checks validity of cast, and
- * calls uncheckedCastTo() to
- * create a cast expression that casts
- * this to a specific type.
- *
- * @param targetType type to be cast to
- * @return cast expression, or converted literal,
- * should never return null
- * @throws AnalysisException when an invalid cast is asked for, for example,
- * failure to convert a string literal to a date literal
- */
- public final Expr castTo(Type targetType) throws AnalysisException {
- if (this instanceof PlaceHolderExpr && this.type.isUnsupported()) {
- return this;
- }
- // If the targetType is NULL_TYPE then ignore the cast because NULL_TYPE
- // is compatible with all types and no cast is necessary.
- if (targetType.isNull()) {
- return this;
- }
-
- if (this.type.isAggStateType()) {
- List<Type> subTypes = ((AggStateType) targetType).getSubTypes();
- List<Boolean> subNullables = ((AggStateType) targetType).getSubTypeNullables();
-
- if (this instanceof FunctionCallExpr) {
- if (subTypes.size() != getChildren().size()) {
- throw new AnalysisException("AggState's subTypes size not euqal to children number");
- }
- for (int i = 0; i < subTypes.size(); i++) {
- setChild(i, getChild(i).castTo(subTypes.get(i)));
- if (getChild(i).isNullable() && !subNullables.get(i)) {
- FunctionCallExpr newChild = new FunctionCallExpr("non_nullable",
- Lists.newArrayList(getChild(i)));
- newChild.analyzeImplForDefaultValue(subTypes.get(i));
- setChild(i, newChild);
- } else if (!getChild(i).isNullable() && subNullables.get(i)) {
- FunctionCallExpr newChild = new FunctionCallExpr("nullable", Lists.newArrayList(getChild(i)));
- newChild.analyzeImplForDefaultValue(subTypes.get(i));
- setChild(i, newChild);
- }
- }
- type = targetType;
- } else {
- List<Type> selfSubTypes = ((AggStateType) type).getSubTypes();
- if (subTypes.size() != selfSubTypes.size()) {
- throw new AnalysisException("AggState's subTypes size did not match");
- }
- for (int i = 0; i < subTypes.size(); i++) {
- if (subTypes.get(i) != selfSubTypes.get(i)) {
- throw new AnalysisException("AggState's subType did not match");
- }
- }
- }
- } else {
- if (this.type.equals(targetType)) {
- return this;
- }
- }
-
- if (targetType.getPrimitiveType() == PrimitiveType.DECIMALV2
- && this.type.getPrimitiveType() == PrimitiveType.DECIMALV2) {
- this.type = targetType;
- return this;
- }
-
- if (this.type.isStringType() && targetType.isStringType()) {
- return this;
- }
-
- // Preconditions.checkState(PrimitiveType.isImplicitCast(type, targetType),
- // "cast %s to %s", this.type, targetType);
- // TODO(zc): use implicit cast
- if (!Type.canCastTo(this.type, targetType)) {
- throw new AnalysisException("can not cast from origin type " + this.type
- + " to target type=" + targetType);
-
- }
- return uncheckedCastTo(targetType);
- }
-
- /**
- * Create an expression equivalent to 'this' but returning targetType;
- * possibly by inserting an implicit cast,
- * or by returning an altogether new expression
- *
- * @param targetType type to be cast to
- * @return cast expression, or converted literal,
- * should never return null
- * @throws AnalysisException when an invalid cast is asked for, for example,
- * failure to convert a string literal to a date literal
- */
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- return new CastExpr(targetType, this);
- }
-
- /**
- * Add a cast expression above child.
- * If child is a literal expression, we attempt to
- * convert the value of the child directly, and not insert a cast node.
- *
- * @param targetType type to be cast to
- * @param childIndex index of child to be cast
- */
- public void castChild(Type targetType, int childIndex) throws AnalysisException {
- Expr child = getChild(childIndex);
- Expr newChild = child.castTo(targetType);
- setChild(childIndex, newChild);
- }
-
- /**
- * Assuming it can cast to the targetType, use for convert literal value to
- * target type without a cast node.
- */
- public static Expr convertLiteral(Expr expr, Type targetType) throws AnalysisException {
- if (!(expr instanceof LiteralExpr)) {
- return expr;
- }
- Expr newExpr = expr.uncheckedCastTo(targetType);
- if (newExpr instanceof CastExpr) {
- return ((LiteralExpr) newExpr.getChild(0)).convertTo(targetType);
- }
- return newExpr;
- }
-
- /**
- * Add a cast expression above child.
- * If child is a literal expression, we attempt to
- * convert the value of the child directly, and not insert a cast node.
- *
- * @param targetType type to be cast to
- * @param childIndex index of child to be cast
- */
- public void uncheckedCastChild(Type targetType, int childIndex)
- throws AnalysisException {
- Expr child = getChild(childIndex);
- //avoid to generate Expr like cast (cast(... as date) as date)
- if (!child.getType().equals(targetType)) {
- Expr newChild = child.uncheckedCastTo(targetType);
- setChild(childIndex, newChild);
- }
- }
-
@Override
public String toString() {
return MoreObjects.toStringHelper(this.getClass()).add("id", id).add("type", type).add("sel",
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
index 91e7925de6c..0c161b3fb95 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
@@ -18,7 +18,6 @@
package org.apache.doris.analysis;
import org.apache.doris.catalog.PrimitiveType;
-import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.Type;
@@ -31,7 +30,6 @@ import org.apache.doris.thrift.TFloatLiteral;
import com.google.gson.annotations.SerializedName;
-import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.NumberFormat;
@@ -210,32 +208,6 @@ public class FloatLiteral extends NumericLiteralExpr {
return value;
}
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (!(targetType.isFloatingPointType() || targetType.isDecimalV2() || targetType.isDecimalV3())) {
- return super.uncheckedCastTo(targetType);
- }
- if (targetType.isFloatingPointType()) {
- if (!type.equals(targetType)) {
- FloatLiteral floatLiteral = new FloatLiteral(this);
- floatLiteral.setType(targetType);
- return floatLiteral;
- }
- return this;
- } else if (targetType.isDecimalV2()) {
- // the double constructor does an exact translation, use valueOf() instead.
- DecimalLiteral res = new DecimalLiteral(BigDecimal.valueOf(value));
- res.setType(targetType);
- return res;
- } else if (targetType.isDecimalV3()) {
- DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
- res.setType(ScalarType.createDecimalV3Type(targetType.getPrecision(),
- ((ScalarType) targetType).decimalScale()));
- return res;
- }
- return this;
- }
-
@Override
public int hashCode() {
return 31 * super.hashCode() + Double.hashCode(value);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
index 33c7b2a8d85..474111de792 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
@@ -830,28 +830,6 @@ public class FunctionCallExpr extends Expr {
}
}
- /**
- * This analyzeImp used for DefaultValueExprDef
- * to generate a builtinFunction.
- *
- * @throws AnalysisException
- */
- public void analyzeImplForDefaultValue(Type type) throws AnalysisException {
- Type[] childTypes = new Type[children.size()];
- for (int i = 0; i < children.size(); i++) {
- childTypes[i] = children.get(i).type;
- }
- fn = new Function(
- getBuiltinFunction(fnName.getFunction(), childTypes, Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF));
- fn.setReturnType(type);
- this.type = type;
- for (int i = 0; i < children.size(); ++i) {
- if (getChild(i).getType().isNull()) {
- uncheckedCastChild(Type.BOOLEAN, i);
- }
- }
- }
-
private static boolean match(String pattern, int pos, String value) {
int length = value.length();
int end = pattern.length();
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java
index 6677934f937..eb6439daebf 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java
@@ -22,7 +22,6 @@ import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
-import org.apache.doris.qe.ConnectContext;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
import org.apache.doris.thrift.TIntLiteral;
@@ -30,7 +29,6 @@ import org.apache.doris.thrift.TIntLiteral;
import com.google.common.base.Preconditions;
import com.google.gson.annotations.SerializedName;
-import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -268,48 +266,6 @@ public class IntLiteral extends NumericLiteralExpr {
msg.int_literal = new TIntLiteral(value);
}
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (targetType.isNumericType()) {
- if (targetType.isFixedPointType()) {
- if (!targetType.isScalarType(PrimitiveType.LARGEINT)) {
- if (!type.equals(targetType)) {
- IntLiteral intLiteral = new IntLiteral(this);
- intLiteral.setType(targetType);
- return intLiteral;
- }
- return this;
- } else {
- return new LargeIntLiteral(Long.toString(value));
- }
- } else if (targetType.isFloatingPointType()) {
- return new FloatLiteral(new Double(value), targetType);
- } else if (targetType.isDecimalV2() || targetType.isDecimalV3()) {
- DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
- res.setType(targetType);
- return res;
- }
- return this;
- } else if (targetType.isDateType()) {
- try {
- //int like 20200101 can be cast to date(2020,01,01)
- DateLiteral res = new DateLiteral("" + value, targetType);
- res.setType(targetType);
- return res;
- } catch (AnalysisException e) {
- if (ConnectContext.get() != null) {
- ConnectContext.get().getState().reset();
- }
- //invalid date format. leave it to BE to cast it as NULL
- }
- } else if (targetType.isStringType()) {
- StringLiteral res = new StringLiteral("" + value);
- res.setType(targetType);
- return res;
- }
- return super.uncheckedCastTo(targetType);
- }
-
@Override
public int hashCode() {
return 31 * super.hashCode() + Long.hashCode(value);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/JsonLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/JsonLiteral.java
index fdfc4cebe14..e617697d34f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/JsonLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/JsonLiteral.java
@@ -135,12 +135,6 @@ public class JsonLiteral extends LiteralExpr {
return getJsonValue();
}
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- // code should not be readched, since JSONB is analyzed as StringLiteral
- throw new AnalysisException("Unknown check type: " + targetType);
- }
-
@Override
public int hashCode() {
return 31 * super.hashCode() + Objects.hashCode(value);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
index 3a2d7f153fe..2ca09e8b6a4 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
@@ -217,24 +217,6 @@ public class LargeIntLiteral extends NumericLiteralExpr {
msg.large_int_literal = new TLargeIntLiteral(value.toString());
}
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (targetType.isFloatingPointType()) {
- return new FloatLiteral(new Double(value.doubleValue()), targetType);
- } else if (targetType.isDecimalV2() || targetType.isDecimalV3()) {
- DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
- res.setType(targetType);
- return res;
- } else if (targetType.isIntegerType()) {
- try {
- return new IntLiteral(value.longValueExact(), targetType);
- } catch (ArithmeticException e) {
- throw new AnalysisException("Number out of range[" + value + "]. type: " + targetType);
- }
- }
- return super.uncheckedCastTo(targetType);
- }
-
@Override
public int hashCode() {
return 31 * super.hashCode() + Objects.hashCode(value);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/LiteralExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/LiteralExpr.java
index 8aa3e45bbe3..36346b69c78 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/LiteralExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/LiteralExpr.java
@@ -22,7 +22,6 @@ package org.apache.doris.analysis;
import org.apache.doris.catalog.MysqlColType;
import org.apache.doris.catalog.PrimitiveType;
-import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.FormatOptions;
@@ -110,39 +109,6 @@ public abstract class LiteralExpr extends Expr implements Comparable<LiteralExpr
return literalExpr;
}
- public Expr convertTo(Type targetType) throws AnalysisException {
- Preconditions.checkArgument(!targetType.equals(Type.INVALID));
- if (this instanceof NullLiteral) {
- return NullLiteral.create(targetType);
- } else if (targetType.isBoolean()) {
- if (this instanceof StringLiteral || this instanceof JsonLiteral) {
- return new BoolLiteral(getStringValue());
- } else {
- if (getLongValue() != 0) {
- return new BoolLiteral(true);
- } else {
- return new BoolLiteral(false);
- }
- }
- } else if (targetType.isIntegerType()) {
- return new IntLiteral(getLongValue(), targetType);
- } else if (targetType.isLargeIntType()) {
- return new LargeIntLiteral(getStringValue());
- } else if (targetType.isFloatingPointType()) {
- return new FloatLiteral(getDoubleValue(), targetType);
- } else if (targetType.isDecimalV2() || targetType.isDecimalV3()) {
- DecimalLiteral literal = new DecimalLiteral(getStringValue(),
- ((ScalarType) targetType).getScalarScale());
- literal.setType(targetType);
- return literal;
- } else if (targetType.isStringType()) {
- return new StringLiteral(getStringValue());
- } else if (targetType.isDateType()) {
- return new StringLiteral(getStringValue()).convertToDate(targetType);
- }
- return this;
- }
-
public static LiteralExpr createInfinity(Type type, boolean isMax) throws AnalysisException {
Preconditions.checkArgument(!type.equals(Type.INVALID));
if (isMax) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/MapLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/MapLiteral.java
index 67c575331f2..6e5a7e1c2b5 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/MapLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/MapLiteral.java
@@ -23,13 +23,11 @@ import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.FormatOptions;
-import org.apache.doris.qe.SessionVariable;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
import org.apache.doris.thrift.TTypeDesc;
import org.apache.doris.thrift.TTypeNode;
-import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
@@ -56,94 +54,10 @@ public class MapLiteral extends LiteralExpr {
}
}
- public MapLiteral(LiteralExpr... exprs) throws AnalysisException {
- Type keyType = Type.NULL;
- Type valueType = Type.NULL;
- children = new ArrayList<>();
- // check types here
- // 1. limit key type with map-key support
- // 2. check type can be assigment for cast
- for (int idx = 0; idx < exprs.length && idx + 1 < exprs.length; idx += 2) {
- if (exprs[idx].getType().isComplexType() || !MapType.MAP.supportSubType(exprs[idx].getType())) {
- throw new AnalysisException("Invalid key type in Map, not support " + exprs[idx].getType());
- }
- if (!MapType.MAP.supportSubType(exprs[idx + 1].getType())) {
- throw new AnalysisException("Invalid value type in Map, not support " + exprs[idx + 1].getType());
- }
- boolean enableDecimal256 = SessionVariable.getEnableDecimal256();
- keyType = Type.getAssignmentCompatibleType(keyType, exprs[idx].getType(), true, enableDecimal256);
- valueType = Type.getAssignmentCompatibleType(valueType, exprs[idx + 1].getType(), true, enableDecimal256);
- }
-
- if (keyType == Type.INVALID) {
- throw new AnalysisException("Invalid key type in Map.");
- }
- if (valueType == Type.INVALID) {
- throw new AnalysisException("Invalid value type in Map.");
- }
-
- try {
- for (int idx = 0; idx < exprs.length && idx + 1 < exprs.length; idx += 2) {
- if (exprs[idx].getType().equals(keyType)) {
- children.add(exprs[idx]);
- } else {
- children.add(exprs[idx].convertTo(keyType));
- }
- if (exprs[idx + 1].getType().equals(valueType)) {
- children.add(exprs[idx + 1]);
- } else {
- children.add(exprs[idx + 1].convertTo(valueType));
- }
- }
- } catch (AnalysisException e) {
- String s = "{" + StringUtils.join(exprs, ',') + "}";
- throw new AnalysisException("Invalid Map " + s + " literal: " + e.getMessage());
- }
-
- type = new MapType(keyType, valueType);
- }
-
protected MapLiteral(MapLiteral other) {
super(other);
}
- @Override
- public LiteralExpr convertTo(Type targetType) throws AnalysisException {
- Preconditions.checkState(targetType instanceof MapType);
- Type keyType = ((MapType) targetType).getKeyType();
- Type valType = ((MapType) targetType).getValueType();
- LiteralExpr[] literals = new LiteralExpr[children.size()];
- for (int i = 0; i < children.size(); i += 2) {
- literals[i] = (LiteralExpr) Expr.convertLiteral(children.get(i), keyType);
- literals[i + 1] = (LiteralExpr) Expr.convertLiteral(children.get(i + 1), valType);
- }
- return new MapLiteral(literals);
- }
-
- @Override
- public Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (!targetType.isMapType()) {
- return super.uncheckedCastTo(targetType);
- }
- MapLiteral literal = new MapLiteral(this);
- Type keyType = ((MapType) targetType).getKeyType();
- Type valueType = ((MapType) targetType).getValueType();
-
- for (int i = 0; i < children.size() && i + 1 < children.size(); i += 2) {
- Expr key = Expr.convertLiteral(children.get(i), keyType);
- Expr value = Expr.convertLiteral(children.get(i + 1), valueType);
- // all children should be literal or else it will make be core
- if ((!key.isLiteral()) || (!value.isLiteral())) {
- throw new AnalysisException("Unexpected map literal cast failed. from type: "
- + this.type + ", to type: " + targetType);
- }
- literal.children.set(i, key);
- literal.children.set(i + 1, value);
- }
- literal.setType(targetType);
- return literal;
- }
-
@Override
public void checkValueValid() throws AnalysisException {
for (Expr e : children) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/NullLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/NullLiteral.java
index b5544d50c0f..ff6a6135037 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/NullLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/NullLiteral.java
@@ -30,8 +30,6 @@ import org.apache.doris.common.FormatOptions;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
-import com.google.common.base.Preconditions;
-
import java.nio.ByteBuffer;
public class NullLiteral extends LiteralExpr {
@@ -148,17 +146,6 @@ public class NullLiteral extends LiteralExpr {
return INT_EXPR.getHashValue(PrimitiveType.INT);
}
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- Preconditions.checkState(targetType.isValid());
- if (!type.equals(targetType)) {
- NullLiteral nullLiteral = new NullLiteral(this);
- nullLiteral.setType(targetType);
- return nullLiteral;
- }
- return this;
- }
-
@Override
protected void toThrift(TExprNode msg) {
msg.node_type = TExprNodeType.NULL_LITERAL;
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java
index 350285fe8c0..786e7eef0bb 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java
@@ -20,14 +20,10 @@
package org.apache.doris.analysis;
-import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.Type;
-import org.apache.doris.common.AnalysisException;
-import org.apache.doris.common.DdlException;
import org.apache.doris.common.FormatOptions;
-import org.apache.doris.qe.VariableVarConverters;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
import org.apache.doris.thrift.TStringLiteral;
@@ -38,7 +34,6 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.UnsupportedEncodingException;
-import java.math.BigDecimal;
import java.util.Objects;
public class StringLiteral extends LiteralExpr {
@@ -176,155 +171,6 @@ public class StringLiteral extends LiteralExpr {
return getStringValue();
}
- /**
- * Convert a string literal to a IPv4 literal
- *
- * @return new converted literal (not null)
- * @throws AnalysisException when entire given string cannot be transformed into a date
- */
- public LiteralExpr convertToIPv4() throws AnalysisException {
- LiteralExpr newLiteral;
- newLiteral = new IPv4Literal(value);
- try {
- newLiteral.checkValueValid();
- } catch (AnalysisException e) {
- return NullLiteral.create(newLiteral.getType());
- }
- return newLiteral;
- }
-
- /**
- * Convert a string literal to a IPv6 literal
- *
- * @return new converted literal (not null)
- * @throws AnalysisException when entire given string cannot be transformed into a date
- */
- public LiteralExpr convertToIPv6() throws AnalysisException {
- LiteralExpr newLiteral;
- newLiteral = new IPv6Literal(value);
- try {
- newLiteral.checkValueValid();
- } catch (AnalysisException e) {
- return NullLiteral.create(newLiteral.getType());
- }
- return newLiteral;
- }
-
- /**
- * Convert a string literal to a date literal
- *
- * @param targetType is the desired type
- * @return new converted literal (not null)
- * @throws AnalysisException when entire given string cannot be transformed into a date
- */
- public LiteralExpr convertToDate(Type targetType) throws AnalysisException {
- LiteralExpr newLiteral = null;
- try {
- newLiteral = new DateLiteral(value, targetType);
- } catch (AnalysisException e) {
- if (targetType.isScalarType(PrimitiveType.DATETIME)) {
- newLiteral = new DateLiteral(value, Type.DATE);
- newLiteral.setType(Type.DATETIME);
- } else if (targetType.isScalarType(PrimitiveType.DATETIMEV2)) {
- newLiteral = new DateLiteral(value, Type.DATEV2);
- newLiteral.setType(targetType);
- } else {
- throw e;
- }
- }
- try {
- newLiteral.checkValueValid();
- } catch (AnalysisException e) {
- return NullLiteral.create(newLiteral.getType());
- }
- return newLiteral;
- }
-
- public boolean canConvertToDateType(Type targetType) {
- try {
- Preconditions.checkArgument(targetType.isDateType());
- new DateLiteral(value, targetType);
- return true;
- } catch (AnalysisException e) {
- return false;
- }
- }
-
- @Override
- protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (targetType.isNumericType()) {
- switch (targetType.getPrimitiveType()) {
- case TINYINT:
- case SMALLINT:
- case INT:
- case BIGINT:
- if (VariableVarConverters.hasConverter(beConverted)) {
- try {
- return new IntLiteral(VariableVarConverters.encode(beConverted, value), targetType);
- } catch (DdlException e) {
- throw new AnalysisException(e.getMessage());
- }
- }
- return new IntLiteral(value, targetType);
- case LARGEINT:
- if (VariableVarConverters.hasConverter(beConverted)) {
- try {
- return new LargeIntLiteral(String.valueOf(
- VariableVarConverters.encode(beConverted, value)));
- } catch (DdlException e) {
- throw new AnalysisException(e.getMessage());
- }
- }
- return new LargeIntLiteral(value);
- case FLOAT:
- case DOUBLE:
- try {
- return new FloatLiteral(Double.valueOf(value), targetType);
- } catch (NumberFormatException e) {
- // consistent with CastExpr's getResultValue() method
- return NullLiteral.create(targetType);
- }
- case DECIMALV2:
- case DECIMAL32:
- case DECIMAL64:
- case DECIMAL128:
- case DECIMAL256:
- try {
- DecimalLiteral res = new DecimalLiteral(new BigDecimal(value).stripTrailingZeros());
- res.setType(targetType);
- return res;
- } catch (Exception e) {
- throw new AnalysisException(
- String.format("input value can't parse to decimal, value=%s", value));
- }
- default:
- break;
- }
- } else if (targetType.isDateType()) {
- // FE only support 'yyyy-MM-dd hh:mm:ss' && 'yyyy-MM-dd' format
- // so if FE unchecked cast fail, we also build CastExpr for BE
- // BE support other format such as 'yyyyMMdd'...
- try {
- return convertToDate(targetType);
- } catch (AnalysisException e) {
- // pass;
- }
- } else if (targetType.isIPv4()) {
- return convertToIPv4();
- } else if (targetType.isIPv6()) {
- return convertToIPv6();
- } else if (targetType.equals(type)) {
- return this;
- } else if (targetType.isStringType()) {
- StringLiteral stringLiteral = new StringLiteral(this);
- stringLiteral.setType(targetType);
- return stringLiteral;
- } else if (targetType.isJsonbType()) {
- return new JsonLiteral(value);
- }
- return super.uncheckedCastTo(targetType);
- }
-
@Override
public int hashCode() {
return 31 * super.hashCode() + Objects.hashCode(value);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/StructLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/StructLiteral.java
index 02020399307..b743d1396d3 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/StructLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/StructLiteral.java
@@ -29,7 +29,6 @@ import org.apache.doris.thrift.TExprNodeType;
import org.apache.doris.thrift.TTypeDesc;
import org.apache.doris.thrift.TTypeNode;
-import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
@@ -167,37 +166,6 @@ public class StructLiteral extends LiteralExpr {
return 0;
}
- @Override
- public LiteralExpr convertTo(Type targetType) throws AnalysisException {
- Preconditions.checkState(targetType instanceof StructType);
- List<StructField> fields = ((StructType) targetType).getFields();
- LiteralExpr[] literals = new LiteralExpr[children.size()];
- for (int i = 0; i < children.size(); i++) {
- literals[i] = (LiteralExpr) Expr.convertLiteral(children.get(i), fields.get(i).getType());
- }
- return new StructLiteral(literals);
- }
-
- @Override
- public Expr uncheckedCastTo(Type targetType) throws AnalysisException {
- if (!targetType.isStructType()) {
- return super.uncheckedCastTo(targetType);
- }
- ArrayList<StructField> fields = ((StructType) targetType).getFields();
- StructLiteral literal = new StructLiteral(this);
- for (int i = 0; i < children.size(); ++ i) {
- Expr child = Expr.convertLiteral(children.get(i), fields.get(i).getType());
- // all children should be literal or else it will make be core
- if (!child.isLiteral()) {
- throw new AnalysisException("Unexpected struct literal cast failed. from type: "
- + this.type + ", to type: " + targetType);
- }
- literal.children.set(i, child);
- }
- literal.setType(targetType);
- return literal;
- }
-
@Override
public void checkValueValid() throws AnalysisException {
for (Expr e : children) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/TryCastExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/TryCastExpr.java
index 6556ffa3e0d..a91b25cf6d9 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/TryCastExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/TryCastExpr.java
@@ -23,7 +23,6 @@ package org.apache.doris.analysis;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.Type;
-import org.apache.doris.common.AnalysisException;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
@@ -31,11 +30,6 @@ import org.apache.doris.thrift.TExprOpcode;
public class TryCastExpr extends CastExpr {
- public TryCastExpr(Type targetType, Expr e) {
- super(targetType, e);
- opcode = TExprOpcode.TRY_CAST;
- }
-
public TryCastExpr(Type targetType, Expr e, Void v) {
super(targetType, e, v);
opcode = TExprOpcode.TRY_CAST;
@@ -106,11 +100,6 @@ public class TryCastExpr extends CastExpr {
originCastNullable.ifPresent(msg::setIsCastNullable);
}
- public void analyze() throws AnalysisException {
- super.analyze();
- this.opcode = TExprOpcode.TRY_CAST;
- }
-
@Override
public int hashCode() {
return super.hashCode();
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
index 8b5596bda97..390a38a2dba 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
@@ -22,8 +22,6 @@ import org.apache.doris.analysis.DefaultValueExprDef;
import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.IndexDef;
import org.apache.doris.analysis.SlotRef;
-import org.apache.doris.analysis.StringLiteral;
-import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.CaseSensibility;
import org.apache.doris.common.Config;
import org.apache.doris.common.DdlException;
@@ -558,20 +556,18 @@ public class Column implements GsonPostProcessable {
return this.defaultValue;
}
- public Expr getDefaultValueExpr() throws AnalysisException {
+ public String getDefaultValueSql() {
if (defaultValue == null) {
return null;
}
- StringLiteral defaultValueLiteral = new StringLiteral(defaultValue);
- if (getDataType() == PrimitiveType.VARCHAR) {
- return defaultValueLiteral;
- }
if (defaultValueExprDef != null) {
- return defaultValueExprDef.getExpr(type);
+ return defaultValueExprDef.getSql();
+ }
+ if (this.type.isNumericType()) {
+ return defaultValue;
+ } else {
+ return "'" + defaultValue.replace("'", "''") + "'";
}
- Expr result = defaultValueLiteral.castTo(getType());
- result.checkValueValid();
- return result;
}
public void setComment(String comment) {
@@ -603,8 +599,8 @@ public class Column implements GsonPostProcessable {
return hasOnUpdateDefaultValue;
}
- public Expr getOnUpdateDefaultValueExpr() {
- return onUpdateDefaultValueExprDef.getExpr(type);
+ public String getOnUpdateDefaultValueSql() {
+ return onUpdateDefaultValueExprDef.getSql();
}
public TColumn toThrift() {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/FileScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/FileScanNode.java
index c79a134b220..8578b094897 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/FileScanNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/FileScanNode.java
@@ -31,7 +31,6 @@ import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.rules.analysis.ExpressionAnalyzer;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
-import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.VarcharType;
import org.apache.doris.nereids.util.TypeCoercionUtils;
@@ -215,15 +214,11 @@ public abstract class FileScanNode extends ExternalScanNode {
Expr expr;
Expression expression;
if (column.getDefaultValue() != null) {
- if (column.getDefaultValueExprDef() != null) {
- expression = new NereidsParser().parseExpression(
- column.getDefaultValueExpr().toSqlWithoutTbl());
- ExpressionAnalyzer analyzer = new ExpressionAnalyzer(
- null, new Scope(ImmutableList.of()), null, true, true);
- expression = analyzer.analyze(expression);
- } else {
- expression = new StringLiteral(column.getDefaultValue());
- }
+ expression = new NereidsParser().parseExpression(
+ column.getDefaultValueSql());
+ ExpressionAnalyzer analyzer = new ExpressionAnalyzer(
+ null, new Scope(ImmutableList.of()), null, true, true);
+ expression = analyzer.analyze(expression);
} else {
if (column.isAllowNull()) {
// For load, use Varchar as Null, for query, use column type.
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadPlanInfoCollector.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadPlanInfoCollector.java
index 628f34882a2..fe014958fc4 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadPlanInfoCollector.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadPlanInfoCollector.java
@@ -58,7 +58,6 @@ import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
-import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapTableSink;
@@ -478,19 +477,10 @@ public class NereidsLoadPlanInfoCollector extends DefaultPlanVisitor<Void, PlanT
if (column != null) {
Expression expression;
if (column.getDefaultValue() != null) {
- if (column.getDefaultValueExprDef() != null) {
- try {
- expression = new NereidsParser().parseExpression(
- column.getDefaultValueExpr().toSqlWithoutTbl());
- ExpressionAnalyzer analyzer = new ExpressionAnalyzer(
- null, new Scope(ImmutableList.of()), null, true, true);
- expression = analyzer.analyze(expression);
- } catch (org.apache.doris.common.AnalysisException e) {
- throw new AnalysisException(e.getMessage(), e.getCause());
- }
- } else {
- expression = new StringLiteral(column.getDefaultValue());
- }
+ expression = new NereidsParser().parseExpression(column.getDefaultValueSql());
+ ExpressionAnalyzer analyzer = new ExpressionAnalyzer(
+ null, new Scope(ImmutableList.of()), null, true, true);
+ expression = analyzer.analyze(expression);
} else {
if (column.isAllowNull()) {
expression = new NullLiteral(VarcharType.SYSTEM_DEFAULT);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadScanProvider.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadScanProvider.java
index d690c4e57c9..0e34e697c55 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadScanProvider.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/load/NereidsLoadScanProvider.java
@@ -536,12 +536,8 @@ public class NereidsLoadScanProvider {
exprs.add(funcExpr.child(1));
} else {
if (column.getDefaultValue() != null) {
- if (column.getDefaultValueExprDef() != null) {
- String exprSql = column.getDefaultValueExpr().toSql();
- exprs.add(NereidsLoadUtils.parseExpressionSeq(exprSql).get(0));
- } else {
- exprs.add(new StringLiteral(column.getDefaultValue()));
- }
+ String exprSql = column.getDefaultValueSql();
+ exprs.add(NereidsLoadUtils.parseExpressionSeq(exprSql).get(0));
} else {
if (column.isAllowNull()) {
exprs.add(new NullLiteral(VarcharType.SYSTEM_DEFAULT));
@@ -560,12 +556,8 @@ public class NereidsLoadScanProvider {
innerIfExprs.add(funcExpr.child(1));
} else {
if (column.getDefaultValue() != null) {
- if (column.getDefaultValueExprDef() != null) {
- String exprSql = column.getDefaultValueExpr().toSql();
- innerIfExprs.add(NereidsLoadUtils.parseExpressionSeq(exprSql).get(0));
- } else {
- innerIfExprs.add(new StringLiteral(column.getDefaultValue()));
- }
+ String exprSql = column.getDefaultValueSql();
+ innerIfExprs.add(NereidsLoadUtils.parseExpressionSeq(exprSql).get(0));
} else {
if (column.isAllowNull()) {
innerIfExprs.add(new NullLiteral(VarcharType.SYSTEM_DEFAULT));
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java
index 0b5d4c9d00d..07ebaab8965 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSink.java
@@ -405,7 +405,7 @@ public class BindSink implements AnalysisRuleFactory {
// is an update on the row
if (column.hasOnUpdateDefaultValue()) {
Expression unboundFunctionDefaultValue = new NereidsParser().parseExpression(
- column.getOnUpdateDefaultValueExpr().toSqlWithoutTbl()
+ column.getOnUpdateDefaultValueSql()
);
Expression defualtValueExpression = ExpressionAnalyzer.analyzeFunction(
boundSink, ctx.cascadesContext, unboundFunctionDefaultValue
@@ -435,29 +435,19 @@ public class BindSink implements AnalysisRuleFactory {
replaceMap.put(output.toSlot(), output.child());
} else {
try {
- // it comes from the original planner, if default value expression is
- // null, we use the literal string of the default value, or it may be
- // default value function, like CURRENT_TIMESTAMP.
- if (column.getDefaultValueExpr() == null) {
- columnToOutput.put(column.getName(),
- new Alias(Literal.of(column.getDefaultValue())
- .checkedCastWithFallback(DataType.fromCatalogType(column.getType())),
- column.getName()));
- } else {
- Expression unboundDefaultValue = new NereidsParser().parseExpression(
- column.getDefaultValueExpr().toSqlWithoutTbl());
- Expression defualtValueExpression = ExpressionAnalyzer.analyzeFunction(
- boundSink, ctx.cascadesContext, unboundDefaultValue);
- if (defualtValueExpression instanceof Alias) {
- defualtValueExpression = ((Alias) defualtValueExpression).child();
- }
- Alias output = new Alias((TypeCoercionUtils.castIfNotSameType(
- defualtValueExpression, DataType.fromCatalogType(column.getType()))),
- column.getName());
- columnToOutput.put(column.getName(), output);
- columnToReplaced.put(column.getName(), output.toSlot());
- replaceMap.put(output.toSlot(), output.child());
+ Expression unboundDefaultValue = new NereidsParser().parseExpression(
+ column.getDefaultValueSql());
+ Expression defualtValueExpression = ExpressionAnalyzer.analyzeFunction(
+ boundSink, ctx.cascadesContext, unboundDefaultValue);
+ if (defualtValueExpression instanceof Alias) {
+ defualtValueExpression = ((Alias) defualtValueExpression).child();
}
+ Alias output = new Alias((TypeCoercionUtils.castIfNotSameType(
+ defualtValueExpression, DataType.fromCatalogType(column.getType()))),
+ column.getName());
+ columnToOutput.put(column.getName(), output);
+ columnToReplaced.put(column.getName(), output.toSlot());
+ replaceMap.put(output.toSlot(), output.child());
} catch (Exception e) {
throw new AnalysisException(e.getMessage(), e.getCause());
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java
index 4bb08164836..c439af0625b 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java
@@ -147,8 +147,7 @@ public class UpdateCommand extends Command implements ForwardWithSync, Explainab
selectItems.add(new UnboundAlias(setExpr, column.getName()));
} else if (column.hasOnUpdateDefaultValue()) {
Expression defualtValueExpression =
- new NereidsParser().parseExpression(column.getOnUpdateDefaultValueExpr()
- .toSqlWithoutTbl());
+ new NereidsParser().parseExpression(column.getOnUpdateDefaultValueSql());
selectItems.add(new UnboundAlias(defualtValueExpression, column.getName()));
} else {
selectItems.add(new UnboundSlot(tableName, column.getName()));
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
index 5549e4ee662..d0e7a425b00 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
@@ -585,33 +585,26 @@ public class InsertUtils {
}
private static NamedExpression generateDefaultExpression(Column column) {
- try {
- GeneratedColumnInfo generatedColumnInfo = column.getGeneratedColumnInfo();
- // Using NullLiteral as a placeholder.
- // If return the expr in generatedColumnInfo, will lead to slot not found error in analyze.
- // Instead, getting the generated column expr and analyze the expr in BindSink can avoid the error.
- if (generatedColumnInfo != null) {
- return new Alias(new NullLiteral(DataType.fromCatalogType(column.getType())), column.getName());
- }
- if (column.getDefaultValue() == null) {
- if (!column.isAllowNull() && !column.isAutoInc()) {
- throw new AnalysisException("Column has no default value, column=" + column.getName());
- }
+ GeneratedColumnInfo generatedColumnInfo = column.getGeneratedColumnInfo();
+ // Using NullLiteral as a placeholder.
+ // If return the expr in generatedColumnInfo, will lead to slot not found error in analyze.
+ // Instead, getting the generated column expr and analyze the expr in BindSink can avoid the error.
+ if (generatedColumnInfo != null) {
+ return new Alias(new NullLiteral(DataType.fromCatalogType(column.getType())), column.getName());
+ }
+ if (column.getDefaultValue() == null) {
+ if (!column.isAllowNull() && !column.isAutoInc()) {
+ throw new AnalysisException("Column has no default value, column=" + column.getName());
}
- if (column.getDefaultValueExpr() != null) {
- Expression defualtValueExpression = new NereidsParser().parseExpression(
- column.getDefaultValueExpr().toSqlWithoutTbl());
- if (!(defualtValueExpression instanceof UnboundAlias)) {
- defualtValueExpression = new UnboundAlias(defualtValueExpression);
- }
- return (NamedExpression) defualtValueExpression;
- } else {
- return new Alias(Literal.of(column.getDefaultValue())
- .checkedCastWithFallback(DataType.fromCatalogType(column.getType())),
- column.getName());
+ return new Alias(Literal.of(column.getDefaultValue()).checkedCastWithFallback(
+ DataType.fromCatalogType(column.getType())), column.getName());
+ } else {
+ Expression defualtValueExpression = new NereidsParser().parseExpression(
+ column.getDefaultValueSql());
+ if (!(defualtValueExpression instanceof UnboundAlias)) {
+ defualtValueExpression = new UnboundAlias(defualtValueExpression);
}
- } catch (org.apache.doris.common.AnalysisException e) {
- throw new AnalysisException(e.getMessage(), e);
+ return (NamedExpression) defualtValueExpression;
}
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
index 81aabcf43b4..75a0d26d5d6 100755
--- a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
@@ -17,7 +17,6 @@
package org.apache.doris.rewrite;
-import org.apache.doris.analysis.ArrayLiteral;
import org.apache.doris.analysis.DateLiteral;
import org.apache.doris.analysis.DecimalLiteral;
import org.apache.doris.analysis.FloatLiteral;
@@ -65,18 +64,6 @@ public class FEFunctions {
return new FloatLiteral((double) (firstTimestamp - secondTimestamp) * 1000, Type.TIMEV2);
}
- @FEFunction(name = "datediff", argTypes = { "DATETIME", "DATETIME" }, returnType = "INT")
- public static IntLiteral dateDiff(LiteralExpr first, LiteralExpr second) throws AnalysisException {
- DateLiteral firstDate = ((DateLiteral) first);
- DateLiteral secondDate = ((DateLiteral) second);
- // DATEDIFF function only uses the date part for calculations and ignores the time part
- firstDate.castToDate();
- secondDate.castToDate();
- long datediff = (firstDate.unixTimestamp(TimeUtils.getTimeZone())
- - secondDate.unixTimestamp(TimeUtils.getTimeZone())) / 1000 / 60 / 60 / 24;
- return new IntLiteral(datediff, Type.INT);
- }
-
@FEFunction(name = "dayofweek", argTypes = {"DATETIME"}, returnType = "TINYINT")
public static IntLiteral dayOfWeek(LiteralExpr date) throws AnalysisException {
// use zellar algorithm.
@@ -247,12 +234,6 @@ public class FEFunctions {
return dateLiteral.plusSeconds(second.getLongValue());
}
- @FEFunction(name = "date_format", argTypes = { "DATETIME", "VARCHAR" }, returnType = "VARCHAR")
- public static StringLiteral dateFormat(LiteralExpr date, StringLiteral fmtLiteral) throws AnalysisException {
- String result = ((DateLiteral) date).dateFormat(fmtLiteral.getStringValue());
- return new StringLiteral(result);
- }
-
@FEFunction(name = "str_to_date", argTypes = { "VARCHAR", "VARCHAR" }, returnType = "DATETIMEV2")
public static DateLiteral dateParse(StringLiteral date, StringLiteral fmtLiteral) throws AnalysisException {
DateLiteral dateLiteral = new DateLiteral();
@@ -425,18 +406,6 @@ public class FEFunctions {
return new StringLiteral(dl.getStringValue());
}
- @FEFunction(name = "from_unixtime", argTypes = { "BIGINT", "VARCHAR" }, returnType = "VARCHAR")
- public static StringLiteral fromUnixTime(LiteralExpr unixTime, StringLiteral fmtLiteral) throws AnalysisException {
- // if unixTime < 0, we should return null, throw a exception and let BE process
- // 32536771199L is max valid timestamp of mysql from_unix_time
- if (unixTime.getLongValue() < 0 || unixTime.getLongValue() >= 32536771199L) {
- throw new AnalysisException("unix timestamp out of range");
- }
- DateLiteral dl = new DateLiteral(unixTime.getLongValue() * 1000, TimeUtils.getTimeZone(),
- Type.DATETIME);
- return new StringLiteral(dl.dateFormat(fmtLiteral.getStringValue()));
- }
-
@FEFunction(name = "now", argTypes = {}, returnType = "DATETIME")
public static DateLiteral now() throws AnalysisException {
return new DateLiteral(LocalDateTime.now(TimeUtils.getTimeZone().toZoneId()),
@@ -1050,23 +1019,4 @@ public class FEFunctions {
return first instanceof NullLiteral ? second : first;
}
- @FEFunctionList({
- @FEFunction(name = "array", argTypes = {"BOOLEAN"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"TINYINT"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"SMALLINT"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"INT"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"BIGINT"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"LARGEINT"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"DATETIME"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"DATE"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"FLOAT"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"DOUBLE"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"DECIMALV2"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"VARCHAR"}, returnType = "ARRAY"),
- @FEFunction(name = "array", argTypes = {"STRING"}, returnType = "ARRAY")
- })
- public static ArrayLiteral array(LiteralExpr... exprs) throws AnalysisException {
- return new ArrayLiteral(exprs);
- }
-
}
diff --git a/fe/fe-core/src/test/java/org/apache/doris/external/elasticsearch/QueryBuildersTest.java b/fe/fe-core/src/test/java/org/apache/doris/external/elasticsearch/QueryBuildersTest.java
index 69eab5198aa..593f12f83da 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/external/elasticsearch/QueryBuildersTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/external/elasticsearch/QueryBuildersTest.java
@@ -243,7 +243,7 @@ public class QueryBuildersTest {
@Test
public void testCastConvertEsDsl() {
FloatLiteral floatLiteral = new FloatLiteral(3.14);
- CastExpr castExpr = new CastExpr(Type.INT, floatLiteral);
+ CastExpr castExpr = new CastExpr(Type.INT, floatLiteral, null);
BinaryPredicate castPredicate = new BinaryPredicate(Operator.EQ, castExpr, new IntLiteral(3));
List<Expr> notPushDownList = new ArrayList<>();
Map<String, String> fieldsContext = new HashMap<>();
@@ -264,7 +264,7 @@ public class QueryBuildersTest {
SlotRef k3 = new SlotRef(null, "k3");
k3.setType(Type.FLOAT);
- CastExpr castDoubleExpr = new CastExpr(Type.DOUBLE, k3);
+ CastExpr castDoubleExpr = new CastExpr(Type.DOUBLE, k3, null);
BinaryPredicate castDoublePredicate = new BinaryPredicate(Operator.GE, castDoubleExpr,
new FloatLiteral(3.0, Type.DOUBLE));
QueryBuilders.toEsDsl(castDoublePredicate, notPushDownList, fieldsContext, builderOptions, col2typeMap);
@@ -272,7 +272,7 @@ public class QueryBuildersTest {
SlotRef k4 = new SlotRef(null, "k4");
k4.setType(Type.FLOAT);
- CastExpr castFloatExpr = new CastExpr(Type.FLOAT, k4);
+ CastExpr castFloatExpr = new CastExpr(Type.FLOAT, k4, null);
BinaryPredicate castFloatPredicate = new BinaryPredicate(Operator.GE, new FloatLiteral(3.0, Type.FLOAT),
castFloatExpr);
QueryBuilders.QueryBuilder queryBuilder = QueryBuilders.toEsDsl(castFloatPredicate, notPushDownList,
diff --git a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
deleted file mode 100644
index ce3b0c6a0c8..00000000000
--- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
+++ /dev/null
@@ -1,840 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-package org.apache.doris.rewrite;
-
-import org.apache.doris.analysis.DateLiteral;
-import org.apache.doris.analysis.DecimalLiteral;
-import org.apache.doris.analysis.FloatLiteral;
-import org.apache.doris.analysis.IntLiteral;
-import org.apache.doris.analysis.LargeIntLiteral;
-import org.apache.doris.analysis.StringLiteral;
-import org.apache.doris.catalog.Type;
-import org.apache.doris.common.AnalysisException;
-import org.apache.doris.common.Config;
-import org.apache.doris.common.util.TimeUtils;
-
-import mockit.Expectations;
-import mockit.Mocked;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import java.time.ZoneId;
-import java.time.ZonedDateTime;
-import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeFormatterBuilder;
-import java.time.temporal.ChronoField;
-import java.util.Locale;
-import java.util.TimeZone;
-
-public class FEFunctionsTest {
-
- @Mocked
- TimeUtils timeUtils;
-
- @Rule
- public ExpectedException expectedEx = ExpectedException.none();
-
- @Before
- public void setUp() {
- TimeZone tz = TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai"));
- new Expectations(timeUtils) {
- {
- TimeUtils.getTimeZone();
- minTimes = 0;
- result = tz;
- }
- };
- }
-
- @Test
- public void unixtimestampTest() {
- try {
- IntLiteral timestamp = FEFunctions.unixTimestamp(new DateLiteral("2018-01-01", Type.DATE));
- Assert.assertEquals(1514736000, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("1970-01-01 08:00:00", Type.DATETIME));
- Assert.assertEquals(0, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("1970-01-01 00:00:00", Type.DATETIME));
- Assert.assertEquals(0, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("1969-01-01 00:00:00", Type.DATETIME));
- Assert.assertEquals(0, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("2038-01-19 03:14:07", Type.DATETIME));
- // CST time zone
- Assert.assertEquals(Integer.MAX_VALUE - 8 * 3600, timestamp.getValue());
-
- } catch (AnalysisException e) {
- e.printStackTrace();
- Assert.fail();
- }
- }
-
- @Test
- public void unixtimestampV2Test() {
- try {
- IntLiteral timestamp = FEFunctions.unixTimestamp(new DateLiteral("2018-01-01", Type.DATEV2));
- Assert.assertEquals(1514736000, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("1970-01-01 08:00:00", Type.DATETIMEV2));
- Assert.assertEquals(0, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("1970-01-01 00:00:00", Type.DATETIMEV2));
- Assert.assertEquals(0, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("1969-01-01 00:00:00", Type.DATETIMEV2));
- Assert.assertEquals(0, timestamp.getValue());
- timestamp = FEFunctions.unixTimestamp(new DateLiteral("2038-01-19 03:14:07", Type.DATETIMEV2));
- // CST time zone
- Assert.assertEquals(Integer.MAX_VALUE - 8 * 3600, timestamp.getValue());
-
- } catch (AnalysisException e) {
- e.printStackTrace();
- Assert.fail();
- }
- }
-
- @Test
- public void dateDiffTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.dateDiff(new DateLiteral("2010-11-30 23:59:59", Type.DATETIME),
- new DateLiteral("2010-12-31", Type.DATE));
- IntLiteral expectedResult = new IntLiteral(-31);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateDiff(new DateLiteral("2010-11-30 23:59:50", Type.DATETIME),
- new DateLiteral("2010-12-30 23:59:59", Type.DATETIME));
- expectedResult = new IntLiteral(-30);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void dateV2DiffTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.dateDiff(new DateLiteral("2010-11-30 23:59:59", Type.DATETIMEV2),
- new DateLiteral("2010-12-31", Type.DATEV2));
- IntLiteral expectedResult = new IntLiteral(-31);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateDiff(new DateLiteral("2010-11-30 23:59:59", Type.DATETIMEV2),
- new DateLiteral("2010-12-31", Type.DATE));
- expectedResult = new IntLiteral(-31);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateDiff(new DateLiteral("2010-11-30 23:59:50", Type.DATETIMEV2),
- new DateLiteral("2010-12-30 23:59:59", Type.DATETIMEV2));
- expectedResult = new IntLiteral(-30);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateDiff(new DateLiteral("2010-11-30 23:59:50", Type.DATETIMEV2),
- new DateLiteral("2010-12-30 23:59:59", Type.DATETIME));
- expectedResult = new IntLiteral(-30);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void dateAddTest() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE),
- new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void dateV2AddTest() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void addDateTest() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
-
- }
-
- @Test
- public void addDateV2Test() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void daysAddTest() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void daysAddTest2() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void dayOfWeekTest() throws AnalysisException {
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-23", Type.DATE)).getStringValue(), "1");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-24", Type.DATE)).getStringValue(), "2");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-25", Type.DATE)).getStringValue(), "3");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-26", Type.DATE)).getStringValue(), "4");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-27", Type.DATE)).getStringValue(), "5");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-28", Type.DATE)).getStringValue(), "6");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-29", Type.DATE)).getStringValue(), "7");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-13", Type.DATE)).getStringValue(), "2");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-14", Type.DATE)).getStringValue(), "3");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-15", Type.DATE)).getStringValue(), "4");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-16", Type.DATE)).getStringValue(), "5");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-17", Type.DATE)).getStringValue(), "6");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-18", Type.DATE)).getStringValue(), "7");
- Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-19", Type.DATE)).getStringValue(), "1");
- }
-
- @Test
- public void fromUnixTimeTest() throws AnalysisException {
- StringLiteral actualResult = FEFunctions.fromUnixTime(new IntLiteral(100000));
- StringLiteral expectedResult = new StringLiteral("1970-01-02 11:46:40");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.fromUnixTime(new IntLiteral(100000), new StringLiteral("%Y-%m-%d"));
- expectedResult = new StringLiteral("1970-01-02");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.fromUnixTime(new IntLiteral(0));
- expectedResult = new StringLiteral("1970-01-01 08:00:00");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void fromUnixTimeTestException() throws AnalysisException {
- expectedEx.expect(AnalysisException.class);
- expectedEx.expectMessage("unix timestamp out of range");
- FEFunctions.fromUnixTime(new IntLiteral(-100));
- }
-
- @Test
- public void dateFormatUtilTest() throws AnalysisException {
- Locale.setDefault(Locale.ENGLISH);
- DateLiteral testDate = new DateLiteral("2001-01-09 13:04:05", Type.DATETIME);
- Assert.assertEquals("Tue", FEFunctions.dateFormat(testDate, new StringLiteral("%a")).getStringValue());
- Assert.assertEquals("Jan", FEFunctions.dateFormat(testDate, new StringLiteral("%b")).getStringValue());
- Assert.assertEquals("1", FEFunctions.dateFormat(testDate, new StringLiteral("%c")).getStringValue());
- Assert.assertEquals("09", FEFunctions.dateFormat(testDate, new StringLiteral("%d")).getStringValue());
- Assert.assertEquals("9", FEFunctions.dateFormat(testDate, new StringLiteral("%e")).getStringValue());
- Assert.assertEquals("13", FEFunctions.dateFormat(testDate, new StringLiteral("%H")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%h")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%I")).getStringValue());
- Assert.assertEquals("04", FEFunctions.dateFormat(testDate, new StringLiteral("%i")).getStringValue());
- Assert.assertEquals("009", FEFunctions.dateFormat(testDate, new StringLiteral("%j")).getStringValue());
- Assert.assertEquals("13", FEFunctions.dateFormat(testDate, new StringLiteral("%k")).getStringValue());
- Assert.assertEquals("1", FEFunctions.dateFormat(testDate, new StringLiteral("%l")).getStringValue());
- Assert.assertEquals("January", FEFunctions.dateFormat(testDate, new StringLiteral("%M")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%m")).getStringValue());
- Assert.assertEquals("PM", FEFunctions.dateFormat(testDate, new StringLiteral("%p")).getStringValue());
- Assert.assertEquals("01:04:05 PM", FEFunctions.dateFormat(testDate,
- new StringLiteral("%r")).getStringValue());
- Assert.assertEquals("05", FEFunctions.dateFormat(testDate, new StringLiteral("%S")).getStringValue());
- Assert.assertEquals("05", FEFunctions.dateFormat(testDate, new StringLiteral("%s")).getStringValue());
- Assert.assertEquals("13:04:05", FEFunctions.dateFormat(testDate,
- new StringLiteral("%T")).getStringValue());
- Assert.assertEquals("02", FEFunctions.dateFormat(testDate, new StringLiteral("%v")).getStringValue());
- Assert.assertEquals("Tuesday", FEFunctions.dateFormat(testDate, new StringLiteral("%W")).getStringValue());
- Assert.assertEquals("2001", FEFunctions.dateFormat(testDate, new StringLiteral("%Y")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%y")).getStringValue());
- Assert.assertEquals("%", FEFunctions.dateFormat(testDate, new StringLiteral("%%")).getStringValue());
- Assert.assertEquals("foo", FEFunctions.dateFormat(testDate, new StringLiteral("foo")).getStringValue());
- Assert.assertEquals("g", FEFunctions.dateFormat(testDate, new StringLiteral("%g")).getStringValue());
- Assert.assertEquals("4", FEFunctions.dateFormat(testDate, new StringLiteral("%4")).getStringValue());
- Assert.assertEquals("2001 02", FEFunctions.dateFormat(testDate, new StringLiteral("%x %v")).getStringValue());
- Assert.assertEquals("2021 52 2022 01", FEFunctions.dateFormat(new DateLiteral("2022-01-02 12:04:05", Type.DATETIME),
- new StringLiteral("%x %v %X %V")).getStringValue());
- Assert.assertEquals("2023 18 2023 19", FEFunctions.dateFormat(new DateLiteral("2023-05-07 12:04:54", Type.DATETIME),
- new StringLiteral("%x %v %X %V")).getStringValue());
- }
-
- @Test
- public void dateV2FormatUtilTest() throws AnalysisException {
- Locale.setDefault(Locale.ENGLISH);
- DateLiteral testDate = new DateLiteral("2001-01-09 13:04:05", Type.DATETIMEV2);
- Assert.assertEquals("Tue", FEFunctions.dateFormat(testDate, new StringLiteral("%a")).getStringValue());
- Assert.assertEquals("Jan", FEFunctions.dateFormat(testDate, new StringLiteral("%b")).getStringValue());
- Assert.assertEquals("1", FEFunctions.dateFormat(testDate, new StringLiteral("%c")).getStringValue());
- Assert.assertEquals("09", FEFunctions.dateFormat(testDate, new StringLiteral("%d")).getStringValue());
- Assert.assertEquals("9", FEFunctions.dateFormat(testDate, new StringLiteral("%e")).getStringValue());
- Assert.assertEquals("13", FEFunctions.dateFormat(testDate, new StringLiteral("%H")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%h")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%I")).getStringValue());
- Assert.assertEquals("04", FEFunctions.dateFormat(testDate, new StringLiteral("%i")).getStringValue());
- Assert.assertEquals("009", FEFunctions.dateFormat(testDate, new StringLiteral("%j")).getStringValue());
- Assert.assertEquals("13", FEFunctions.dateFormat(testDate, new StringLiteral("%k")).getStringValue());
- Assert.assertEquals("1", FEFunctions.dateFormat(testDate, new StringLiteral("%l")).getStringValue());
- Assert.assertEquals("January", FEFunctions.dateFormat(testDate, new StringLiteral("%M")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%m")).getStringValue());
- Assert.assertEquals("PM", FEFunctions.dateFormat(testDate, new StringLiteral("%p")).getStringValue());
- Assert.assertEquals("01:04:05 PM", FEFunctions.dateFormat(testDate,
- new StringLiteral("%r")).getStringValue());
- Assert.assertEquals("05", FEFunctions.dateFormat(testDate, new StringLiteral("%S")).getStringValue());
- Assert.assertEquals("05", FEFunctions.dateFormat(testDate, new StringLiteral("%s")).getStringValue());
- Assert.assertEquals("13:04:05", FEFunctions.dateFormat(testDate,
- new StringLiteral("%T")).getStringValue());
- Assert.assertEquals("02", FEFunctions.dateFormat(testDate, new StringLiteral("%v")).getStringValue());
- Assert.assertEquals("Tuesday", FEFunctions.dateFormat(testDate, new StringLiteral("%W")).getStringValue());
- Assert.assertEquals("2001", FEFunctions.dateFormat(testDate, new StringLiteral("%Y")).getStringValue());
- Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%y")).getStringValue());
- Assert.assertEquals("%", FEFunctions.dateFormat(testDate, new StringLiteral("%%")).getStringValue());
- Assert.assertEquals("foo", FEFunctions.dateFormat(testDate, new StringLiteral("foo")).getStringValue());
- Assert.assertEquals("g", FEFunctions.dateFormat(testDate, new StringLiteral("%g")).getStringValue());
- Assert.assertEquals("4", FEFunctions.dateFormat(testDate, new StringLiteral("%4")).getStringValue());
- Assert.assertEquals("2001 02", FEFunctions.dateFormat(testDate, new StringLiteral("%x %v")).getStringValue());
- Assert.assertEquals("2021 52 2022 01", FEFunctions.dateFormat(new DateLiteral("2022-01-02 12:04:05", Type.DATETIME),
- new StringLiteral("%x %v %X %V")).getStringValue());
- Assert.assertEquals("2023 18 2023 19", FEFunctions.dateFormat(new DateLiteral("2023-05-07 12:04:54", Type.DATETIME),
- new StringLiteral("%x %v %X %V")).getStringValue());
- }
-
- @Test
- public void dateParseTest() {
- try {
- // TODO: "2019-05-09 00:00:00" need to get type datetime(0), not datetime(6)
- // Assert.assertEquals("2019-05-09 00:00:00", FEFunctions.dateParse(new StringLiteral("2019-05-09"),
- // new StringLiteral("%Y-%m-%d %H:%i:%s")).getStringValue());
- Assert.assertEquals("2013-05-10", FEFunctions.dateParse(new StringLiteral("2013,05,10"),
- new StringLiteral("%Y,%m,%d")).getStringValue());
- Assert.assertEquals("2013-05-17 00:35:10", FEFunctions.dateParse(
- new StringLiteral("2013-05-17 12:35:10"),
- new StringLiteral("%Y-%m-%d %h:%i:%s")).getStringValue());
- Assert.assertEquals("2013-05-17 00:35:10", FEFunctions.dateParse(new StringLiteral("2013-05-17 00:35:10"),
- new StringLiteral("%Y-%m-%d %H:%i:%s")).getStringValue());
- Assert.assertEquals("2013-05-17 00:35:10", FEFunctions.dateParse(
- new StringLiteral("2013-05-17 12:35:10 AM"),
- new StringLiteral("%Y-%m-%d %h:%i:%s %p")).getStringValue());
- Assert.assertEquals("2013-05-17 12:35:10", FEFunctions.dateParse(
- new StringLiteral("2013-05-17 12:35:10 PM"),
- new StringLiteral("%Y-%m-%d %h:%i:%s %p")).getStringValue());
- Assert.assertEquals("2013-05-17 23:35:10", FEFunctions.dateParse(
- new StringLiteral("abc 2013-05-17 fff 23:35:10 xyz"),
- new StringLiteral("abc %Y-%m-%d fff %H:%i:%s xyz")).getStringValue());
- Assert.assertEquals("2016-01-28 23:45:46", FEFunctions.dateParse(
- new StringLiteral("28-JAN-16 11.45.46 PM"),
- new StringLiteral("%d-%b-%y %l.%i.%s %p")).getStringValue());
- Assert.assertEquals("2019-05-09", FEFunctions.dateParse(new StringLiteral("2019/May/9"),
- new StringLiteral("%Y/%b/%d")).getStringValue());
- Assert.assertEquals("2019-05-09", FEFunctions.dateParse(new StringLiteral("2019,129"),
- new StringLiteral("%Y,%j")).getStringValue());
- Assert.assertEquals("2019-05-09", FEFunctions.dateParse(new StringLiteral("2019,19,Thursday"),
- new StringLiteral("%x,%v,%W")).getStringValue());
- // Assert.assertEquals("2019-05-09 12:10:45", FEFunctions.dateParse(new StringLiteral("12:10:45-20190509"),
- // new StringLiteral("%T-%Y%m%d")).getStringValue());
- Assert.assertEquals("2019-05-09 09:10:45", FEFunctions.dateParse(new StringLiteral("20190509-9:10:45"),
- new StringLiteral("%Y%m%d-%k:%i:%S")).getStringValue());
- Assert.assertEquals("0000-00-20", FEFunctions.dateParse(new StringLiteral("2013-05-17"),
- new StringLiteral("%D")).getStringValue());
- Assert.assertEquals("0000-00-00", FEFunctions.dateParse(new StringLiteral("2013-05-17"),
- new StringLiteral("%U")).getStringValue());
- Assert.assertEquals("0000-00-00", FEFunctions.dateParse(new StringLiteral("2013-05-17"),
- new StringLiteral("%u")).getStringValue());
- Assert.assertEquals("0000-00-00", FEFunctions.dateParse(new StringLiteral("2013-05-17"),
- new StringLiteral("%V")).getStringValue());
- Assert.assertEquals("0000-00-00", FEFunctions.dateParse(new StringLiteral("2013-05-17"),
- new StringLiteral("%w")).getStringValue());
- Assert.assertEquals("0000-00-00", FEFunctions.dateParse(new StringLiteral("2013-05-17"),
- new StringLiteral("%x")).getStringValue());
- Assert.assertEquals("0000-00-00", FEFunctions.dateParse(new StringLiteral("2013-05-17"),
- new StringLiteral("%X")).getStringValue());
- // Assert.assertEquals("2013-05-17 20:07:05", FEFunctions.dateParse(
- // new StringLiteral("2013-05-17 08:07:05 PM"), new StringLiteral("%Y-%m-%d %r")).getStringValue());
- // Assert.assertEquals("2013-05-17 08:07:05", FEFunctions.dateParse(new StringLiteral("2013-05-17 08:07:05"),
- // new StringLiteral("%Y-%m-%d %T")).getStringValue());
- Assert.assertEquals("2021 52 2021 52", FEFunctions.dateFormat(new DateLiteral("2022-01-01 00:12:42", Type.DATETIMEV2),
- new StringLiteral("%x %v %X %V")).getStringValue());
- Assert.assertEquals("2023 18 2023 19", FEFunctions.dateFormat(new DateLiteral("2023-05-07 02:41:42", Type.DATETIMEV2),
- new StringLiteral("%x %v %X %V")).getStringValue());
- } catch (AnalysisException e) {
- e.printStackTrace();
- Assert.fail("Junit test dateParse fail");
- }
-
- try {
- FEFunctions.dateParse(new StringLiteral("2013-05-17"), new StringLiteral("%W"));
- Assert.fail("Junit test dateParse fail");
- } catch (AnalysisException e) {
- Assert.assertEquals(e.getMessage(), "errCode = 2, detailMessage = '' is invalid");
- }
- }
-
- @Test
- public void dateSubTest() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-07", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-09", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void dateV2SubTest() throws AnalysisException {
- DateLiteral actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
- DateLiteral expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
- expectedResult = new DateLiteral("2018-08-07", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-09", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-09", Type.DATE);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
- expectedResult = new DateLiteral("2018-08-09", Type.DATEV2);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void yearTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.year(new DateLiteral("2018-08-08", Type.DATE));
- IntLiteral expectedResult = new IntLiteral(2018, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.year(new DateLiteral("1970-01-02 11:46:40", Type.DATETIME));
- expectedResult = new IntLiteral(1970, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void yearTest2() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.year(new DateLiteral("2018-08-08", Type.DATE));
- IntLiteral expectedResult = new IntLiteral(2018, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.year(new DateLiteral("2018-08-08", Type.DATEV2));
- expectedResult = new IntLiteral(2018, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.year(new DateLiteral("1970-01-02 11:46:40", Type.DATETIME));
- expectedResult = new IntLiteral(1970, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.year(new DateLiteral("1970-01-02 11:46:40", Type.DATETIMEV2));
- expectedResult = new IntLiteral(1970, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void monthTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.month(new DateLiteral("2018-08-08", Type.DATE));
- IntLiteral expectedResult = new IntLiteral(8, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.month(new DateLiteral("1970-01-02 11:46:40", Type.DATETIME));
- expectedResult = new IntLiteral(1, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void monthTest2() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.month(new DateLiteral("2018-08-08", Type.DATEV2));
- IntLiteral expectedResult = new IntLiteral(8, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.month(new DateLiteral("1970-01-02 11:46:40", Type.DATETIMEV2));
- expectedResult = new IntLiteral(1, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void dayTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.day(new DateLiteral("2018-08-08", Type.DATE));
- IntLiteral expectedResult = new IntLiteral(8, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.day(new DateLiteral("1970-01-02 11:46:40", Type.DATETIME));
- expectedResult = new IntLiteral(2, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void dayTest2() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.day(new DateLiteral("2018-08-08", Type.DATEV2));
- IntLiteral expectedResult = new IntLiteral(8, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.day(new DateLiteral("1970-01-02 11:46:40", Type.DATETIMEV2));
- expectedResult = new IntLiteral(2, Type.INT);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void floorTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.floor(new FloatLiteral(3.3));
- IntLiteral expectedResult = new IntLiteral(3);
- Assert.assertEquals(expectedResult, actualResult);
-
-
- actualResult = FEFunctions.floor(new FloatLiteral(-3.3));
- expectedResult = new IntLiteral(-4);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void addIntTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.addInt(new IntLiteral(1234), new IntLiteral(4321));
- IntLiteral expectedResult = new IntLiteral(5555);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.addInt(new IntLiteral(-1111), new IntLiteral(3333));
- expectedResult = new IntLiteral(2222);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void addDoubleTest() throws AnalysisException {
- FloatLiteral actualResult = FEFunctions.addDouble(new FloatLiteral(1.1), new FloatLiteral(1.1));
- FloatLiteral expectedResult = new FloatLiteral(2.2);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.addDouble(new FloatLiteral(-1.1), new FloatLiteral(1.1));
- expectedResult = new FloatLiteral(0.0);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void addDecimalV2Test() throws AnalysisException {
- DecimalLiteral actualResult = FEFunctions.addDecimalV2(new DecimalLiteral("2.2"), new DecimalLiteral("3.3"));
- DecimalLiteral expectedResult = new DecimalLiteral("5.5");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.addDecimalV2(new DecimalLiteral("-2.2"), new DecimalLiteral("3.3"));
- expectedResult = new DecimalLiteral("1.1");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void addBigIntTest() throws AnalysisException {
- LargeIntLiteral actualResult = FEFunctions.addBigInt(
- new LargeIntLiteral("170141183460469231731687303715884105727"),
- new LargeIntLiteral("-170141183460469231731687303715884105728"));
- LargeIntLiteral expectedResult = new LargeIntLiteral("-1");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.addBigInt(new LargeIntLiteral("10"), new LargeIntLiteral("-1"));
- expectedResult = new LargeIntLiteral("9");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void subtractIntTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.subtractInt(new IntLiteral(5555), new IntLiteral(4321));
- IntLiteral expectedResult = new IntLiteral(1234);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.subtractInt(new IntLiteral(-3333), new IntLiteral(1111));
- expectedResult = new IntLiteral(-4444);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void subtractDoubleTest() throws AnalysisException {
- FloatLiteral actualResult = FEFunctions.subtractDouble(new FloatLiteral(1.1), new FloatLiteral(1.1));
- FloatLiteral expectedResult = new FloatLiteral(0.0);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.subtractDouble(new FloatLiteral(-1.1), new FloatLiteral(1.1));
- expectedResult = new FloatLiteral(-2.2);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void subtractDecimalV2Test() throws AnalysisException {
- DecimalLiteral actualResult = FEFunctions.subtractDecimalV2(new DecimalLiteral("2.2"),
- new DecimalLiteral("3.3"));
- DecimalLiteral expectedResult = new DecimalLiteral("-1.1");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.subtractDecimalV2(new DecimalLiteral("5.5"), new DecimalLiteral("3.3"));
- expectedResult = new DecimalLiteral("2.2");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void subtractBigIntTest() throws AnalysisException {
- LargeIntLiteral actualResult = FEFunctions.subtractBigInt(
- new LargeIntLiteral("170141183460469231731687303715884105726"),
- new LargeIntLiteral("170141183460469231731687303715884105727"));
- LargeIntLiteral expectedResult = new LargeIntLiteral("-1");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.subtractBigInt(new LargeIntLiteral("10"), new LargeIntLiteral("-1"));
- expectedResult = new LargeIntLiteral("11");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void multiplyIntTest() throws AnalysisException {
- IntLiteral actualResult = FEFunctions.multiplyInt(new IntLiteral(100), new IntLiteral(2));
- IntLiteral expectedResult = new IntLiteral(200);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.multiplyInt(new IntLiteral(-100), new IntLiteral(1));
- expectedResult = new IntLiteral(-100);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.multiplyInt(new IntLiteral(-100), new IntLiteral(-2));
- expectedResult = new IntLiteral(200);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void multiplyDoubleTest() throws AnalysisException {
- FloatLiteral actualResult = FEFunctions.multiplyDouble(new FloatLiteral(-1.1), new FloatLiteral(1.0));
- FloatLiteral expectedResult = new FloatLiteral(-1.1);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.multiplyDouble(new FloatLiteral(-1.1), new FloatLiteral(-10.0));
- expectedResult = new FloatLiteral(11.0);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.multiplyDouble(new FloatLiteral(-1.1), new FloatLiteral(1.1));
- expectedResult = new FloatLiteral(-1.2100000000000002);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void multiplyDecimalV2Test() throws AnalysisException {
- DecimalLiteral actualResult = FEFunctions.multiplyDecimalV2(new DecimalLiteral("1.1"),
- new DecimalLiteral("1.0"));
- DecimalLiteral expectedResult = new DecimalLiteral("1.1");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.multiplyDecimalV2(new DecimalLiteral("-1.1"), new DecimalLiteral("-10.0"));
- expectedResult = new DecimalLiteral("11.0");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.multiplyDecimalV2(new DecimalLiteral("-1.1"), new DecimalLiteral("-1.1"));
- expectedResult = new DecimalLiteral("1.21");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void multiplyBigIntTest() throws AnalysisException {
- LargeIntLiteral actualResult = FEFunctions.multiplyBigInt(
- new LargeIntLiteral("-170141183460469231731687303715884105727"), new LargeIntLiteral("1"));
- LargeIntLiteral expectedResult = new LargeIntLiteral("-170141183460469231731687303715884105727");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.multiplyBigInt(new LargeIntLiteral("-10"), new LargeIntLiteral("-1"));
- expectedResult = new LargeIntLiteral("10");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void divideDoubleTest() throws AnalysisException {
- FloatLiteral actualResult = FEFunctions.divideDouble(new FloatLiteral(-1.1), new FloatLiteral(1.0));
- FloatLiteral expectedResult = new FloatLiteral(-1.1);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.divideDouble(new FloatLiteral(-1.1), new FloatLiteral(-10.0));
- expectedResult = new FloatLiteral(0.11000000000000001);
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.divideDouble(new FloatLiteral(-1.1), new FloatLiteral(1.1));
- expectedResult = new FloatLiteral(-1.0);
- Assert.assertEquals(expectedResult, actualResult);
- }
-
- @Test
- public void divideDecimalV2Test() throws AnalysisException {
- DecimalLiteral actualResult = FEFunctions.divideDecimalV2(new DecimalLiteral("1.1"),
- new DecimalLiteral("1.0"));
- DecimalLiteral expectedResult = new DecimalLiteral("1.1");
- Assert.assertEquals(expectedResult, actualResult);
-
- actualResult = FEFunctions.divideDecimalV2(new DecimalLiteral("-1.1"), new DecimalLiteral("-10.0"));
- expectedResult = new DecimalLiteral("0.11");
- Assert.assertEquals(expectedResult, actualResult);
- }
-
-
- @Test
- public void timeNowTest() throws AnalysisException {
- String curTimeString = FEFunctions.curTime().toSqlImpl(false, false, null, null).replace("'", "");
- String currentTimestampString = FEFunctions.currentTimestamp().toSqlImpl(false, false, null, null)
- .replace("'", "");
-
- ZonedDateTime zonedDateTime = ZonedDateTime.now(TimeUtils.getTimeZone().toZoneId());
- DateTimeFormatter formatter = null;
- if (Config.enable_date_conversion) {
- formatter = new DateTimeFormatterBuilder()
- .appendPattern("uuuu-MM-dd HH:mm:ss")
- .appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true)
- .toFormatter();
- } else {
- formatter = new DateTimeFormatterBuilder()
- .appendPattern("uuuu-MM-dd HH:mm:ss")
- .toFormatter();
- }
-
- Assert.assertTrue(formatter.format(zonedDateTime).compareTo(currentTimestampString) >= 0);
- String nowTimeString = formatter.format(zonedDateTime).substring(
- formatter.format(zonedDateTime).indexOf(" ") + 1);
- Assert.assertTrue(nowTimeString.compareTo(curTimeString) >= 0);
- }
-
- @Test
- public void datePlusAndSubTest() throws AnalysisException {
- DateLiteral dateLiteral = new DateLiteral("2019-11-11 00:00:00", Type.DATETIME);
-
- Assert.assertEquals(new DateLiteral("2020-11-11 00:00:00", Type.DATETIME),
- FEFunctions.yearsAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2018-11-11 00:00:00", Type.DATETIME),
- FEFunctions.yearsSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-12-11 00:00:00", Type.DATETIME),
- FEFunctions.monthsAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-10-11 00:00:00", Type.DATETIME),
- FEFunctions.monthsSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-12 00:00:00", Type.DATETIME),
- FEFunctions.daysAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 00:00:00", Type.DATETIME),
- FEFunctions.daysSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-11 01:00:00", Type.DATETIME),
- FEFunctions.hoursAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 23:00:00", Type.DATETIME),
- FEFunctions.hoursSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-11 00:01:00", Type.DATETIME),
- FEFunctions.minutesAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 23:59:00", Type.DATETIME),
- FEFunctions.minutesSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-11 00:00:01", Type.DATETIME),
- FEFunctions.secondsAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 23:59:59", Type.DATETIME),
- FEFunctions.secondsSub(dateLiteral, new IntLiteral(1)));
- }
-
- @Test
- public void dateV2PlusAndSubTest() throws AnalysisException {
- DateLiteral dateLiteral = new DateLiteral("2019-11-11 00:00:00", Type.DATETIMEV2);
-
- Assert.assertEquals(new DateLiteral("2020-11-11 00:00:00", Type.DATETIME),
- FEFunctions.yearsAdd(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2020-11-11 00:00:00", Type.DATETIMEV2),
- FEFunctions.yearsAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2018-11-11 00:00:00", Type.DATETIME),
- FEFunctions.yearsSub(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2018-11-11 00:00:00", Type.DATETIMEV2),
- FEFunctions.yearsSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-12-11 00:00:00", Type.DATETIME),
- FEFunctions.monthsAdd(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-12-11 00:00:00", Type.DATETIMEV2),
- FEFunctions.monthsAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-10-11 00:00:00", Type.DATETIME),
- FEFunctions.monthsSub(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-10-11 00:00:00", Type.DATETIMEV2),
- FEFunctions.monthsSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-12 00:00:00", Type.DATETIME),
- FEFunctions.daysAdd(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-12 00:00:00", Type.DATETIMEV2),
- FEFunctions.daysAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 00:00:00", Type.DATETIME),
- FEFunctions.daysSub(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-10 00:00:00", Type.DATETIMEV2),
- FEFunctions.daysSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-11 01:00:00", Type.DATETIME),
- FEFunctions.hoursAdd(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-11 01:00:00", Type.DATETIMEV2),
- FEFunctions.hoursAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 23:00:00", Type.DATETIME),
- FEFunctions.hoursSub(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-10 23:00:00", Type.DATETIMEV2),
- FEFunctions.hoursSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-11 00:01:00", Type.DATETIME),
- FEFunctions.minutesAdd(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-11 00:01:00", Type.DATETIMEV2),
- FEFunctions.minutesAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 23:59:00", Type.DATETIME),
- FEFunctions.minutesSub(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-10 23:59:00", Type.DATETIMEV2),
- FEFunctions.minutesSub(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-11 00:00:01", Type.DATETIME),
- FEFunctions.secondsAdd(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-11 00:00:01", Type.DATETIMEV2),
- FEFunctions.secondsAdd(dateLiteral, new IntLiteral(1)));
-
- Assert.assertEquals(new DateLiteral("2019-11-10 23:59:59", Type.DATETIME),
- FEFunctions.secondsSub(dateLiteral, new IntLiteral(1)));
- Assert.assertEquals(new DateLiteral("2019-11-10 23:59:59", Type.DATETIMEV2),
- FEFunctions.secondsSub(dateLiteral, new IntLiteral(1)));
- }
-}
diff --git a/fe/fe-core/src/test/java/org/apache/doris/rewrite/RewriteDateLiteralRuleTest.java b/fe/fe-core/src/test/java/org/apache/doris/rewrite/RewriteDateLiteralRuleTest.java
deleted file mode 100644
index bb04adb9e53..00000000000
--- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/RewriteDateLiteralRuleTest.java
+++ /dev/null
@@ -1,255 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-package org.apache.doris.rewrite;
-
-import org.apache.doris.common.AnalysisException;
-import org.apache.doris.common.FeConstants;
-import org.apache.doris.qe.ConnectContext;
-import org.apache.doris.utframe.DorisAssert;
-
-import org.junit.Assert;
-
-public class RewriteDateLiteralRuleTest {
- private DorisAssert dorisAssert;
- private static final String DB_NAME = "rewritedaterule";
- private static final String TABLE_NAME_1 = "tb1";
- private static final String TABLE_NAME_2 = "tb2";
-
- public void before(ConnectContext ctx) throws Exception {
- FeConstants.default_scheduler_interval_millisecond = 10;
- FeConstants.runningUnitTest = true;
- dorisAssert = new DorisAssert(ctx);
- dorisAssert.withDatabase(DB_NAME).useDatabase(DB_NAME);
- String createTableSQL = "create table " + DB_NAME + "." + TABLE_NAME_1
- + " (k1 datetime, k2 int) "
- + "distributed by hash(k2) buckets 3 properties('replication_num' = '1');";
- dorisAssert.withTable(createTableSQL);
- createTableSQL = "create table " + DB_NAME + "." + TABLE_NAME_2
- + " (k1 datetime(3), k2 int) "
- + "distributed by hash(k2) buckets 3 properties('replication_num' = '1');";
- dorisAssert.withTable(createTableSQL);
- }
-
- public void after() throws Exception {
- dorisAssert.dropDB(DB_NAME);
- }
-
- public void testWithIntFormatDate() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb1 where k1 > 20210301";
- String planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > 20210301 from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > 20210301223344 from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 22:33:44'"));
- }
-
- public void testWithIntFormatDateV2() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb2 where k1 > 20210301";
- String planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > 20210301 from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > 20210301223344 from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 22:33:44'"));
- }
-
- public void testWithStringFormatDate() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb1 where k1 > '2021030112334455'";
- String planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 12:33:44'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '20210301' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '20210301233234.34' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 23:32:34'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb1 where k1 > '2021-03-01'";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01 11:22:33' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 11:22:33'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01 16:22:33' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 16:22:33'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01 11:22' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 11:22:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '20210301T221133' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 22:11:33'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01dd 11:22' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '80-03-01 11:22' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '1980-03-01 11:22:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '12-03-01 11:22' from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2012-03-01 11:22:00'"));
- }
-
- public void testWithStringFormatDateV2() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb2 where k1 > '2021030112334455'";
- String planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 12:33:44.550'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '20210301' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '20210301233234.34' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 23:32:34.340'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb2 where k1 > '2021-03-01'";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01 11:22:33' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 11:22:33'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01 16:22:33' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 16:22:33'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01 11:22' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 11:22:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '20210301T221133' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 22:11:33'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03-01 11:22' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2021-03-01 11:22:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '80-03-01 11:22' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '1980-03-01 11:22:00'"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '12-03-01 11:22' from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > '2012-03-01 11:22:00'"));
- }
-
- public void testWithDoubleFormatDate() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb1 where k1 > 20210301.22";
- String planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > 20210301.22"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > 20210331.22 from " + DB_NAME + ".tb1";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > 20210331.22"));
- }
-
- public void testWithDoubleFormatDateV2() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb2 where k1 > 20210301.22";
- String planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > 20210301.22"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > 20210331.22 from " + DB_NAME + ".tb2";
- planString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(planString.contains("`k1` > 20210331.22"));
- }
-
- public void testWithInvalidFormatDate() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb1 where k1 > '2021030125334455'";
- try {
- dorisAssert.query(query).explainQuery();
- } catch (AnalysisException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Incorrect datetime value: '2021030125334455' in expression: `k1` > '2021030125334455'"));
- }
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021030125334455' from " + DB_NAME + ".tb1";
- String plainString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(plainString.contains("NULL"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb1 where k1 > '2021-03-32 23:33:55'";
- try {
- dorisAssert.query(query).explainQuery();
- } catch (AnalysisException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Incorrect datetime value: '2021-03-32 23:33:55' in expression: `k1` > '2021-03-32 23:33:55'"));
- }
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb1 where k1 > '2021-03- 03 23:33:55'";
- try {
- dorisAssert.query(query).explainQuery();
- } catch (AnalysisException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Incorrect datetime value: '2021-03- 03 23:33:55' in expression: `k1` > '2021-03- 03 23:33:55'"));
- }
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03- 03 23:33:55' from " + DB_NAME + ".tb1";
- plainString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(plainString.contains("NULL"));
- }
-
- public void testWithInvalidFormatDateV2() throws Exception {
- String query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb2 where k1 > '2021030125334455'";
- try {
- dorisAssert.query(query).explainQuery();
- } catch (AnalysisException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Incorrect datetime value: '2021030125334455' in expression: `k1` > '2021030125334455'"));
- }
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021030125334455' from " + DB_NAME + ".tb2";
- String plainString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(plainString.contains("NULL"));
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb2 where k1 > '2021-03-32 23:33:55'";
- try {
- dorisAssert.query(query).explainQuery();
- } catch (AnalysisException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Incorrect datetime value: '2021-03-32 23:33:55' in expression: `k1` > '2021-03-32 23:33:55'"));
- }
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ * from " + DB_NAME + ".tb2 where k1 > '2021-03- 03 23:33:55'";
- try {
- dorisAssert.query(query).explainQuery();
- } catch (AnalysisException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Incorrect datetime value: '2021-03- 03 23:33:55' in expression: `k1` > '2021-03- 03 23:33:55'"));
- }
-
- query = "select /*+ SET_VAR(enable_nereids_planner=false) */ k1 > '2021-03- 03 23:33:55' from " + DB_NAME + ".tb2";
- plainString = dorisAssert.query(query).explainQuery();
- Assert.assertTrue(plainString.contains("NULL"));
- }
-}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org