-
Notifications
You must be signed in to change notification settings - Fork 411
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
FLASH-437 Support time zone in coprocessor #259
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a3909dd
do not allow timestamp literal in DAG request
windtalker f7934f5
Merge branch 'cop' of https://github.com/pingcap/tics into time_zone
windtalker 91bc77c
Merge branch 'cop' of https://github.com/pingcap/tics into time_zone
windtalker 4ebb30e
Merge branch 'cop' of https://github.com/pingcap/tics into time_zone
windtalker 4d725f4
refine code
windtalker 73ea83a
fix cop date type encode error
windtalker 155ca15
support tz info in DAG request
windtalker 71a1ada
address comments
windtalker 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
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#include <DataTypes/DataTypeSet.h> | ||
#include <DataTypes/DataTypesNumber.h> | ||
#include <DataTypes/FieldToDataType.h> | ||
#include <Flash/Coprocessor/DAGCodec.h> | ||
#include <Flash/Coprocessor/DAGUtils.h> | ||
#include <Functions/FunctionFactory.h> | ||
#include <Interpreters/Context.h> | ||
|
@@ -44,7 +45,7 @@ static String genFuncString(const String & func_name, const Names & argument_nam | |
return ss.str(); | ||
} | ||
|
||
DAGExpressionAnalyzer::DAGExpressionAnalyzer(const NamesAndTypesList & source_columns_, const Context & context_) | ||
DAGExpressionAnalyzer::DAGExpressionAnalyzer(const std::vector<NameAndTypePair> && source_columns_, const Context & context_) | ||
: source_columns(source_columns_), | ||
context(context_), | ||
after_agg(false), | ||
|
@@ -177,28 +178,114 @@ void DAGExpressionAnalyzer::appendOrderBy(ExpressionActionsChain & chain, const | |
} | ||
} | ||
|
||
const NamesAndTypesList & DAGExpressionAnalyzer::getCurrentInputColumns() { return after_agg ? aggregated_columns : source_columns; } | ||
const std::vector<NameAndTypePair> & DAGExpressionAnalyzer::getCurrentInputColumns() | ||
{ | ||
return after_agg ? aggregated_columns : source_columns; | ||
} | ||
|
||
void DAGExpressionAnalyzer::appendFinalProject(ExpressionActionsChain & chain, const NamesWithAliases & final_project) | ||
{ | ||
initChain(chain, getCurrentInputColumns()); | ||
for (auto name : final_project) | ||
for (const auto & name : final_project) | ||
{ | ||
chain.steps.back().required_output.push_back(name.first); | ||
} | ||
} | ||
|
||
void DAGExpressionAnalyzer::appendAggSelect(ExpressionActionsChain & chain, const tipb::Aggregation & aggregation) | ||
void constructTZExpr(tipb::Expr & tz_expr, const tipb::DAGRequest & rqst, bool from_utc) | ||
{ | ||
if (rqst.has_time_zone_name() && rqst.time_zone_name().length() > 0) | ||
{ | ||
tz_expr.set_tp(tipb::ExprType::String); | ||
tz_expr.set_val(rqst.time_zone_name()); | ||
auto * field_type = tz_expr.mutable_field_type(); | ||
field_type->set_tp(0xfe); | ||
field_type->set_flag(1); | ||
} | ||
else | ||
{ | ||
tz_expr.set_tp(tipb::ExprType::Int64); | ||
std::stringstream ss; | ||
encodeDAGInt64(from_utc ? rqst.time_zone_offset() : -rqst.time_zone_offset(), ss); | ||
tz_expr.set_val(ss.str()); | ||
auto * field_type = tz_expr.mutable_field_type(); | ||
field_type->set_tp(8); | ||
field_type->set_flag(1); | ||
} | ||
} | ||
|
||
bool hasMeaningfullTZInfo(const tipb::DAGRequest & rqst) | ||
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. Meaningful. |
||
{ | ||
if (rqst.has_time_zone_name() && rqst.time_zone_name().length() > 0) | ||
return rqst.time_zone_name() != "UTC"; | ||
if (rqst.has_time_zone_offset()) | ||
return rqst.has_time_zone_offset() != 0; | ||
return false; | ||
} | ||
|
||
String DAGExpressionAnalyzer::appendTimeZoneCast( | ||
const String & tz_col, const String & ts_col, const String & func_name, ExpressionActionsPtr & actions) | ||
{ | ||
Names cast_argument_names; | ||
cast_argument_names.push_back(ts_col); | ||
cast_argument_names.push_back(tz_col); | ||
String cast_expr_name = applyFunction(func_name, cast_argument_names, actions); | ||
return cast_expr_name; | ||
} | ||
|
||
bool DAGExpressionAnalyzer::appendTimeZoneCastAfterTS( | ||
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. May call it |
||
ExpressionActionsChain & chain, std::vector<bool> is_ts_column, const tipb::DAGRequest & rqst) | ||
{ | ||
if (!hasMeaningfullTZInfo(rqst)) | ||
return false; | ||
|
||
bool ret = false; | ||
initChain(chain, getCurrentInputColumns()); | ||
ExpressionActionsPtr actions = chain.getLastActions(); | ||
tipb::Expr tz_expr; | ||
constructTZExpr(tz_expr, rqst, true); | ||
String tz_col; | ||
String func_name | ||
= rqst.has_time_zone_name() && rqst.time_zone_name().length() > 0 ? "ConvertTimeZoneFromUTC" : "ConvertTimeZoneByOffset"; | ||
for (size_t i = 0; i < is_ts_column.size(); i++) | ||
{ | ||
if (is_ts_column[i]) | ||
{ | ||
if (tz_col.length() == 0) | ||
tz_col = getActions(tz_expr, actions); | ||
String casted_name = appendTimeZoneCast(tz_col, source_columns[i].name, func_name, actions); | ||
source_columns.emplace_back(source_columns[i].name, source_columns[i].type); | ||
source_columns[i].name = casted_name; | ||
ret = true; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
void DAGExpressionAnalyzer::appendAggSelect( | ||
ExpressionActionsChain & chain, const tipb::Aggregation & aggregation, const tipb::DAGRequest & rqst) | ||
{ | ||
initChain(chain, getCurrentInputColumns()); | ||
bool need_update_aggregated_columns = false; | ||
NamesAndTypesList updated_aggregated_columns; | ||
ExpressionActionsChain::Step step = chain.steps.back(); | ||
auto agg_col_names = aggregated_columns.getNames(); | ||
bool need_append_timezone_cast = hasMeaningfullTZInfo(rqst); | ||
tipb::Expr tz_expr; | ||
if (need_append_timezone_cast) | ||
constructTZExpr(tz_expr, rqst, false); | ||
String tz_col; | ||
String tz_cast_func_name | ||
= rqst.has_time_zone_name() && rqst.time_zone_name().length() > 0 ? "ConvertTimeZoneToUTC" : "ConvertTimeZoneByOffset"; | ||
for (Int32 i = 0; i < aggregation.agg_func_size(); i++) | ||
{ | ||
String & name = agg_col_names[i]; | ||
String & name = aggregated_columns[i].name; | ||
String updated_name = appendCastIfNeeded(aggregation.agg_func(i), step.actions, name); | ||
if (need_append_timezone_cast && aggregation.agg_func(i).field_type().tp() == TiDB::TypeTimestamp) | ||
{ | ||
if (tz_col.length() == 0) | ||
tz_col = getActions(tz_expr, step.actions); | ||
updated_name = appendTimeZoneCast(tz_col, updated_name, tz_cast_func_name, step.actions); | ||
} | ||
if (name != updated_name) | ||
{ | ||
need_update_aggregated_columns = true; | ||
|
@@ -208,14 +295,20 @@ void DAGExpressionAnalyzer::appendAggSelect(ExpressionActionsChain & chain, cons | |
} | ||
else | ||
{ | ||
updated_aggregated_columns.emplace_back(name, aggregated_columns.getTypes()[i]); | ||
updated_aggregated_columns.emplace_back(name, aggregated_columns[i].type); | ||
step.required_output.push_back(name); | ||
} | ||
} | ||
for (Int32 i = 0; i < aggregation.group_by_size(); i++) | ||
{ | ||
String & name = agg_col_names[i + aggregation.agg_func_size()]; | ||
String & name = aggregated_columns[i + aggregation.agg_func_size()].name; | ||
String updated_name = appendCastIfNeeded(aggregation.group_by(i), step.actions, name); | ||
if (need_append_timezone_cast && aggregation.group_by(i).field_type().tp() == TiDB::TypeTimestamp) | ||
{ | ||
if (tz_col.length() == 0) | ||
tz_col = getActions(tz_expr, step.actions); | ||
updated_name = appendTimeZoneCast(tz_col, updated_name, tz_cast_func_name, step.actions); | ||
} | ||
if (name != updated_name) | ||
{ | ||
need_update_aggregated_columns = true; | ||
|
@@ -225,7 +318,7 @@ void DAGExpressionAnalyzer::appendAggSelect(ExpressionActionsChain & chain, cons | |
} | ||
else | ||
{ | ||
updated_aggregated_columns.emplace_back(name, aggregated_columns.getTypes()[i]); | ||
updated_aggregated_columns.emplace_back(name, aggregated_columns[i].type); | ||
step.required_output.push_back(name); | ||
} | ||
} | ||
|
@@ -263,7 +356,6 @@ String DAGExpressionAnalyzer::appendCastIfNeeded(const tipb::Expr & expr, Expres | |
// first construct the second argument | ||
tipb::Expr type_expr; | ||
type_expr.set_tp(tipb::ExprType::String); | ||
std::stringstream ss; | ||
type_expr.set_val(expected_type->getName()); | ||
auto * type_field_type = type_expr.mutable_field_type(); | ||
type_field_type->set_tp(0xfe); | ||
|
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
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.
Are there symbols representing these magic numbers in TiDB.h?