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());
}
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));
}
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);
}
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);
}
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;
}
Aggregations