use of org.apache.phoenix.schema.ColumnRef in project phoenix by apache.
the class CorrelatePlanTest method testCorrelatePlan.
private void testCorrelatePlan(Object[][] leftRelation, Object[][] rightRelation, int leftCorrelColumn, int rightCorrelColumn, JoinType type, Object[][] expectedResult, Integer offset) throws SQLException {
TableRef leftTable = createProjectedTableFromLiterals(leftRelation[0]);
TableRef rightTable = createProjectedTableFromLiterals(rightRelation[0]);
String varName = "$cor0";
RuntimeContext runtimeContext = new RuntimeContextImpl();
runtimeContext.defineCorrelateVariable(varName, leftTable);
QueryPlan leftPlan = newLiteralResultIterationPlan(leftRelation, offset);
QueryPlan rightPlan = newLiteralResultIterationPlan(rightRelation, offset);
Expression columnExpr = new ColumnRef(rightTable, rightCorrelColumn).newColumnExpression();
Expression fieldAccess = new CorrelateVariableFieldAccessExpression(runtimeContext, varName, new ColumnRef(leftTable, leftCorrelColumn).newColumnExpression());
Expression filter = ComparisonExpression.create(CompareOp.EQUAL, Arrays.asList(columnExpr, fieldAccess), CONTEXT.getTempPtr(), false);
rightPlan = new ClientScanPlan(CONTEXT, SelectStatement.SELECT_ONE, rightTable, RowProjector.EMPTY_PROJECTOR, null, null, filter, OrderBy.EMPTY_ORDER_BY, rightPlan);
PTable joinedTable = JoinCompiler.joinProjectedTables(leftTable.getTable(), rightTable.getTable(), type);
CorrelatePlan correlatePlan = new CorrelatePlan(leftPlan, rightPlan, varName, type, false, runtimeContext, joinedTable, leftTable.getTable(), rightTable.getTable(), leftTable.getTable().getColumns().size());
ResultIterator iter = correlatePlan.iterator();
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
for (Object[] row : expectedResult) {
Tuple next = iter.next();
assertNotNull(next);
for (int i = 0; i < row.length; i++) {
PColumn column = joinedTable.getColumns().get(i);
boolean eval = new ProjectedColumnExpression(column, joinedTable, column.getName().getString()).evaluate(next, ptr);
Object o = eval ? column.getDataType().toObject(ptr) : null;
assertEquals(row[i], o);
}
}
}
use of org.apache.phoenix.schema.ColumnRef in project phoenix by apache.
the class LiteralResultIteratorPlanTest method createProjectedTableFromLiterals.
private TableRef createProjectedTableFromLiterals(Object[] row) {
List<PColumn> columns = Lists.<PColumn>newArrayList();
for (int i = 0; i < row.length; i++) {
String name = ParseNodeFactory.createTempAlias();
Expression expr = LiteralExpression.newConstant(row[i]);
PName colName = PNameFactory.newName(name);
columns.add(new PColumnImpl(PNameFactory.newName(name), PNameFactory.newName(VALUE_COLUMN_FAMILY), expr.getDataType(), expr.getMaxLength(), expr.getScale(), expr.isNullable(), i, expr.getSortOrder(), null, null, false, name, false, false, colName.getBytes()));
}
try {
PTable pTable = PTableImpl.makePTable(null, PName.EMPTY_NAME, PName.EMPTY_NAME, PTableType.SUBQUERY, null, MetaDataProtocol.MIN_TABLE_TIMESTAMP, PTable.INITIAL_SEQ_NUM, null, null, columns, null, null, Collections.<PTable>emptyList(), false, Collections.<PName>emptyList(), null, null, false, false, false, null, null, null, true, false, 0, 0L, false, null, false, ImmutableStorageScheme.ONE_CELL_PER_COLUMN, QualifierEncodingScheme.NON_ENCODED_QUALIFIERS, EncodedCQCounter.NULL_COUNTER, true);
TableRef sourceTable = new TableRef(pTable);
List<ColumnRef> sourceColumnRefs = Lists.<ColumnRef>newArrayList();
for (PColumn column : sourceTable.getTable().getColumns()) {
sourceColumnRefs.add(new ColumnRef(sourceTable, column.getPosition()));
}
return new TableRef(TupleProjectionCompiler.createProjectedTable(sourceTable, sourceColumnRefs, false));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of org.apache.phoenix.schema.ColumnRef in project phoenix by apache.
the class ExpressionUtil method isPkPositionChanging.
public static boolean isPkPositionChanging(TableRef tableRef, List<Expression> projectedExpressions) throws SQLException {
for (int i = 0; i < tableRef.getTable().getPKColumns().size(); i++) {
PColumn column = tableRef.getTable().getPKColumns().get(i);
Expression source = projectedExpressions.get(i);
if (source == null || !source.equals(new ColumnRef(tableRef, column.getPosition()).newColumnExpression())) {
return true;
}
}
return false;
}
use of org.apache.phoenix.schema.ColumnRef in project phoenix by apache.
the class ExpressionCompiler method visit.
@Override
public Expression visit(ColumnParseNode node) throws SQLException {
ColumnRef ref = resolveColumn(node);
TableRef tableRef = ref.getTableRef();
ImmutableBytesWritable ptr = context.getTempPtr();
PColumn column = ref.getColumn();
// query would become degenerate.
if (!resolveViewConstants && IndexUtil.getViewConstantValue(column, ptr)) {
return LiteralExpression.newConstant(column.getDataType().toObject(ptr), column.getDataType());
}
if (tableRef.equals(context.getCurrentTable()) && !SchemaUtil.isPKColumn(column)) {
// project only kv columns
addColumn(column);
}
Expression expression = ref.newColumnExpression(node.isTableNameCaseSensitive(), node.isCaseSensitive());
Expression wrappedExpression = wrapGroupByExpression(expression);
// This catches cases like this: SELECT sum(a_integer) + a_integer FROM atable GROUP BY a_string
if (isAggregate && aggregateFunction == null && wrappedExpression == expression) {
throwNonAggExpressionInAggException(expression.toString());
}
return wrappedExpression;
}
use of org.apache.phoenix.schema.ColumnRef in project phoenix by apache.
the class JoinCompiler method getSubqueryForOptimizedPlan.
private static SelectStatement getSubqueryForOptimizedPlan(HintNode hintNode, List<ColumnDef> dynamicCols, TableRef tableRef, Map<ColumnRef, ColumnRefType> columnRefs, ParseNode where, List<ParseNode> groupBy, List<OrderByNode> orderBy, boolean isWildCardSelect, boolean hasSequence, Map<String, UDFParseNode> udfParseNodes) {
String schemaName = tableRef.getTable().getSchemaName().getString();
TableName tName = TableName.create(schemaName.length() == 0 ? null : schemaName, tableRef.getTable().getTableName().getString());
List<AliasedNode> selectList = new ArrayList<AliasedNode>();
if (isWildCardSelect) {
selectList.add(NODE_FACTORY.aliasedNode(null, WildcardParseNode.INSTANCE));
} else {
for (ColumnRef colRef : columnRefs.keySet()) {
if (colRef.getTableRef().equals(tableRef)) {
ParseNode node = NODE_FACTORY.column(tName, '"' + colRef.getColumn().getName().getString() + '"', null);
if (groupBy != null) {
node = NODE_FACTORY.function(CountAggregateFunction.NAME, Collections.singletonList(node));
}
selectList.add(NODE_FACTORY.aliasedNode(null, node));
}
}
}
String tableAlias = tableRef.getTableAlias();
TableNode from = NODE_FACTORY.namedTable(tableAlias == null ? null : '"' + tableAlias + '"', tName, dynamicCols);
return NODE_FACTORY.select(from, hintNode, false, selectList, where, groupBy, null, orderBy, null, null, 0, groupBy != null, hasSequence, Collections.<SelectStatement>emptyList(), udfParseNodes);
}
Aggregations