Search in sources :

Example 1 with RowKeyValueAccessor

use of org.apache.phoenix.schema.RowKeyValueAccessor in project phoenix by apache.

the class UnnestArrayPlanTest method testUnnestArrays.

private void testUnnestArrays(PArrayDataType arrayType, List<Object[]> arrays, boolean withOrdinality) throws Exception {
    PDataType baseType = PDataType.fromTypeId(arrayType.getSqlType() - PDataType.ARRAY_TYPE_BASE);
    List<Tuple> tuples = toTuples(arrayType, arrays);
    LiteralResultIterationPlan subPlan = new LiteralResultIterationPlan(tuples, CONTEXT, SelectStatement.SELECT_ONE, TableRef.EMPTY_TABLE_REF, RowProjector.EMPTY_PROJECTOR, null, null, OrderBy.EMPTY_ORDER_BY, null);
    LiteralExpression dummy = LiteralExpression.newConstant(null, arrayType);
    RowKeyValueAccessor accessor = new RowKeyValueAccessor(Arrays.asList(dummy), 0);
    UnnestArrayPlan plan = new UnnestArrayPlan(subPlan, new RowKeyColumnExpression(dummy, accessor), withOrdinality);
    PName colName = PNameFactory.newName("ELEM");
    PColumn elemColumn = new PColumnImpl(PNameFactory.newName("ELEM"), PNameFactory.newName(VALUE_COLUMN_FAMILY), baseType, null, null, true, 0, SortOrder.getDefault(), null, null, false, "", false, false, colName.getBytes());
    colName = PNameFactory.newName("IDX");
    PColumn indexColumn = withOrdinality ? new PColumnImpl(colName, PNameFactory.newName(VALUE_COLUMN_FAMILY), PInteger.INSTANCE, null, null, true, 0, SortOrder.getDefault(), null, null, false, "", false, false, colName.getBytes()) : null;
    List<PColumn> columns = withOrdinality ? Arrays.asList(elemColumn, indexColumn) : Arrays.asList(elemColumn);
    ProjectedColumnExpression elemExpr = new ProjectedColumnExpression(elemColumn, columns, 0, elemColumn.getName().getString());
    ProjectedColumnExpression indexExpr = withOrdinality ? new ProjectedColumnExpression(indexColumn, columns, 1, indexColumn.getName().getString()) : null;
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    ResultIterator iterator = plan.iterator();
    for (Object[] o : flatten(arrays)) {
        Tuple tuple = iterator.next();
        assertNotNull(tuple);
        assertTrue(elemExpr.evaluate(tuple, ptr));
        Object elem = baseType.toObject(ptr);
        assertEquals(o[0], elem);
        if (withOrdinality) {
            assertTrue(indexExpr.evaluate(tuple, ptr));
            Object index = PInteger.INSTANCE.toObject(ptr);
            assertEquals(o[1], index);
        }
    }
    assertNull(iterator.next());
}
Also used : PColumnImpl(org.apache.phoenix.schema.PColumnImpl) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) RowKeyValueAccessor(org.apache.phoenix.schema.RowKeyValueAccessor) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ResultIterator(org.apache.phoenix.iterate.ResultIterator) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) PColumn(org.apache.phoenix.schema.PColumn) PDataType(org.apache.phoenix.schema.types.PDataType) PName(org.apache.phoenix.schema.PName) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Example 2 with RowKeyValueAccessor

use of org.apache.phoenix.schema.RowKeyValueAccessor in project phoenix by apache.

the class PhoenixRuntime method getFirstPKColumnExpression.

private static Expression getFirstPKColumnExpression(PTable table) throws SQLException {
    if (table.getIndexType() == IndexType.LOCAL) {
        /*
             * With some hackery, we could deduce the tenant ID from a multi-tenant local index,
             * however it's not clear that we'd want to maintain the same prefixing of the region
             * start key, as the region boundaries may end up being different on a cluster being
             * replicated/backed-up to (which is the use case driving the method).
             */
        throw new SQLFeatureNotSupportedException();
    }
    // skip salt and viewIndexId columns.
    int pkPosition = (table.getBucketNum() == null ? 0 : 1) + (table.getViewIndexId() == null ? 0 : 1);
    List<PColumn> pkColumns = table.getPKColumns();
    return new RowKeyColumnExpression(pkColumns.get(pkPosition), new RowKeyValueAccessor(pkColumns, pkPosition));
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) RowKeyValueAccessor(org.apache.phoenix.schema.RowKeyValueAccessor) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression)

Example 3 with RowKeyValueAccessor

use of org.apache.phoenix.schema.RowKeyValueAccessor in project phoenix by apache.

the class WhereCompilerTest method testOrFilter.

