use of org.apache.calcite.rex.RexInputRef in project calcite by apache.
the class DruidQuery method extractColumnName.
/**
* @param rexNode Druid input ref node
* @param rowType rowType
* @param query Druid Query
*
* @return Druid column name or null when not possible to translate.
*/
@Nullable
protected static String extractColumnName(RexNode rexNode, RelDataType rowType, DruidQuery query) {
if (rexNode.getKind() == SqlKind.INPUT_REF) {
final RexInputRef ref = (RexInputRef) rexNode;
final String columnName = rowType.getFieldNames().get(ref.getIndex());
if (columnName == null) {
return null;
}
// calcite has this un-direct renaming of timestampFieldName to native druid `__time`
if (query.getDruidTable().timestampFieldName.equals(columnName)) {
return DruidTable.DEFAULT_TIMESTAMP_COLUMN;
}
return columnName;
}
return null;
}
use of org.apache.calcite.rex.RexInputRef in project drill by axbaretto.
the class ConvertHiveParquetScanToDrillParquetScan method createColumnFormatConversion.
/**
* Apply any data format conversion expressions.
*/
private RexNode createColumnFormatConversion(final DrillScanRel hiveScanRel, final DrillScanRel nativeScanRel, final String colName, final RexBuilder rb) {
final RelDataType outputType = hiveScanRel.getRowType().getField(colName, false, false).getType();
final RelDataTypeField inputField = nativeScanRel.getRowType().getField(colName, false, false);
final RexInputRef inputRef = rb.makeInputRef(inputField.getType(), inputField.getIndex());
if (outputType.getSqlTypeName() == SqlTypeName.TIMESTAMP) {
// TODO: Remove this conversion once "store.parquet.reader.int96_as_timestamp" will be true by default
return rb.makeCall(INT96_TO_TIMESTAMP, inputRef);
}
return inputRef;
}
use of org.apache.calcite.rex.RexInputRef in project drill by axbaretto.
the class DrillFilterItemStarReWriterRule method transformFilterCall.
/**
* Removes item star call from filter expression and propagates changes into project (if present) and scan.
*
* @param filterRel original filter expression
* @param projectRel original project expression
* @param scanRel original scan expression
* @param call original rule call
*/
private static void transformFilterCall(DrillFilterRel filterRel, DrillProjectRel projectRel, DrillScanRel scanRel, RelOptRuleCall call) {
List<String> fieldNames = projectRel == null ? scanRel.getRowType().getFieldNames() : projectRel.getRowType().getFieldNames();
ItemStarFieldsVisitor itemStarFieldsVisitor = new ItemStarFieldsVisitor(fieldNames);
filterRel.getCondition().accept(itemStarFieldsVisitor);
// if there are no item fields, no need to proceed further
if (itemStarFieldsVisitor.hasNoItemStarFields()) {
return;
}
Map<String, DesiredField> itemStarFields = itemStarFieldsVisitor.getItemStarFields();
DrillScanRel newScan = createNewScan(scanRel, itemStarFields);
// create new project if was present in call
DrillProjectRel newProject = null;
if (projectRel != null) {
// add new projects to the already existing in original project
int projectIndex = scanRel.getRowType().getFieldCount();
List<RexNode> newProjects = new ArrayList<>(projectRel.getProjects());
for (DesiredField desiredField : itemStarFields.values()) {
newProjects.add(new RexInputRef(projectIndex, desiredField.getType()));
projectIndex++;
}
RelDataType newProjectRowType = createNewRowType(projectRel.getCluster().getTypeFactory(), projectRel.getRowType().getFieldList(), itemStarFields.keySet());
newProject = new DrillProjectRel(projectRel.getCluster(), projectRel.getTraitSet(), newScan, newProjects, newProjectRowType);
}
// transform filter condition
Map<RexNode, Integer> fieldMapper = createFieldMapper(itemStarFields.values(), scanRel.getRowType().getFieldCount());
FieldsReWriter fieldsReWriter = new FieldsReWriter(fieldMapper);
RexNode newCondition = filterRel.getCondition().accept(fieldsReWriter);
// create new filter
DrillFilterRel newFilter = DrillFilterRel.create(newProject != null ? newProject : newScan, newCondition);
// wrap with project to have the same row type as before
List<RexNode> newProjects = new ArrayList<>();
RelDataType rowType = filterRel.getRowType();
List<RelDataTypeField> fieldList = rowType.getFieldList();
for (RelDataTypeField field : fieldList) {
RexInputRef inputRef = new RexInputRef(field.getIndex(), field.getType());
newProjects.add(inputRef);
}
DrillProjectRel wrapper = new DrillProjectRel(filterRel.getCluster(), filterRel.getTraitSet(), newFilter, newProjects, filterRel.getRowType());
call.transformTo(wrapper);
}
use of org.apache.calcite.rex.RexInputRef in project drill by axbaretto.
the class DrillRelOptUtil method findOperators.
/**
* Travesal RexNode to find at least one operator in the given collection. Continue search if RexNode has a
* RexInputRef which refers to a RexNode in project expressions.
*
* @param node : RexNode to search
* @param projExprs : the list of project expressions. Empty list means there is No project operator underneath.
* @param operators collection of operators to find
* @return : Return null if there is NONE; return the first appearance of item/flatten RexCall.
*/
public static RexCall findOperators(final RexNode node, final List<RexNode> projExprs, final Collection<String> operators) {
try {
RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
public Void visitCall(RexCall call) {
if (operators.contains(call.getOperator().getName().toLowerCase())) {
throw new Util.FoundOne(call);
/* throw exception to interrupt tree walk (this is similar to
other utility methods in RexUtil.java */
}
return super.visitCall(call);
}
public Void visitInputRef(RexInputRef inputRef) {
if (projExprs.size() == 0) {
return super.visitInputRef(inputRef);
} else {
final int index = inputRef.getIndex();
RexNode n = projExprs.get(index);
if (n instanceof RexCall) {
RexCall r = (RexCall) n;
if (operators.contains(r.getOperator().getName().toLowerCase())) {
throw new Util.FoundOne(r);
}
}
return super.visitInputRef(inputRef);
}
}
};
node.accept(visitor);
return null;
} catch (Util.FoundOne e) {
Util.swallow(e, null);
return (RexCall) e.getNode();
}
}
use of org.apache.calcite.rex.RexInputRef in project drill by axbaretto.
the class StarColumnConverter method visitProject.
@Override
public Prel visitProject(ProjectPrel prel, Void value) throws RuntimeException {
ProjectPrel proj = (ProjectPrel) prel;
// Require prefix rename : there exists other expression, in addition to a star column.
if (// not set yet.
!prefixedForStar && StarColumnHelper.containsStarColumnInProject(prel.getInput().getRowType(), proj.getProjects()) && prel.getRowType().getFieldNames().size() > 1) {
prefixedForStar = true;
}
// For project, we need make sure that the project's field name is same as the input,
// when the project expression is RexInPutRef, since we may insert a PAS which will
// rename the projected fields.
RelNode child = ((Prel) prel.getInput(0)).accept(this, null);
List<String> fieldNames = Lists.newArrayList();
for (Pair<String, RexNode> pair : Pair.zip(prel.getRowType().getFieldNames(), proj.getProjects())) {
if (pair.right instanceof RexInputRef) {
String name = child.getRowType().getFieldNames().get(((RexInputRef) pair.right).getIndex());
fieldNames.add(name);
} else {
fieldNames.add(pair.left);
}
}
// Make sure the field names are unique : no allow of duplicate field names in a rowType.
fieldNames = makeUniqueNames(fieldNames);
RelDataType rowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), proj.getProjects(), fieldNames);
ProjectPrel newProj = (ProjectPrel) proj.copy(proj.getTraitSet(), child, proj.getProjects(), rowType);
if (ProjectRemoveRule.isTrivial(newProj)) {
return (Prel) child;
} else {
return newProj;
}
}
Aggregations