Java Code Examples for org.apache.calcite.rel.type.RelDataType#getComponentType()

The following examples show how to use org.apache.calcite.rel.type.RelDataType#getComponentType() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bats   File: AbstractNamespace.java    License: Apache License 2.0 6 votes vote down vote up
protected RelDataType convertToStruct(RelDataType type) {
  // "MULTISET [<expr>, ...]" needs to be wrapped in a record if
  // <expr> has a scalar type.
  // For example, "MULTISET [8, 9]" has type
  // "RECORD(INTEGER EXPR$0 NOT NULL) NOT NULL MULTISET NOT NULL".
  final RelDataType componentType = type.getComponentType();
  if (componentType == null || componentType.isStruct()) {
    return type;
  }
  final RelDataTypeFactory typeFactory = validator.getTypeFactory();
  final RelDataType structType = toStruct(componentType, getNode());
  final RelDataType collectionType;
  switch (type.getSqlTypeName()) {
  case ARRAY:
    collectionType = typeFactory.createArrayType(structType, -1);
    break;
  case MULTISET:
    collectionType = typeFactory.createMultisetType(structType, -1);
    break;
  default:
    throw new AssertionError(type);
  }
  return typeFactory.createTypeWithNullability(collectionType,
      type.isNullable());
}
 
Example 2
Source Project: Bats   File: MultisetSqlType.java    License: Apache License 2.0 6 votes vote down vote up
@Override public RelDataTypePrecedenceList getPrecedenceList() {
  return new RelDataTypePrecedenceList() {
    public boolean containsType(RelDataType type) {
      return type.getSqlTypeName() == getSqlTypeName()
          && type.getComponentType() != null
          && getComponentType().getPrecedenceList().containsType(
              type.getComponentType());
    }

    public int compareTypePrecedence(RelDataType type1, RelDataType type2) {
      if (!containsType(type1)) {
        throw new IllegalArgumentException("must contain type: " + type1);
      }
      if (!containsType(type2)) {
        throw new IllegalArgumentException("must contain type: " + type2);
      }
      return getComponentType().getPrecedenceList()
          .compareTypePrecedence(type1.getComponentType(), type2.getComponentType());
    }
  };
}
 
Example 3
Source Project: Bats   File: ArraySqlType.java    License: Apache License 2.0 6 votes vote down vote up
@Override public RelDataTypePrecedenceList getPrecedenceList() {
  return new RelDataTypePrecedenceList() {
    public boolean containsType(RelDataType type) {
      return type.getSqlTypeName() == getSqlTypeName()
          && type.getComponentType() != null
          && getComponentType().getPrecedenceList().containsType(
              type.getComponentType());
    }

    public int compareTypePrecedence(RelDataType type1, RelDataType type2) {
      if (!containsType(type1)) {
        throw new IllegalArgumentException("must contain type: " + type1);
      }
      if (!containsType(type2)) {
        throw new IllegalArgumentException("must contain type: " + type2);
      }
      return getComponentType().getPrecedenceList()
          .compareTypePrecedence(type1.getComponentType(), type2.getComponentType());
    }
  };
}
 
Example 4
Source Project: calcite   File: AbstractNamespace.java    License: Apache License 2.0 6 votes vote down vote up
protected RelDataType convertToStruct(RelDataType type) {
  // "MULTISET [<expr>, ...]" needs to be wrapped in a record if
  // <expr> has a scalar type.
  // For example, "MULTISET [8, 9]" has type
  // "RECORD(INTEGER EXPR$0 NOT NULL) NOT NULL MULTISET NOT NULL".
  final RelDataType componentType = type.getComponentType();
  if (componentType == null || componentType.isStruct()) {
    return type;
  }
  final RelDataTypeFactory typeFactory = validator.getTypeFactory();
  final RelDataType structType = toStruct(componentType, getNode());
  final RelDataType collectionType;
  switch (type.getSqlTypeName()) {
  case ARRAY:
    collectionType = typeFactory.createArrayType(structType, -1);
    break;
  case MULTISET:
    collectionType = typeFactory.createMultisetType(structType, -1);
    break;
  default:
    throw new AssertionError(type);
  }
  return typeFactory.createTypeWithNullability(collectionType,
      type.isNullable());
}
 
