From 1dc2ccd35cc6e751afafd564d240646403b4d7c0 Mon Sep 17 00:00:00 2001 From: SongY123 <121220087@smail.nju.edu.cn> Date: Thu, 11 Apr 2024 16:06:29 +0800 Subject: [PATCH] code optimization --- README.md | 2 - .../openhufu/common/exception/ErrorCode.java | 5 ++ .../core/implementor/UserSideImplementor.java | 64 +++++++++++-------- .../data/function/AggregateFunction.java | 4 +- .../openhufu/data/function/Aggregator.java | 4 +- .../openhufu/data/storage/RandomDataSet.java | 35 ++++++---- .../hufudb/openhufu/owner/OwnerService.java | 6 +- .../implementor/OwnerImplementorFactory.java | 14 ++-- .../aggregate/OwnerAggregateFunction.java | 7 +- .../implementor/aggregate/sum/GMWSum.java | 7 +- .../aggregate/sum/SecretSharingSum.java | 2 +- scripts/test/genSyntheticData.py | 2 +- .../java/com/hufudb/openhufu/udf/DWithin.java | 10 ++- .../com/hufudb/openhufu/udf/Distance.java | 5 +- .../java/com/hufudb/openhufu/udf/Point.java | 5 +- 15 files changed, 109 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 5cf00689..65757f0a 100644 --- a/README.md +++ b/README.md @@ -146,10 +146,8 @@ openhufu: implementor: aggregate: sum: com.hufudb.openhufu.owner.implementor.aggregate.sum.SecretSharingSum - count: null max: null min: null - avg: null join: com.hufudb.openhufu.owner.implementor.join.HashJoin ``` 3. Build OpenHuFu diff --git a/common/src/main/java/com/hufudb/openhufu/common/exception/ErrorCode.java b/common/src/main/java/com/hufudb/openhufu/common/exception/ErrorCode.java index 65360287..7c3da299 100644 --- a/common/src/main/java/com/hufudb/openhufu/common/exception/ErrorCode.java +++ b/common/src/main/java/com/hufudb/openhufu/common/exception/ErrorCode.java @@ -35,6 +35,11 @@ public enum ErrorCode { // data type error DATA_TYPE_NOT_SUPPORT(40001, "data type: {} not support"), + // query error + QUERY_ERROR(50001, "query error"), + + UNSUPPORTED_OPERATION(60001, "unsupported operation {}"), + SETUP_FAILED(90001, "setup failed"), ; diff --git a/core/src/main/java/com/hufudb/openhufu/core/implementor/UserSideImplementor.java b/core/src/main/java/com/hufudb/openhufu/core/implementor/UserSideImplementor.java index 35dce8a4..ae001091 100644 --- a/core/src/main/java/com/hufudb/openhufu/core/implementor/UserSideImplementor.java +++ b/core/src/main/java/com/hufudb/openhufu/core/implementor/UserSideImplementor.java @@ -132,23 +132,24 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin) { return privacySpatialJoin(plan, isDistanceJoin, false); } - private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, boolean isUsingKNNFunc) { + private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, + boolean isUsingKNNFunc) { DataSet left = ownerSideQuery(plan.getChildren().get(0)); DataSetIterator leftIter = left.getIterator(); List arrayRows = new ArrayList<>(); boolean containsLeftKey = false; int leftKey = -1; - for (OpenHuFuPlan.Expression expression: plan.getSelectExps()) { + for (OpenHuFuPlan.Expression expression : plan.getSelectExps()) { if (expression.getOpType().equals(OpenHuFuPlan.OperatorType.REF) - && expression.getI32() == plan.getJoinCond().getCondition().getIn(0).getI32()) { + && expression.getI32() == plan.getJoinCond().getCondition().getIn(0).getI32()) { containsLeftKey = true; } } if (!containsLeftKey) { for (int i = 0; i < plan.getChildren().get(0).getSelectExps().size(); i++) { - if (plan.getChildren().get(0).getSelectExps().get(i).getI32() - == plan.getJoinCond().getCondition().getIn(0).getI32()) { + if (plan.getChildren().get(0).getSelectExps().get(i).getI32() == plan.getJoinCond() + .getCondition().getIn(0).getI32()) { leftKey = i; break; } @@ -157,16 +158,17 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, bool boolean containsRightKey = false; int rightKey = -1; - for (OpenHuFuPlan.Expression expression: plan.getSelectExps()) { + for (OpenHuFuPlan.Expression expression : plan.getSelectExps()) { if (expression.getOpType().equals(OpenHuFuPlan.OperatorType.REF) - && expression.getI32() == plan.getJoinCond().getCondition().getIn(1).getI32()) { + && expression.getI32() == plan.getJoinCond().getCondition().getIn(1).getI32()) { containsRightKey = true; } } if (!containsRightKey) { for (int i = 0; i < plan.getChildren().get(1).getSelectExps().size(); i++) { if (plan.getChildren().get(1).getSelectExps().get(i).getI32() - == plan.getJoinCond().getCondition().getIn(1).getI32() - plan.getChildren().get(0).getSelectExps().size()) { + == plan.getJoinCond().getCondition().getIn(1).getI32() - plan.getChildren().get(0) + .getSelectExps().size()) { rightKey = i; break; } @@ -176,11 +178,13 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, bool int leftRef = plan.getJoinCond().getCondition().getIn(0).getI32(); DataSet rightDataSet; if (isDistanceJoin) { - rightDataSet = ownerSideQuery(DistanceJoin - .generateDistanceQueryPlan(plan, leftIter.get(leftRef).toString(), rightKey)); - } - else { - rightDataSet = privacyKNN((UnaryPlan) KNNJoin.generateKNNQueryPlan(plan, leftIter.get(leftRef).toString(), rightKey), isUsingKNNFunc); + rightDataSet = ownerSideQuery( + DistanceJoin.generateDistanceQueryPlan(plan, leftIter.get(leftRef).toString(), + rightKey)); + } else { + rightDataSet = privacyKNN( + (UnaryPlan) KNNJoin.generateKNNQueryPlan(plan, leftIter.get(leftRef).toString(), + rightKey), isUsingKNNFunc); } DataSetIterator rightIter = rightDataSet.getIterator(); while (rightIter.next()) { @@ -189,7 +193,7 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, bool } } Schema schema; - schema = ExpressionUtils.createSchema(plan.getSelectExps()); + schema = ExpressionUtils.createSchema(plan.getSelectExps()); LOG.info(schema.toString()); return new ArrayDataSet(schema, arrayRows); } @@ -197,10 +201,10 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, bool @Override public DataSet implement(Plan plan) { LOG.info(plan.toString()); - boolean isUsingKNNFuc = plan instanceof LeafPlan - && !plan.getWhereExps().isEmpty() - && plan.getWhereExps().get(0).getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC) - && plan.getWhereExps().get(0).getStr().equals("knn"); + boolean isUsingKNNFuc = + plan instanceof LeafPlan && !plan.getWhereExps().isEmpty() && plan.getWhereExps().get(0) + .getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC) && plan.getWhereExps().get(0) + .getStr().equals("knn"); if (isUsingKNNFuc) { plan = KNNConverter.convertKNN((LeafPlan) plan); } @@ -231,7 +235,7 @@ private DataSet privacyKNN(UnaryPlan plan, boolean isUsingKNNFunc) { double left = 0; double right = 1000000; // if (USE_DP) { - right = kNNRadiusQuery(plan) * 2; + right = kNNRadiusQuery(plan) * 2; // } double deviation = 1e-6; int loop = 0; @@ -257,7 +261,6 @@ private DataSet privacyKNN(UnaryPlan plan, boolean isUsingKNNFunc) { } while (left + deviation <= right) { double mid = (left + right) / 2; -// int sign = privacyCompare(plan); int sign = (int) privacyCompare(plan, mid, k); LOG.debug("loop {} with sign {}", loop, sign); if (sign < 0) { @@ -265,7 +268,6 @@ private DataSet privacyKNN(UnaryPlan plan, boolean isUsingKNNFunc) { } else if (sign > 0) { right = mid; } else { - loop++; DataSet dataSet = ArrayDataSet.materialize(kNNCircleRangeQuery(plan, mid, isUsingKNNFunc)); return dataSet; } @@ -273,9 +275,11 @@ private DataSet privacyKNN(UnaryPlan plan, boolean isUsingKNNFunc) { } return kNNCircleRangeQuery(plan, right, isUsingKNNFunc); } + private double kNNRadiusQuery(UnaryPlan plan) { //todo -sjz - DataSetIterator dataSet = ownerSideQuery(BinarySearchKNN.generateKNNRadiusQueryPlan(plan)).getIterator(); + DataSetIterator dataSet = + ownerSideQuery(BinarySearchKNN.generateKNNRadiusQueryPlan(plan)).getIterator(); double right = 1000000; while (dataSet.next()) { double res = (double) dataSet.get(0); @@ -286,21 +290,27 @@ private double kNNRadiusQuery(UnaryPlan plan) { } return right; } + private Pair dPRangeCount(UnaryPlan plan) { //todo -sjz ownerSideQuery(BinarySearchKNN.generateDPRangeCountPlan(plan)); return null; } + private long privacyCompare(UnaryPlan plan, double range, long k) { - //todo -sjz now it is using secretSharingSum - DataSetIterator dataSet = ownerSideQuery(BinarySearchKNN.generatePrivacyComparePlan(plan, range)).getIterator(); + //todo -sjz now it is using secretSharingSum + DataSetIterator dataSet = + ownerSideQuery(BinarySearchKNN.generatePrivacyComparePlan(plan, range)).getIterator(); dataSet.next(); long res = (long) dataSet.get(0); return res - k; } + private DataSet kNNCircleRangeQuery(UnaryPlan plan, double range, boolean isUsingKNNFunc) { - return ownerSideQuery(BinarySearchKNN.generateKNNCircleRangeQueryPlan(plan, range, isUsingKNNFunc)); + return ownerSideQuery( + BinarySearchKNN.generateKNNCircleRangeQueryPlan(plan, range, isUsingKNNFunc)); } + private boolean isMultiPartySecureKNN(UnaryPlan unary) { LeafPlan leaf = (LeafPlan) unary.getChildren().get(0); boolean hasLimit = leaf.getOffset() != 0 || leaf.getFetch() != 0; @@ -311,7 +321,8 @@ private boolean isMultiPartySecureKNN(UnaryPlan unary) { return false; } int orderRef = leaf.getOrders().get(0).getRef(); - if (!(leaf.getSelectExps().get(orderRef).getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC) + if (!( + leaf.getSelectExps().get(orderRef).getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC) && leaf.getSelectExps().get(orderRef).getStr().equals("distance"))) { return false; } @@ -321,6 +332,7 @@ private boolean isMultiPartySecureKNN(UnaryPlan unary) { } return false; } + @Override public DataSet binaryQuery(BinaryPlan binary) { List children = binary.getChildren(); diff --git a/data/src/main/java/com/hufudb/openhufu/data/function/AggregateFunction.java b/data/src/main/java/com/hufudb/openhufu/data/function/AggregateFunction.java index aa3f34b6..ce7e8d51 100644 --- a/data/src/main/java/com/hufudb/openhufu/data/function/AggregateFunction.java +++ b/data/src/main/java/com/hufudb/openhufu/data/function/AggregateFunction.java @@ -12,11 +12,13 @@ public interface AggregateFunction { * add a T element to the set which is to be aggregated, * meanwhile, update the output value (which is a E) */ + // local add void add(T ele); + // global aggregate E aggregate(); AggregateFunction copy(); - public static List> copy(List> funcs) { + static List> copy(List> funcs) { return funcs.stream().map(func -> func.copy()).collect(Collectors.toList()); } } diff --git a/data/src/main/java/com/hufudb/openhufu/data/function/Aggregator.java b/data/src/main/java/com/hufudb/openhufu/data/function/Aggregator.java index 76efbb77..dd150670 100644 --- a/data/src/main/java/com/hufudb/openhufu/data/function/Aggregator.java +++ b/data/src/main/java/com/hufudb/openhufu/data/function/Aggregator.java @@ -2,6 +2,6 @@ import com.hufudb.openhufu.data.storage.Row; -public abstract interface Aggregator extends AggregateFunction { - public abstract boolean hasNext(); +public interface Aggregator extends AggregateFunction { + boolean hasNext(); } diff --git a/data/src/main/java/com/hufudb/openhufu/data/storage/RandomDataSet.java b/data/src/main/java/com/hufudb/openhufu/data/storage/RandomDataSet.java index 9012f320..b28ab3f4 100644 --- a/data/src/main/java/com/hufudb/openhufu/data/storage/RandomDataSet.java +++ b/data/src/main/java/com/hufudb/openhufu/data/storage/RandomDataSet.java @@ -4,6 +4,15 @@ import com.hufudb.openhufu.common.exception.OpenHuFuException; import com.hufudb.openhufu.data.schema.Schema; import com.hufudb.openhufu.proto.OpenHuFuData; +import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.function.Function; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.math3.distribution.LaplaceDistribution; import org.locationtech.jts.geom.Coordinate; @@ -11,18 +20,18 @@ import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; -import java.util.*; -import java.util.function.Function; - +/* + * Used for security union, insert fake record to dataset + */ public class RandomDataSet { - public final static GeometryFactory geoFactory = new GeometryFactory(); - private final static double RANDOM_SET_SCALE = 0.5; - private final static double EPS = 1.0; - public final static int RANDOM_SET_OFFSET = 10; - private final static LaplaceDistribution lap = new LaplaceDistribution(0, 1 / EPS); - private final static Random random = new Random(); + + private static final GeometryFactory geoFactory = new GeometryFactory(); + private static final double RANDOM_SET_SCALE = 0.5; + private static final double EPS = 1.0; + private static final int RANDOM_SET_OFFSET = 10; + private static final LaplaceDistribution lap = new LaplaceDistribution(0, 1 / EPS); + private static final Random random = new SecureRandom(); private final Schema schema; - private final DataSet source; private final List originRows; private final int originSize; private final int resultSize; @@ -31,7 +40,6 @@ public class RandomDataSet { public RandomDataSet(DataSet source) { this.schema = source.getSchema(); - this.source = source; this.originRows = ArrayDataSet.materialize(source).rows; this.originSize = originRows.size(); this.randomRows = new ArrayList<>(); @@ -129,7 +137,8 @@ private Object getRandomValueFromData(int columnIndex) { Geometry geometry = (Geometry) originRows.get(r).get(columnIndex); if (geometry instanceof Point) { Point p = (Point) geometry; - return geoFactory.createPoint(new Coordinate(p.getX() + lap.sample(), p.getX() + lap.sample())); + return geoFactory.createPoint( + new Coordinate(p.getX() + lap.sample(), p.getX() + lap.sample())); } else { throw new OpenHuFuException(ErrorCode.DATA_TYPE_NOT_SUPPORT, type); } @@ -163,7 +172,7 @@ private Object getRandomValue(int columnIndex) { case GEOMETRY: return geoFactory.createPoint(new Coordinate(lap.sample(), lap.sample())); case STRING: - return RandomStringUtils.randomAlphanumeric(RANDOM_SET_OFFSET); + return RandomStringUtils.randomAlphanumeric(RANDOM_SET_OFFSET); // NOSONAR default: throw new OpenHuFuException(ErrorCode.DATA_TYPE_NOT_SUPPORT, type); } diff --git a/owner/src/main/java/com/hufudb/openhufu/owner/OwnerService.java b/owner/src/main/java/com/hufudb/openhufu/owner/OwnerService.java index 0febcbe9..70e32f86 100644 --- a/owner/src/main/java/com/hufudb/openhufu/owner/OwnerService.java +++ b/owner/src/main/java/com/hufudb/openhufu/owner/OwnerService.java @@ -1,5 +1,7 @@ package com.hufudb.openhufu.owner; +import com.hufudb.openhufu.common.exception.ErrorCode; +import com.hufudb.openhufu.common.exception.OpenHuFuException; import com.hufudb.openhufu.owner.adapter.Adapter; import com.hufudb.openhufu.owner.checker.Checker; import com.hufudb.openhufu.owner.config.ImplementorConfig; @@ -61,7 +63,7 @@ public void query(QueryPlanProto request, StreamObserver responseO Plan plan = Plan.fromProto(request); LOG.info("receives plan:\n{}", plan); if (!Checker.check(plan, schemaManager)) { - LOG.warn("Check fail for plan {}", request.toString()); + LOG.warn("Check fail for plan {}", request); responseObserver.onCompleted(); return; } @@ -71,7 +73,7 @@ public void query(QueryPlanProto request, StreamObserver responseO output.stream(); output.close(); } catch (Exception e) { - LOG.error("Error in query", e); + throw new OpenHuFuException(ErrorCode.QUERY_ERROR, e); } } diff --git a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/OwnerImplementorFactory.java b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/OwnerImplementorFactory.java index a747fb2a..4cf33927 100644 --- a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/OwnerImplementorFactory.java +++ b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/OwnerImplementorFactory.java @@ -23,16 +23,16 @@ public class OwnerImplementorFactory { static { aggFuncType2ClassName = new HashMap<>(); - aggFuncType2ClassName.put(AggFuncType.COUNT, ImplementorConfig.getImplementorClassName( - Implementor.AGG_COUNT)); + aggFuncType2ClassName.put(AggFuncType.SUM, ImplementorConfig.getImplementorClassName( + Implementor.AGG_SUM)); aggFuncType2ClassName.put(AggFuncType.MAX, ImplementorConfig.getImplementorClassName( Implementor.AGG_MAX)); aggFuncType2ClassName.put(AggFuncType.MIN, ImplementorConfig.getImplementorClassName( Implementor.AGG_MIN)); - aggFuncType2ClassName.put(AggFuncType.SUM, ImplementorConfig.getImplementorClassName( - Implementor.AGG_SUM)); - aggFuncType2ClassName.put(AggFuncType.AVG, ImplementorConfig.getImplementorClassName( - Implementor.AGG_AVG)); +// aggFuncType2ClassName.put(AggFuncType.COUNT, ImplementorConfig.getImplementorClassName( +// Implementor.AGG_COUNT)); +// aggFuncType2ClassName.put(AggFuncType.AVG, ImplementorConfig.getImplementorClassName( +// Implementor.AGG_AVG)); joinClassName = ImplementorConfig.getImplementorClassName(Implementor.JOIN); } @@ -50,7 +50,7 @@ public static OwnerAggregateFunction getAggregationFunction(AggFuncType aggFuncT } catch (ClassNotFoundException e) { throw new OpenHuFuException(e, ErrorCode.IMPLEMENTOR_CLASS_NOT_FOUND, className); } catch (NoSuchMethodException e) { - throw new OpenHuFuException(e, ErrorCode.IMPLEMENTOR_CONSTRUCTOR_NOT_FOUND, className); + throw new OpenHuFuException(e , ErrorCode.IMPLEMENTOR_CONSTRUCTOR_NOT_FOUND, className); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new OpenHuFuException(e, ErrorCode.IMPLEMENTOR_CREATE_FAILED, className); } diff --git a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/OwnerAggregateFunction.java b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/OwnerAggregateFunction.java index 8ebe0467..3a6ba10c 100644 --- a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/OwnerAggregateFunction.java +++ b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/OwnerAggregateFunction.java @@ -9,15 +9,20 @@ public abstract class OwnerAggregateFunction implements AggregateFunction { + protected final boolean twoParty; protected final int inputRef; protected final OpenHuFuData.ColumnType type; protected final OpenHuFuPlan.TaskInfo taskInfo; - public OwnerAggregateFunction(int inputRef, OpenHuFuData.ColumnType type, OpenHuFuPlan.TaskInfo taskInfo) { + public OwnerAggregateFunction(int inputRef, OpenHuFuData.ColumnType type, OpenHuFuPlan.TaskInfo taskInfo, Boolean twoParty) { this.inputRef = inputRef; this.type = type; this.taskInfo = taskInfo; + this.twoParty = twoParty; + // if the algorithm is twoParty, the size of the parties should be 2 + assert (taskInfo.getPartiesList().size() == 2) == twoParty; } + /* * the parameters of constructor for each aggregate function class */ diff --git a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/GMWSum.java b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/GMWSum.java index 4e3fbd24..4ec07c9b 100644 --- a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/GMWSum.java +++ b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/GMWSum.java @@ -18,6 +18,9 @@ import java.util.List; import java.util.concurrent.ExecutorService; +/* + * support two owners + */ public class GMWSum extends OwnerAggregateFunction { static final Logger LOG = LoggerFactory.getLogger(GMWSum.class); @@ -26,7 +29,7 @@ public class GMWSum extends OwnerAggregateFunction { final private Boardcast boardcast; GMWSum(int inputRef, GMW gmw, Boardcast boardcast, OpenHuFuPlan.TaskInfo taskInfo) { - super(inputRef, null, taskInfo); + super(inputRef, null, taskInfo, true); this.sum = 0; this.gmw = gmw; this.boardcast = boardcast; @@ -51,7 +54,7 @@ public Comparable aggregate() { return OpenHuFuCodec.decodeInt(res); } } catch (ProtocolException e) { - LOG.error("Error when executing GMW", e); + LOG.error("Error when executing GMW Sum", e); return null; } } diff --git a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/SecretSharingSum.java b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/SecretSharingSum.java index 8ce41fda..62564242 100644 --- a/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/SecretSharingSum.java +++ b/owner/src/main/java/com/hufudb/openhufu/owner/implementor/aggregate/sum/SecretSharingSum.java @@ -20,7 +20,7 @@ public class SecretSharingSum extends OwnerAggregateFunction { final private boolean hasOutput; SecretSharingSum(int inputRef, SecretSharing ss, OpenHuFuData.ColumnType type, OpenHuFuPlan.TaskInfo taskInfo) { - super(inputRef, type, taskInfo); + super(inputRef, type, taskInfo, false); this.sum = 0; this.ss = ss; this.hasOutput = ss.getOwnId() == taskInfo.getPartiesList().get(0); diff --git a/scripts/test/genSyntheticData.py b/scripts/test/genSyntheticData.py index 8e182641..e44c038b 100644 --- a/scripts/test/genSyntheticData.py +++ b/scripts/test/genSyntheticData.py @@ -131,7 +131,7 @@ def genSynData(points, desPath, prefix, size, databaseID): fout.write(")\n") with open(scmFileName, "w") as fout: fout.write("S_ID | S_POINT\n") - fout.write("LONG | POINT\n") + fout.write("LONG | GEOMETRY\n") diff --git a/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/DWithin.java b/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/DWithin.java index a4c6e09f..25bce951 100644 --- a/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/DWithin.java +++ b/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/DWithin.java @@ -1,5 +1,7 @@ package com.hufudb.openhufu.udf; +import com.hufudb.openhufu.common.exception.ErrorCode; +import com.hufudb.openhufu.common.exception.OpenHuFuException; import java.util.List; import com.google.common.collect.ImmutableList; import com.hufudb.openhufu.proto.OpenHuFuData.ColumnType; @@ -25,7 +27,8 @@ public Boolean dwithin(Geometry left, Geometry right, Double distance) { public Object implement(List inputs) { if (inputs.size() != 3) { LOG.error("DWithin UDF expect 3 parameters, but give {}", inputs.size()); - throw new RuntimeException("DWithin UDF expect 3 parameters"); + throw new OpenHuFuException(ErrorCode.FUNCTION_PARAMS_SIZE_ERROR, getName(), 3, + inputs.size()); } if (inputs.get(0) == null || inputs.get(1) == null || inputs.get(2) == null) { return null; @@ -33,11 +36,12 @@ public Object implement(List inputs) { if (!(inputs.get(0) instanceof Geometry) || !(inputs.get(1) instanceof Geometry) || !(inputs.get(2) instanceof Number)) { LOG.error("DWithin UDF requires (Point, Point, Double)"); - throw new RuntimeException("DWithin UDF requires (Point, Point)"); + throw new OpenHuFuException(ErrorCode.FUNCTION_PARAMS_TYPE_ERROR, getName(), + "(Point, Point, Double)"); } Geometry left = (Geometry) inputs.get(0); Geometry right = (Geometry) inputs.get(1); return left.distance(right) <= ((Number) inputs.get(2)) - .doubleValue(); + .doubleValue(); } } diff --git a/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Distance.java b/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Distance.java index e07c27b6..44ebf212 100644 --- a/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Distance.java +++ b/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Distance.java @@ -1,5 +1,7 @@ package com.hufudb.openhufu.udf; +import com.hufudb.openhufu.common.exception.ErrorCode; +import com.hufudb.openhufu.common.exception.OpenHuFuException; import java.util.List; import com.google.common.collect.ImmutableList; import com.hufudb.openhufu.proto.OpenHuFuData.ColumnType; @@ -24,7 +26,8 @@ public Double distance(Geometry left, Geometry right) { public Object implement(List inputs) { if (inputs.size() != 2) { LOG.error("Distance UDF expect 2 parameters, but give {}", inputs.size()); - throw new RuntimeException("Distance UDF expect 2 parameters"); + throw new OpenHuFuException(ErrorCode.FUNCTION_PARAMS_SIZE_ERROR, getName(), 2, + inputs.size()); } if (inputs.get(0) == null || inputs.get(1) == null) { return null; diff --git a/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Point.java b/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Point.java index 1a2a4323..a62a42e4 100644 --- a/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Point.java +++ b/udf/spatial-udf/src/main/java/com/hufudb/openhufu/udf/Point.java @@ -1,5 +1,7 @@ package com.hufudb.openhufu.udf; +import com.hufudb.openhufu.common.exception.ErrorCode; +import com.hufudb.openhufu.common.exception.OpenHuFuException; import com.hufudb.openhufu.proto.OpenHuFuData.ColumnType; import java.util.List; import com.google.common.collect.ImmutableList; @@ -28,7 +30,8 @@ public Geometry point(Double x, Double y) { public Object implement(List inputs) { if (inputs.size() != 2) { LOG.error("Point UDF expect 2 parameters, but give {}", inputs.size()); - throw new RuntimeException("Point UDF expect 2 parameters"); + throw new OpenHuFuException(ErrorCode.FUNCTION_PARAMS_SIZE_ERROR, getName(), 2, + inputs.size()); } if (inputs.get(0) == null || inputs.get(1) == null) { return null;