Java Code Examples for org.apache.calcite.rel.type.RelDataTypeField#getType()

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeField#getType() . 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 File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a cast for a row type.
 *
 * @param rexBuilder RexBuilder to use for constructing casts
 * @param lhsRowType target row type
 * @param rhsExps    expressions to be cast
 * @return cast expressions
 */
public static List<RexNode> generateCastExpressions(
    RexBuilder rexBuilder,
    RelDataType lhsRowType,
    List<RexNode> rhsExps) {
  List<RelDataTypeField> lhsFields = lhsRowType.getFieldList();
  List<RexNode> castExps = new ArrayList<>();
  for (Pair<RelDataTypeField, RexNode> pair
      : Pair.zip(lhsFields, rhsExps, true)) {
    RelDataTypeField lhsField = pair.left;
    RelDataType lhsType = lhsField.getType();
    final RexNode rhsExp = pair.right;
    RelDataType rhsType = rhsExp.getType();
    if (lhsType.equals(rhsType)) {
      castExps.add(rhsExp);
    } else {
      castExps.add(rexBuilder.makeCast(lhsType, rhsExp));
    }
  }
  return castExps;
}
 
Example 2
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a cast for a row type.
 *
 * @param rexBuilder RexBuilder to use for constructing casts
 * @param lhsRowType target row type
 * @param rhsExps    expressions to be cast
 * @return cast expressions
 */
public static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType,
        List<RexNode> rhsExps) {
    List<RelDataTypeField> lhsFields = lhsRowType.getFieldList();
    List<RexNode> castExps = new ArrayList<>();
    for (Pair<RelDataTypeField, RexNode> pair : Pair.zip(lhsFields, rhsExps, true)) {
        RelDataTypeField lhsField = pair.left;
        RelDataType lhsType = lhsField.getType();
        final RexNode rhsExp = pair.right;
        RelDataType rhsType = rhsExp.getType();
        if (lhsType.equals(rhsType)) {
            castExps.add(rhsExp);
        } else {
            castExps.add(rexBuilder.makeCast(lhsType, rhsExp));
        }
    }
    return castExps;
}
 
Example 3
Source File: RelNodeConvertor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private static List<FieldType> getFields(RelNode relNode) {
    RelDataType rowType = relNode.getRowType();
    List<RelDataTypeField> fieldList = rowType.getFieldList();
    ArrayList<FieldType> fieldSchemas = new ArrayList<>(fieldList.size());
    for (RelDataTypeField relDataTypeField : fieldList) {
        String name = relDataTypeField.getName();
        RelDataType type = relDataTypeField.getType();
        SqlTypeName sqlTypeName = type.getSqlTypeName();
        boolean nullable = type.isNullable();
        Integer precision = null;
        Integer scale = null;
        if (sqlTypeName.allowsPrec()) {
            precision = type.getPrecision();
        }
        if (sqlTypeName.allowsScale()) {
            scale = type.getScale();
        }
        fieldSchemas.add(new FieldType(name, ExprExplain.type(sqlTypeName), nullable, precision, scale));
    }
    return fieldSchemas;
}
 
Example 4
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private RelDataType validateUsingCol(SqlIdentifier id, SqlNode leftOrRight) {
	if (id.names.size() == 1) {
		String name = id.names.get(0);
		final SqlValidatorNamespace namespace = getNamespace(leftOrRight);
		final RelDataType rowType = namespace.getRowType();
		final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
		final RelDataTypeField field = nameMatcher.field(rowType, name);
		if (field != null) {
			if (nameMatcher.frequency(rowType.getFieldNames(), name) > 1) {
				throw newValidationError(id,
					RESOURCE.columnInUsingNotUnique(id.toString()));
			}
			return field.getType();
		}
	}
	throw newValidationError(id, RESOURCE.columnNotFound(id.toString()));
}
 
Example 5
Source File: RexBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an expression accessing a given field from a record.
 *
 * @param expr  Expression yielding a record
 * @param field Field
 * @return Expression accessing given field
 */
