Skip to content

Commit 43cf45b

Browse files
zhanghaohitShouren
authored andcommitted
add gcformat_index udf for gcformat feature sign (#4020)
* support gcformat index * no space if no gcformat_index provided
1 parent e0e70a4 commit 43cf45b

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

hybridse/src/udf/default_defs/feature_signature_def.cc

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ enum FeatureSignatureType {
2929
kFeatureSignatureBinaryLabel = 200,
3030
kFeatureSignatureMulticlassLabel = 201,
3131
kFeatureSignatureRegressionLabel = 202,
32+
kFeatureSignatureIndex = 300,
3233
};
3334

3435
template <typename T>
@@ -115,6 +116,17 @@ struct MulticlassLabel {
115116
}
116117
};
117118

119+
template <typename T>
120+
struct GCFormatIndex {
121+
using Args = Tuple<int32_t, Nullable<T>>(Nullable<T>);
122+
void operator()(T v, bool is_null, int32_t* feature_signature, T* ret, bool* null_flag) {
123+
*feature_signature = kFeatureSignatureIndex;
124+
*null_flag = is_null;
125+
if (!is_null) {
126+
*ret = v;
127+
}
128+
}
129+
};
118130

119131
template <typename T>
120132
struct RegressionLabel {
@@ -246,6 +258,12 @@ struct GCFormat {
246258
}
247259
break;
248260
}
261+
case kFeatureSignatureIndex: {
262+
if (!is_null) {
263+
instance_index = input;
264+
}
265+
break;
266+
}
249267
default: {
250268
++slot_number;
251269
break;
@@ -258,12 +276,18 @@ struct GCFormat {
258276
}
259277

260278
std::string Output() {
261-
return instance_label + " | " + instance_feature;
279+
std::string instance_index_str = "";
280+
if (instance_index >= 0) {
281+
instance_index_str = " " + std::to_string(instance_index);
282+
}
283+
return instance_label + instance_index_str + "| " +
284+
instance_feature;
262285
}
263286

264287
size_t slot_number = 1;
265288
std::string instance_label;
266289
std::string instance_feature;
290+
int64_t instance_index = -1;
267291
};
268292

269293
struct CSV {
@@ -473,6 +497,19 @@ void DefaultUdfLibrary::InitFeatureSignature() {
473497
)")
474498
.args_in<bool, int16_t, int32_t, int64_t>();
475499

500+
RegisterExternalTemplate<v1::GCFormatIndex>("gcformat_index")
501+
.doc(R"(
502+
@brief Set the index/lineno of gcformat output.
503+
Example:
504+
@code{.sql}
505+
select gcformat(gcformat_index(6));
506+
-- output 6
507+
@endcode
508+
509+
@since 0.9.3
510+
)")
511+
.args_in<int16_t, int32_t, int64_t>();
512+
476513
RegisterExternalTemplate<v1::RegressionLabel>("regression_label")
477514
.doc(R"(
478515
@brief Set the column signature to regression label.

0 commit comments

Comments
 (0)