Example 5
Source Project: calcite   File: MultisetSqlType.java    License: Apache License 2.0 6 votes vote down vote up
@Override public RelDataTypePrecedenceList getPrecedenceList() {
  return new RelDataTypePrecedenceList() {
    public boolean containsType(RelDataType type) {
      return type.getSqlTypeName() == getSqlTypeName()
          && type.getComponentType() != null
          && getComponentType().getPrecedenceList().containsType(
              type.getComponentType());
    }

    public int compareTypePrecedence(RelDataType type1, RelDataType type2) {
      if (!containsType(type1)) {
        throw new IllegalArgumentException("must contain type: " + type1);
      }
      if (!containsType(type2)) {
        throw new IllegalArgumentException("must contain type: " + type2);
      }
      return getComponentType().getPrecedenceList()
          .compareTypePrecedence(type1.getComponentType(), type2.getComponentType());
    }
  };
}
 
Example 6
Source Project: calcite   File: ArraySqlType.java    License: Apache License 2.0 6 votes vote down vote up
@Override public RelDataTypePrecedenceList getPrecedenceList() {
  return new RelDataTypePrecedenceList() {
    public boolean containsType(RelDataType type) {
      return type.getSqlTypeName() == getSqlTypeName()
          && type.getComponentType() != null
          && getComponentType().getPrecedenceList().containsType(
              type.getComponentType());
    }

    public int compareTypePrecedence(RelDataType type1, RelDataType type2) {
      if (!containsType(type1)) {
        throw new IllegalArgumentException("must contain type: " + type1);
      }
      if (!containsType(type2)) {
        throw new IllegalArgumentException("must contain type: " + type2);
      }
      return getComponentType().getPrecedenceList()
          .compareTypePrecedence(type1.getComponentType(), type2.getComponentType());
    }
  };
}
 
Example 7
Source Project: Bats   File: SqlTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether two types have the same name and structure, possibly with
 * differing modifiers. For example, VARCHAR(1) and VARCHAR(10) are
 * considered the same, while VARCHAR(1) and CHAR(1) are considered
 * different. Likewise, VARCHAR(1) MULTISET and VARCHAR(10) MULTISET are
 * considered the same.
 *
 * @return true if types have same name and structure
 */
public static boolean sameNamedType(RelDataType t1, RelDataType t2) {
  if (t1.isStruct() || t2.isStruct()) {
    if (!t1.isStruct() || !t2.isStruct()) {
      return false;
    }
    if (t1.getFieldCount() != t2.getFieldCount()) {
      return false;
    }
    List<RelDataTypeField> fields1 = t1.getFieldList();
    List<RelDataTypeField> fields2 = t2.getFieldList();
    for (int i = 0; i < fields1.size(); ++i) {
      if (!sameNamedType(
          fields1.get(i).getType(),
          fields2.get(i).getType())) {
        return false;
      }
    }
    return true;
  }
  RelDataType comp1 = t1.getComponentType();
  RelDataType comp2 = t2.getComponentType();
  if ((comp1 != null) || (comp2 != null)) {
    if ((comp1 == null) || (comp2 == null)) {
      return false;
    }
    if (!sameNamedType(comp1, comp2)) {
      return false;
    }
  }
  return t1.getSqlTypeName() == t2.getSqlTypeName();
}
 
Example 8
Source Project: marble   File: RelDataTypeHolder.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataTypeHolder(RelDataType relDataType, boolean isConstant,
    Object value) {
  this.sqlTypeName = relDataType.getSqlTypeName();
  RelDataType componentType = relDataType.getComponentType();
  if (componentType != null) {
    this.componentType = new RelDataTypeHolder(componentType);
  }
  this.isConstant = isConstant;
  this.value = value;
}
 
Example 9
Source Project: marble   File: RelDataTypeHolder.java    License: Apache License 2.0 5 votes vote down vote up
public static  Expression generateExpression(RelDataType relDataType) {
  SqlTypeName sqlTypeName = relDataType.getSqlTypeName();
  RelDataType componentType = relDataType.getComponentType();
  if (componentType == null) {
    return Expressions.new_(RelDataTypeHolder.class,
        new ConstantExpression(SqlTypeName.class, sqlTypeName));
  } else {
    Expression componentTypeExpr = generateExpression(componentType);
    return Expressions.new_(RelDataTypeHolder.class,
        new ConstantExpression(SqlTypeName.class, sqlTypeName),
        componentTypeExpr);
  }
}
 
