Repository: apache/arrow
File: cpp/src/arrow/type_traits.cc (C++)

Code before:
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#include "arrow/type_traits.h"

#include "arrow/result.h"
#include "arrow/status.h"
#include "arrow/type.h"
#include "arrow/util/logging_internal.h"

namespace arrow {

Result<std::shared_ptr<DataType>> type_singleton(Type::type id) {
  switch (id) {
    case Type::NA:
      return null();
    case Type::BOOL:
      return boolean();
    case Type::INT8:
      return int8();
    case Type::INT16:
      return int16();
    case Type::INT32:
      return int32();
    case Type::INT64:
      return int64();
    case Type::UINT8:
      return uint8();
    case Type::UINT16:
      return uint16();
    case Type::UINT32:
      return uint32();
    case Type::UINT64:
      return uint64();
    case Type::HALF_FLOAT:
      return float16();
    case Type::FLOAT:
      return float32();
    case Type::DOUBLE:
      return float64();

Review comment (suggestion):
We don't need to handle these explicitly, we can use `VisitTypeIdInline` and then use something like `TypeTraits<T>::type_singleton`. This will ensure that this function works with all existing types at all times.

Code after:
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#include "arrow/type_traits.h"

#include "arrow/util/logging_internal.h"

namespace arrow {

int RequiredValueAlignmentForBuffer(Type::type type_id, int buffer_index) {
  if (buffer_index == 2 && type_id == Type::DENSE_UNION) {
    // A dense union array is the only array (so far) that requires alignment
    // on a buffer with a buffer_index that is not equal to 1
    return 4;
  }
  if (buffer_index != 1) {
    // If the buffer index is 0 then either:
    //  * The array type has no buffers, thus this shouldn't be called anyways
    //  * The array has a validity buffer at 0, no alignment needed
    //  * The array is a union array and has a types buffer at 0, no alignment needed
    // If the buffer index is > 1 then, in all current cases, it represents binary
    //  data and no alignment is needed.  The only exception is dense union buffers
    //  which are checked above.
    return 1;
  }
  DCHECK_NE(type_id, Type::DICTIONARY);
  DCHECK_NE(type_id, Type::EXTENSION);

  switch (type_id) {
    case Type::NA:                 // No buffers
    case Type::FIXED_SIZE_LIST:    // No second buffer (values in child array)
    case Type::FIXED_SIZE_BINARY:  // Fixed size binary could be dangerous but the
                                   // compute kernels don't type pun this.  E.g. if
                                   // an extension type is storing some kind of struct
                                   // here then the user should do their own alignment
                                   // check before casting to an array of structs
    case Type::BOOL:               // Always treated as uint8_t*
    case Type::INT8:               // Always treated as uint8_t*
    case Type::UINT8:              // Always treated as uint8_t*
    case Type::DENSE_UNION:        // Union arrays have a uint8_t* types buffer here