/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed 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.
 */
#pragma once

#include "velox/functions/lib/string/StringImpl.h"

namespace facebook::velox::functions {

/// split_part(string, delimiter, index) -> varchar
///
///     Splits string on delimiter and returns the part at index.
///     Field indexes start with 1. If the index is larger than the
///     number of fields, then null is returned.
template <typename T>
struct SplitPart {
  VELOX_DEFINE_FUNCTION_TYPES(T);

  // Results refer to strings in the first argument.
  static constexpr int32_t reuse_strings_from_arg = 0;

  // ASCII input always produces ASCII result.
  static constexpr bool is_default_ascii_behavior = true;

  FOLLY_ALWAYS_INLINE bool call(
      out_type<Varchar>& result,
      const arg_type<Varchar>& input,
      const arg_type<Varchar>& delimiter,
      const int64_t& index) {
    return stringImpl::splitPart<false>(result, input, delimiter, index);
  }

  FOLLY_ALWAYS_INLINE bool callAscii(
      out_type<Varchar>& result,
      const arg_type<Varchar>& input,
      const arg_type<Varchar>& delimiter,
      const int64_t& index) {
    return stringImpl::splitPart<true>(result, input, delimiter, index);
  }
};

} // namespace facebook::velox::functions