Example 10
Source Project: calcite   File: PigRelExVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override public void visit(CastExpression op) throws FrontendException {
  final RelDataType relType = PigTypes.convertSchemaField(op.getFieldSchema());
  final RexNode castOperand = stack.pop();
  if (castOperand instanceof RexLiteral
      && ((RexLiteral) castOperand).getValue() == null) {
    if (!relType.isStruct() && relType.getComponentType() == null) {
      stack.push(builder.getRexBuilder().makeNullLiteral(relType));
    } else {
      stack.push(castOperand);
    }
  } else {
    stack.push(builder.getRexBuilder().makeCast(relType, castOperand));
  }
}
 
Example 11
Source Project: calcite   File: PigRelBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Builds the projection expressions for a data type on top of an input data type.
 * For any field in output type, if there is no matching input field, we build
 * the literal null expression with the corresponding output field type.
 *
 * @param inputType The input data type
 * @param outputType The output data type that defines the types of projection expressions
 * @return List of projection expressions
 */
private List<RexNode> projects(RelDataType inputType, RelDataType outputType) {
  final List<RelDataTypeField> outputFields = outputType.getFieldList();
  final List<RelDataTypeField> inputFields = inputType.getFieldList();
  final List<RexNode> projectionExprs = new ArrayList<>();

  for (RelDataTypeField outputField : outputFields) {
    RelDataTypeField matchInputField = null;
    // First find the matching input field
    for (RelDataTypeField inputField : inputFields) {
      if (inputField.getName().equals(outputField.getName())) {
        // Matched if same name
        matchInputField = inputField;
        break;
      }
    }
    if (matchInputField != null) {
      RexNode fieldProject = field(matchInputField.getIndex());
      if (matchInputField.getType().equals(outputField.getType())) {
        // If found and on same type, just project the field
        projectionExprs.add(fieldProject);
      } else {
        // Different types, CAST is required
        projectionExprs.add(getRexBuilder().makeCast(outputField.getType(), fieldProject));
      }
    } else {
      final RelDataType columnType = outputField.getType();
      if (!columnType.isStruct() && columnType.getComponentType() == null) {
        // If not, project the null Literal with the same basic type
        projectionExprs.add(getRexBuilder().makeNullLiteral(outputField.getType()));
      } else {
        // If Record or Multiset just project a constant null
        projectionExprs.add(literal(null));
      }
    }
  }
  return projectionExprs;
}
 
Example 12
Source Project: calcite   File: PigRelBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Checks if two relational data types are compatible.
 *
 * @param t1 first type
 * @param t2 second type
 * @return true if t1 is compatible with t2
 */
public static boolean compatibleType(RelDataType t1, RelDataType t2) {
  if (t1.isStruct() || t2.isStruct()) {
    if (!t1.isStruct() || !t2.isStruct()) {
      return false;
    }
    if (t1.getFieldCount() != t2.getFieldCount()) {
      return false;
    }
    List<RelDataTypeField> fields1 = t1.getFieldList();
    List<RelDataTypeField> fields2 = t2.getFieldList();
    for (int i = 0; i < fields1.size(); ++i) {
      if (!compatibleType(
          fields1.get(i).getType(),
          fields2.get(i).getType())) {
        return false;
      }
    }
    return true;
  }
  RelDataType comp1 = t1.getComponentType();
  RelDataType comp2 = t2.getComponentType();
  if ((comp1 != null) || (comp2 != null)) {
    if ((comp1 == null) || (comp2 == null)) {
      return false;
    }
    if (!compatibleType(comp1, comp2)) {
      return false;
    }
  }
  return t1.getSqlTypeName().getFamily() == t2.getSqlTypeName().getFamily();
}
 
Example 13
Source Project: calcite   File: EnumerableTableScan.java    License: Apache License 2.0 5 votes vote down vote up
private Expression fieldExpression(ParameterExpression row_, int i,
    PhysType physType, JavaRowFormat format) {
  final Expression e =
      format.field(row_, i, null, physType.getJavaFieldType(i));
  final RelDataType relFieldType =
      physType.getRowType().getFieldList().get(i).getType();
  switch (relFieldType.getSqlTypeName()) {
  case ARRAY:
  case MULTISET:
    final RelDataType fieldType = relFieldType.getComponentType();
    if (fieldType.isStruct()) {
      // We can't represent a multiset or array as a List<Employee>, because
      // the consumer does not know the element type.
      // The standard element type is List.
      // We need to convert to a List<List>.
      final JavaTypeFactory typeFactory =
              (JavaTypeFactory) getCluster().getTypeFactory();
      final PhysType elementPhysType = PhysTypeImpl.of(
              typeFactory, fieldType, JavaRowFormat.CUSTOM);
      final MethodCallExpression e2 =
              Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, e);
      final Expression e3 = elementPhysType.convertTo(e2, JavaRowFormat.LIST);
      return Expressions.call(e3, BuiltInMethod.ENUMERABLE_TO_LIST.method);
    } else {
      return e;
    }
  default:
    return e;
  }
}
 
