private static List validatePartitioningExpressionsImpl()

in asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/util/ValidateUtil.java [150:248]


    private static List<IAType> validatePartitioningExpressionsImpl(ARecordType recType, ARecordType metaRecType,
            List<List<String>> partitioningExprs, List<Integer> keySourceIndicators, boolean autogenerated,
            boolean forPrimaryKey, SourceLocation sourceLoc, List<TypeExpression> partitioningExprTypes)
            throws AlgebricksException {
        checkDuplicateFields(partitioningExprs, sourceLoc);
        String keyKindDisplayName = forPrimaryKey ? PRIMARY : "";
        List<IAType> computedPartitioningExprTypes = new ArrayList<>(partitioningExprs.size());
        if (autogenerated) {
            if (partitioningExprs.size() > 1) {
                throw new CompilationException(ErrorCode.COMPILATION_CANNOT_AUTOGENERATE_COMPOSITE_KEY, sourceLoc,
                        keyKindDisplayName);
            }
            List<String> fieldName = partitioningExprs.get(0);
            IAType fieldType = recType.getSubFieldType(fieldName);
            if (fieldType == null) {
                if (partitioningExprTypes != null && partitioningExprTypes.size() > 0) {
                    String typeName =
                            ((TypeReferenceExpression) partitioningExprTypes.get(0)).getIdent().second.getValue();
                    fieldType = BuiltinTypeMap.getBuiltinType(typeName);
                    if (fieldType == null) {
                        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_KEY_TYPE, sourceLoc, typeName,
                                keyKindDisplayName);
                    }
                } else {
                    String unTypeField = fieldName.get(0) == null ? "" : fieldName.get(0);
                    throw new CompilationException(ErrorCode.COMPILATION_FIELD_NOT_FOUND, sourceLoc,
                            LogRedactionUtil.userData(unTypeField));
                }
            }
            computedPartitioningExprTypes.add(fieldType);
            ATypeTag pkTypeTag = fieldType.getTypeTag();
            if (pkTypeTag != ATypeTag.UUID) {
                throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_AUTOGENERATED_TYPE, sourceLoc,
                        keyKindDisplayName, pkTypeTag.name(), ATypeTag.UUID.name());
            }
        } else {
            if (partitioningExprTypes == null) {
                computedPartitioningExprTypes =
                        KeyFieldTypeUtil.getKeyTypes(recType, metaRecType, partitioningExprs, keySourceIndicators);
            }
            for (int i = 0; i < partitioningExprs.size(); i++) {
                List<String> partitioningExpr = partitioningExprs.get(i);
                IAType fieldType;
                if (partitioningExprTypes != null) {
                    String typeName =
                            ((TypeReferenceExpression) partitioningExprTypes.get(i)).getIdent().second.getValue();
                    fieldType = BuiltinTypeMap.getBuiltinType(typeName);
                    if (fieldType == null) {
                        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_KEY_TYPE, sourceLoc, typeName,
                                keyKindDisplayName);
                    }
                    computedPartitioningExprTypes.add(fieldType);
                } else {
                    fieldType = computedPartitioningExprTypes.get(i);
                    if (fieldType == null) {
                        throw new CompilationException(ErrorCode.COMPILATION_FIELD_NOT_FOUND, sourceLoc,
                                LogRedactionUtil.userData(RecordUtil.toFullyQualifiedName(partitioningExpr)));
                    }
                    if (forPrimaryKey) {
                        boolean nullable = KeyFieldTypeUtil.chooseSource(keySourceIndicators, i, recType, metaRecType)
                                .isSubFieldNullable(partitioningExpr);
                        if (nullable) {
                            // key field is nullable
                            throw new CompilationException(ErrorCode.COMPILATION_KEY_CANNOT_BE_NULLABLE, sourceLoc,
                                    keyKindDisplayName,
                                    LogRedactionUtil.userData(RecordUtil.toFullyQualifiedName(partitioningExpr)));
                        }
                    } else {
                        fieldType = TypeComputeUtils.getActualType(fieldType);
                    }
                }
                switch (fieldType.getTypeTag()) {
                    case TINYINT:
                    case SMALLINT:
                    case INTEGER:
                    case BIGINT:
                    case FLOAT:
                    case DOUBLE:
                    case STRING:
                    case BINARY:
                    case DATE:
                    case TIME:
                    case UUID:
                    case DATETIME:
                    case YEARMONTHDURATION:
                    case DAYTIMEDURATION:
                        break;
                    case UNION:
                        throw new CompilationException(ErrorCode.COMPILATION_KEY_CANNOT_BE_NULLABLE, sourceLoc,
                                keyKindDisplayName,
                                LogRedactionUtil.userData(RecordUtil.toFullyQualifiedName(partitioningExpr)));
                    default:
                        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_KEY_TYPE, sourceLoc,
                                fieldType.getTypeTag(), keyKindDisplayName);
                }
            }
        }
        return computedPartitioningExprTypes;
    }