@@ -70,6 +70,22 @@ std::shared_ptr<arrow::DataType> concreteArrowType(atype::type type)
7070 }
7171}
7272
73+ std::string upcastTo (atype::type f)
74+ {
75+ switch (f) {
76+ case atype::INT32 :
77+ return " castINT" ;
78+ case atype::INT64 :
79+ return " castBIGINT" ;
80+ case atype::FLOAT :
81+ return " castFLOAT4" ;
82+ case atype::DOUBLE :
83+ return " castFLOAT8" ;
84+ default :
85+ throw std::runtime_error (fmt::format (" Do not know how to cast to {}" , f));
86+ }
87+ }
88+
7389bool operator ==(DatumSpec const & lhs, DatumSpec const & rhs)
7490{
7591 return (lhs.datum == rhs.datum ) && (lhs.type == rhs.type );
@@ -385,7 +401,11 @@ gandiva::NodePtr createExpressionTree(Operations const& opSpecs,
385401 if (lookup != fieldNodes.end ()) {
386402 return lookup->second ;
387403 }
388- auto node = gandiva::TreeExprBuilder::MakeField (Schema->GetFieldByName (name));
404+ auto field = Schema->GetFieldByName (name);
405+ if (field == nullptr ) {
406+ throw std::runtime_error (fmt::format (" Cannot find field \" {}\" " , name));
407+ }
408+ auto node = gandiva::TreeExprBuilder::MakeField (field);
389409 fieldNodes.insert ({name, node});
390410 return node;
391411 }
@@ -396,6 +416,15 @@ gandiva::NodePtr createExpressionTree(Operations const& opSpecs,
396416 for (auto it = opSpecs.rbegin (); it != opSpecs.rend (); ++it) {
397417 auto leftNode = datumNode (it->left );
398418 auto rightNode = datumNode (it->right );
419+
420+ auto insertUpcastNode = [&](gandiva::NodePtr node, atype::type t) {
421+ if (t != it->type ) {
422+ auto upcast = gandiva::TreeExprBuilder::MakeFunction (upcastTo (it->type ), {node}, concreteArrowType (it->type ));
423+ node = upcast;
424+ }
425+ return node;
426+ };
427+
399428 switch (it->op ) {
400429 case BasicOp::LogicalOr:
401430 tree = gandiva::TreeExprBuilder::MakeOr ({leftNode, rightNode});
@@ -405,8 +434,13 @@ gandiva::NodePtr createExpressionTree(Operations const& opSpecs,
405434 break ;
406435 default :
407436 if (it->op < BasicOp::Exp) {
437+ if (it->type != atype::BOOL ) {
438+ leftNode = insertUpcastNode (leftNode, it->left .type );
439+ rightNode = insertUpcastNode (rightNode, it->right .type );
440+ }
408441 tree = gandiva::TreeExprBuilder::MakeFunction (binaryOperationsMap[it->op ], {leftNode, rightNode}, concreteArrowType (it->type ));
409442 } else {
443+ leftNode = insertUpcastNode (leftNode, it->left .type );
410444 tree = gandiva::TreeExprBuilder::MakeFunction (binaryOperationsMap[it->op ], {leftNode}, concreteArrowType (it->type ));
411445 }
412446 break ;
0 commit comments