Search in sources :

Example 16 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveSubQRemoveRelBuilder method field.

/**
 * As {@link #field(int, int, int)}, but if {@code alias} is true, the method
 * may apply an alias to make sure that the field has the same name as in the
 * input frame. If no alias is applied the expression is definitely a
 * {@link RexInputRef}.
 */
private RexNode field(int inputCount, int inputOrdinal, int fieldOrdinal, boolean alias) {
    final Frame frame = peek_(inputCount, inputOrdinal);
    final RelNode input = frame.rel;
    final RelDataType rowType = input.getRowType();
    if (fieldOrdinal < 0 || fieldOrdinal > rowType.getFieldCount()) {
        throw new IllegalArgumentException("field ordinal [" + fieldOrdinal + "] out of range; input fields are: " + rowType.getFieldNames());
    }
    final RelDataTypeField field = rowType.getFieldList().get(fieldOrdinal);
    final int offset = inputOffset(inputCount, inputOrdinal);
    final RexInputRef ref = cluster.getRexBuilder().makeInputRef(field.getType(), offset + fieldOrdinal);
    final RelDataTypeField aliasField = frame.fields().get(fieldOrdinal);
    if (!alias || field.getName().equals(aliasField.getName())) {
        return ref;
    } else {
        return alias(ref, aliasField.getName());
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 17 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType 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;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) RexNode(org.apache.calcite.rex.RexNode)

Example 18 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project herddb by diennea.

the class CalcitePlanner method planEnumerableThetaJoin.

private PlannerOp planEnumerableThetaJoin(EnumerableThetaJoin op, RelDataType rowType) {
    PlannerOp left = convertRelNode(op.getLeft(), null, false);
    PlannerOp right = convertRelNode(op.getRight(), null, false);
    CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(op.getCondition());
    boolean generateNullsOnLeft = op.getJoinType().generatesNullsOnLeft();
    boolean generateNullsOnRight = op.getJoinType().generatesNullsOnRight();
    final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
    List<RelDataTypeField> fieldList = _rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    return new ThetaJoinOp(fieldNames, columns, left, right, condition, generateNullsOnLeft, generateNullsOnRight, false);
}
Also used : ThetaJoinOp(herddb.model.planner.ThetaJoinOp) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PlannerOp(herddb.model.planner.PlannerOp) Column(herddb.model.Column) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 19 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project herddb by diennea.

the class CalcitePlanner method planEnumerableJoin.

private PlannerOp planEnumerableJoin(EnumerableJoin op, RelDataType rowType) {
    // please note that EnumerableJoin has a condition field which actually is not useful
    PlannerOp left = convertRelNode(op.getLeft(), null, false);
    PlannerOp right = convertRelNode(op.getRight(), null, false);
    int[] leftKeys = op.getLeftKeys().toIntArray();
    int[] rightKeys = op.getRightKeys().toIntArray();
    boolean generateNullsOnLeft = op.getJoinType().generatesNullsOnLeft();
    boolean generateNullsOnRight = op.getJoinType().generatesNullsOnRight();
    final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
    List<RelDataTypeField> fieldList = _rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    return new JoinOp(fieldNames, columns, leftKeys, left, rightKeys, right, generateNullsOnLeft, generateNullsOnRight, false);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PlannerOp(herddb.model.planner.PlannerOp) Column(herddb.model.Column) RelDataType(org.apache.calcite.rel.type.RelDataType) JoinOp(herddb.model.planner.JoinOp) ThetaJoinOp(herddb.model.planner.ThetaJoinOp) SemiJoinOp(herddb.model.planner.SemiJoinOp)

Example 20 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project herddb by diennea.

the class CalcitePlanner method planProject.

private PlannerOp planProject(EnumerableProject op, RelDataType rowType) {
    PlannerOp input = convertRelNode(op.getInput(), null, false, 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);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ProjectOp(herddb.model.planner.ProjectOp) Projection(herddb.model.Projection) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)834 RexNode (org.apache.calcite.rex.RexNode)268 ArrayList (java.util.ArrayList)214 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)209 RelNode (org.apache.calcite.rel.RelNode)153 SqlNode (org.apache.calcite.sql.SqlNode)143 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)123 RexBuilder (org.apache.calcite.rex.RexBuilder)118 Test (org.junit.Test)62 ImmutableList (com.google.common.collect.ImmutableList)58 RexInputRef (org.apache.calcite.rex.RexInputRef)57 List (java.util.List)51 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)45 RexLiteral (org.apache.calcite.rex.RexLiteral)44 SqlNodeList (org.apache.calcite.sql.SqlNodeList)42 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)40 AggregateCall (org.apache.calcite.rel.core.AggregateCall)39 BitString (org.apache.calcite.util.BitString)38 BigDecimal (java.math.BigDecimal)35 RelBuilder (org.apache.calcite.tools.RelBuilder)34