Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failed cases in regression test #299

Merged
merged 2 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions be/src/exprs/cast_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,16 @@ StringVal CastFunctions::cast_to_string_val(FunctionContext* ctx, const DateTime
return sv;
}

#if 0

StringVal CastFunctions::CastToStringVal(FunctionContext* ctx, const StringVal& val) {
StringVal CastFunctions::cast_to_string_val(FunctionContext* ctx, const StringVal& val) {
if (val.is_null) return StringVal::null();
StringVal sv;
sv.ptr = val.ptr;
sv.len = val.len;
AnyValUtil::TruncateIfNecessary(ctx->GetReturnType(), &sv);
AnyValUtil::TruncateIfNecessary(ctx->get_return_type(), &sv);
return sv;
}

#if 0
StringVal CastFunctions::CastToChar(FunctionContext* ctx, const StringVal& val) {
if (val.is_null) return StringVal::null();

Expand Down
2 changes: 1 addition & 1 deletion be/src/exprs/cast_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class CastFunctions {
static StringVal cast_to_string_val(FunctionContext* context, const FloatVal& val);
static StringVal cast_to_string_val(FunctionContext* context, const DoubleVal& val);
static StringVal cast_to_string_val(FunctionContext* context, const DateTimeVal& val);
static StringVal cast_to_string_val(FunctionContext* context, const StringVal& val);
#if 0
static StringVal CastToStringVal(FunctionContext* context, const StringVal& val);
static StringVal CastToChar(FunctionContext* context, const StringVal& val);
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {

type = castBinaryOp(commonType);
fn = getBuiltinFunction(analyzer, fnName, collectChildReturnTypes(),
Function.CompareMode.IS_SUPERTYPE_OF);
Function.CompareMode.IS_IDENTICAL);
if (fn == null) {
Preconditions.checkState(false, String.format(
"No match for '%s' with operand types %s and %s", toSql(), t1, t2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
import org.apache.doris.common.AnalysisException;
import com.google.common.collect.Lists;


/**
* Combination of expr and ASC/DESC, and nulls ordering.
*/
public class OrderByElement {
private Expr expr;
private Expr expr;
private final boolean isAsc;

// Represents the NULLs ordering specified: true when "NULLS FIRST", false when
Expand Down Expand Up @@ -72,7 +71,8 @@ public static List<OrderByElement> reverse(List<OrderByElement> src) {
for (int i = 0; i < src.size(); ++i) {
OrderByElement element = src.get(i);
OrderByElement reverseElement =
new OrderByElement(element.getExpr().clone(), !element.isAsc, !element.nullsFirstParam);
new OrderByElement(element.getExpr().clone(), !element.isAsc,
Boolean.valueOf(!nullsFirst(element.nullsFirstParam, element.isAsc)));
result.add(reverseElement);
}

Expand Down
53 changes: 43 additions & 10 deletions fe/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

/**
* This rule replaces a constant Expr with its equivalent LiteralExpr by evaluating the
Expand All @@ -58,14 +64,19 @@
public class FoldConstantsRule implements ExprRewriteRule {
public static ExprRewriteRule INSTANCE = new FoldConstantsRule();

private Multimap<String, FEFunctionInvoker> functions = ArrayListMultimap.create();
private ImmutableMultimap<String, FEFunctionInvoker> functions;
// For most build-in functions, it will return NullLiteral when params contain NullLiteral.
// But a few functions need to handle NullLiteral differently, such as "if". It need to add
// an attribute to LiteralExpr to mark null and check the attribute to decide whether to
// replace the result with NullLiteral when function finished. It leaves to be realized.
// TODO chenhao16.
private ImmutableSet<String> nonNullResultWithNullParamFunctions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need more comment here. I'm not very clear about this attribute's purpose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i will add some comments.


@Override
public Expr apply(Expr expr, Analyzer analyzer) throws AnalysisException {
if (functions.isEmpty()) {
if (functions == null) {
registerFunctions();
}

// Avoid calling Expr.isConstant() because that would lead to repeated traversals
// of the Expr tree. Assumes the bottom-up application of this rule. Constant
// children should have been folded at this point.
Expand Down Expand Up @@ -103,6 +114,22 @@ private Expr simplify(Expr constExpr) throws AnalysisException {
|| constExpr instanceof FunctionCallExpr
|| constExpr instanceof CastExpr) {
Function fn = constExpr.getFn();
// unused cast
if (fn == null) {
Preconditions.checkState((constExpr instanceof CastExpr)
&& constExpr.getChildren().size() == 1);
return constExpr.getChild(0);
}

// null
if (!nonNullResultWithNullParamFunctions.contains(fn.getFunctionName().getFunction())) {
for (Expr e : constExpr.getChildren()) {
if (e instanceof NullLiteral) {
return new NullLiteral();
}
}
}

List<ScalarType> argTypes = new ArrayList<>();
for (Type type : fn.getArgs()) {
argTypes.add((ScalarType) type);
Expand Down Expand Up @@ -139,7 +166,6 @@ private FEFunctionInvoker getFunction(FEFunctionSignature signature) {
if (!Arrays.equals(argTypes1, argTypes2)) {
continue;
}

return invoker;
}
return null;
Expand All @@ -148,10 +174,11 @@ private FEFunctionInvoker getFunction(FEFunctionSignature signature) {
private synchronized void registerFunctions() {
// double checked locking pattern
// functions only need to init once
if (!functions.isEmpty()) {
if (functions != null) {
return;
}

ImmutableMultimap.Builder<String, FEFunctionInvoker> mapBuilder =
new ImmutableMultimap.Builder<String, FEFunctionInvoker>();
Class clazz = FEFunctions.class;
for (Method method : clazz.getDeclaredMethods()) {
FEFunction annotation = method.getAnnotation(FEFunction.class);
Expand All @@ -162,12 +189,18 @@ private synchronized void registerFunctions() {
for (String type : annotation.argTypes()) {
argTypes.add(ScalarType.createType(type));
}

FEFunctionSignature signature = new FEFunctionSignature(name,
FEFunctionSignature signature = new FEFunctionSignature(name,
argTypes.toArray(new ScalarType[argTypes.size()]), returnType);
functions.put(name, new FEFunctionInvoker(method, signature));
mapBuilder.put(name, new FEFunctionInvoker(method, signature));
}
}
this.functions = mapBuilder.build();

// Functions that need to handle null.
ImmutableSet.Builder<String> setBuilder =
new ImmutableSet.Builder<String>();
setBuilder.add("if");
this.nonNullResultWithNullParamFunctions = setBuilder.build();
}

public static class FEFunctionInvoker {
Expand All @@ -190,7 +223,7 @@ public FEFunctionSignature getSignature() {
public LiteralExpr invoke(List<Expr> args) throws AnalysisException {
try {
return (LiteralExpr) method.invoke(null, args.toArray());
} catch (InvocationTargetException | IllegalAccessException e) {
} catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
throw new AnalysisException(e.getLocalizedMessage());
}
}
Expand Down