private RexNode makeFieldAccessInternal(
    RexNode expr,
    final RelDataTypeField field) {
  if (expr instanceof RexRangeRef) {
    RexRangeRef range = (RexRangeRef) expr;
    if (field.getIndex() < 0) {
      return makeCall(
          field.getType(),
          GET_OPERATOR,
          ImmutableList.of(
              expr,
              makeLiteral(field.getName())));
    }
    return new RexInputRef(
        range.getOffset() + field.getIndex(),
        field.getType());
  }
  return new RexFieldAccess(expr, field);
}
 
Example 6
Source File: RexProgramBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void validate(final RexNode expr, final int fieldOrdinal) {
  final RexVisitor<Void> validator =
      new RexVisitorImpl<Void>(true) {
        public Void visitInputRef(RexInputRef input) {
          final int index = input.getIndex();
          final List<RelDataTypeField> fields =
              inputRowType.getFieldList();
          if (index < fields.size()) {
            final RelDataTypeField inputField = fields.get(index);
            if (input.getType() != inputField.getType()) {
              throw new AssertionError("in expression " + expr
                  + ", field reference " + input + " has inconsistent type");
            }
          } else {
            if (index >= fieldOrdinal) {
              throw new AssertionError("in expression " + expr
                  + ", field reference " + input + " is out of bounds");
            }
            RexNode refExpr = exprList.get(index);
            if (refExpr.getType() != input.getType()) {
              throw new AssertionError("in expression " + expr
                  + ", field reference " + input + " has inconsistent type");
            }
          }
          return null;
        }
      };
  expr.accept(validator);
}
 
Example 7
Source File: PigRelBuilder.java    From calcite with 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 8
Source File: PlanExecutor.java    From quark with Apache License 2.0 5 votes vote down vote up
private List<ColumnMetaData> getColumnMetaDataList(
    JavaTypeFactory typeFactory, RelDataType x, RelDataType jdbcType) {
  final List<ColumnMetaData> columns = new ArrayList<>();
  for (Ord<RelDataTypeField> pair : Ord.zip(jdbcType.getFieldList())) {
    final RelDataTypeField field = pair.e;
    final RelDataType type = field.getType();
    final RelDataType fieldType =
        x.isStruct() ? x.getFieldList().get(pair.i).getType() : type;
    columns.add(
        metaData(typeFactory, columns.size(), field.getName(), type,
            fieldType, null));
  }
  return columns;
}
 
Example 9
Source File: SqlSplittableAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RexNode singleton(RexBuilder rexBuilder,
    RelDataType inputRowType, AggregateCall aggregateCall) {
  final int arg = aggregateCall.getArgList().get(0);
  final RelDataTypeField field = inputRowType.getFieldList().get(arg);
  final RelDataType fieldType = field.getType();
  final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
  RelDataType type = typeFactory.getTypeSystem().deriveSumType(typeFactory, fieldType);
  return rexBuilder.makeInputRef(type, arg);
}
 
Example 10
Source File: TableNamespace.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures that extended columns that have the same name as a base column also
 * have the same data-type.
 */
private void checkExtendedColumnTypes(SqlNodeList extendList) {
  final List<RelDataTypeField> extendedFields =
      SqlValidatorUtil.getExtendedColumns(validator, table, extendList);
  final List<RelDataTypeField> baseFields =
      getBaseRowType().getFieldList();
  final Map<String, Integer> nameToIndex =
      SqlValidatorUtil.mapNameToIndex(baseFields);

  for (final RelDataTypeField extendedField : extendedFields) {
    final String extFieldName = extendedField.getName();
    if (nameToIndex.containsKey(extFieldName)) {
      final Integer baseIndex = nameToIndex.get(extFieldName);
      final RelDataType baseType = baseFields.get(baseIndex).getType();
      final RelDataType extType = extendedField.getType();

      if (!extType.equals(baseType)) {
        // Get the extended column node that failed validation.
        final SqlNode extColNode =
            Iterables.find(extendList.getList(),
                sqlNode -> sqlNode instanceof SqlIdentifier
                    && Util.last(((SqlIdentifier) sqlNode).names).equals(
                        extendedField.getName()));

        throw validator.getValidationErrorFunction().apply(extColNode,
            RESOURCE.typeNotAssignable(
                baseFields.get(baseIndex).getName(), baseType.getFullTypeString(),
                extendedField.getName(), extType.getFullTypeString()));
      }
    }
  }
}
 
