Skip to content
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
48 changes: 48 additions & 0 deletions fe/fe-common/src/main/java/org/apache/doris/catalog/AnyType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.catalog;

import org.apache.doris.thrift.TColumnType;
import org.apache.doris.thrift.TTypeDesc;

/**
* Describes a AnyType type, used for SQL function return type,
* NOT used for table column type.
*/
public class AnyType extends Type {

@Override
protected String toSql(int depth) {
return null;
}

@Override
protected String prettyPrint(int lpad) {
return null;
}

@Override
public void toThrift(TTypeDesc container) {
throw new RuntimeException("can not call toThrift on AnyType.");
}

@Override
public TColumnType toColumnTypeThrift() {
throw new RuntimeException("can not call toColumnTypeThrift on AnyType");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ public List<Type> expandVariadicTemplateType(Map<String, Integer> expandSizeMap)
return Lists.newArrayList(this);
}

public StructType replaceFieldsWithNames(List<String> names) {
Preconditions.checkState(names.size() == fields.size());
ArrayList<StructField> newFields = Lists.newArrayList();
for (int i = 0; i < names.size(); i++) {
newFields.add(new StructField(names.get(i), fields.get(i).type));
}
return new StructType(newFields);
}

@Override
public boolean equals(Object other) {
if (!(other instanceof StructType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public void collectTemplateExpandSize(Type[] args, Map<String, Integer> expandSi
expandSizeMap.computeIfAbsent(name, k -> args.length);
if (expandSizeMap.get(name) != args.length) {
throw new TypeException(
String.format("can not expand variadic template type %s to %s size since it's "
+ "already expand as %s size", name, args.length, expandSizeMap.get(name)));
String.format("can not expand variadic template type %s to %s size since it's "
+ "already expand as %s size", name, args.length, expandSizeMap.get(name)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public abstract class Type {
new StructField("generic_struct", new ScalarType(PrimitiveType.NULL_TYPE))));
public static final StructType STRUCT = new StructType();
public static final VariantType VARIANT = new VariantType();
public static final AnyType ANY_TYPE = new AnyType();

private static final Logger LOG = LogManager.getLogger(Type.class);
private static final ArrayList<ScalarType> integerTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@ private boolean isIndistinguishable(Function o) {
}
}

public boolean isInferenceFunction() {
for (Type arg : argTypes) {
if (arg instanceof AnyType) {
return true;
}
}
return retType instanceof AnyType;
}

public TFunction toThrift(Type realReturnType, Type[] realArgTypes) {
TFunction fn = new TFunction();
fn.setSignature(signatureString());
Expand Down
37 changes: 35 additions & 2 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/FunctionSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,12 @@ public Function getFunction(Function desc, Function.CompareMode mode, boolean is
List<Function> normalFunctions = Lists.newArrayList();
List<Function> templateFunctions = Lists.newArrayList();
List<Function> variadicTemplateFunctions = Lists.newArrayList();
List<Function> inferenceFunctions = Lists.newArrayList();
for (Function fn : fns) {
if (fn.isInferenceFunction()) {
inferenceFunctions.add(fn);
continue;
}
if (fn.hasTemplateArg()) {
if (!fn.hasVariadicTemplateArg()) {
templateFunctions.add(fn);
Expand Down Expand Up @@ -1274,8 +1279,25 @@ public Function getFunction(Function desc, Function.CompareMode mode, boolean is
}
}

// try variadic template function
return getFunction(desc, mode, specializedVariadicTemplateFunctions);
// try variadic template function third
fn = getFunction(desc, mode, specializedVariadicTemplateFunctions);
if (fn != null) {
return fn;
}

List<Function> inferredFunctions = Lists.newArrayList();
for (Function f : inferenceFunctions) {
if (f.hasTemplateArg()) {
f = specializeTemplateFunction(f, desc, f.hasVariadicTemplateArg());
}
f = resolveInferenceFunction(f, desc);
if (f != null) {
inferredFunctions.add(f);
}
}

// try inference function at last
return getFunction(desc, mode, inferredFunctions);
}

private Function getFunction(Function desc, Function.CompareMode mode, List<Function> fns) {
Expand Down Expand Up @@ -1384,6 +1406,17 @@ public Function specializeTemplateFunction(Function templateFunction, Function r
}
}

public Function resolveInferenceFunction(Function inferenceFunction, Function requestFunction) {
Type[] args = requestFunction.getArgs();
Type newRetType = FunctionTypeDeducers.deduce(inferenceFunction.functionName(), args);
if (newRetType != null && inferenceFunction instanceof ScalarFunction) {
ScalarFunction f = (ScalarFunction) inferenceFunction;
return new ScalarFunction(f.getFunctionName(), Lists.newArrayList(f.getArgs()), newRetType, f.hasVarArgs(),
f.getSymbolName(), f.getBinaryType(), f.isUserVisible(), f.isVectorized(), f.getNullableMode());
}
return null;
}

/**
* There are essential differences in the implementation of some functions for different
* types params, which should be prohibited.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.catalog;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import java.util.List;

public class FunctionTypeDeducers {

public interface TypeDeducer {
public Type deduce(Type[] args);
}

public static final ImmutableMap<String, TypeDeducer> DEDUCERS = ImmutableMap.<String, TypeDeducer>builder()
.put("named_struct", new NamedStructDeducer())
.put("element_at", new ElementAtDeducer())
.build();

public static Type deduce(String fnName, Type[] args) {
if (DEDUCERS.containsKey(fnName)) {
return DEDUCERS.get(fnName).deduce(args);
}
return null;
}

public static class NamedStructDeducer implements TypeDeducer {
@Override
public Type deduce(Type[] args) {
List<Type> evenArgs = Lists.newArrayList();
for (int i = 0; i < args.length; i++) {
if ((i & 1) == 1) {
evenArgs.add(args[i]);
}
}
return new StructType(evenArgs);
}
}

public static class ElementAtDeducer implements TypeDeducer {
@Override
public Type deduce(Type[] args) {
// todo(xy)
return null;
}
}
}
1 change: 1 addition & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

# struct functions
[['struct'], 'STRUCT<TYPES>', ['TYPES'], 'ALWAYS_NOT_NULLABLE', ['TYPES...']],
[['named_struct'], 'ANY_TYPE', ['TYPES'], 'ALWAYS_NOT_NULLABLE', ['TYPES...']],

# array functions
[['array'], 'ARRAY', ['BOOLEAN', '...'], 'ALWAYS_NOT_NULLABLE'],
Expand Down