Skip to content

Commit

Permalink
str_to_date function push down (#1961)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaySon-Huang authored Jun 3, 2021
1 parent fb45ed9 commit 30db147
Show file tree
Hide file tree
Showing 20 changed files with 1,290 additions and 37 deletions.
720 changes: 719 additions & 1 deletion dbms/src/Common/MyTime.cpp

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions dbms/src/Common/MyTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Core/Field.h>
#include <common/DateLUTImpl.h>

struct StringRef;
namespace DB
{

Expand Down Expand Up @@ -133,6 +134,24 @@ struct MyDateTimeFormatter
}
};

struct MyDateTimeParser
{
explicit MyDateTimeParser(String format_);

std::optional<UInt64> parseAsPackedUInt(const StringRef & str_view) const;

struct Context;

private:
const String format;

// Parsing method. Parse from ctx.view[ctx.pos].
// If success, update `datetime`, `ctx` and return true.
// If fail, return false.
using ParserCallback = std::function<bool(MyDateTimeParser::Context & ctx, MyTimeBase & datetime)>;
std::vector<ParserCallback> parsers;
};

Field parseMyDateTime(const String & str, int8_t fsp = 6);

void convertTimeZone(UInt64 from_time, UInt64 & to_time, const DateLUTImpl & time_zone_from, const DateLUTImpl & time_zone_to);
Expand Down
30 changes: 30 additions & 0 deletions dbms/src/Common/StringUtils/StringRefUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <Common/StringUtils/StringUtils.h>
#include <common/StringRef.h>

inline bool startsWith(const StringRef & view, const StringRef & prefix)
{
return detail::startsWith(view.data, view.size, prefix.data, prefix.size);
}

// case insensitive version of startsWith
inline bool startsWithCI(const StringRef & view, const StringRef & prefix)
{
return detail::startsWithCI(view.data, view.size, prefix.data, prefix.size);
}

inline bool endsWith(const StringRef & view, const char * prefix)
{
return detail::endsWith(view.data, view.size, prefix, strlen(prefix)); //
}

// case insensitive version of endsWith
inline bool endsWithCI(const StringRef & view, const char * prefix)
{
return detail::endsWithCI(view.data, view.size, prefix, strlen(prefix));
}

// n - number of characters to remove from the start of the view,
// The behavior is undefined if `n > view.size`
inline StringRef removePrefix(const StringRef & view, size_t n) { return StringRef{view.data + n, view.size - n}; }
36 changes: 32 additions & 4 deletions dbms/src/Common/StringUtils/StringUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
#include "StringUtils.h"

#include <cctype>

namespace detail
{

bool startsWith(const std::string & s, const char * prefix, size_t prefix_size)
bool startsWith(const char * s, size_t size, const char * prefix, size_t prefix_size)
{
return size >= prefix_size && 0 == memcmp(s, prefix, prefix_size);
}

bool endsWith(const char * s, size_t size, const char * suffix, size_t suffix_size)
{
return s.size() >= prefix_size && 0 == memcmp(s.data(), prefix, prefix_size);
return size >= suffix_size && 0 == memcmp(s + size - suffix_size, suffix, suffix_size);
}

bool endsWith(const std::string & s, const char * suffix, size_t suffix_size)
bool startsWithCI(const char * s, size_t size, const char * prefix, size_t prefix_size)
{
return s.size() >= suffix_size && 0 == memcmp(s.data() + s.size() - suffix_size, suffix, suffix_size);
if (size < prefix_size)
return false;
// case insensitive compare
for (size_t i = 0; i < prefix_size; ++i)
{
if (std::tolower(s[i]) != std::tolower(prefix[i]))
return false;
}
return true;
}

bool endsWithCI(const char * s, size_t size, const char * suffix, size_t suffix_size)
{
if (size < suffix_size)
return false;
// case insensitive compare
for (size_t i = 0; i < suffix_size; ++i)
{
if (std::tolower(s[i]) != std::tolower(suffix[i]))
return false;
}
return true;
}

} // namespace detail
19 changes: 12 additions & 7 deletions dbms/src/Common/StringUtils/StringUtils.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
#pragma once

#include <string>
#include <string_view>
#include <cstring>
#include <cstddef>


namespace detail
{
bool startsWith(const std::string & s, const char * prefix, size_t prefix_size);
bool endsWith(const std::string & s, const char * suffix, size_t suffix_size);
}
bool startsWith(const char * s, size_t size, const char * prefix, size_t prefix_size);
bool endsWith(const char * s, size_t size, const char * suffix, size_t suffix_size);

// case insensitive version
bool startsWithCI(const char * s, size_t size, const char * prefix, size_t prefix_size);
bool endsWithCI(const char * s, size_t size, const char * suffix, size_t suffix_size);
} // namespace detail


inline bool startsWith(const std::string & s, const std::string & prefix)
{
return detail::startsWith(s, prefix.data(), prefix.size());
return detail::startsWith(s.data(), s.size(), prefix.data(), prefix.size());
}

inline bool endsWith(const std::string & s, const std::string & suffix)
{
return detail::endsWith(s, suffix.data(), suffix.size());
return detail::endsWith(s.data(), s.size(), suffix.data(), suffix.size());
}


/// With GCC, strlen is evaluated compile time if we pass it a constant
/// string that is known at compile time.
inline bool startsWith(const std::string & s, const char * prefix)
{
return detail::startsWith(s, prefix, strlen(prefix));
return detail::startsWith(s.data(), s.size(), prefix, strlen(prefix));
}

inline bool endsWith(const std::string & s, const char * suffix)
{
return detail::endsWith(s, suffix, strlen(suffix));
return detail::endsWith(s.data(), s.size(), suffix, strlen(suffix)); //
}

/// Given an integer, return the adequate suffix for
Expand Down
Loading

0 comments on commit 30db147

Please sign in to comment.