-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support push function concat
, concat_ws
, datediff
, year
, day
, unix_timestamp
down to TiFlash.
#1588
Closed
Closed
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0dc3249
support year,month,day.
LittleFall 1642b6f
support datediff
LittleFall fe1a467
support unix_timestamp
LittleFall f56a6ba
support concat.
LittleFall 0914085
support tidb_concat_ws
LittleFall 417462d
fix concat_ws bug.
LittleFall f67b63f
add TiDBConcat.
LittleFall 0c88797
refine code.
LittleFall 1f50318
Merge branch 'master' into function-pushdown
LittleFall 15ce428
fix bug.
LittleFall 000b82b
Merge remote-tracking branch 'qizhi/function-pushdown' into function-…
LittleFall 042aefd
Merge remote-tracking branch 'pingcap/master' into function-pushdown
LittleFall 5c7e755
Merge remote-tracking branch 'pingcap/master' into function-pushdown
LittleFall 8792b0e
refine type check
LittleFall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -434,8 +434,8 @@ struct ToDayOfMonthImpl | |
{ | ||
return time_zone.toDayOfMonth(DayNum_t(d)); | ||
} | ||
static inline UInt8 execute(UInt64 , const DateLUTImpl & ) { | ||
throw Exception("Illegal type MyTime of argument for function toDayOfMonth", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | ||
static inline UInt8 execute(UInt64 t, const DateLUTImpl & ) { | ||
return (UInt8)((t >> 41) & 31); | ||
} | ||
|
||
using FactorTransform = ToStartOfMonthImpl; | ||
|
@@ -1303,13 +1303,11 @@ class FunctionTiDBTimestampDiff : public IFunction | |
throw Exception("First argument for function " + getName() + " (unit) must be String", | ||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | ||
|
||
if(!checkDataType<DataTypeMyDateTime>(removeNullable(arguments[1]).get()) && | ||
!checkDataType<DataTypeMyDate>(removeNullable(arguments[1]).get())) | ||
if(!removeNullable(arguments[1])->isDateOrDateTime()) | ||
throw Exception("Second argument for function " + getName() + " must be MyDate or MyDateTime", | ||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | ||
|
||
if(!checkDataType<DataTypeMyDateTime>(removeNullable(arguments[2]).get()) && | ||
!checkDataType<DataTypeMyDate>(removeNullable(arguments[2]).get())) | ||
if(!removeNullable(arguments[2])->isDateOrDateTime()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
throw Exception("Third argument for function " + getName() + " must be MyDate or MyDateTime", | ||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | ||
|
||
|
@@ -1652,6 +1650,176 @@ class FunctionTiDBTimestampDiff : public IFunction | |
} | ||
}; | ||
|
||
/** TiDBDateDiff(t1, t2) | ||
* Supports for tidb's dateDiff, | ||
* returns t1 − t2 expressed as a value in days from one date to the other. | ||
* Only the date parts of the values are used in the calculation. | ||
*/ | ||
class FunctionTiDBDateDiff : public IFunction | ||
{ | ||
public: | ||
static constexpr auto name = "tidbDateDiff"; | ||
static FunctionPtr create(const Context &) { return std::make_shared<FunctionTiDBDateDiff>(); }; | ||
|
||
String getName() const override | ||
{ | ||
return name; | ||
} | ||
|
||
bool isVariadic() const override { return false; } | ||
size_t getNumberOfArguments() const override { return 2; } | ||
|
||
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override | ||
{ | ||
if(!removeNullable(arguments[0]).get()->isDateOrDateTime()) | ||
throw Exception("First argument for function " + getName() + " must be MyDate or MyDateTime", | ||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | ||
|
||
if(!removeNullable(arguments[1]).get()->isDateOrDateTime()) | ||
throw Exception("Second argument for function " + getName() + " must be MyDate or MyDateTime", | ||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | ||
|
||
// to align with tidb, dateDiff with zeroDate input should return null, so always return nullable type | ||
return makeNullable(std::make_shared<DataTypeInt64>()); | ||
} | ||
|
||
bool useDefaultImplementationForNulls() const override { return false; } | ||
bool useDefaultImplementationForConstants() const override { return true; } | ||
|
||
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override | ||
{ | ||
bool has_nullable = false; | ||
bool has_null_constant = false; | ||
for(const auto & arg : arguments) | ||
{ | ||
const auto & elem = block.getByPosition(arg); | ||
has_nullable |= elem.type->isNullable(); | ||
has_null_constant |= elem.type->onlyNull(); | ||
} | ||
|
||
if (has_null_constant) | ||
{ | ||
block.getByPosition(result).column = block.getByPosition(result).type->createColumnConst(block.rows(), Null()); | ||
return; | ||
} | ||
|
||
ColumnPtr x_p = block.getByPosition(arguments[0]).column; | ||
ColumnPtr y_p = block.getByPosition(arguments[1]).column; | ||
if (has_nullable) | ||
{ | ||
Block temporary_block = createBlockWithNestedColumns(block, arguments, result); | ||
x_p = temporary_block.getByPosition(arguments[0]).column; | ||
y_p = temporary_block.getByPosition(arguments[1]).column; | ||
} | ||
|
||
const IColumn & x = *x_p; | ||
const IColumn & y = *y_p; | ||
|
||
size_t rows = block.rows(); | ||
auto res = ColumnInt64::create(rows); | ||
auto result_null_map = ColumnUInt8::create(rows); | ||
|
||
dispatch(x, y, res->getData(), result_null_map->getData()); | ||
|
||
if (block.getByPosition(arguments[0]).type->isNullable() | ||
|| block.getByPosition(arguments[1]).type->isNullable()) | ||
{ | ||
ColumnUInt8::Container &vec_result_null_map = result_null_map->getData(); | ||
ColumnPtr x_p = block.getByPosition(arguments[0]).column; | ||
ColumnPtr y_p = block.getByPosition(arguments[1]).column; | ||
for (size_t i = 0; i < rows; i++) { | ||
vec_result_null_map[i] |= (x_p->isNullAt(i) || y_p->isNullAt(i)); | ||
} | ||
} | ||
block.getByPosition(result).column = ColumnNullable::create(std::move(res), std::move(result_null_map)); | ||
} | ||
private: | ||
void dispatch(const IColumn & x, const IColumn & y, ColumnInt64::Container & res, ColumnUInt8::Container & res_null_map) | ||
{ | ||
auto * x_const = checkAndGetColumnConst<ColumnUInt64>(&x); | ||
auto * y_const = checkAndGetColumnConst<ColumnUInt64>(&y); | ||
if(x_const) | ||
{ | ||
auto * y_vec = checkAndGetColumn<ColumnUInt64>(&y); | ||
constant_vector(x_const->getValue<UInt64>(), *y_vec, res, res_null_map); | ||
} | ||
else if (y_const) | ||
{ | ||
auto * x_vec = checkAndGetColumn<ColumnUInt64>(&x); | ||
vector_constant(*x_vec, y_const->getValue<UInt64>(), res, res_null_map); | ||
} | ||
else | ||
{ | ||
auto * x_vec = checkAndGetColumn<ColumnUInt64>(&x); | ||
auto * y_vec = checkAndGetColumn<ColumnUInt64>(&y); | ||
vector_vector(*x_vec, *y_vec, res, res_null_map); | ||
} | ||
} | ||
|
||
void vector_vector(const ColumnVector<UInt64> & x, const ColumnVector<UInt64> & y, | ||
ColumnInt64::Container & result, ColumnUInt8::Container & result_null_map) | ||
{ | ||
const auto & x_data = x.getData(); | ||
const auto & y_data = y.getData(); | ||
for (size_t i = 0, size = x.size(); i < size; ++i) | ||
{ | ||
result_null_map[i] = (x_data[i] == 0 || y_data[i] == 0); | ||
if (!result_null_map[i]) | ||
result[i] = calculate(x_data[i], y_data[i]); | ||
} | ||
} | ||
|
||
void vector_constant(const ColumnVector<UInt64> & x, UInt64 y, | ||
ColumnInt64::Container & result, ColumnUInt8::Container & result_null_map) | ||
{ | ||
const auto & x_data = x.getData(); | ||
if (y == 0) | ||
{ | ||
for (size_t i = 0, size = x.size(); i < size; ++i) | ||
result_null_map[i] = 1; | ||
} | ||
else | ||
{ | ||
for (size_t i = 0, size = x.size(); i < size; ++i) | ||
{ | ||
result_null_map[i] = (x_data[i] == 0); | ||
if (!result_null_map[i]) | ||
result[i] = calculate(x_data[i], y); | ||
} | ||
} | ||
} | ||
|
||
void constant_vector(UInt64 x, const ColumnVector<UInt64> & y, | ||
ColumnInt64::Container & result, ColumnUInt8::Container & result_null_map) | ||
{ | ||
const auto & y_data = y.getData(); | ||
if (x == 0) | ||
{ | ||
for (size_t i = 0, size = y.size(); i < size; ++i) | ||
result_null_map[i] = 1; | ||
} | ||
else | ||
{ | ||
for (size_t i = 0, size = y.size(); i < size; ++i) { | ||
result_null_map[i] = (y_data[i] == 0); | ||
if (!result_null_map[i]) | ||
result[i] = calculate(x, y_data[i]); | ||
} | ||
} | ||
} | ||
|
||
Int64 calculate(UInt64 x_packed, UInt64 y_packed) | ||
{ | ||
MyDateTime x(x_packed); | ||
MyDateTime y(y_packed); | ||
|
||
Int64 days_x = calcDayNum(x.year, x.month, x.day); | ||
Int64 days_y = calcDayNum(y.year, y.month, y.day); | ||
|
||
return days_y - days_x; | ||
} | ||
}; | ||
|
||
/** dateDiff('unit', t1, t2, [timezone]) | ||
* t1 and t2 can be Date or DateTime | ||
* | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this?
DataTypeDate
,DataTypeDatetime
is not supported yet.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we assume that this
TiDB
Function will never accept thenon-TiDB
datatype?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we won't build
DataTypeDate
orDataTypeDateTime
arguments for this function anyway?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we do not delete
DataTypeDate
andDataTypeDateTime
, I think it's better to throw error explicitly if the function does not supportDataTypeDate
andDataTypeDateTime