Example 11
Source File: RexProgramBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void validate(final RexNode expr, final int fieldOrdinal) {
    final RexVisitor<Void> validator = new RexVisitorImpl<Void>(true) {
        @Override
        public Void visitInputRef(RexInputRef input) {
            final int index = input.getIndex();
            final List<RelDataTypeField> fields = inputRowType.getFieldList();
            if (index < fields.size()) {
                final RelDataTypeField inputField = fields.get(index);
                if (input.getType() != inputField.getType()) {
                    throw new AssertionError(
                            "in expression " + expr + ", field reference " + input + " has inconsistent type");
                }
            } else {
                if (index >= fieldOrdinal) {
                    throw new AssertionError(
                            "in expression " + expr + ", field reference " + input + " is out of bounds");
                }
                RexNode refExpr = exprList.get(index);
                if (refExpr.getType() != input.getType()) {
                    throw new AssertionError(
                            "in expression " + expr + ", field reference " + input + " has inconsistent type");
                }
            }
            return null;
        }
    };
    expr.accept(validator);
}
 
Example 12
Source File: SqlItemOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final RelDataType operandType = opBinding.getOperandType(0);
  switch (operandType.getSqlTypeName()) {
  case ARRAY:
    return typeFactory.createTypeWithNullability(
        operandType.getComponentType(), true);
  case MAP:
    return typeFactory.createTypeWithNullability(operandType.getValueType(),
        true);
  case ROW:
    String fieldName = opBinding.getOperandLiteralValue(1, String.class);
    RelDataTypeField field = operandType.getField(fieldName, false, false);
    if (field == null) {
      throw new AssertionError("Cannot infer type of field '"
          + fieldName + "' within ROW type: " + operandType);
    } else {
      return field.getType();
    }
  case ANY:
  case DYNAMIC_STAR:
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  default:
    throw new AssertionError();
  }
}
 
Example 13
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private List<ColumnMetaData> getColumnMetaDataList(
    JavaTypeFactory typeFactory, RelDataType x, RelDataType jdbcType,
    List<List<String>> originList) {
  final List<ColumnMetaData> columns = new ArrayList<>();
  for (Ord<RelDataTypeField> pair : Ord.zip(jdbcType.getFieldList())) {
    final RelDataTypeField field = pair.e;
    final RelDataType type = field.getType();
    final RelDataType fieldType =
        x.isStruct() ? x.getFieldList().get(pair.i).getType() : type;
    columns.add(
        metaData(typeFactory, columns.size(), field.getName(), type,
            fieldType, originList.get(pair.i)));
  }
  return columns;
}
 
Example 14
Source File: OrderByScope.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType resolveColumn(String name, SqlNode ctx) {
  final SqlValidatorNamespace selectNs = validator.getNamespace(select);
  final RelDataType rowType = selectNs.getRowType();
  final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher();
  final RelDataTypeField field = nameMatcher.field(rowType, name);
  if (field != null) {
    return field.getType();
  }
  final SqlValidatorScope selectScope = validator.getSelectScope(select);
  return selectScope.resolveColumn(name, ctx);
}
 
Example 15
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
SqlValidatorNamespace lookupFieldNamespace(RelDataType rowType, String name) {
	final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
	final RelDataTypeField field = nameMatcher.field(rowType, name);
	return new FieldNamespace(this, field.getType());
}
 
