use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by axbaretto.
the class TopProjectVisitor method addTopProjectPrel.
/**
* Adds top project to ensure final output field names are preserved.
* In case of duplicated column names, will rename duplicates.
* Top project will be added only if top project is non-trivial and
* child physical relational node is not project.
*
* @param prel physical relational node
* @param validatedRowType final output row type
* @return physical relational node with top project if necessary
*/
private Prel addTopProjectPrel(Prel prel, RelDataType validatedRowType) {
RelDataType rowType = prel.getRowType();
if (rowType.getFieldCount() != validatedRowType.getFieldCount()) {
return prel;
}
RexBuilder rexBuilder = prel.getCluster().getRexBuilder();
List<RexNode> projections = new ArrayList<>();
int projectCount = rowType.getFieldList().size();
for (int i = 0; i < projectCount; i++) {
projections.add(rexBuilder.makeInputRef(prel, i));
}
List<String> fieldNames = SqlValidatorUtil.uniquify(validatedRowType.getFieldNames(), SqlValidatorUtil.EXPR_SUGGESTER, prel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
RelDataType newRowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), projections, fieldNames, null);
ProjectPrel topProject = new ProjectPrel(prel.getCluster(), prel.getTraitSet(), prel, projections, newRowType, // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
true);
if (prel instanceof Project && DrillRelOptUtil.isTrivialProject(topProject, true)) {
return new ProjectPrel(prel.getCluster(), prel.getTraitSet(), ((Project) prel).getInput(), ((Project) prel).getProjects(), prel.getRowType(), // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
true);
} else {
return topProject;
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by axbaretto.
the class SqlHandlerUtil method qualifyPartitionCol.
/**
* Resolve the partition columns specified in "PARTITION BY" clause of CTAS statement.
*
* A partition column is resolved, either (1) the same column appear in the select list of CTAS
* or (2) CTAS has a * in select list.
*
* In the second case, a PROJECT with ITEM expression would be created and returned.
* Throw validation error if a partition column is not resolved correctly.
*
* @param input : the RelNode represents the select statement in CTAS.
* @param partitionColumns : the list of partition columns.
* @return : 1) the original RelNode input, if all partition columns are in select list of CTAS
* 2) a New Project, if a partition column is resolved to * column in select list
* 3) validation error, if partition column is not resolved.
*/
public static RelNode qualifyPartitionCol(RelNode input, List<String> partitionColumns) {
final RelDataType inputRowType = input.getRowType();
final List<RexNode> colRefStarExprs = Lists.newArrayList();
final List<String> colRefStarNames = Lists.newArrayList();
final RexBuilder builder = input.getCluster().getRexBuilder();
final int originalFieldSize = inputRowType.getFieldCount();
for (final String col : partitionColumns) {
final RelDataTypeField field = inputRowType.getField(col, false, false);
if (field == null) {
throw UserException.validationError().message("Partition column %s is not in the SELECT list of CTAS!", col).build(logger);
} else {
if (SchemaPath.DYNAMIC_STAR.equals(field.getName())) {
colRefStarNames.add(col);
final List<RexNode> operands = Lists.newArrayList();
operands.add(new RexInputRef(field.getIndex(), field.getType()));
operands.add(builder.makeLiteral(col));
final RexNode item = builder.makeCall(SqlStdOperatorTable.ITEM, operands);
colRefStarExprs.add(item);
}
}
}
if (colRefStarExprs.isEmpty()) {
return input;
} else {
final List<String> names = new AbstractList<String>() {
@Override
public String get(int index) {
if (index < originalFieldSize) {
return inputRowType.getFieldNames().get(index);
} else {
return colRefStarNames.get(index - originalFieldSize);
}
}
@Override
public int size() {
return originalFieldSize + colRefStarExprs.size();
}
};
final List<RexNode> refs = new AbstractList<RexNode>() {
@Override
public int size() {
return originalFieldSize + colRefStarExprs.size();
}
@Override
public RexNode get(int index) {
if (index < originalFieldSize) {
return RexInputRef.of(index, inputRowType.getFieldList());
} else {
return colRefStarExprs.get(index - originalFieldSize);
}
}
};
return RelOptUtil.createProject(input, refs, names, false, DrillRelFactories.LOGICAL_BUILDER.create(input.getCluster(), null));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by apache.
the class ConvertHiveParquetScanToDrillParquetScan method createProjectRel.
/**
* Create a project that converts the native scan output to expected output of Hive scan.
*/
private DrillProjectRel createProjectRel(final DrillScanRel hiveScanRel, final Map<String, String> partitionColMapping, final DrillScanRel nativeScanRel) {
final List<RexNode> rexNodes = new ArrayList<>();
final RexBuilder rb = hiveScanRel.getCluster().getRexBuilder();
final RelDataType hiveScanRowType = hiveScanRel.getRowType();
for (String colName : hiveScanRowType.getFieldNames()) {
final String dirColName = partitionColMapping.get(colName);
if (dirColName != null) {
rexNodes.add(createPartitionColumnCast(hiveScanRel, nativeScanRel, colName, dirColName, rb));
} else {
rexNodes.add(createColumnFormatConversion(hiveScanRel, nativeScanRel, colName, rb));
}
}
return DrillProjectRel.create(hiveScanRel.getCluster(), hiveScanRel.getTraitSet(), nativeScanRel, rexNodes, hiveScanRowType);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by apache.
the class SqlConverter method toRel.
public RelRoot toRel(final SqlNode validatedNode) {
initCluster(initPlanner());
DrillViewExpander viewExpander = new DrillViewExpander(this);
final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(viewExpander, validator, catalog, cluster, DrillConvertletTable.INSTANCE, sqlToRelConverterConfig);
boolean topLevelQuery = !isInnerQuery || isExpandedView;
RelRoot rel = sqlToRelConverter.convertQuery(validatedNode, false, topLevelQuery);
// add another project to remove them.
if (topLevelQuery && rel.rel.getRowType().getFieldCount() - rel.fields.size() > 0) {
RexBuilder builder = rel.rel.getCluster().getRexBuilder();
RelNode relNode = rel.rel;
List<RexNode> expressions = rel.fields.stream().map(f -> builder.makeInputRef(relNode, f.left)).collect(Collectors.toList());
RelNode project = LogicalProject.create(rel.rel, expressions, rel.validatedRowType);
rel = RelRoot.of(project, rel.validatedRowType, rel.kind);
}
return rel.withRel(sqlToRelConverter.flattenTypes(rel.rel, true));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by apache.
the class TopProjectVisitor method addTopProjectPrel.
/**
* Adds top project to ensure final output field names are preserved.
* In case of duplicated column names, will rename duplicates.
* Top project will be added only if top project is non-trivial and
* child physical relational node is not project.
*
* @param prel physical relational node
* @param validatedRowType final output row type
* @return physical relational node with top project if necessary
*/
private Prel addTopProjectPrel(Prel prel, RelDataType validatedRowType) {
RelDataType rowType = prel.getRowType();
if (rowType.getFieldCount() != validatedRowType.getFieldCount()) {
return prel;
}
RexBuilder rexBuilder = prel.getCluster().getRexBuilder();
List<RexNode> projections = new ArrayList<>();
int projectCount = rowType.getFieldList().size();
for (int i = 0; i < projectCount; i++) {
projections.add(rexBuilder.makeInputRef(prel, i));
}
List<String> fieldNames = SqlValidatorUtil.uniquify(validatedRowType.getFieldNames(), SqlValidatorUtil.EXPR_SUGGESTER, prel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
RelDataType newRowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), projections, fieldNames, null);
ProjectPrel topProject = new ProjectPrel(prel.getCluster(), prel.getTraitSet(), prel, projections, newRowType, // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
true);
if (prel instanceof Project && DrillRelOptUtil.isTrivialProject(topProject, true)) {
return new ProjectPrel(prel.getCluster(), prel.getTraitSet(), ((Project) prel).getInput(), ((Project) prel).getProjects(), prel.getRowType(), // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
true);
} else {
return topProject;
}
}
Aggregations