Search in sources :

Example 1 with RowKeyColumnExpression

use of org.apache.phoenix.expression.RowKeyColumnExpression 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 RowKeyColumnExpression

use of org.apache.phoenix.expression.RowKeyColumnExpression 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 RowKeyColumnExpression

use of org.apache.phoenix.expression.RowKeyColumnExpression 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 RowKeyColumnExpression

use of org.apache.phoenix.expression.RowKeyColumnExpression 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 RowKeyColumnExpression

use of org.apache.phoenix.expression.RowKeyColumnExpression in project phoenix by apache.

the class ColumnRef method newColumnExpression.

public Expression newColumnExpression(boolean schemaNameCaseSensitive, boolean colNameCaseSensitive) throws SQLException {
    PTable table = tableRef.getTable();
    PColumn column = this.getColumn();
    String displayName = tableRef.getColumnDisplayName(this, schemaNameCaseSensitive, colNameCaseSensitive);
    if (SchemaUtil.isPKColumn(column)) {
        return new RowKeyColumnExpression(column, new RowKeyValueAccessor(table.getPKColumns(), pkSlotPosition), displayName);
    }
    if (table.getType() == PTableType.PROJECTED || table.getType() == PTableType.SUBQUERY) {
        return new ProjectedColumnExpression(column, table, displayName);
    }
    Expression expression = table.getImmutableStorageScheme() == ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS ? new SingleCellColumnExpression(column, displayName, table.getEncodingScheme(), table.getImmutableStorageScheme()) : new KeyValueColumnExpression(column, displayName);
    if (column.getExpressionStr() != null) {
        String url = PhoenixRuntime.JDBC_PROTOCOL + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + PhoenixRuntime.CONNECTIONLESS;
        PhoenixConnection conn = DriverManager.getConnection(url).unwrap(PhoenixConnection.class);
        StatementContext context = new StatementContext(new PhoenixStatement(conn));
        ExpressionCompiler compiler = new ExpressionCompiler(context);
        ParseNode defaultParseNode = new SQLParser(column.getExpressionStr()).parseExpression();
        Expression defaultExpression = defaultParseNode.accept(compiler);
        if (!ExpressionUtil.isNull(defaultExpression, new ImmutableBytesWritable())) {
            return new DefaultValueExpression(Arrays.asList(expression, defaultExpression));
        }
    }
    return expression;
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) StatementContext(org.apache.phoenix.compile.StatementContext) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) Expression(org.apache.phoenix.expression.Expression) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) DefaultValueExpression(org.apache.phoenix.expression.function.DefaultValueExpression) SQLParser(org.apache.phoenix.parse.SQLParser) ParseNode(org.apache.phoenix.parse.ParseNode) ExpressionCompiler(org.apache.phoenix.compile.ExpressionCompiler) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) DefaultValueExpression(org.apache.phoenix.expression.function.DefaultValueExpression)

Aggregations

RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)10 RowKeyValueAccessor (org.apache.phoenix.schema.RowKeyValueAccessor)7 Expression (org.apache.phoenix.expression.Expression)6 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 StatementContext (org.apache.phoenix.compile.StatementContext)3 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)3 RowKeyComparisonFilter (org.apache.phoenix.filter.RowKeyComparisonFilter)3 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)3 PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)3 PDataType (org.apache.phoenix.schema.types.PDataType)3 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)2 ColumnResolver (org.apache.phoenix.compile.ColumnResolver)2 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)2 MutationState (org.apache.phoenix.execute.MutationState)2 ProjectedColumnExpression (org.apache.phoenix.expression.ProjectedColumnExpression)2