use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class TupleUtil method getConcatenatedValue.
/** Concatenate results evaluated against a list of expressions
*
* @param result the tuple for expression evaluation
* @param expressions
* @return the concatenated byte array as ImmutableBytesWritable
* @throws IOException
*/
public static ImmutableBytesPtr getConcatenatedValue(Tuple result, List<Expression> expressions) throws IOException {
ImmutableBytesPtr value = new ImmutableBytesPtr(ByteUtil.EMPTY_BYTE_ARRAY);
Expression expression = expressions.get(0);
boolean evaluated = expression.evaluate(result, value);
if (expressions.size() == 1) {
if (!evaluated) {
value.set(ByteUtil.EMPTY_BYTE_ARRAY);
}
return value;
} else {
TrustedByteArrayOutputStream output = new TrustedByteArrayOutputStream(value.getLength() * expressions.size());
try {
if (evaluated) {
output.write(value.get(), value.getOffset(), value.getLength());
}
for (int i = 1; i < expressions.size(); i++) {
if (!expression.getDataType().isFixedWidth()) {
output.write(SchemaUtil.getSeparatorByte(true, value.getLength() == 0, expression));
}
expression = expressions.get(i);
if (expression.evaluate(result, value)) {
output.write(value.get(), value.getOffset(), value.getLength());
} else if (i < expressions.size() - 1 && expression.getDataType().isFixedWidth()) {
// converted to a variable length type (i.e. DECIMAL) to allow an empty byte array to represent null.
throw new DoNotRetryIOException("Non terminating null value found for fixed width expression (" + expression + ") in row: " + result);
}
}
// Write trailing separator if last expression was variable length and descending
if (!expression.getDataType().isFixedWidth() && SchemaUtil.getSeparatorByte(true, value.getLength() == 0, expression) == QueryConstants.DESC_SEPARATOR_BYTE) {
output.write(QueryConstants.DESC_SEPARATOR_BYTE);
}
byte[] outputBytes = output.getBuffer();
value.set(outputBytes, 0, output.size());
return value;
} finally {
output.close();
}
}
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class HavingCompilerTest method testHavingToAndWhere.
@Test
public void testHavingToAndWhere() throws SQLException {
String query = "select count(1) from atable where b_string > 'bar' group by a_string having a_string = 'foo'";
List<Object> binds = Collections.emptyList();
Expressions expressions = compileStatement(query, binds);
Expression w = and(constantComparison(CompareOp.GREATER, B_STRING, "bar"), constantComparison(CompareOp.EQUAL, A_STRING, "foo"));
assertEquals(w, expressions.whereClause);
assertNull(expressions.havingClause);
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class HavingCompilerTest method testHavingFuncToWhere.
@Test
public void testHavingFuncToWhere() throws SQLException {
// TODO: confirm that this is a valid optimization
String query = "select count(1) from atable group by a_date having round(a_date, 'hour') > ?";
Date date = new Date(System.currentTimeMillis());
List<Object> binds = Arrays.<Object>asList(date);
Expressions expressions = compileStatement(query, binds);
Expression w = constantComparison(CompareOp.GREATER, RoundDateExpression.create(Arrays.asList(A_DATE, LiteralExpression.newConstant("hour"), LiteralExpression.newConstant(1))), date);
assertEquals(w, expressions.whereClause);
assertNull(expressions.havingClause);
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class HavingCompilerTest method testAndHavingToAndWhere.
@Test
public void testAndHavingToAndWhere() throws SQLException {
String query = "select count(1) from atable where b_string > 'bar' group by a_string having count(1) >= 1 and a_string = 'foo'";
List<Object> binds = Collections.emptyList();
Expressions expressions = compileStatement(query, binds);
Expression h = constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(), 1L);
Expression w = and(constantComparison(CompareOp.GREATER, B_STRING, "bar"), constantComparison(CompareOp.EQUAL, A_STRING, "foo"));
assertEquals(w, expressions.whereClause);
assertEquals(h, expressions.havingClause);
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class HavingCompilerTest method testOrAggColsInHaving.
@Test
public void testOrAggColsInHaving() throws SQLException {
String query = "select count(1) from atable group by a_string,b_string having a_string = 'a' or b_string = 'b'";
List<Object> binds = Collections.emptyList();
Expressions expressions = compileStatement(query, binds);
Expression w = or(constantComparison(CompareOp.EQUAL, A_STRING, "a"), constantComparison(CompareOp.EQUAL, B_STRING, "b"));
assertEquals(w, expressions.whereClause);
assertNull(expressions.havingClause);
}
Aggregations