Search in sources :

Example 41 with Expression

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();
        }
    }
}
Also used : Expression(org.apache.phoenix.expression.Expression) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)

Example 42 with Expression

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);
}
Also used : RoundDateExpression(org.apache.phoenix.expression.function.RoundDateExpression) Expression(org.apache.phoenix.expression.Expression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 43 with Expression

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);
}
Also used : RoundDateExpression(org.apache.phoenix.expression.function.RoundDateExpression) Expression(org.apache.phoenix.expression.Expression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Date(java.sql.Date) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 44 with Expression

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);
}
Also used : CountAggregateFunction(org.apache.phoenix.expression.function.CountAggregateFunction) RoundDateExpression(org.apache.phoenix.expression.function.RoundDateExpression) Expression(org.apache.phoenix.expression.Expression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 45 with Expression

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);
}
Also used : RoundDateExpression(org.apache.phoenix.expression.function.RoundDateExpression) Expression(org.apache.phoenix.expression.Expression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

Expression (org.apache.phoenix.expression.Expression)182 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)101 PDataType (org.apache.phoenix.schema.types.PDataType)54 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)39 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)37 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)33 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)30 PTable (org.apache.phoenix.schema.PTable)25 ParseNode (org.apache.phoenix.parse.ParseNode)23 RowValueConstructorExpression (org.apache.phoenix.expression.RowValueConstructorExpression)22 Test (org.junit.Test)22 ComparisonExpression (org.apache.phoenix.expression.ComparisonExpression)21 AndExpression (org.apache.phoenix.expression.AndExpression)20 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)20 PColumn (org.apache.phoenix.schema.PColumn)20 ArrayList (java.util.ArrayList)19 InListExpression (org.apache.phoenix.expression.InListExpression)17 IsNullExpression (org.apache.phoenix.expression.IsNullExpression)16 OrExpression (org.apache.phoenix.expression.OrExpression)16 LikeExpression (org.apache.phoenix.expression.LikeExpression)15