Example 16
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RelDataType getFieldType(RelNode relNode, int i) {
  final RelDataTypeField inputField =
      relNode.getRowType().getFieldList().get(i);
  return inputField.getType();
}
 
Example 17
Source File: CalciteRowMetaData.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isNullable(int column) {
    RelDataTypeField column1 = getColumn(column);
    RelDataType type = column1.getType();
    return type.isNullable();
}
 
Example 18
Source File: GlobalDictionaryVisitor.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private PrelWithDictionaryInfo visitParquetScanPrel(ParquetScanPrel parquetScanPrel, Void value) throws RuntimeException {
  final ReadDefinition readDefinition = parquetScanPrel.getTableMetadata().getReadDefinition();

  if (readDefinition == null || readDefinition.getExtendedProperty() == null) {
    return new PrelWithDictionaryInfo(parquetScanPrel);
  }

  // Make sure we don't apply global dictionary on columns that have conditions pushed into the scan
  final Set<String> columnsPushedToScan = new HashSet<>();
  if (parquetScanPrel.getFilter() != null) {
    Iterables.addAll(columnsPushedToScan,
        Iterables.transform(parquetScanPrel.getFilter().getConditions(), ParquetFilterCondition.EXTRACT_COLUMN_NAME));
  }

  final Map<String, String> dictionaryEncodedColumnsToDictionaryFilePath = Maps.newHashMap();
  long dictionaryVersion = -1;

  ParquetDatasetXAttr xAttr = null;

  boolean isIcebergDataset = isIcebergDataset(parquetScanPrel.getTableMetadata());
  // Dremio 5.0 release had ParquetDatasetXAttr in extended property
  boolean newIcebergDataset = false;
  if (isIcebergDataset) {
    try {
      IcebergDatasetXAttr icebergDatasetXAttr = LegacyProtobufSerializer.parseFrom(IcebergDatasetXAttr.PARSER,
        readDefinition.getExtendedProperty().asReadOnlyByteBuffer());
      xAttr = icebergDatasetXAttr.getParquetDatasetXAttr();
      newIcebergDataset = true;
    } catch (InvalidProtocolBufferException ignored) {
    }
  }

  if (!isIcebergDataset || !newIcebergDataset) {
    try {
      xAttr = LegacyProtobufSerializer.parseFrom(ParquetDatasetXAttr.PARSER,
        readDefinition.getExtendedProperty().asReadOnlyByteBuffer());
    } catch (InvalidProtocolBufferException e) {
      if (isIcebergDataset) {
        throw new RuntimeException("Could not deserialize Iceberg dataset info");
      } else {
        throw new RuntimeException("Could not deserialize parquet dataset info");
      }
    }
  }

  if (xAttr.hasDictionaryEncodedColumns()) {
    final DictionaryEncodedColumns dictionaryEncodedColumns = xAttr.getDictionaryEncodedColumns();
    dictionaryVersion = dictionaryEncodedColumns.getVersion();
    // Construct paths to dictionary files based on the version found in namespace. Do NOT look for files during planning.
    final Path dictionaryRootPath = Path.of(dictionaryEncodedColumns.getRootPath());
    for (String dictionaryEncodedColumn : dictionaryEncodedColumns.getColumnsList()) {
      if (!columnsPushedToScan.contains(dictionaryEncodedColumn)) {
        dictionaryEncodedColumnsToDictionaryFilePath.put(dictionaryEncodedColumn,
          GlobalDictionaryBuilder.dictionaryFilePath(dictionaryRootPath, dictionaryEncodedColumn).toString());
      }
    }
  }

  if (dictionaryEncodedColumnsToDictionaryFilePath.isEmpty()) {
    return new PrelWithDictionaryInfo(parquetScanPrel);
  }

  final StoragePluginId storagePluginId = parquetScanPrel.getPluginId();
  boolean encodedColumns = false;
  final List<RelDataTypeField> newFields = Lists.newArrayList();
  final GlobalDictionaryFieldInfo[] fieldInfos = new GlobalDictionaryFieldInfo[parquetScanPrel.getRowType().getFieldCount()];
  final List<GlobalDictionaryFieldInfo> globalDictionaryColumns = Lists.newArrayList();

  for (int i = 0; i < parquetScanPrel.getRowType().getFieldCount(); ++i) {
    final RelDataTypeField field = parquetScanPrel.getRowType().getFieldList().get(i);
    if (dictionaryEncodedColumnsToDictionaryFilePath.containsKey(field.getName())) {
      fieldInfos[i] = new GlobalDictionaryFieldInfo(
        dictionaryVersion,
        field.getName(),
        storagePluginId,
        CalciteArrowHelper.fromRelAndMinorType(field
          .getType(), TypeInferenceUtils.getMinorTypeFromCalciteType(field
          .getType())).getType(),
        dictionaryEncodedColumnsToDictionaryFilePath.get(field.getName()),
        new RelDataTypeFieldImpl(field.getName(), field.getIndex(), field.getType()));
      newFields.add(dictionaryEncodedField(field));
      globalDictionaryColumns.add(fieldInfos[i]);
      encodedColumns = true;
    } else {
      fieldInfos[i] = null;
      newFields.add(field);
    }
  }

  if (!encodedColumns) {
    return new PrelWithDictionaryInfo(parquetScanPrel);
  }

  final RelDataType newRelDataType = PrelWithDictionaryInfo.toRowDataType(newFields, parquetScanPrel.getCluster().getTypeFactory());
  final ParquetScanPrel newParquetScanPrel = parquetScanPrel.cloneWithGlobalDictionaryColumns(globalDictionaryColumns, newRelDataType);
  return new PrelWithDictionaryInfo(newParquetScanPrel, fieldInfos);
}
 
