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

Function buildJoinQuery

src/optimizer/foreign_join_push_down_optimizer.cpp:354–467  ·  view source on GitHub ↗

Build the SQL join query string and collect column names for result mapping. Keep result column names SQL-safe while using display names that preserve the user's labels.

Source from the content-addressed store, hash-verified

352// Build the SQL join query string and collect column names for result mapping.
353// Keep result column names SQL-safe while using display names that preserve the user's labels.
354static JoinQueryInfo buildJoinQuery(const ForeignJoinPatternInfo& info,
355 const expression_vector& outputColumns, main::ClientContext* context) {
356 auto extend = info.extend;
357 auto srcNode = extend->getBoundNode();
358 auto dstNode = extend->getNbrNode();
359 auto rel = extend->getRel();
360
361 // Get raw variable names (user-facing, like 'a', 'b', 'c')
362 std::string srcAlias = srcNode->getVariableName();
363 std::string dstAlias = dstNode->getVariableName();
364 std::string relAlias = rel->getVariableName();
365
366 // Determine join columns based on direction and foreign table schema
367 std::string srcJoinCol, dstJoinCol;
368 auto tableColumnNames =
369 getForeignTableColumnNames(info.dbName, getUnqualifiedTableName(info.relTable), context);
370 if (tableColumnNames.size() < 2) {
371 throw RuntimeException(std::format(
372 "Foreign join push down optimizer: unable to retrieve column names for table '{}.{}', "
373 "got {} columns but need at least 2 for join",
374 info.dbName, info.relTable, tableColumnNames.size()));
375 }
376
377 std::string firstCol = tableColumnNames[0];
378 std::string secondCol = tableColumnNames[1];
379 if (extend->getDirection() == ExtendDirection::FWD) {
380 srcJoinCol = firstCol;
381 dstJoinCol = secondCol;
382 } else {
383 srcJoinCol = secondCol;
384 dstJoinCol = firstCol;
385 }
386
387 auto getNodeIDColumn = [&](const std::string& tableName) {
388 auto columnNames =
389 getForeignTableColumnNames(info.dbName, getUnqualifiedTableName(tableName), context);
390 if (columnNames.empty()) {
391 return std::string{InternalKeyword::ID};
392 }
393 return columnNames[0];
394 };
395 auto srcIDCol = getNodeIDColumn(info.srcTable);
396 auto dstIDCol = getNodeIDColumn(info.dstTable);
397
398 // Build SELECT items from output columns and collect column names
399 std::vector<std::string> columnNames;
400 std::vector<std::string> displayNames;
401
402 for (auto& col : outputColumns) {
403 std::string colExpr;
404 std::string colName;
405 std::string displayName;
406
407 // Determine which table the column comes from based on variable name
408 if (col->expressionType == ExpressionType::PROPERTY) {
409 auto& prop = col->constCast<PropertyExpression>();
410 // Use raw variable name for SQL query (e.g., 'a' instead of '_0_a')
411 auto rawVarName = prop.getRawVariableName();

Callers 1

visitHashJoinReplaceMethod · 0.85

Calls 14

getUnqualifiedTableNameFunction · 0.85
sanitizeSQLAliasFunction · 0.85
getNbrNodeMethod · 0.80
getBoundNodeMethod · 0.45
getRelMethod · 0.45
getVariableNameMethod · 0.45
sizeMethod · 0.45
getDirectionMethod · 0.45
emptyMethod · 0.45
getPropertyNameMethod · 0.45
getUniqueNameMethod · 0.45

Tested by

no test coverage detected