Skip to content

Commit

Permalink
Fixes #1268 and a bug in the values output by VALUESPLIT
Browse files Browse the repository at this point in the history
  • Loading branch information
hbs committed Jun 26, 2023
1 parent 9d670c0 commit f3cb719
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
42 changes: 38 additions & 4 deletions warp10/src/main/java/io/warp10/continuum/gts/GTSHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ private static final void quicksortByValue(GeoTimeSerie gts, int low, int high,
long lpivot = 0L;
double dpivot = 0.0D;
String spivot = null;
Boolean bpivot = null;

TYPE type = gts.getType();

Expand All @@ -695,8 +696,7 @@ private static final void quicksortByValue(GeoTimeSerie gts, int low, int high,
} else if (TYPE.STRING == type) {
spivot = gts.stringValues[low + (high-low)/2];
} else if (TYPE.BOOLEAN == type) {
// Do nothing for booleans
return;
bpivot = gts.booleanValues.get(low + (high-low)/2);
}

long pivotTick = gts.ticks[low + (high-low) / 2];
Expand All @@ -705,8 +705,6 @@ private static final void quicksortByValue(GeoTimeSerie gts, int low, int high,
while (i <= j) {

if (TYPE.LONG == type) {


if (!reversed) {
// If the current value from the left list is smaller
// (or greater if reversed is true) than the pivot
Expand Down Expand Up @@ -769,6 +767,27 @@ private static final void quicksortByValue(GeoTimeSerie gts, int low, int high,
j--;
}
}
} else if (TYPE.BOOLEAN == type) {
if (!reversed) {
// If the current value from the left list is smaller
// (or greater if reversed is true) than the pivot
// element then get the next element from the left list
while(booleanCompare(gts.booleanValues.get(i), bpivot) < 0 || (0 == booleanCompare(gts.booleanValues.get(i), bpivot) && gts.ticks[i] < pivotTick)) {
i++;
}
// If the current value from the right list is larger (or lower if reversed is true)
// than the pivot element then get the next element from the right list
while(booleanCompare(gts.booleanValues.get(j), bpivot) > 0 || (0 == booleanCompare(gts.booleanValues.get(j), bpivot) && gts.ticks[j] > pivotTick)) {
j--;
}
} else {
while(booleanCompare(gts.booleanValues.get(i), bpivot) > 0 || (0 == booleanCompare(gts.booleanValues.get(i), bpivot) && gts.ticks[i] > pivotTick)) {
i++;
}
while(booleanCompare(gts.booleanValues.get(j), bpivot) < 0 || (0 == booleanCompare(gts.booleanValues.get(j), bpivot) && gts.ticks[j] < pivotTick)) {
j--;
}
}
}

// If we have found a values in the left list which is larger then
Expand Down Expand Up @@ -828,6 +847,21 @@ private static final void quicksortByValue(GeoTimeSerie gts, int low, int high,
}
}

private static final int booleanCompare(boolean a, boolean b) {
if (a) {
if (b) {
return 0;
} else {
return 1;
}
} else {
if (b) {
return -1;
} else {
return 0;
}
}
}
/**
* Sort GTS according to location, using HHCodes, between two indexes.
* The ticks with no locations are considered the smallest.
Expand Down
46 changes: 23 additions & 23 deletions warp10/src/main/java/io/warp10/script/functions/VALUESPLIT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2018 SenX S.A.S.
// Copyright 2018-2023 SenX S.A.S.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,77 +16,77 @@

package io.warp10.script.functions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.warp10.continuum.gts.GTSHelper;
import io.warp10.continuum.gts.GeoTimeSerie;
import io.warp10.script.GTSStackFunction;
import io.warp10.script.WarpScriptException;
import io.warp10.script.WarpScriptStack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Split a GTS into N distinct GTS, one for each distinct value
*
*
* @param name Name of label to use for storing the value
*/
public class VALUESPLIT extends GTSStackFunction {

private static final String PARAM_LABEL = "label";

public VALUESPLIT(String name) {
super(name);
}

@Override
protected Map<String, Object> retrieveParameters(WarpScriptStack stack) throws WarpScriptException {
Object top = stack.pop();

if (!(top instanceof String)) {
throw new WarpScriptException(getName() + " expects a label name on top of the stack.");
}

Map<String,Object> params = new HashMap<String, Object>();
params.put(PARAM_LABEL, top.toString());

return params;
}

@Override
protected Object gtsOp(Map<String, Object> params, GeoTimeSerie gts) throws WarpScriptException {

String label = params.get(PARAM_LABEL).toString();

//
// Sort gts by values
//

GTSHelper.valueSort(gts);

List<GeoTimeSerie> series = new ArrayList<GeoTimeSerie>();

GeoTimeSerie split = null;
Object lastvalue = null;

for (int i = 0; i < gts.size(); i++) {
long tick = GTSHelper.tickAtIndex(gts, i);
long location = GTSHelper.locationAtIndex(gts, i);
long elevation = GTSHelper.elevationAtIndex(gts, i);
Object value = GTSHelper.valueAtIndex(gts, i);

if (!value.equals(lastvalue)) {
split = gts.cloneEmpty();
split.getMetadata().putToLabels(label, value.toString());
series.add(split);
}
GTSHelper.setValue(split, tick, location, elevation, Boolean.TRUE, false);

GTSHelper.setValue(split, tick, location, elevation, value, false);

lastvalue = value;
}

return series;
}
}

0 comments on commit f3cb719

Please sign in to comment.