use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class ProjectionCompiler method projectTableColumnFamily.
private static void projectTableColumnFamily(StatementContext context, String cfName, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
PTable table = tableRef.getTable();
PColumnFamily pfamily = table.getColumnFamily(cfName);
for (PColumn column : pfamily.getColumns()) {
ColumnRef ref = new ColumnRef(tableRef, column.getPosition());
if (resolveColumn) {
ref = context.getResolver().resolveColumn(table.getTableName().getString(), cfName, column.getName().getString());
}
Expression expression = ref.newColumnExpression();
projectedExpressions.add(expression);
String colName = column.getName().toString();
boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
}
}
use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class ProjectionCompiler method projectAllTableColumns.
private static void projectAllTableColumns(StatementContext context, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns, List<? extends PDatum> targetColumns) throws SQLException {
ColumnResolver resolver = context.getResolver();
PTable table = tableRef.getTable();
int projectedOffset = projectedExpressions.size();
int posOffset = table.getBucketNum() == null ? 0 : 1;
int minPKOffset = getMinPKOffset(table, context.getConnection().getTenantId());
for (int i = posOffset, j = posOffset; i < table.getColumns().size(); i++) {
PColumn column = table.getColumns().get(i);
// Skip tenant ID column (which may not be the first column, but is the first PK column)
if (SchemaUtil.isPKColumn(column) && j++ < minPKOffset) {
posOffset++;
continue;
}
ColumnRef ref = new ColumnRef(tableRef, i);
String colName = ref.getColumn().getName().getString();
String tableAlias = tableRef.getTableAlias();
if (resolveColumn) {
try {
if (tableAlias != null) {
ref = resolver.resolveColumn(null, tableAlias, colName);
} else {
String schemaName = table.getSchemaName().getString();
ref = resolver.resolveColumn(schemaName.length() == 0 ? null : schemaName, table.getTableName().getString(), colName);
}
} catch (AmbiguousColumnException e) {
if (column.getFamilyName() != null) {
ref = resolver.resolveColumn(tableAlias != null ? tableAlias : table.getTableName().getString(), column.getFamilyName().getString(), colName);
} else {
throw e;
}
}
}
Expression expression = ref.newColumnExpression();
expression = coerceIfNecessary(i - posOffset + projectedOffset, targetColumns, expression);
ImmutableBytesWritable ptr = context.getTempPtr();
if (IndexUtil.getViewConstantValue(column, ptr)) {
expression = LiteralExpression.newConstant(column.getDataType().toObject(ptr), expression.getDataType());
}
projectedExpressions.add(expression);
boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
}
}
use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class UpsertCompiler method prependTenantAndViewConstants.
private static SelectStatement prependTenantAndViewConstants(PTable table, SelectStatement select, String tenantId, Set<PColumn> addViewColumns, boolean useServerTimestamp) {
if ((!table.isMultiTenant() || tenantId == null) && table.getViewIndexId() == null && addViewColumns.isEmpty() && !useServerTimestamp) {
return select;
}
List<AliasedNode> selectNodes = newArrayListWithCapacity(select.getSelect().size() + 1 + addViewColumns.size());
if (table.getViewIndexId() != null) {
selectNodes.add(new AliasedNode(null, new LiteralParseNode(table.getViewIndexId())));
}
if (table.isMultiTenant() && tenantId != null) {
selectNodes.add(new AliasedNode(null, new LiteralParseNode(tenantId)));
}
selectNodes.addAll(select.getSelect());
for (PColumn column : addViewColumns) {
byte[] byteValue = column.getViewConstant();
Object value = column.getDataType().toObject(byteValue, 0, byteValue.length - 1);
selectNodes.add(new AliasedNode(null, new LiteralParseNode(value)));
}
if (useServerTimestamp) {
PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
selectNodes.add(new AliasedNode(null, getNodeForRowTimestampColumn(rowTimestampCol)));
}
return SelectStatement.create(select, selectNodes);
}
use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class ColumnParseNode method toSQL.
@Override
public void toSQL(ColumnResolver resolver, StringBuilder buf) {
// If resolver is not null, then resolve to get fully qualified name
String tableName = null;
if (resolver == null) {
if (this.tableName != null) {
tableName = this.tableName.getTableName();
}
} else {
try {
ColumnRef ref = resolver.resolveColumn(this.getSchemaName(), this.getTableName(), this.getName());
PColumn column = ref.getColumn();
if (!SchemaUtil.isPKColumn(column)) {
PTable table = ref.getTable();
String defaultFamilyName = table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString();
// Translate to the data table column name
String dataFamilyName = column.getFamilyName().getString();
tableName = defaultFamilyName.equals(dataFamilyName) ? null : dataFamilyName;
}
} catch (SQLException e) {
// Already resolved, so not possible
throw new RuntimeException(e);
}
}
if (tableName != null) {
if (isTableNameCaseSensitive()) {
buf.append('"');
buf.append(tableName);
buf.append('"');
} else {
buf.append(tableName);
}
buf.append('.');
}
toSQL(buf);
}
use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class HavingCompilerTest method testOrAggFuncInHaving.
@Test
public void testOrAggFuncInHaving() throws SQLException {
String query = "select count(1) from atable group by a_string having count(1) >= 1 or a_string = 'foo'";
List<Object> binds = Collections.emptyList();
Expressions expressions = compileStatement(query, binds);
PColumn aCol = ATABLE.getColumnForColumnName("A_STRING");
Expression h = or(constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(), 1L), constantComparison(CompareOp.EQUAL, new // a_string comes from group by key in this case
RowKeyColumnExpression(// a_string comes from group by key in this case
aCol, new RowKeyValueAccessor(Arrays.<PColumn>asList(aCol), 0)), "foo"));
assertNull(expressions.whereClause);
assertEquals(h, expressions.havingClause);
}
Aggregations