Example 19
Source File: MergeTableLikeUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
private void verifyRowtimeAttribute(
		Map<FeatureOption, MergingStrategy> mergingStrategies,
		SqlIdentifier eventTimeColumnName,
		Map<String, RelDataType> allFieldsTypes) {
	String fullRowtimeExpression = eventTimeColumnName.toString();
	boolean specAlreadyExists = watermarkSpecs.containsKey(fullRowtimeExpression);

	if (specAlreadyExists &&
		mergingStrategies.get(FeatureOption.WATERMARKS) != MergingStrategy.OVERWRITING) {
		throw new ValidationException(String.format(
			"There already exists a watermark spec for column '%s' in the base table. You " +
				"might want to specify EXCLUDING WATERMARKS or OVERWRITING WATERMARKS.",
			fullRowtimeExpression));
	}

	List<String> components = eventTimeColumnName.names;
	if (!allFieldsTypes.containsKey(components.get(0))) {
		throw new ValidationException(
			String.format(
				"The rowtime attribute field '%s' is not defined in the table schema, at %s\n" +
					"Available fields: [%s]",
				fullRowtimeExpression,
				eventTimeColumnName.getParserPosition(),
				allFieldsTypes.keySet().stream().collect(Collectors.joining("', '", "'", "'"))
			));
	}

	if (components.size() > 1) {
		RelDataType componentType = allFieldsTypes.get(components.get(0));
		for (int i = 1; i < components.size(); i++) {
			RelDataTypeField field = componentType.getField(components.get(i), true, false);
			if (field == null) {
				throw new ValidationException(
					String.format(
						"The rowtime attribute field '%s' is not defined in the table schema, at %s\n" +
							"Nested field '%s' was not found in a composite type: %s.",
						fullRowtimeExpression,
						eventTimeColumnName.getComponent(i).getParserPosition(),
						components.get(i),
						FlinkTypeFactory.toLogicalType(allFieldsTypes.get(components.get(0))))
					);
			}
			componentType = field.getType();
		}
	}
}
 
Example 20
Source File: AggregateReduceFunctionsRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RelDataType getFieldType(RelNode relNode, int i) {
  final RelDataTypeField inputField =
      relNode.getRowType().getFieldList().get(i);
  return inputField.getType();
}