Search in sources :

Example 41 with PhoenixPreparedStatement

use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.

the class WhereCompilerTest method testPaddedStartStopKey.

@Test
public void testPaddedStartStopKey() throws SQLException {
    String tenantId = "000000000000001";
    String keyPrefix = "fo";
    String query = "select * from atable where organization_id=? AND entity_id=?";
    List<Object> binds = Arrays.<Object>asList(tenantId, 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();
    byte[] expectedStartRow = ByteUtil.concat(Bytes.toBytes(tenantId), StringUtil.padChar(Bytes.toBytes(keyPrefix), 15));
    assertArrayEquals(expectedStartRow, scan.getStartRow());
    assertArrayEquals(ByteUtil.nextKey(expectedStartRow), scan.getStopRow());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Scan(org.apache.hadoop.hbase.client.Scan) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 42 with PhoenixPreparedStatement

use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.

the class WhereCompilerTest method testTenantConstraintsAddedToScanWithNullTenantTypeId.

@Test
public void testTenantConstraintsAddedToScanWithNullTenantTypeId() throws SQLException {
    String tenantId = "000000000000123";
    createTestTable(getUrl(), "create table base_table_for_tenant_filter_test (tenant_id char(15) not null, " + "id char(5) not null, a_integer integer, a_string varchar(100) constraint pk primary key (tenant_id, id)) multi_tenant=true");
    createTestTable(getUrl(tenantId), "create view tenant_filter_test (tenant_col integer) AS SELECT * FROM BASE_TABLE_FOR_TENANT_FILTER_TEST");
    String query = "select * from tenant_filter_test where a_integer=0 and a_string='foo'";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(tenantId), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    PTable table = plan.getTableRef().getTable();
    Expression aInteger = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression();
    Expression aString = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression();
    assertEquals(multiEncodedKVFilter(and(constantComparison(CompareOp.EQUAL, aInteger, 0), constantComparison(CompareOp.EQUAL, aString, "foo")), TWO_BYTE_QUALIFIERS), filter);
    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = startRow;
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());
}
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) 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) ColumnRef(org.apache.phoenix.schema.ColumnRef) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) PTable(org.apache.phoenix.schema.PTable) TableRef(org.apache.phoenix.schema.TableRef) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 43 with PhoenixPreparedStatement

use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.

the class WhereCompilerTest method testPartialRangeFilter.

@Test
public void testPartialRangeFilter() throws SQLException {
    // I know these id's are ridiculous, but users can write queries that look like this
    String tenantId1 = "001";
    String tenantId2 = "02";
    String query = String.format("select * from %s where organization_id > '%s' AND organization_id < '%s'", ATABLE_NAME, tenantId1, tenantId2);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    assertNull(scan.getFilter());
    byte[] wideLower = ByteUtil.nextKey(StringUtil.padChar(Bytes.toBytes(tenantId1), 15));
    byte[] wideUpper = StringUtil.padChar(Bytes.toBytes(tenantId2), 15);
    assertArrayEquals(wideLower, scan.getStartRow());
    assertArrayEquals(wideUpper, scan.getStopRow());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Scan(org.apache.hadoop.hbase.client.Scan) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 44 with PhoenixPreparedStatement

use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.

the class WhereCompilerTest method testSingleEqualFilter.

@Test
public void testSingleEqualFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_integer=0";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();
    assertEquals(singleKVFilter(constantComparison(CompareOp.EQUAL, A_INTEGER, 0)), 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) Scan(org.apache.hadoop.hbase.client.Scan) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 45 with PhoenixPreparedStatement

use of org.apache.phoenix.jdbc.PhoenixPreparedStatement in project phoenix by apache.

the class WhereCompilerTest method testInFilter.

@Test
public void testInFilter() throws SQLException {
    String tenantId = "000000000000001";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_string IN ('a','b')";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId);
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = startRow;
    assertArrayEquals(ByteUtil.nextKey(stopRow), scan.getStopRow());
    Filter filter = scan.getFilter();
    assertEquals(singleKVFilter(in(A_STRING, "a", "b")), 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) Scan(org.apache.hadoop.hbase.client.Scan) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

PhoenixPreparedStatement (org.apache.phoenix.jdbc.PhoenixPreparedStatement)54 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)51 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)41 Test (org.junit.Test)41 Scan (org.apache.hadoop.hbase.client.Scan)36 Filter (org.apache.hadoop.hbase.filter.Filter)29 RowKeyComparisonFilter (org.apache.phoenix.filter.RowKeyComparisonFilter)28 SkipScanFilter (org.apache.phoenix.filter.SkipScanFilter)28 TestUtil.multiEncodedKVFilter (org.apache.phoenix.util.TestUtil.multiEncodedKVFilter)28 TestUtil.singleKVFilter (org.apache.phoenix.util.TestUtil.singleKVFilter)28 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)5 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)5 Expression (org.apache.phoenix.expression.Expression)4 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)4 Connection (java.sql.Connection)3 FilterList (org.apache.hadoop.hbase.filter.FilterList)3 ColumnRef (org.apache.phoenix.schema.ColumnRef)3 PTable (org.apache.phoenix.schema.PTable)3 RowKeyValueAccessor (org.apache.phoenix.schema.RowKeyValueAccessor)3 TableRef (org.apache.phoenix.schema.TableRef)3