use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project hive by apache.
the class HiveSubQRemoveRelBuilder method project.
/**
* Creates a {@link org.apache.calcite.rel.core.Project} of the given list
* of expressions, using the given names.
*
* <p>Names are deduced as follows:
* <ul>
* <li>If the length of {@code fieldNames} is greater than the index of
* the current entry in {@code nodes}, and the entry in
* {@code fieldNames} is not null, uses it; otherwise
* <li>If an expression projects an input field,
* or is a cast an input field,
* uses the input field name; otherwise
* <li>If an expression is a call to
* {@link org.apache.calcite.sql.fun.SqlStdOperatorTable#AS}
* (see {@link #alias}), removes the call but uses the intended alias.
* </ul>
*
* <p>After the field names have been inferred, makes the
* field names unique by appending numeric suffixes.
*
* @param nodes Expressions
* @param fieldNames Suggested field names
* @param force create project even if it is identity
*/
public HiveSubQRemoveRelBuilder project(Iterable<? extends RexNode> nodes, Iterable<String> fieldNames, boolean force) {
final List<String> names = new ArrayList<>();
final List<RexNode> exprList = Lists.newArrayList(nodes);
final Iterator<String> nameIterator = fieldNames.iterator();
for (RexNode node : nodes) {
final String name = nameIterator.hasNext() ? nameIterator.next() : null;
final String name2 = inferAlias(exprList, node);
names.add(Util.first(name, name2));
}
final RelDataType inputRowType = peek().getRowType();
if (!force && RexUtil.isIdentity(exprList, inputRowType)) {
if (names.equals(inputRowType.getFieldNames())) {
// Do not create an identity project if it does not rename any fields
return this;
} else {
// create "virtual" row type for project only rename fields
final Frame frame = stack.pop();
final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), exprList, names, SqlValidatorUtil.F_SUGGESTER);
stack.push(new Frame(frame.rel, ImmutableList.of(Pair.of(frame.right.get(0).left, rowType))));
return this;
}
}
final RelNode project = projectFactory.createProject(build(), ImmutableList.copyOf(exprList), names);
push(project);
return this;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project hive by apache.
the class HiveJoinAddNotNullRule method getNotNullConditions.
private static List<RexNode> getNotNullConditions(RelOptCluster cluster, RexBuilder rexBuilder, List<RexNode> inputJoinExprs, Set<String> pushedPredicates) {
final List<RexNode> newConditions = Lists.newArrayList();
for (RexNode rexNode : inputJoinExprs) {
RexNode cond = rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, rexNode);
String digest = cond.toString();
if (pushedPredicates.add(digest)) {
newConditions.add(cond);
}
}
return newConditions;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project herddb by diennea.
the class CalcitePlanner method planUpdate.
private PlannerOp planUpdate(EnumerableTableModify dml, boolean returnValues) {
PlannerOp input = convertRelNode(dml.getInput(), null, false);
List<String> updateColumnList = dml.getUpdateColumnList();
List<RexNode> sourceExpressionList = dml.getSourceExpressionList();
final String tableSpace = dml.getTable().getQualifiedName().get(0);
final String tableName = dml.getTable().getQualifiedName().get(1);
final TableImpl tableImpl = (TableImpl) dml.getTable().unwrap(org.apache.calcite.schema.Table.class);
Table table = tableImpl.tableManager.getTable();
List<CompiledSQLExpression> expressions = new ArrayList<>(sourceExpressionList.size());
for (RexNode node : sourceExpressionList) {
CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
expressions.add(exp);
}
RecordFunction function = new SQLRecordFunction(updateColumnList, table, expressions);
UpdateStatement update = null;
if (input instanceof TableScanOp) {
update = new UpdateStatement(tableSpace, tableName, null, function, null);
} else if (input instanceof FilterOp) {
FilterOp filter = (FilterOp) input;
if (filter.getInput() instanceof TableScanOp) {
SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
}
} else if (input instanceof ProjectOp) {
ProjectOp proj = (ProjectOp) input;
if (proj.getInput() instanceof TableScanOp) {
update = new UpdateStatement(tableSpace, tableName, null, function, null);
} else if (proj.getInput() instanceof FilterOp) {
FilterOp filter = (FilterOp) proj.getInput();
if (filter.getInput() instanceof TableScanOp) {
SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
}
} else if (proj.getInput() instanceof FilteredTableScanOp) {
FilteredTableScanOp filter = (FilteredTableScanOp) proj.getInput();
Predicate pred = filter.getPredicate();
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
} else if (proj.getInput() instanceof BindableTableScanOp) {
BindableTableScanOp filter = (BindableTableScanOp) proj.getInput();
ScanStatement scan = filter.getStatement();
if (scan.getComparator() == null && scan.getLimits() == null && scan.getTableDef() != null) {
Predicate pred = scan.getPredicate();
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
}
}
}
if (update != null) {
return new SimpleUpdateOp(update.setReturnValues(returnValues));
} else {
return new UpdateOp(tableSpace, tableName, input, returnValues, function);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project herddb by diennea.
the class CalcitePlanner method planBindableTableScan.
private PlannerOp planBindableTableScan(BindableTableScan scan, RelDataType rowType) {
if (rowType == null) {
rowType = scan.getRowType();
}
final String tableSpace = scan.getTable().getQualifiedName().get(0);
final TableImpl tableImpl = (TableImpl) scan.getTable().unwrap(org.apache.calcite.schema.Table.class);
Table table = tableImpl.tableManager.getTable();
SQLRecordPredicate predicate = null;
if (!scan.filters.isEmpty()) {
CompiledSQLExpression where = null;
if (scan.filters.size() == 1) {
RexNode expr = scan.filters.get(0);
where = SQLExpressionCompiler.compileExpression(expr);
} else {
CompiledSQLExpression[] operands = new CompiledSQLExpression[scan.filters.size()];
int i = 0;
for (RexNode expr : scan.filters) {
CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(expr);
operands[i++] = condition;
}
where = new CompiledMultiAndExpression(operands);
}
predicate = new SQLRecordPredicate(table, null, where);
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
IndexOperation op = scanForIndexAccess(where, table, tableSpaceManager);
predicate.setIndexOperation(op);
CompiledSQLExpression filterPk = findFiltersOnPrimaryKey(table, where);
if (filterPk != null) {
filterPk = remapPositionalAccessToToPrimaryKeyAccessor(filterPk, table, scan);
}
predicate.setPrimaryKeyFilter(filterPk);
}
List<RexNode> projections = new ArrayList<>(scan.projects.size());
int i = 0;
for (int fieldpos : scan.projects) {
projections.add(new RexInputRef(fieldpos, rowType.getFieldList().get(i++).getType()));
}
Projection projection = buildProjection(projections, rowType, true, table.columns);
ScanStatement scanStatement = new ScanStatement(tableSpace, table.name, projection, predicate, null, null);
scanStatement.setTableDef(table);
return new BindableTableScanOp(scanStatement);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project herddb by diennea.
the class CalcitePlanner method planProject.
private PlannerOp planProject(EnumerableProject op, RelDataType rowType) {
PlannerOp input = convertRelNode(op.getInput(), null, false);
final List<RexNode> projects = op.getProjects();
final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
Projection projection = buildProjection(projects, _rowType, false, null);
return new ProjectOp(projection, input);
}
Aggregations