use of org.apache.calcite.rel.type.RelDataTypeField in project drill by apache.
the class FindLimit0Visitor method getDirectScanRelIfFullySchemaed.
/**
* If all field types of the given node are {@link #TYPES recognized types} and honored by execution, then this
* method returns the tree: DrillDirectScanRel(field types). Otherwise, the method returns null.
*
* @param rel calcite logical rel tree
* @return drill logical rel tree
*/
public static DrillRel getDirectScanRelIfFullySchemaed(RelNode rel) {
final List<RelDataTypeField> fieldList = rel.getRowType().getFieldList();
final List<TypeProtos.MajorType> columnTypes = Lists.newArrayList();
for (final RelDataTypeField field : fieldList) {
final SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
if (!TYPES.contains(sqlTypeName)) {
return null;
} else {
final TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder().setMode(field.getType().isNullable() ? TypeProtos.DataMode.OPTIONAL : TypeProtos.DataMode.REQUIRED).setMinorType(TypeInferenceUtils.getDrillTypeFromCalciteType(sqlTypeName));
if (TypeInferenceUtils.isScalarStringType(sqlTypeName)) {
builder.setPrecision(field.getType().getPrecision());
}
columnTypes.add(builder.build());
}
}
final RelTraitSet traits = rel.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
final RelDataTypeReader reader = new RelDataTypeReader(rel.getRowType().getFieldNames(), columnTypes);
return new DrillDirectScanRel(rel.getCluster(), traits, new DirectGroupScan(reader, ScanStats.ZERO_RECORD_TABLE), rel.getRowType());
}
use of org.apache.calcite.rel.type.RelDataTypeField in project drill by apache.
the class DrillFixedRelDataTypeImpl method addField.
private void addField(String columnName) {
RelDataTypeField newField = new RelDataTypeFieldImpl(columnName, fields.size(), typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true));
fields.add(newField);
}
use of org.apache.calcite.rel.type.RelDataTypeField in project drill by apache.
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 (field.getName().startsWith(StarColumnHelper.STAR_COLUMN)) {
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>() {
public int size() {
return originalFieldSize + colRefStarExprs.size();
}
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);
}
}
use of org.apache.calcite.rel.type.RelDataTypeField in project drill by apache.
the class ConvertHiveParquetScanToDrillParquetScan method createNativeScanRel.
/**
* Helper method which creates a DrillScalRel with native HiveScan.
*/
private DrillScanRel createNativeScanRel(final Map<String, String> partitionColMapping, final DrillScanRel hiveScanRel) throws Exception {
final RelDataTypeFactory typeFactory = hiveScanRel.getCluster().getTypeFactory();
final RelDataType varCharType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
final List<String> nativeScanColNames = Lists.newArrayList();
final List<RelDataType> nativeScanColTypes = Lists.newArrayList();
for (RelDataTypeField field : hiveScanRel.getRowType().getFieldList()) {
final String dirColName = partitionColMapping.get(field.getName());
if (dirColName != null) {
// partition column
nativeScanColNames.add(dirColName);
nativeScanColTypes.add(varCharType);
} else {
nativeScanColNames.add(field.getName());
nativeScanColTypes.add(field.getType());
}
}
final RelDataType nativeScanRowType = typeFactory.createStructType(nativeScanColTypes, nativeScanColNames);
// Create the list of projected columns set in HiveScan. The order of this list may not be same as the order of
// columns in HiveScan row type. Note: If the HiveScan.getColumn() contains a '*', we just need to add it as it is,
// unlike above where we expanded the '*'. HiveScan and related (subscan) can handle '*'.
final List<SchemaPath> nativeScanCols = Lists.newArrayList();
for (SchemaPath colName : hiveScanRel.getColumns()) {
final String partitionCol = partitionColMapping.get(colName.getAsUnescapedPath());
if (partitionCol != null) {
nativeScanCols.add(SchemaPath.getSimplePath(partitionCol));
} else {
nativeScanCols.add(colName);
}
}
final HiveScan hiveScan = (HiveScan) hiveScanRel.getGroupScan();
final HiveDrillNativeParquetScan nativeHiveScan = new HiveDrillNativeParquetScan(hiveScan.getUserName(), hiveScan.hiveReadEntry, hiveScan.storagePlugin, nativeScanCols, null);
return new DrillScanRel(hiveScanRel.getCluster(), hiveScanRel.getTraitSet(), hiveScanRel.getTable(), nativeHiveScan, nativeScanRowType, nativeScanCols);
}
use of org.apache.calcite.rel.type.RelDataTypeField in project drill by apache.
the class SplitUpComplexExpressions method visitProject.
@Override
public Prel visitProject(ProjectPrel project, Object unused) throws RelConversionException {
// Apply the rule to the child
RelNode originalInput = ((Prel) project.getInput(0)).accept(this, null);
project = (ProjectPrel) project.copy(project.getTraitSet(), Lists.newArrayList(originalInput));
List<RexNode> exprList = new ArrayList<>();
List<RelDataTypeField> relDataTypes = new ArrayList<>();
List<RelDataTypeField> origRelDataTypes = new ArrayList<>();
int i = 0;
final int lastColumnReferenced = PrelUtil.getLastUsedColumnReference(project.getProjects());
if (lastColumnReferenced == -1) {
return project;
}
final int lastRexInput = lastColumnReferenced + 1;
RexVisitorComplexExprSplitter exprSplitter = new RexVisitorComplexExprSplitter(factory, funcReg, lastRexInput);
for (RexNode rex : project.getChildExps()) {
origRelDataTypes.add(project.getRowType().getFieldList().get(i));
i++;
exprList.add(rex.accept(exprSplitter));
}
List<RexNode> complexExprs = exprSplitter.getComplexExprs();
if (complexExprs.size() == 1 && findTopComplexFunc(project.getChildExps()).size() == 1) {
return project;
}
ProjectPrel childProject;
List<RexNode> allExprs = new ArrayList<>();
int exprIndex = 0;
List<String> fieldNames = originalInput.getRowType().getFieldNames();
for (int index = 0; index < lastRexInput; index++) {
RexBuilder builder = new RexBuilder(factory);
allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
if (fieldNames.get(index).contains(StarColumnHelper.STAR_COLUMN)) {
relDataTypes.add(new RelDataTypeFieldImpl(fieldNames.get(index), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
} else {
relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + exprIndex, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
exprIndex++;
}
}
RexNode currRexNode;
int index = lastRexInput - 1;
// if the projection expressions contained complex outputs, split them into their own individual projects
if (complexExprs.size() > 0) {
while (complexExprs.size() > 0) {
if (index >= lastRexInput) {
allExprs.remove(allExprs.size() - 1);
RexBuilder builder = new RexBuilder(factory);
allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
}
index++;
exprIndex++;
currRexNode = complexExprs.remove(0);
allExprs.add(currRexNode);
relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + exprIndex, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
childProject = new ProjectPrel(project.getCluster(), project.getTraitSet(), originalInput, ImmutableList.copyOf(allExprs), new RelRecordType(relDataTypes));
originalInput = childProject;
}
// copied from above, find a better way to do this
allExprs.remove(allExprs.size() - 1);
RexBuilder builder = new RexBuilder(factory);
allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + index, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
}
return (Prel) project.copy(project.getTraitSet(), originalInput, exprList, new RelRecordType(origRelDataTypes));
}
Aggregations