forked from apache/pinot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polymorphic binary arithmetic scalar functions (apache#14089)
- Loading branch information
Showing
17 changed files
with
406 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...src/main/java/org/apache/pinot/common/function/scalar/arithmetic/MinusScalarFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* 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.pinot.common.function.scalar.arithmetic; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import org.apache.pinot.common.function.FunctionInfo; | ||
import org.apache.pinot.common.utils.DataSchema.ColumnDataType; | ||
import org.apache.pinot.spi.annotations.ScalarFunction; | ||
|
||
|
||
@ScalarFunction(names = {"sub", "minus"}) | ||
public class MinusScalarFunction extends PolymorphicBinaryArithmeticScalarFunction { | ||
|
||
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class); | ||
|
||
static { | ||
try { | ||
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.LONG, | ||
new FunctionInfo(MinusScalarFunction.class.getMethod("longMinus", long.class, long.class), | ||
MinusScalarFunction.class, false)); | ||
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.DOUBLE, | ||
new FunctionInfo(MinusScalarFunction.class.getMethod("doubleMinus", double.class, double.class), | ||
MinusScalarFunction.class, false)); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected FunctionInfo functionInfoForType(ColumnDataType argumentType) { | ||
FunctionInfo functionInfo = TYPE_FUNCTION_INFO_MAP.get(argumentType); | ||
|
||
// Fall back to double based comparison by default | ||
return functionInfo != null ? functionInfo : TYPE_FUNCTION_INFO_MAP.get(ColumnDataType.DOUBLE); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "minus"; | ||
} | ||
|
||
public static long longMinus(long a, long b) { | ||
return a - b; | ||
} | ||
|
||
public static double doubleMinus(double a, double b) { | ||
return a - b; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
.../src/main/java/org/apache/pinot/common/function/scalar/arithmetic/MultScalarFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* 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.pinot.common.function.scalar.arithmetic; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import org.apache.pinot.common.function.FunctionInfo; | ||
import org.apache.pinot.common.utils.DataSchema.ColumnDataType; | ||
import org.apache.pinot.spi.annotations.ScalarFunction; | ||
|
||
|
||
@ScalarFunction(names = {"mult", "times"}) | ||
public class MultScalarFunction extends PolymorphicBinaryArithmeticScalarFunction { | ||
|
||
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class); | ||
|
||
static { | ||
try { | ||
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.LONG, | ||
new FunctionInfo(MultScalarFunction.class.getMethod("longMult", long.class, long.class), | ||
MultScalarFunction.class, false)); | ||
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.DOUBLE, | ||
new FunctionInfo(MultScalarFunction.class.getMethod("doubleMult", double.class, double.class), | ||
MultScalarFunction.class, false)); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected FunctionInfo functionInfoForType(ColumnDataType argumentType) { | ||
FunctionInfo functionInfo = TYPE_FUNCTION_INFO_MAP.get(argumentType); | ||
|
||
// Fall back to double based comparison by default | ||
return functionInfo != null ? functionInfo : TYPE_FUNCTION_INFO_MAP.get(ColumnDataType.DOUBLE); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "mult"; | ||
} | ||
|
||
public static long longMult(long a, long b) { | ||
return a * b; | ||
} | ||
|
||
public static double doubleMult(double a, double b) { | ||
return a * b; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
.../src/main/java/org/apache/pinot/common/function/scalar/arithmetic/PlusScalarFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* 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.pinot.common.function.scalar.arithmetic; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import org.apache.pinot.common.function.FunctionInfo; | ||
import org.apache.pinot.common.utils.DataSchema.ColumnDataType; | ||
import org.apache.pinot.spi.annotations.ScalarFunction; | ||
|
||
|
||
@ScalarFunction(names = {"add", "plus"}) | ||
public class PlusScalarFunction extends PolymorphicBinaryArithmeticScalarFunction { | ||
|
||
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class); | ||
|
||
static { | ||
try { | ||
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.LONG, | ||
new FunctionInfo(PlusScalarFunction.class.getMethod("longPlus", long.class, long.class), | ||
PlusScalarFunction.class, false)); | ||
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.DOUBLE, | ||
new FunctionInfo(PlusScalarFunction.class.getMethod("doublePlus", double.class, double.class), | ||
PlusScalarFunction.class, false)); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected FunctionInfo functionInfoForType(ColumnDataType argumentType) { | ||
FunctionInfo functionInfo = TYPE_FUNCTION_INFO_MAP.get(argumentType); | ||
|
||
// Fall back to double based comparison by default | ||
return functionInfo != null ? functionInfo : TYPE_FUNCTION_INFO_MAP.get(ColumnDataType.DOUBLE); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "plus"; | ||
} | ||
|
||
public static long longPlus(long a, long b) { | ||
return a + b; | ||
} | ||
|
||
public static double doublePlus(double a, double b) { | ||
return a + b; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...he/pinot/common/function/scalar/arithmetic/PolymorphicBinaryArithmeticScalarFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* 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.pinot.common.function.scalar.arithmetic; | ||
|
||
import javax.annotation.Nullable; | ||
import org.apache.pinot.common.function.FunctionInfo; | ||
import org.apache.pinot.common.function.PinotScalarFunction; | ||
import org.apache.pinot.common.utils.DataSchema.ColumnDataType; | ||
|
||
|
||
/** | ||
* Base class for polymorphic binary arithmetic scalar functions | ||
*/ | ||
public abstract class PolymorphicBinaryArithmeticScalarFunction implements PinotScalarFunction { | ||
|
||
@Nullable | ||
@Override | ||
public FunctionInfo getFunctionInfo(ColumnDataType[] argumentTypes) { | ||
if (argumentTypes.length != 2) { | ||
return null; | ||
} | ||
|
||
return functionInfoForTypes(argumentTypes[0].getStoredType(), argumentTypes[1].getStoredType()); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public FunctionInfo getFunctionInfo(int numArguments) { | ||
if (numArguments != 2) { | ||
return null; | ||
} | ||
|
||
// For backward compatibility | ||
return functionInfoForType(ColumnDataType.DOUBLE); | ||
} | ||
|
||
private FunctionInfo functionInfoForTypes(ColumnDataType argumentType1, ColumnDataType argumentType2) { | ||
if ((argumentType1 == ColumnDataType.LONG || argumentType1 == ColumnDataType.INT) && ( | ||
argumentType2 == ColumnDataType.LONG || argumentType2 == ColumnDataType.INT)) { | ||
return functionInfoForType(ColumnDataType.LONG); | ||
} | ||
|
||
// Fall back to double based comparison by default | ||
return functionInfoForType(ColumnDataType.DOUBLE); | ||
} | ||
|
||
/** | ||
* Get the binary arithmetic scalar function's {@link FunctionInfo} for the given argument type. | ||
*/ | ||
protected abstract FunctionInfo functionInfoForType(ColumnDataType argumentType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.