Example 14
Source Project: calcite   File: SqlTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether two types have the same name and structure, possibly with
 * differing modifiers. For example, VARCHAR(1) and VARCHAR(10) are
 * considered the same, while VARCHAR(1) and CHAR(1) are considered
 * different. Likewise, VARCHAR(1) MULTISET and VARCHAR(10) MULTISET are
 * considered the same.
 *
 * @return true if types have same name and structure
 */
public static boolean sameNamedType(RelDataType t1, RelDataType t2) {
  if (t1.isStruct() || t2.isStruct()) {
    if (!t1.isStruct() || !t2.isStruct()) {
      return false;
    }
    if (t1.getFieldCount() != t2.getFieldCount()) {
      return false;
    }
    List<RelDataTypeField> fields1 = t1.getFieldList();
    List<RelDataTypeField> fields2 = t2.getFieldList();
    for (int i = 0; i < fields1.size(); ++i) {
      if (!sameNamedType(
          fields1.get(i).getType(),
          fields2.get(i).getType())) {
        return false;
      }
    }
    return true;
  }
  RelDataType comp1 = t1.getComponentType();
  RelDataType comp2 = t2.getComponentType();
  if ((comp1 != null) || (comp2 != null)) {
    if ((comp1 == null) || (comp2 == null)) {
      return false;
    }
    if (!sameNamedType(comp1, comp2)) {
      return false;
    }
  }
  return t1.getSqlTypeName() == t2.getSqlTypeName();
}
 
Example 15
Source Project: calcite   File: EnumerableUncollect.java    License: Apache License 2.0 4 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  final BlockBuilder builder = new BlockBuilder();
  final EnumerableRel child = (EnumerableRel) getInput();
  final Result result = implementor.visitChild(this, 0, child, pref);
  final PhysType physType =
      PhysTypeImpl.of(
          implementor.getTypeFactory(),
          getRowType(),
          JavaRowFormat.LIST);

  // final Enumerable<List<Employee>> child = <<child adapter>>;
  // return child.selectMany(FLAT_PRODUCT);
  final Expression child_ =
      builder.append(
          "child", result.block);

  final List<Integer> fieldCounts = new ArrayList<>();
  final List<FlatProductInputType> inputTypes = new ArrayList<>();

  Expression lambdaForStructWithSingleItem = null;
  for (RelDataTypeField field : child.getRowType().getFieldList()) {
    final RelDataType type = field.getType();
    if (type instanceof MapSqlType) {
      fieldCounts.add(2);
      inputTypes.add(FlatProductInputType.MAP);
    } else {
      final RelDataType elementType = type.getComponentType();
      if (elementType.isStruct()) {
        if (elementType.getFieldCount() == 1 && child.getRowType().getFieldList().size() == 1
            && !withOrdinality) {
          // Solves CALCITE-4063: if we are processing a single field, which is a struct with a
          // single item inside, and no ordinality; the result must be a scalar, hence use a
          // special lambda that does not return lists, but the (single) items within those lists
          lambdaForStructWithSingleItem = Expressions.call(BuiltInMethod.FLAT_LIST.method);
        } else {
          fieldCounts.add(elementType.getFieldCount());
          inputTypes.add(FlatProductInputType.LIST);
        }
      } else {
        fieldCounts.add(-1);
        inputTypes.add(FlatProductInputType.SCALAR);
      }
    }
  }

  final Expression lambda = lambdaForStructWithSingleItem != null
      ? lambdaForStructWithSingleItem
      : Expressions.call(BuiltInMethod.FLAT_PRODUCT.method,
          Expressions.constant(Ints.toArray(fieldCounts)),
          Expressions.constant(withOrdinality),
          Expressions.constant(
              inputTypes.toArray(new FlatProductInputType[0])));
  builder.add(
      Expressions.return_(null,
          Expressions.call(child_,
              BuiltInMethod.SELECT_MANY.method,
              lambda)));
  return implementor.result(physType, builder.toBlock());
}