Skip to content

Commit

Permalink
Polymorphic binary arithmetic scalar functions (apache#14089)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmayya authored Sep 30, 2024
1 parent 3452ef9 commit 19b79f4
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ public class ArithmeticFunctions {
private ArithmeticFunctions() {
}

@ScalarFunction(names = {"add", "plus"})
public static double plus(double a, double b) {
return a + b;
}

@ScalarFunction(names = {"sub", "minus"})
public static double minus(double a, double b) {
return a - b;
}

@ScalarFunction(names = {"mult", "times"})
public static double times(double a, double b) {
return a * b;
}

@ScalarFunction(names = {"div", "divide"})
public static double divide(double a, double b) {
return a / b;
Expand Down
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;
}
}
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;
}
}
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;
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import org.apache.pinot.common.function.FunctionInfo;
Expand All @@ -33,7 +33,7 @@
@ScalarFunction
public class EqualsScalarFunction extends PolymorphicComparisonScalarFunction {

private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new HashMap<>();
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class);
private static final FunctionInfo DOUBLE_EQUALS_WITH_TOLERANCE;

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.pinot.common.function.scalar.comparison;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import org.apache.pinot.common.function.FunctionInfo;
import org.apache.pinot.common.utils.DataSchema.ColumnDataType;
Expand All @@ -32,7 +32,7 @@
@ScalarFunction
public class GreaterThanOrEqualScalarFunction extends PolymorphicComparisonScalarFunction {

private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new HashMap<>();
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class);

static {
try {
Expand All @@ -51,9 +51,8 @@ public class GreaterThanOrEqualScalarFunction extends PolymorphicComparisonScala
GreaterThanOrEqualScalarFunction.class.getMethod("doubleGreaterThanOrEqual", double.class, double.class),
GreaterThanOrEqualScalarFunction.class, false));
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.BIG_DECIMAL, new FunctionInfo(
GreaterThanOrEqualScalarFunction.class.getMethod("bigDecimalGreaterThanOrEqual",
BigDecimal.class, BigDecimal.class),
GreaterThanOrEqualScalarFunction.class, false));
GreaterThanOrEqualScalarFunction.class.getMethod("bigDecimalGreaterThanOrEqual", BigDecimal.class,
BigDecimal.class), GreaterThanOrEqualScalarFunction.class, false));
TYPE_FUNCTION_INFO_MAP.put(ColumnDataType.STRING, new FunctionInfo(
GreaterThanOrEqualScalarFunction.class.getMethod("stringGreaterThanOrEqual", String.class, String.class),
GreaterThanOrEqualScalarFunction.class, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.pinot.common.function.scalar.comparison;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import org.apache.pinot.common.function.FunctionInfo;
import org.apache.pinot.common.utils.DataSchema.ColumnDataType;
Expand All @@ -32,7 +32,7 @@
@ScalarFunction
public class GreaterThanScalarFunction extends PolymorphicComparisonScalarFunction {

private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new HashMap<>();
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class);

static {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.pinot.common.function.scalar.comparison;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import org.apache.pinot.common.function.FunctionInfo;
import org.apache.pinot.common.utils.DataSchema.ColumnDataType;
Expand All @@ -32,7 +32,7 @@
@ScalarFunction
public class LessThanOrEqualScalarFunction extends PolymorphicComparisonScalarFunction {

private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new HashMap<>();
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class);

static {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.pinot.common.function.scalar.comparison;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import org.apache.pinot.common.function.FunctionInfo;
import org.apache.pinot.common.utils.DataSchema.ColumnDataType;
Expand All @@ -32,7 +32,7 @@
@ScalarFunction
public class LessThanScalarFunction extends PolymorphicComparisonScalarFunction {

private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new HashMap<>();
private static final Map<ColumnDataType, FunctionInfo> TYPE_FUNCTION_INFO_MAP = new EnumMap<>(ColumnDataType.class);

static {
try {
Expand Down
Loading

0 comments on commit 19b79f4

Please sign in to comment.