Skip to content

Commit c1eec85

Browse files
[feature](variable) enables views, materialized views, generated columns, and alias functions to persist session variables (apache#58031)
### What problem does this PR solve? Problem Summary: When creating views, generated columns, materialized views, or alias functions, session variables that affect query results (e.g., `enable_decimal256`) are not persisted. This causes inconsistent query results when session variables differ between creation and query time. This PR persists session variables marked with `affectQueryResult()` annotation for: - Views - Generated Columns - Materialized Views - Alias Functions When querying these objects, the system automatically uses the persisted session variables from creation time, ensuring consistent results. #### Key Changes - Added `sessionVariables` field to `View`, `Column`, `MTMV`, and `AliasFunction` classes - Created `AutoCloseSessionVariable` utility to temporarily apply persisted variables during query processing - Modified view/column/MV/function parsing logic to use persisted session variables - Added `SessionVarGuardExpr` to protect expressions that depend on session variables --------- Co-authored-by: jacktengg <[email protected]>
1 parent c893dd9 commit c1eec85

File tree

214 files changed

+4629
-1105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+4629
-1105
lines changed

be/src/olap/memtable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ void MemTable::_init_agg_functions(const vectorized::Block* block) {
114114
// the aggregate function manually.
115115
if (_skip_bitmap_col_idx != cid) {
116116
function = vectorized::AggregateFunctionSimpleFactory::instance().get(
117-
"replace_load", {block->get_data_type(cid)},
117+
"replace_load", {block->get_data_type(cid)}, block->get_data_type(cid),
118118
block->get_data_type(cid)->is_nullable(),
119119
BeExecVersionManager::get_newest_version());
120120
} else {
121121
function = vectorized::AggregateFunctionSimpleFactory::instance().get(
122-
"bitmap_intersect", {block->get_data_type(cid)}, false,
123-
BeExecVersionManager::get_newest_version());
122+
"bitmap_intersect", {block->get_data_type(cid)}, block->get_data_type(cid),
123+
false, BeExecVersionManager::get_newest_version());
124124
}
125125
} else {
126126
function = _tablet_schema->column(cid).get_aggregate_function(

be/src/olap/tablet_schema.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,8 @@ vectorized::AggregateFunctionPtr TabletColumn::get_aggregate_function(
776776
std::transform(agg_name.begin(), agg_name.end(), agg_name.begin(),
777777
[](unsigned char c) { return std::tolower(c); });
778778
function = vectorized::AggregateFunctionSimpleFactory::instance().get(
779-
agg_name, {type}, type->is_nullable(), BeExecVersionManager::get_newest_version());
779+
agg_name, {type}, type, type->is_nullable(),
780+
BeExecVersionManager::get_newest_version());
780781
if (!function) {
781782
LOG(WARNING) << "get column aggregate function failed, aggregation_name=" << origin_name
782783
<< ", column_type=" << type->get_name();

be/src/pipeline/exec/analytic_sink_operator.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,15 @@ class AnalyticSinkOperatorX final : public DataSinkOperatorX<AnalyticSinkLocalSt
186186
#ifdef BE_TEST
187187
AnalyticSinkOperatorX(ObjectPool* pool)
188188
: _pool(pool),
189+
_intermediate_tuple_id(0),
190+
_output_tuple_id(0),
189191
_buffered_tuple_id(0),
190192
_is_colocate(false),
191-
_require_bucket_distribution(false) {}
193+
_require_bucket_distribution(false),
194+
_has_window(false),
195+
_has_range_window(false),
196+
_has_window_start(false),
197+
_has_window_end(false) {}
192198
#endif
193199

194200
Status init(const TDataSink& tsink) override {

be/src/runtime/primitive_type.h

Lines changed: 17 additions & 112 deletions
Large diffs are not rendered by default.

be/src/runtime/runtime_state.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ class RuntimeState {
206206
return _query_options.__isset.enable_insert_strict && _query_options.enable_insert_strict;
207207
}
208208

209-
bool enable_decimal256() const {
210-
return _query_options.__isset.enable_decimal256 && _query_options.enable_decimal256;
211-
}
212-
213209
bool enable_common_expr_pushdown() const {
214210
return _query_options.__isset.enable_common_expr_pushdown &&
215211
_query_options.enable_common_expr_pushdown;

be/src/vec/aggregate_functions/aggregate_function.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class IColumn;
4646
class IDataType;
4747

4848
struct AggregateFunctionAttr {
49-
bool enable_decimal256 {false};
5049
bool is_window_function {false};
5150
bool is_foreach {false};
5251
std::vector<std::string> column_names;

be/src/vec/aggregate_functions/aggregate_function_ai_agg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ QueryContext* AggregateFunctionAIAggData::_ctx = nullptr;
2626
void register_aggregate_function_ai_agg(AggregateFunctionSimpleFactory& factory) {
2727
factory.register_function_both("ai_agg",
2828
[](const std::string& name, const DataTypes& argument_types,
29-
const bool result_is_nullable,
29+
const DataTypePtr& result_type, const bool result_is_nullable,
3030
const AggregateFunctionAttr& attr) -> AggregateFunctionPtr {
3131
return creator_without_type::create<AggregateFunctionAIAgg>(
3232
argument_types, result_is_nullable, attr);

be/src/vec/aggregate_functions/aggregate_function_approx_count_distinct.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ namespace doris::vectorized {
2525
#include "common/compile_check_begin.h"
2626

2727
AggregateFunctionPtr create_aggregate_function_approx_count_distinct(
28-
const std::string& name, const DataTypes& argument_types, const bool result_is_nullable,
29-
const AggregateFunctionAttr& attr) {
28+
const std::string& name, const DataTypes& argument_types, const DataTypePtr& result_type,
29+
const bool result_is_nullable, const AggregateFunctionAttr& attr) {
3030
return creator_with_type_list<
3131
TYPE_BOOLEAN, TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, TYPE_LARGEINT,
3232
TYPE_FLOAT, TYPE_DOUBLE, TYPE_DECIMAL32, TYPE_DECIMAL64, TYPE_DECIMAL128I,

be/src/vec/aggregate_functions/aggregate_function_array_agg.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ AggregateFunctionPtr do_create_agg_function_collect(const DataTypes& argument_ty
4242

4343
AggregateFunctionPtr create_aggregate_function_array_agg(const std::string& name,
4444
const DataTypes& argument_types,
45+
const DataTypePtr& result_type,
4546
const bool result_is_nullable,
4647
const AggregateFunctionAttr& attr) {
4748
AggregateFunctionPtr agg_fn;

be/src/vec/aggregate_functions/aggregate_function_avg.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,43 @@
2828
namespace doris::vectorized {
2929
#include "common/compile_check_begin.h"
3030

31+
// TODO: use result type got from FE plan
3132
template <PrimitiveType T>
3233
struct Avg {
33-
using FieldType = typename PrimitiveTypeTraits<T>::AvgNearestFieldType;
34+
static constexpr PrimitiveType ResultPType = T == TYPE_DECIMALV2 ? T : TYPE_DOUBLE;
3435
using Function = AggregateFunctionAvg<
35-
T, AggregateFunctionAvgData<PrimitiveTypeTraits<T>::AvgNearestPrimitiveType>>;
36+
T, ResultPType,
37+
AggregateFunctionAvgData<PrimitiveTypeTraits<T>::AvgNearestPrimitiveType>>;
3638
};
3739

3840
template <PrimitiveType T>
3941
using AggregateFuncAvg = typename Avg<T>::Function;
4042

41-
template <PrimitiveType T>
42-
struct AvgDecimal256 {
43-
using FieldType = typename PrimitiveTypeTraits<T>::AvgNearestFieldType256;
44-
using Function = AggregateFunctionAvg<
45-
T, AggregateFunctionAvgData<PrimitiveTypeTraits<T>::AvgNearestPrimitiveType256>>;
43+
// use result type got from FE plan
44+
template <PrimitiveType InputType, PrimitiveType ResultType>
45+
struct AvgDecimalV3 {
46+
using Function =
47+
AggregateFunctionAvg<InputType, ResultType, AggregateFunctionAvgData<ResultType>>;
4648
};
4749

48-
template <PrimitiveType T>
49-
using AggregateFuncAvgDecimal256 = typename AvgDecimal256<T>::Function;
50+
template <PrimitiveType InputType, PrimitiveType ResultType>
51+
using AggregateFuncAvgDecimalV3 = typename AvgDecimalV3<InputType, ResultType>::Function;
5052

5153
void register_aggregate_function_avg(AggregateFunctionSimpleFactory& factory) {
5254
AggregateFunctionCreator creator = [&](const std::string& name, const DataTypes& types,
55+
const DataTypePtr& result_type,
5356
const bool result_is_nullable,
5457
const AggregateFunctionAttr& attr) {
55-
if (attr.enable_decimal256 && is_decimal(types[0]->get_primitive_type())) {
56-
return creator_with_type_list<
57-
TYPE_DECIMAL32, TYPE_DECIMAL64, TYPE_DECIMAL128I,
58-
TYPE_DECIMAL256>::creator<AggregateFuncAvgDecimal256>(name, types,
59-
result_is_nullable, attr);
58+
if (is_decimalv3(types[0]->get_primitive_type())) {
59+
return creator_with_type_list<TYPE_DECIMAL32, TYPE_DECIMAL64, TYPE_DECIMAL128I,
60+
TYPE_DECIMAL256>::
61+
creator_with_result_type<AggregateFuncAvgDecimalV3>(name, types, result_type,
62+
result_is_nullable, attr);
6063
} else {
6164
return creator_with_type_list<
6265
TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, TYPE_LARGEINT, TYPE_DOUBLE,
63-
TYPE_DECIMAL32, TYPE_DECIMAL64, TYPE_DECIMAL128I,
64-
TYPE_DECIMALV2>::creator<AggregateFuncAvg>(name, types, result_is_nullable,
65-
attr);
66+
TYPE_DECIMALV2>::creator<AggregateFuncAvg>(name, types, result_type,
67+
result_is_nullable, attr);
6668
}
6769
};
6870
factory.register_function_both("avg", creator);

0 commit comments

Comments
 (0)