@Test
public void testOrFilter() throws SQLException {
    String tenantId = "000000000000001";
    String keyPrefix = "foo";
    int aInt = 2;
    String query = "select * from atable where organization_id=? and (substr(entity_id,1,3)=? or a_integer=?)";
    List<Object> binds = Arrays.<Object>asList(tenantId, keyPrefix, aInt);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    bindParams(pstmt, binds);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(singleKVFilter(or(constantComparison(CompareOp.EQUAL, new SubstrFunction(Arrays.<Expression>asList(new RowKeyColumnExpression(ENTITY_ID, new RowKeyValueAccessor(ATABLE.getPKColumns(), 1)), LiteralExpression.newConstant(1), LiteralExpression.newConstant(3))), keyPrefix), constantComparison(CompareOp.EQUAL, A_INTEGER, aInt))), filter);
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) TestUtil.multiEncodedKVFilter(org.apache.phoenix.util.TestUtil.multiEncodedKVFilter) SkipScanFilter(org.apache.phoenix.filter.SkipScanFilter) Filter(org.apache.hadoop.hbase.filter.Filter) TestUtil.singleKVFilter(org.apache.phoenix.util.TestUtil.singleKVFilter) RowKeyValueAccessor(org.apache.phoenix.schema.RowKeyValueAccessor) Scan(org.apache.hadoop.hbase.client.Scan) SubstrFunction(org.apache.phoenix.expression.function.SubstrFunction) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 4 with RowKeyValueAccessor

use of org.apache.phoenix.schema.RowKeyValueAccessor in project phoenix by apache.

the class WhereCompilerTest method testRowKeyFilter.

@Test
public void testRowKeyFilter() throws SQLException {
    String keyPrefix = "foo";
    String query = "select * from atable where substr(entity_id,1,3)=?";
    List<Object> binds = Arrays.<Object>asList(keyPrefix);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    bindParams(pstmt, binds);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(new RowKeyComparisonFilter(constantComparison(CompareOp.EQUAL, new SubstrFunction(Arrays.<Expression>asList(new RowKeyColumnExpression(ENTITY_ID, new RowKeyValueAccessor(ATABLE.getPKColumns(), 1)), LiteralExpression.newConstant(1), LiteralExpression.newConstant(3))), keyPrefix), QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES), filter);
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) RowKeyValueAccessor(org.apache.phoenix.schema.RowKeyValueAccessor) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) TestUtil.multiEncodedKVFilter(org.apache.phoenix.util.TestUtil.multiEncodedKVFilter) SkipScanFilter(org.apache.phoenix.filter.SkipScanFilter) Filter(org.apache.hadoop.hbase.filter.Filter) TestUtil.singleKVFilter(org.apache.phoenix.util.TestUtil.singleKVFilter) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) Scan(org.apache.hadoop.hbase.client.Scan) SubstrFunction(org.apache.phoenix.expression.function.SubstrFunction) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) RowKeyComparisonFilter(org.apache.phoenix.filter.RowKeyComparisonFilter) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 5 with RowKeyValueAccessor

use of org.apache.phoenix.schema.RowKeyValueAccessor in project phoenix by apache.

the class ExpressionCompiler method wrapGroupByExpression.

private Expression wrapGroupByExpression(Expression expression) {
    // column.
    if (aggregateFunction == null) {
        int index = groupBy.getExpressions().indexOf(expression);
        if (index >= 0) {
            isAggregate = true;
            RowKeyValueAccessor accessor = new RowKeyValueAccessor(groupBy.getKeyExpressions(), index);
            expression = new RowKeyColumnExpression(expression, accessor, groupBy.getKeyExpressions().get(index).getDataType());
        }
    }
    return expression;
}
Also used : RowKeyValueAccessor(org.apache.phoenix.schema.RowKeyValueAccessor) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression)

Aggregations

RowKeyValueAccessor (org.apache.phoenix.schema.RowKeyValueAccessor)8 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)7 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)4 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)4 Test (org.junit.Test)4 Scan (org.apache.hadoop.hbase.client.Scan)3 Filter (org.apache.hadoop.hbase.filter.Filter)3 Expression (org.apache.phoenix.expression.Expression)3 RowKeyComparisonFilter (org.apache.phoenix.filter.RowKeyComparisonFilter)3 SkipScanFilter (org.apache.phoenix.filter.SkipScanFilter)3 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)3 PhoenixPreparedStatement (org.apache.phoenix.jdbc.PhoenixPreparedStatement)3 PColumn (org.apache.phoenix.schema.PColumn)3 TestUtil.multiEncodedKVFilter (org.apache.phoenix.util.TestUtil.multiEncodedKVFilter)3 TestUtil.singleKVFilter (org.apache.phoenix.util.TestUtil.singleKVFilter)3 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)2 SubstrFunction (org.apache.phoenix.expression.function.SubstrFunction)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 FilterList (org.apache.hadoop.hbase.filter.FilterList)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1