Skip to content

Conversation

@stephenkgu
Copy link
Contributor

Description

Issue(s)

  • Close/close/Fix/fix/Resolve/resolve: Issue Link

Checklist

Please check the items in the checklist if applicable.

  • Is the user manual updated?
  • Are the test cases passed and automated?
  • Is there no significant decrease in test coverage?
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @stephenkgu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly extends the system's capabilities by introducing comprehensive support for subqueries within stream calculation plans. It involves fundamental changes to how query plans are structured and processed, from their definition in header files to their execution by runners. The changes ensure that subqueries can be correctly parsed, translated, serialized, and integrated into the overall stream processing workflow, enabling more advanced and flexible stream analytics.

Highlights

  • Subquery Support in Stream Plans: The SSubplan structure now includes a pSubQ field, a SNodeList*, to store references to subqueries' subplans, enabling more complex stream calculations.
  • Enhanced Runner Task Building: The msmBuildRunnerTasksImpl function has been updated to distinguish between main query plans and subquery plans. It now creates SDownstreamSourceNodes for subquery results and manages their endpoints, ensuring proper data flow from subquery execution units.
  • Stream Reader Task Integration: A new function, msmUpdateCalcReaderTasks, was introduced to update stream reader tasks. It processes existing reader tasks, parses their calculation scan plans, and integrates the collected subquery endpoint information into them.
  • Executor Task Management for Subqueries: The qCreateStreamExecTask function in the executor has been modified to recognize and handle subquery endpoints. It now creates execution tasks that are aware of these subquery dependencies and can manage their lifecycle.
  • Serialization and Deserialization of Subquery Plans: JSON serialization and deserialization logic has been added for the new pSubQ field within SSubplan, allowing subquery plan structures to be persistently stored and transmitted across the system.
  • Parser Updates for CREATE STREAM: The parser now supports subqueries within CREATE STREAM statements. New functions, checkSubQueryStmt and transferSubQueries, were added and integrated into the stream calculation plan building process to correctly translate and manage subquery contexts.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for subqueries in stream calculations. The changes span across the parser, planner, and execution layers. A new field pSubQ is added to SSubplan to hold subquery information, and logic is added to build and manage runner tasks for these subqueries.

The overall approach is sound, but there are several areas for improvement:

  • There are critical risks of NULL pointer dereferences due to missing checks on plan structures.
  • The code contains some inefficiencies, such as repeated parsing of plan strings.
  • There are also minor issues like commented-out code and an incorrect type cast that should be addressed.

Please review the detailed comments for specific suggestions.

SNodeList* pSubEP = NULL;

TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
pSubQPlan = (SQueryPlan*)pPlan->pChildren->pHead->pNode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This line accesses pPlan->pChildren->pHead->pNode without checking if pPlan->pChildren or pPlan->pChildren->pHead are NULL. This could lead to a NULL pointer dereference and a crash. Please add checks to handle this case gracefully.

  if (pPlan->pChildren == NULL || pPlan->pChildren->pHead == NULL || pPlan->pChildren->pHead->pNode == NULL) {
    mstsError("stream:%s invalid calc plan, no subquery plan found", pStream->pCreate->name);
    code = TSDB_CODE_INVALID_SQL;
    goto _exit;
  }
  pSubQPlan = (SQueryPlan*)pPlan->pChildren->pHead->pNode;
}

// TODO: for each subquery plan do the following plan xform
SQueryPlan* calcSubQPlan = ((SQueryPlan*)calcPlan ->pChildren ->pHead ->pNode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This line accesses calcPlan->pChildren->pHead->pNode without any checks for NULL pointers. This is unsafe and can lead to a crash if pChildren or pHead is NULL. Please add validation to ensure these pointers are valid before dereferencing them.

  if (calcPlan->pChildren == NULL || calcPlan->pChildren->pHead == NULL || calcPlan->pChildren->pHead->pNode == NULL) {
    PAR_ERR_JRET(TSDB_CODE_INVALID_SQL);
  }
  SQueryPlan* calcSubQPlan = ((SQueryPlan*)calcPlan ->pChildren ->pHead ->pNode);
Comment on lines +2067 to 2072
code = msmBuildRunnerTasksImpl(pCtx, pSubQPlan, pInfo, pStream, pPlan, &pSubEP);
pPlan = NULL;
TAOS_CHECK_EXIT(code);
TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream, pPlan, &pSubEP);
pPlan = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The calculation plan is parsed from a string (nodesStringToNode) multiple times within this function and also within the called function msmBuildRunnerTasksImpl. This is inefficient. Consider parsing it once at the beginning, and if the plan needs to be modified by a function, pass a clone of the plan instead of destroying and re-parsing it. This would improve performance and make the data flow clearer.

SDownstreamSourceNode* pSource = NULL;

for (int32_t i = 0; i < subTaskNum; ++i) {
SValueNode* pVal = (SValueNode*)nodesListGetNode(pSubplan->pSubQ, i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The node from pSubplan->pSubQ is cast to SValueNode*, but based on how it's created in mndStreamMgmt.c, it should be a SDownstreamSourceNode*. This cast is misleading and potentially incorrect. Please use the correct type, SDownstreamSourceNode*, or a generic SNode* to avoid confusion and potential bugs.

    SNode* pVal = (SNode*)nodesListGetNode(pSubplan->pSubQ, i);
Comment on lines +1759 to +1760
// SQueryPlan* calcSubQPlan = ((SQueryPlan*)pDag->pChildren->pHead->pNode);
// SNodeList* subQSubplans = calcSubQPlan->pSubplans;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These lines are commented out. If this code is no longer needed, it should be removed to improve code clarity.

Comment on lines +1816 to +1843
if (subQ) {
SStmTaskSrcAddr addr = {0};
SDownstreamSourceNode* pSource = NULL;

TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));

addr.taskId = pDeploy->task.taskId;
addr.vgId = pDeploy->task.nodeId;
addr.groupId = plan->id.groupId;
addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);

pSource->addr.epSet = addr.epset;
pSource->addr.nodeId = addr.vgId;

pSource->clientId = streamId;
pSource->taskId = pDeploy->task.taskId;
pSource->sId = 0;
pSource->execId = 0;
pSource->fetchMsgType = TDMT_STREAM_FETCH_FROM_RUNNER;
pSource->localExec = false;

code = nodesListMakeStrictAppend(subEP, (SNode *)pSource);
if (code) {
TAOS_CHECK_EXIT(code);
}
} else {
TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The indentation in this if/else block seems to be inconsistent with the surrounding code, possibly due to mixed tabs and spaces. Please fix the indentation to improve readability.

static int32_t msmUpdateCalcReaderTasks(SStreamObj* pStream, SNodeList* pSubEP) {
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
int64_t streamId = pStream->pCreate->streamId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The variable streamId is initialized but never used in this function. It should be removed if it's not needed.

Comment on lines +2031 to +2033
// string to node
// for each subplan, adjoin subEP
// node to the string of calcScanPlan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These commented-out lines appear to be notes or old code. They should be removed to keep the codebase clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants