private List parseArray()

in jdbc/src/main/java/software/amazon/timestream/jdbc/TimestreamArray.java [262:397]


  private List<Object> parseArray(
    final List<?> array,
    final TimestreamDataType sourceType,
    final Map<String, Class<?>> conversionMap)
    throws SQLException {
    final List<Object> arrayList = new ArrayList<>();

    if (array.isEmpty()) {
      return arrayList;
    }

    final JdbcType targetType = TimestreamDataType
      .retrieveTargetType(sourceType, conversionMap)
      .getJdbcType();

    if (array.get(0) instanceof TimeSeriesDataPoint) {
      if (sourceType != TimestreamDataType.TIMESERIES) {
        // If the sourceType is not TIMESERIES, then a logical error has happened when creating
        // column metadata in `TimestreamArrayResultSet` and the driver is trying to parse
        // the data using the faulty ResultSet metadata.
        throw new RuntimeException(
            Error.getErrorMessage(LOGGER, Error.INVALID_DATA_AT_ARRAY, array));
      }

      if (targetType == TimestreamDataType.TIMESERIES.getJdbcType()) {
        return ImmutableList.of(array);
      }

      arrayList.add(Conversions.convert(
        sourceType,
        targetType,
        new Datum().withTimeSeriesValue((Collection<TimeSeriesDataPoint>) array),
        this.parentResultSet::addWarning));

      return arrayList;
    }

    if (array.get(0) instanceof Datum) {
      switch (sourceType) {
        case ARRAY: {
          for (final Object o : array) {
            final Object arrayValue = ((Datum) o).getArrayValue();
            if (arrayValue == null) {
              // If the sourceType is TimestreamDataType.ARRAY but the datum does not
              // contain arrayValue, then a logical error has happened when creating a
              // column metadata for the array.
              throw new RuntimeException(
                  Error.getErrorMessage(LOGGER, Error.INVALID_DATA_AT_ARRAY, array));
            }

            final TimestreamArray result = new TimestreamArray(
              (List<Object>) arrayValue,
              this.timestreamBaseType.getArrayColumnInfo().getType(),
              this.parentResultSet,
              conversionMap);

            if (targetType == JdbcType.VARCHAR) {
              arrayList.add(String.valueOf(result.getArrayList()));
              continue;
            }

            final List<Object> resultList = populateArrayListToDatum(result.getArrayList(), result);

            final TimestreamArray convertedResult = new TimestreamArray(
                resultList,
                TimestreamDataType.createTypeWithMap(
                    new Type(),
                    this.timestreamBaseType.getArrayColumnInfo().getType(),
                    conversionMap),
                this.parentResultSet,
                conversionMap);
            arrayList.add(convertedResult);
          }
          break;
        }

        case ROW: {
          for (final Object o : array) {
            final Row rowValue = ((Datum) o).getRowValue();
            if ((rowValue == null) || (rowValue.getData() == null)) {
              throw new RuntimeException(
                  Error.getErrorMessage(LOGGER, Error.INVALID_DATA_AT_ROW, array));
            }

            final TimestreamStruct struct = new TimestreamStruct(
                rowValue.getData(),
                this.timestreamBaseType.getRowColumnInfo(),
                this.parentResultSet,
                conversionMap);

            if (targetType == JdbcType.VARCHAR) {
              final List<Datum> data = new ArrayList<>();
              for (final Object attribute : struct.getAttributes()) {
                data.add(new Datum().withScalarValue(attribute.toString()));
              }
              final Object convertedResult = Conversions.convert(
                  baseType,
                  targetType,
                  new Datum().withRowValue(new Row().withData(data)),
                  this.parentResultSet::addWarning
              );
              arrayList.add(convertedResult);
              continue;
            }

            final List<Datum> convertedDatumList = TimestreamStruct
                .populateObjectToDatum(struct.getAttributes());

            final TimestreamStruct convertedStruct = new TimestreamStruct(
                convertedDatumList,
                this.timestreamBaseType.getRowColumnInfo(),
                this.parentResultSet,
                conversionMap);

            arrayList.add(convertedStruct);
          }
          break;
        }

        default: {
          for (final Object object : array) {
            arrayList.add(Conversions.convert(
              sourceType,
              targetType,
              (Datum) object,
              this.parentResultSet::addWarning));
          }
          break;
        }
      }
    } else {
      throw new RuntimeException(Error.getErrorMessage(LOGGER, Error.INVALID_DATA_AT_ARRAY, array));
    }

    return arrayList;
  }