@@ -114,13 +114,51 @@ VeloxToSubstraitExprConvertor::toSubstraitExpr(
114114 ::substrait::Expression_FieldReference* substraitFieldExpr =
115115 google::protobuf::Arena::CreateMessage<
116116 ::substrait::Expression_FieldReference>(&arena);
117-
118- std::string exprName = fieldExpr->name ();
119-
120117 ::substrait::Expression_ReferenceSegment_StructField* directStruct =
121118 substraitFieldExpr->mutable_direct_reference ()->mutable_struct_field ();
119+ std::string exprName = fieldExpr->name ();
120+ // FieldAccessTypedExpr represents one of two things: a leaf in an expression
121+ // or a dereference expression(fieldExpr->isInputColumn() == false)
122+ // for a leaf in an expression, find idx from child by exprName.
123+ // for a dereference expression, find idx from every child by exprName.
124+ if (fieldExpr->isInputColumn ()) {
125+ auto idx = inputType->getChildIdxIfExists (exprName);
126+ if (idx.has_value ()) {
127+ directStruct->set_field (idx.value ());
128+ } else {
129+ VELOX_USER_FAIL (" idx has_value return false." );
130+ }
131+ } else {
132+ int matchCount = 0 ;
133+ uint32_t idxOfExprName = -1 ;
134+ for (auto child : inputType->children ()) {
135+ auto rowChild = asRowType (child);
136+ if (!rowChild) {
137+ continue ;
138+ }
139+ auto idxInChild = rowChild->getChildIdxIfExists (exprName);
140+ if (idxInChild.has_value ()) {
141+ matchCount++;
142+ idxOfExprName = idxInChild.value ();
143+ }
144+ }
145+ if (matchCount == 0 ) {
146+ VELOX_USER_FAIL (" exprName :{} no name matched!" , exprName);
147+ }
148+ if (matchCount > 1 ) {
149+ VELOX_USER_FAIL (" exprName :{} multiple names matched!" , exprName);
150+ }
151+
152+ ::substrait::Expression_ReferenceSegment* refSegment =
153+ google::protobuf::Arena::CreateMessage<
154+ ::substrait::Expression_ReferenceSegment>(&arena);
155+ ::substrait::Expression_ReferenceSegment_StructField* childStruct =
156+ refSegment->mutable_struct_field ();
157+ childStruct->set_field (idxOfExprName);
158+ refSegment->set_allocated_struct_field (childStruct);
159+ directStruct->set_allocated_child (refSegment);
160+ }
122161
123- directStruct->set_field (inputType->getChildIdx (exprName));
124162 return *substraitFieldExpr;
125163}
126164
@@ -235,20 +273,20 @@ VeloxToSubstraitExprConvertor::toSubstraitNotNullLiteral(
235273 google::protobuf::Arena::CreateMessage<::substrait::Expression_Literal>(
236274 &arena);
237275 switch (variantValue.kind ()) {
238- case velox::TypeKind::DOUBLE: {
239- literalExpr->set_fp64 (variantValue.value <TypeKind::DOUBLE>());
240- break ;
241- }
242- case velox::TypeKind::BIGINT: {
243- literalExpr->set_i64 (variantValue.value <TypeKind::BIGINT>());
276+ case velox::TypeKind::BOOLEAN: {
277+ literalExpr->set_boolean (variantValue.value <TypeKind::BOOLEAN>());
244278 break ;
245279 }
246280 case velox::TypeKind::INTEGER: {
247281 literalExpr->set_i32 (variantValue.value <TypeKind::INTEGER>());
248282 break ;
249283 }
250- case velox::TypeKind::BOOLEAN: {
251- literalExpr->set_boolean (variantValue.value <TypeKind::BOOLEAN>());
284+ case velox::TypeKind::BIGINT: {
285+ literalExpr->set_i64 (variantValue.value <TypeKind::BIGINT>());
286+ break ;
287+ }
288+ case velox::TypeKind::DOUBLE: {
289+ literalExpr->set_fp64 (variantValue.value <TypeKind::DOUBLE>());
252290 break ;
253291 }
254292 default :
0 commit comments