Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:[TS-4897] virtual table #29240

Open
wants to merge 2 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/common/taosdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ typedef enum {
TSDB_SYSTEM_TABLE = 5,
TSDB_TSMA_TABLE = 6, // time-range-wise sma
TSDB_VIEW_TABLE = 7,
TSDB_TABLE_MAX = 8
TSDB_VIRTUAL_TABLE = 8,
TSDB_VIRTUAL_CHILD_TABLE = 9,
TSDB_TABLE_MAX = 10
} ETableType;

typedef enum {
Expand Down
1 change: 1 addition & 0 deletions include/common/tcol.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ uint8_t columnEncodeVal(const char* encode);
uint16_t columnCompressVal(const char* compress);

bool useCompress(uint8_t tableType);
bool hasRefCol(uint8_t tableType);
bool checkColumnEncode(char encode[TSDB_CL_COMPRESS_OPTION_LEN]);
bool checkColumnEncodeOrSetDefault(uint8_t type, char encode[TSDB_CL_COMPRESS_OPTION_LEN]);
bool checkColumnCompress(char compress[TSDB_CL_COMPRESS_OPTION_LEN]);
Expand Down
90 changes: 83 additions & 7 deletions include/common/tmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ typedef enum _mgmt_table {
#define TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS 13
#define TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION 14
#define TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL 15
#define TSDB_ALTER_TABLE_ALTER_COLUMN_REF 16
#define TSDB_ALTER_TABLE_REMOVE_COLUMN_REF 17
#define TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF 18

#define TSDB_FILL_NONE 0
#define TSDB_FILL_NULL 1
Expand Down Expand Up @@ -222,9 +225,11 @@ typedef enum _mgmt_table {
#define TSDB_COL_IS_UD_COL(f) ((f & (~(TSDB_COL_NULL))) == TSDB_COL_UDC)
#define TSDB_COL_REQ_NULL(f) (((f)&TSDB_COL_NULL) != 0)

#define TD_SUPER_TABLE TSDB_SUPER_TABLE
#define TD_CHILD_TABLE TSDB_CHILD_TABLE
#define TD_NORMAL_TABLE TSDB_NORMAL_TABLE
#define TD_SUPER_TABLE TSDB_SUPER_TABLE
#define TD_CHILD_TABLE TSDB_CHILD_TABLE
#define TD_NORMAL_TABLE TSDB_NORMAL_TABLE
#define TD_VIRTUAL_TABLE TSDB_VIRTUAL_TABLE
#define TD_VIRTUAL_CHILD_TABLE TSDB_VIRTUAL_CHILD_TABLE

typedef enum ENodeType {
// Syntax nodes are used in parser and planner module, and some are also used in executor module, such as COLUMN,
Expand Down Expand Up @@ -269,6 +274,7 @@ typedef enum ENodeType {
QUERY_NODE_TSMA_OPTIONS,
QUERY_NODE_ANOMALY_WINDOW,
QUERY_NODE_RANGE_AROUND,
QUERY_NODE_VIRTUAL_TABLE,

// Statement nodes are used in parser and planner module.
QUERY_NODE_SET_OPERATOR = 100,
Expand Down Expand Up @@ -326,6 +332,13 @@ typedef enum ENodeType {
QUERY_NODE_REVOKE_STMT,
QUERY_NODE_ALTER_CLUSTER_STMT,
QUERY_NODE_S3MIGRATE_DATABASE_STMT,
QUERY_NODE_CREATE_TSMA_STMT,
QUERY_NODE_DROP_TSMA_STMT,
QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT,
QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT,
QUERY_NODE_DROP_VIRTUAL_TABLE_STMT,
QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT,

// placeholder for [154, 180]
QUERY_NODE_SHOW_CREATE_VIEW_STMT = 181,
QUERY_NODE_SHOW_CREATE_DATABASE_STMT,
Expand Down Expand Up @@ -358,6 +371,8 @@ typedef enum ENodeType {
QUERY_NODE_CREATE_ANODE_STMT,
QUERY_NODE_DROP_ANODE_STMT,
QUERY_NODE_UPDATE_ANODE_STMT,
QUERY_NODE_SHOW_CREATE_TSMA_STMT,
QUERY_NODE_SHOW_CREATE_VTABLE_STMT,

// show statement nodes
// see 'sysTableShowAdapter', 'SYSTABLE_SHOW_TYPE_OFFSET'
Expand Down Expand Up @@ -402,11 +417,9 @@ typedef enum ENodeType {
QUERY_NODE_SHOW_ANODES_STMT,
QUERY_NODE_SHOW_ANODES_FULL_STMT,
QUERY_NODE_SHOW_USAGE_STMT,
QUERY_NODE_CREATE_TSMA_STMT,
QUERY_NODE_SHOW_CREATE_TSMA_STMT,
QUERY_NODE_DROP_TSMA_STMT,
QUERY_NODE_SHOW_FILESETS_STMT,
QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT,
QUERY_NODE_SHOW_VTABLES_STMT,

// logic plan node
QUERY_NODE_LOGIC_PLAN_SCAN = 1000,
Expand All @@ -427,6 +440,7 @@ typedef enum ENodeType {
QUERY_NODE_LOGIC_PLAN_GROUP_CACHE,
QUERY_NODE_LOGIC_PLAN_DYN_QUERY_CTRL,
QUERY_NODE_LOGIC_PLAN_FORECAST_FUNC,
QUERY_NODE_LOGIC_PLAN_VIRTUAL_TABLE_SCAN,

// physical plan node
QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN = 1100,
Expand Down Expand Up @@ -482,6 +496,7 @@ typedef enum ENodeType {
QUERY_NODE_PHYSICAL_PLAN_STREAM_ANOMALY,
QUERY_NODE_PHYSICAL_PLAN_FORECAST_FUNC,
QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERP_FUNC,
QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN,
} ENodeType;

typedef struct {
Expand Down Expand Up @@ -583,6 +598,19 @@ STSRow* tGetSubmitBlkNext(SSubmitBlkIter* pIter);
// for debug
int32_t tPrintFixedSchemaSubmitReq(SSubmitReq* pReq, STSchema* pSchema);

typedef struct {
bool hasRef;
col_id_t id;
char refTableName[TSDB_TABLE_NAME_LEN];
char refColName[TSDB_COL_NAME_LEN];
} SColRef;

typedef struct {
int32_t nCols;
int32_t version;
SColRef* pColRef;
} SColRefWrapper;

struct SSchema {
int8_t type;
int8_t flags;
Expand Down Expand Up @@ -623,6 +651,7 @@ typedef struct {
int8_t sysInfo;
SSchema* pSchemas;
SSchemaExt* pSchemaExt;
SColRef* pColRefs;
} STableMetaRsp;

typedef struct {
Expand Down Expand Up @@ -707,6 +736,22 @@ typedef struct {
SColCmpr* pColCmpr;
} SColCmprWrapper;


static FORCE_INLINE int32_t tInitDefaultSColRefWrapperByCols(SColRefWrapper* pRef, int32_t nCols) {
if (pRef->pColRef) {
return TSDB_CODE_INVALID_PARA;
}
pRef->pColRef = (SColRef*)taosMemoryCalloc(nCols, sizeof(SColRef));
if (pRef->pColRef == NULL) {
return terrno;
}
pRef->nCols = nCols;
for (int32_t i = 0; i < nCols; i++) {
pRef->pColRef[i].hasRef = false;
}
return 0;
}

static FORCE_INLINE SColCmprWrapper* tCloneSColCmprWrapper(const SColCmprWrapper* pSrcWrapper) {
if (pSrcWrapper->pColCmpr == NULL || pSrcWrapper->nCols == 0) {
terrno = TSDB_CODE_INVALID_PARA;
Expand Down Expand Up @@ -849,6 +894,27 @@ static FORCE_INLINE int32_t tDecodeSSchemaExt(SDecoder* pDecoder, SSchemaExt* pS
return 0;
}

static FORCE_INLINE int32_t tEncodeSColRef(SEncoder* pEncoder, const SColRef* pColRef) {
TAOS_CHECK_RETURN(tEncodeI8(pEncoder, pColRef->hasRef));
if (pColRef->hasRef) {
TAOS_CHECK_RETURN(tEncodeI16(pEncoder, pColRef->id));
TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pColRef->refTableName));
TAOS_CHECK_RETURN(tEncodeCStr(pEncoder, pColRef->refColName));
}
return 0;
}

static FORCE_INLINE int32_t tDecodeSColRef(SDecoder* pDecoder, SColRef* pColRef) {
TAOS_CHECK_RETURN(tDecodeI8(pDecoder, (int8_t*)&pColRef->hasRef));
if (pColRef->hasRef) {
TAOS_CHECK_RETURN(tDecodeI16(pDecoder, &pColRef->id));
TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, pColRef->refTableName));
TAOS_CHECK_RETURN(tDecodeCStrTo(pDecoder, pColRef->refColName));
}

return 0;
}

static FORCE_INLINE int32_t taosEncodeSSchemaWrapper(void** buf, const SSchemaWrapper* pSW) {
int32_t tlen = 0;
tlen += taosEncodeVariantI32(buf, pSW->nCols);
Expand Down Expand Up @@ -945,6 +1011,7 @@ typedef struct {
int64_t deleteMark2;
int32_t sqlLen;
char* sql;
int8_t virtualStb;
} SMCreateStbReq;

int32_t tSerializeSMCreateStbReq(void* buf, int32_t bufLen, SMCreateStbReq* pReq);
Expand Down Expand Up @@ -1299,6 +1366,8 @@ typedef struct {
int32_t tagsLen;
char* pTags;
SSchemaExt* pSchemaExt;
int8_t virtualStb;
SColRef* pColRefs;
} STableCfg;

typedef STableCfg STableCfgRsp;
Expand Down Expand Up @@ -3227,6 +3296,7 @@ typedef struct SVCreateTbReq {
int32_t sqlLen;
char* sql;
SColCmprWrapper colCmpr;
SColRefWrapper colRef; // col reference for virtual table
} SVCreateTbReq;

int tEncodeSVCreateTbReq(SEncoder* pCoder, const SVCreateTbReq* pReq);
Expand All @@ -3250,6 +3320,7 @@ static FORCE_INLINE void tdDestroySVCreateTbReq(SVCreateTbReq* req) {
taosMemoryFreeClear(req->ntb.schemaRow.pSchema);
}
taosMemoryFreeClear(req->colCmpr.pColCmpr);
taosMemoryFreeClear(req->colRef.pColRef);
}

typedef struct {
Expand Down Expand Up @@ -3297,6 +3368,7 @@ typedef struct {
uint64_t suid; // for tmq in wal format
int64_t uid;
int8_t igNotExists;
int8_t isVirtual;
} SVDropTbReq;

typedef struct {
Expand Down Expand Up @@ -3336,7 +3408,7 @@ typedef struct SMultiTagUpateVal {
int8_t isNull;
SArray* pTagArray;
} SMultiTagUpateVal;
typedef struct {
typedef struct SVAlterTbReq{
char* tbName;
int8_t action;
char* colName;
Expand Down Expand Up @@ -3366,6 +3438,10 @@ typedef struct {
int8_t source; // TD_REQ_FROM_TAOX-taosX or TD_REQ_FROM_APP-taosClient
uint32_t compress; // TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS
SArray* pMultiTag; // TSDB_ALTER_TABLE_ADD_MULTI_TAGS
// TSDB_ALTER_TABLE_ALTER_COLUMN_REF
char* refTbName;
char* refColName;
// TSDB_ALTER_TABLE_REMOVE_COLUMN_REF
} SVAlterTbReq;

int32_t tEncodeSVAlterTbReq(SEncoder* pEncoder, const SVAlterTbReq* pReq);
Expand Down
1 change: 1 addition & 0 deletions include/libs/executor/storageapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ typedef struct SMetaEntry {
uint8_t* pBuf;

SColCmprWrapper colCmpr; // col compress alg
SColRefWrapper colRef; // col reference for virtual table
} SMetaEntry;

typedef struct SMetaReader {
Expand Down
38 changes: 38 additions & 0 deletions include/libs/nodes/cmdnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ extern "C" {

#define DESCRIBE_RESULT_COLS 4
#define DESCRIBE_RESULT_COLS_COMPRESS 7
#define DESCRIBE_RESULT_COLS_REF 5
#define DESCRIBE_RESULT_FIELD_LEN (TSDB_COL_NAME_LEN - 1 + VARSTR_HEADER_SIZE)
#define DESCRIBE_RESULT_TYPE_LEN (20 + VARSTR_HEADER_SIZE)
#define DESCRIBE_RESULT_NOTE_LEN (16 + VARSTR_HEADER_SIZE)
#define DESCRIBE_RESULT_COPRESS_OPTION_LEN (TSDB_CL_COMPRESS_OPTION_LEN + VARSTR_HEADER_SIZE)
#define DESCRIBE_RESULT_COL_REF_LEN (TSDB_COL_FNAME_LEN + VARSTR_HEADER_SIZE)

#define SHOW_CREATE_DB_RESULT_COLS 2
#define SHOW_CREATE_DB_RESULT_FIELD1_LEN (TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE)
Expand Down Expand Up @@ -180,6 +182,7 @@ typedef struct SCompactVgroupsStmt {

typedef struct STableOptions {
ENodeType type;
bool virtualStb;
bool commentNull;
char comment[TSDB_TB_COMMENT_LEN];
SNodeList* pMaxDelay;
Expand All @@ -204,7 +207,11 @@ typedef struct SColumnOptions {
char compress[TSDB_CL_COMPRESS_OPTION_LEN];
char compressLevel[TSDB_CL_COMPRESS_OPTION_LEN];
bool bPrimaryKey;
bool hasRef;
char refTable[TSDB_TABLE_NAME_LEN];
char refColumn[TSDB_COL_NAME_LEN];
} SColumnOptions;

typedef struct SColumnDefNode {
ENodeType type;
char colName[TSDB_COL_NAME_LEN];
Expand All @@ -223,6 +230,27 @@ typedef struct SCreateTableStmt {
STableOptions* pOptions;
} SCreateTableStmt;

typedef struct SCreateVTableStmt {
ENodeType type;
char dbName[TSDB_DB_NAME_LEN];
char tableName[TSDB_TABLE_NAME_LEN];
bool ignoreExists;
SNodeList* pCols;
} SCreateVTableStmt;

typedef struct SCreateVSubTableStmt {
ENodeType type;
char dbName[TSDB_DB_NAME_LEN];
char tableName[TSDB_TABLE_NAME_LEN];
char useDbName[TSDB_DB_NAME_LEN];
char useTableName[TSDB_TABLE_NAME_LEN];
bool ignoreExists;
SNodeList* pSpecificTags;
SNodeList* pValsOfTags;
SNodeList* pSpecificColRefs;
SNodeList* pColRefs;
} SCreateVSubTableStmt;

typedef struct SCreateSubTableClause {
ENodeType type;
char dbName[TSDB_DB_NAME_LEN];
Expand Down Expand Up @@ -272,6 +300,14 @@ typedef struct SDropSuperTableStmt {
bool withOpt;
} SDropSuperTableStmt;

typedef struct SDropVirtualTableStmt {
ENodeType type;
char dbName[TSDB_DB_NAME_LEN];
char tableName[TSDB_TABLE_NAME_LEN];
bool ignoreNotExists;
bool withOpt;
} SDropVirtualTableStmt;

typedef struct SAlterTableStmt {
ENodeType type;
char dbName[TSDB_DB_NAME_LEN];
Expand All @@ -284,6 +320,8 @@ typedef struct SAlterTableStmt {
SValueNode* pVal;
SColumnOptions* pColOptions;
SNodeList* pNodeListTagValue;
char refTableName[TSDB_TABLE_NAME_LEN];
char refColName[TSDB_COL_NAME_LEN];
} SAlterTableStmt;

typedef struct SAlterTableMultiStmt {
Expand Down
17 changes: 17 additions & 0 deletions include/libs/nodes/plannodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ typedef struct SJoinLogicNode {
SNode* pRightOnCond; // table onCond filter
} SJoinLogicNode;

typedef struct SVirtualScanLogicNode {
SLogicNode node;
SNodeList* pScanCols;
SNodeList* pScanPseudoCols;
int8_t tableType;
uint64_t tableId;
uint64_t stableId;
SVgroupsInfo* pVgroupList;
EScanType scanType;
SName tableName;
} SVirtualScanLogicNode;

typedef struct SAggLogicNode {
SLogicNode node;
SNodeList* pGroupKeys;
Expand Down Expand Up @@ -455,6 +467,11 @@ typedef struct STagScanPhysiNode {

typedef SScanPhysiNode SBlockDistScanPhysiNode;

typedef struct SVirtualScanPhysiNode {
SScanPhysiNode scan;
SNodeList* pTargets;
}SVirtualScanPhysiNode;

typedef struct SLastRowScanPhysiNode {
SScanPhysiNode scan;
SNodeList* pGroupTags;
Expand Down
Loading