MCPcopy Create free account
hub / github.com/LadybugDB/ladybug / compatible

Function compatible

src/binder/expression/expression_util.cpp:289–328  ·  view source on GitHub ↗

For primitive types, two types are compatible if they have the same id. For nested types, two types are compatible if they have the same id and their children are also compatible. E.g. [NULL] is compatible with [1,2] E.g. {a: NULL, b: NULL} is compatible with {a: [1,2], b: ['c']}

Source from the content-addressed store, hash-verified

287// E.g. [NULL] is compatible with [1,2]
288// E.g. {a: NULL, b: NULL} is compatible with {a: [1,2], b: ['c']}
289static bool compatible(const LogicalType& type, const LogicalType& target) {
290 if (type.isInternalType() != target.isInternalType()) {
291 return false;
292 }
293 if (type.getLogicalTypeID() == LogicalTypeID::ANY) {
294 return true;
295 }
296 if (type.getLogicalTypeID() != target.getLogicalTypeID()) {
297 return false;
298 }
299 switch (type.getLogicalTypeID()) {
300 case LogicalTypeID::LIST: {
301 return compatible(ListType::getChildType(type), ListType::getChildType(target));
302 }
303 case LogicalTypeID::ARRAY: {
304 return compatible(ArrayType::getChildType(type), ArrayType::getChildType(target));
305 }
306 case LogicalTypeID::STRUCT: {
307 if (StructType::getNumFields(type) != StructType::getNumFields(target)) {
308 return false;
309 }
310 for (auto i = 0u; i < StructType::getNumFields(type); ++i) {
311 if (!compatible(StructType::getField(type, i).getType(),
312 StructType::getField(target, i).getType())) {
313 return false;
314 }
315 }
316 return true;
317 }
318 case LogicalTypeID::DECIMAL:
319 case LogicalTypeID::UNION:
320 case LogicalTypeID::MAP:
321 case LogicalTypeID::NODE:
322 case LogicalTypeID::REL:
323 case LogicalTypeID::RECURSIVE_REL:
324 return false;
325 default:
326 return true;
327 }
328}
329
330// Handle special cases where value can be compatible to a type. This happens when a value is a
331// nested value but does not have any child.

Callers 2

tryCombineDataTypeMethod · 0.85
canCastStaticallyMethod · 0.85

Calls 8

getChildTypeFunction · 0.85
getKeyTypeFunction · 0.85
isInternalTypeMethod · 0.80
getLogicalTypeIDMethod · 0.80
getDataTypeMethod · 0.80
hasNoneNullChildrenMethod · 0.80
getTypeMethod · 0.45
isNullMethod · 0.45

Tested by

no test coverage detected