Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
anoopsjohn committed Jul 3, 2013
1 parent 42c2ea1 commit 574ab45
Show file tree
Hide file tree
Showing 19 changed files with 1,018 additions and 145 deletions.
4 changes: 1 addition & 3 deletions src/main/antlr3/PhoenixSQL.g
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ tokens
SHOW='show';
TABLES='tables';
ALL='all';
PERCENTILE_CONT='percentile_cont';
WITHIN='within';
}

Expand Down Expand Up @@ -630,10 +629,9 @@ expression_term returns [ParseNode ret]
@init{ParseNode n;boolean isAscending=true;}
: field=identifier oj=OUTER_JOIN? {n = factory.column(field); $ret = oj==null ? n : factory.outer(n); }
| tableName=table_name DOT field=identifier oj=OUTER_JOIN? {n = factory.column(tableName, field); $ret = oj==null ? n : factory.outer(n); }
| field=identifier LPAREN l=expression_list RPAREN { $ret = factory.function(field, l);}
| field=identifier LPAREN l=expression_list RPAREN wg=(WITHIN GROUP LPAREN ORDER BY l2=expression_list (ASC {isAscending = true;} | DESC {isAscending = false;}) RPAREN)?{ $ret = wg==null ? factory.function(field, l) : factory.function(field,l,l2,isAscending);}
| field=identifier LPAREN t=ASTERISK RPAREN { if (!isCountFunction(field)) { throwRecognitionException(t); } $ret = factory.function(field, LiteralParseNode.STAR);}
| field=identifier LPAREN t=DISTINCT l=expression_list RPAREN { $ret = factory.functionDistinct(field, l);}
| PERCENTILE_CONT LPAREN e1=expression RPAREN WITHIN GROUP LPAREN ORDER BY e2=expression (ASC {isAscending = true;} | DESC {isAscending = false;}) RPAREN { $ret = factory.percentileCont(e1,e2,isAscending);}
| e=expression_literal_bind oj=OUTER_JOIN? { n = e; $ret = oj==null ? n : factory.outer(n); }
| e=case_statement { $ret = e; }
| LPAREN e=expression RPAREN { $ret = e; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public enum SQLExceptionCode {
MISSING_BINARY_LENGTH(210, "22003", "Missing length for BINARY."),
NONPOSITIVE_BINARY_LENGTH(211, "22003", "BINARY must have a positive length."),
SERVER_ARITHMATIC_ERROR(212, "22012", "Arithmatic error on server."),
VALUE_OUTSIDE_RANGE(213,"22003","Value outside range."),

/**
* Constraint Violation (errorcode 03, sqlstate 23)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public enum ExpressionType {
LowerFunction(LowerFunction.class),
TrimFunction(TrimFunction.class),
DistinctCountAggregateFunction(DistinctCountAggregateFunction.class),
PercentileContAggregateFunction(PercentileContAggregateFunction.class);
PercentileContAggregateFunction(PercentileContAggregateFunction.class),
PercentRankAggregateFunction(PercentRankAggregateFunction.class),
StddevPopFunction(StddevPopFunction.class),
StddevSampFunction(StddevSampFunction.class);

ExpressionType(Class<? extends Expression> clazz) {
this.clazz = clazz;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2013, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of Salesforce.com nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.salesforce.phoenix.expression.aggregator;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map.Entry;

import org.apache.hadoop.hbase.io.ImmutableBytesWritable;

import com.salesforce.phoenix.expression.Expression;
import com.salesforce.phoenix.schema.PDataType;
import com.salesforce.phoenix.schema.tuple.Tuple;
import com.salesforce.phoenix.util.ImmutableBytesPtr;

/**
*
* @author anoopsjohn
* @since 1.2.1
*/
public abstract class BaseStddevAggregator extends DistinctValueWithCountClientAggregator {

protected Expression stdDevColExp;

public BaseStddevAggregator(List<Expression> exps) {
this.stdDevColExp = exps.get(0);
}

@Override
protected int getBufferLength() {
return PDataType.DECIMAL.getByteSize();
}

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
double ssd = sumSquaredDeviation();
double result = Math.sqrt(ssd / getDataPointsCount());
if (buffer == null) {
initBuffer();
}
buffer = PDataType.DECIMAL.toBytes(new BigDecimal(result));
ptr.set(buffer);
return true;
}

protected abstract long getDataPointsCount();

private double sumSquaredDeviation() {
double m = mean();
double result = 0.0;
for (Entry<ImmutableBytesPtr, Integer> entry : valueVsCount.entrySet()) {
double colValue = ((Number) this.stdDevColExp.getDataType().toObject(entry.getKey()))
.doubleValue();
double delta = colValue - m;
result += (delta * delta) * entry.getValue();
}
return result;
}

private double mean() {
double sum = 0.0;
for (Entry<ImmutableBytesPtr, Integer> entry : valueVsCount.entrySet()) {
double colValue = ((Number) this.stdDevColExp.getDataType().toObject(entry.getKey()))
.doubleValue();
sum += colValue * entry.getValue();
}
return sum / totalCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2013, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of Salesforce.com nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.salesforce.phoenix.expression.aggregator;

import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;

import org.apache.hadoop.hbase.io.ImmutableBytesWritable;

import com.salesforce.phoenix.expression.*;
import com.salesforce.phoenix.schema.PDataType;
import com.salesforce.phoenix.schema.tuple.Tuple;
import com.salesforce.phoenix.util.ImmutableBytesPtr;

/**
* Client side Aggregator for PERCENT_RANK aggregations
*
* @author anoopsjohn
* @since 1.2.1
*/
public class PercentRankClientAggregator extends DistinctValueWithCountClientAggregator {

private List<Expression> exps = null;

public PercentRankClientAggregator(List<Expression> exps) {
this.exps = exps;
}

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
ColumnExpression columnExp = (ColumnExpression)exps.get(0);
// Second exp will be a LiteralExpression of Boolean type indicating whether the ordering to
// be ASC/DESC
LiteralExpression isAscendingExpression = (LiteralExpression)exps.get(1);
boolean isAscending = (Boolean)isAscendingExpression.getValue();

// Third expression will be LiteralExpression
LiteralExpression valueExp = (LiteralExpression)exps.get(2);
// To sort the valueVsCount
NavigableMap<ImmutableBytesPtr, Integer> sortedMap = new TreeMap<ImmutableBytesPtr, Integer>(valueVsCount);
if (!isAscending) {
sortedMap = sortedMap.descendingMap();
}

long distinctCountsSum = 0;
for (Entry<ImmutableBytesPtr, Integer> entry : sortedMap.entrySet()) {
Object value = valueExp.getValue();
Object colValue = columnExp.getDataType().toObject(entry.getKey());
int compareResult = columnExp.getDataType().compareTo(colValue, value, valueExp.getDataType());
boolean done = isAscending ? compareResult > 0 : compareResult <= 0;
if (done) break;
distinctCountsSum += entry.getValue();
}

float result = (float)distinctCountsSum / totalCount;
if (buffer == null) {
initBuffer();
}
buffer = PDataType.DECIMAL.toBytes(new BigDecimal(result));
ptr.set(buffer);
return true;
}

@Override
protected int getBufferLength() {
return PDataType.DECIMAL.getByteSize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@
package com.salesforce.phoenix.expression.aggregator;

import java.math.BigDecimal;
import java.util.List;
import java.util.*;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;

import org.apache.hadoop.hbase.io.ImmutableBytesWritable;

import com.salesforce.phoenix.expression.ColumnExpression;
import com.salesforce.phoenix.expression.Expression;
import com.salesforce.phoenix.expression.LiteralExpression;
import com.salesforce.phoenix.expression.*;
import com.salesforce.phoenix.schema.PDataType;
import com.salesforce.phoenix.schema.tuple.Tuple;
import com.salesforce.phoenix.util.ImmutableBytesPtr;
Expand All @@ -58,24 +54,24 @@ public PercentileClientAggregator(List<Expression> exps) {

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
ColumnExpression columnExp = (ColumnExpression) exps.get(0);
ColumnExpression columnExp = (ColumnExpression)exps.get(0);
// Second exp will be a LiteralExpression of Boolean type indicating whether the ordering to
// be ASC/DESC
LiteralExpression isAscendingExpression = (LiteralExpression) exps.get(1);
boolean isAscending = (Boolean) isAscendingExpression.getValue();
LiteralExpression isAscendingExpression = (LiteralExpression)exps.get(1);
boolean isAscending = (Boolean)isAscendingExpression.getValue();

// Third expression will be LiteralExpression
LiteralExpression percentileExp = (LiteralExpression) exps.get(2);
float p = ((Number) percentileExp.getValue()).floatValue();
NavigableMap<ImmutableBytesPtr, Integer> sortedMap = new TreeMap<ImmutableBytesPtr, Integer>(
valueVsCount); // To sort the valueVsCount.
LiteralExpression percentileExp = (LiteralExpression)exps.get(2);
float p = ((Number)percentileExp.getValue()).floatValue();

// To sort the valueVsCount
NavigableMap<ImmutableBytesPtr, Integer> sortedMap = new TreeMap<ImmutableBytesPtr, Integer>(valueVsCount);
if (!isAscending) {
sortedMap = sortedMap.descendingMap();
}

float i = (p * this.totalCount) + 0.5F;
long k = (long) i;
long k = (long)i;
float f = i - k;
ImmutableBytesPtr pi1 = null;
ImmutableBytesPtr pi2 = null;
Expand All @@ -95,11 +91,11 @@ public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
}

float result = 0F;
Number n1 = (Number) columnExp.getDataType().toObject(pi1);
Number n1 = (Number)columnExp.getDataType().toObject(pi1);
if (pi2 == null || pi1 == pi2) {
result = n1.floatValue();
} else {
Number n2 = (Number) columnExp.getDataType().toObject(pi2);
Number n2 = (Number)columnExp.getDataType().toObject(pi2);
result = (n1.floatValue() * (1.0F - f)) + (n2.floatValue() * f);
}
if (buffer == null) {
Expand All @@ -110,8 +106,8 @@ public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
return true;
}

@Override
protected int getBufferLength() {
return PDataType.DECIMAL.getByteSize();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2013, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of Salesforce.com nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.salesforce.phoenix.expression.aggregator;

import java.util.List;

import com.salesforce.phoenix.expression.Expression;

/**
* Client side Aggregator for STDDEV_POP aggregations
*
* @author anoopsjohn
* @since 1.2.1
*/
public class StddevPopAggregator extends BaseStddevAggregator {

public StddevPopAggregator(List<Expression> exps) {
super(exps);
}

@Override
protected long getDataPointsCount() {
return totalCount;
}
}
Loading

0 comments on commit 574ab45

Please sign in to comment.