Compare commits

..

No commits in common. "b1c8a64804785e208642a5bf061f5b0cb800bdad" and "a63b373baec2296f5af98f0095dac5297c3ad100" have entirely different histories.

6 changed files with 19 additions and 138 deletions

View File

@ -55,7 +55,7 @@ console.log(result)
**Notice: A rule must start with a/an statement/entity and stop with an entity. You should add a node keywords(keyword is in your parser with format: `RULE_[keyword]`) into stmts/entities before using it.** **Notice: A rule must start with a/an statement/entity and stop with an entity. You should add a node keywords(keyword is in your parser with format: `RULE_[keyword]`) into stmts/entities before using it.**
## Rule Chain Operator ## Rule Chain Opeator
You can set a negative number whose abs equals to a ruleIndex. That means exclude this rule. You can set a negative number whose abs equals to a ruleIndex. That means exclude this rule.
@ -75,20 +75,18 @@ select_target_alias: [
Some node names in dt-sql-parser code are different from their antlr4's definition. Some node names in dt-sql-parser code are different from their antlr4's definition.
Example: You can find possible alias in https://github.com/DTStack/dt-sql-parser/blob/main/src/grammar/postgresql/PostgreSqlParser.g4, then add it into `alias` option.
Example:
![alt text](./assets/alias-example.png) ![alt text](./assets/alias-example.png)
You can find possible alias in https://github.com/DTStack/dt-sql-parser/blob/main/src/grammar/postgresql/PostgreSqlParser.g4, then add it into `alias` option.
## Add a preprocessor ## Add a preprocessor
```typescript ```typescript
const myPlugin = new DtSqlParserSemAnalysePlugin({ const myPlugin = new DtSqlParserSemAnalysePlugin({
preprocessor: [ preprocessor: [
(sql) => sql.toUpperCase(), (sql) => sql.toUpperCase()
...
]
}) })
``` ```

View File

@ -12,7 +12,7 @@
<script type="module"> <script type="module">
const { DtSqlParserSemAnalysePlugin } = await import(/* @vite-ignore */import.meta.env.VITE_ENTRY_PATH) const { DtSqlParserSemAnalysePlugin } = await import(/* @vite-ignore */import.meta.env.VITE_ENTRY_PATH)
const myPlugin = new DtSqlParserSemAnalysePlugin() const myPlugin = new DtSqlParserSemAnalysePlugin()
const sql = 'CREATE TABLE "public"."h1" (c1 int8 not null, c2 int8, c3 int8) with (appendonly = true, orientation = horc) TABLESPACE horc_default DISTRIBUTED BY (|)' const sql = 'SELECT a.| AS c'
const caretColumn = sql.indexOf('|') + 1 const caretColumn = sql.indexOf('|') + 1
const result = myPlugin.parse(sql.replace('|', ''), { lineNumber: 1, columnNumber: caretColumn }) const result = myPlugin.parse(sql.replace('|', ''), { lineNumber: 1, columnNumber: caretColumn })
console.log(result) console.log(result)

13
package-lock.json generated
View File

@ -1,14 +1,14 @@
{ {
"name": "dt-sql-parser-semantic-analyse-plugin", "name": "dt-sql-parser-semantic-analyse-plugin",
"version": "0.0.1-alpha.14", "version": "0.0.1-alpha.7",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "dt-sql-parser-semantic-analyse-plugin", "name": "dt-sql-parser-semantic-analyse-plugin",
"version": "0.0.1-alpha.14", "version": "0.0.1-alpha.6",
"dependencies": { "dependencies": {
"dt-sql-parser": "npm:dt-sql-parser-oushudb@^4.0.2-5" "dt-sql-parser": "^4.0.0-beta.4.12"
}, },
"devDependencies": { "devDependencies": {
"@eslint/eslintrc": "^3.0.2", "@eslint/eslintrc": "^3.0.2",
@ -1904,10 +1904,9 @@
} }
}, },
"node_modules/dt-sql-parser": { "node_modules/dt-sql-parser": {
"name": "dt-sql-parser-oushudb", "version": "4.0.0-beta.4.12",
"version": "4.0.2-5", "resolved": "http://npm.oushu.com:14837/dt-sql-parser/-/dt-sql-parser-4.0.0-beta.4.12.tgz",
"resolved": "http://npm.oushu.com:14837/dt-sql-parser-oushudb/-/dt-sql-parser-oushudb-4.0.2-5.tgz", "integrity": "sha512-kfLRecn+dfdZjrKt3Ovm52ryoh9UGF+dCjNzV2pk8XI5kgF7lgQJhPZdRi+2yn1AU/BWQ1/0E6LnvFYbZ7Wr9Q==",
"integrity": "sha512-fHu00/+Mk+QtCrS24Uc1vYV3xsy8BxyLyD0Oh4/TJwhBq7U+ZWunjHzcQBMccPyb8JnTYcG/HZ/f5EZ2GqQG4Q==",
"dependencies": { "dependencies": {
"antlr4-c3": "3.3.7", "antlr4-c3": "3.3.7",
"antlr4ng": "2.0.11" "antlr4ng": "2.0.11"

View File

@ -1,6 +1,6 @@
{ {
"name": "lava-dt-sql-parser-semantic-analyse-plugin", "name": "dt-sql-parser-semantic-analyse-plugin",
"version": "0.0.1-alpha.16", "version": "0.0.1-alpha.8",
"description": "an dt-sql-parser plugin with semantic result", "description": "an dt-sql-parser plugin with semantic result",
"type": "module", "type": "module",
"files": [ "files": [
@ -37,11 +37,6 @@
"vite-plugin-node-polyfills": "^0.21.0" "vite-plugin-node-polyfills": "^0.21.0"
}, },
"dependencies": { "dependencies": {
"dt-sql-parser": "npm:lava-oushudb-dt-sql-parser@^4.0.2-10" "dt-sql-parser": "^4.0.0-beta.4.12"
},
"git repository": "https://git.yevpt.com/oushu/lava-dt-sql-parser-semantic-analyse-plugin",
"repository": "https://git.yevpt.com/oushu/lava-dt-sql-parser-semantic-analyse-plugin",
"volta": {
"node": "14.21.3"
} }
} }

View File

@ -2,104 +2,25 @@ import { PostgreSqlParser } from 'dt-sql-parser/dist/lib/postgresql/PostgreSqlPa
export const defaultAlias = { export const defaultAlias = {
selectstmt: 'selectStatement', selectstmt: 'selectStatement',
insertstmt: 'insertStatement', target_el: 'target_label'
target_el: 'target_label',
table_name: 'tableName',
view_name: 'viewName',
column_name: 'columnName',
schema_name: 'schemaName'
} }
export const defaultStmts = [ export const defaultStmts = [
'selectstmt', 'simple_select',
'insertstmt',
'updatestmt',
'deletestmt',
'altertablestmt',
'alterfunctionstmt',
'dropstmt',
'createfunctionstmt'
] ]
export const defaultEntities = [ export const defaultEntities = [
'target_el', 'target_el',
'colid', 'colid',
'attr_name', 'attr_name',
'collabel', 'collabel'
'table_name',
'view_name',
'function_with_argtypes',
'column_name',
'schema_name',
'anysconst'
] ]
export const defaultRules: Record<string, number[]> = { export const defaultRules: Record<string, number[]> = {
select_target: [ select_target: [
PostgreSqlParser.RULE_selectstmt, PostgreSqlParser.RULE_simple_select,
PostgreSqlParser.RULE_target_el, PostgreSqlParser.RULE_target_el,
], ],
select_from_table: [
PostgreSqlParser.RULE_selectstmt,
PostgreSqlParser.RULE_from_clause,
PostgreSqlParser.RULE_table_name
],
insert_target_table: [
PostgreSqlParser.RULE_insertstmt,
PostgreSqlParser.RULE_insert_target,
PostgreSqlParser.RULE_table_name
],
update_relation_table: [
PostgreSqlParser.RULE_updatestmt,
PostgreSqlParser.RULE_relation_expr,
PostgreSqlParser.RULE_table_name
],
delete_relation_table: [
PostgreSqlParser.RULE_deletestmt,
PostgreSqlParser.RULE_relation_expr,
PostgreSqlParser.RULE_table_name
],
alter_table: [
PostgreSqlParser.RULE_altertablestmt,
PostgreSqlParser.RULE_relation_expr,
PostgreSqlParser.RULE_table_name
],
alter_view: [
PostgreSqlParser.RULE_altertablestmt,
PostgreSqlParser.RULE_view_name
],
alter_function: [
PostgreSqlParser.RULE_alterfunctionstmt,
PostgreSqlParser.RULE_alterFunctionTypeClause,
PostgreSqlParser.RULE_function_with_argtypes
],
alter_table_drop_column: [
PostgreSqlParser.RULE_altertablestmt,
PostgreSqlParser.RULE_alter_table_cmds,
PostgreSqlParser.RULE_column_name
],
drop_table: [
PostgreSqlParser.RULE_dropstmt,
PostgreSqlParser.RULE_table_name_list,
PostgreSqlParser.RULE_table_name
],
drop_view: [
PostgreSqlParser.RULE_dropstmt,
PostgreSqlParser.RULE_view_nameList,
PostgreSqlParser.RULE_view_name
],
drop_schema: [
PostgreSqlParser.RULE_dropstmt,
PostgreSqlParser.RULE_schema_name_list,
PostgreSqlParser.RULE_schema_name
],
create_function_sub_content: [
PostgreSqlParser.RULE_createfunctionstmt,
PostgreSqlParser.RULE_createfunc_opt_list,
PostgreSqlParser.RULE_colid,
PostgreSqlParser.RULE_anysconst,
],
// sub entities
select_target_colid: [ select_target_colid: [
PostgreSqlParser.RULE_target_el, PostgreSqlParser.RULE_target_el,
PostgreSqlParser.RULE_function_name, PostgreSqlParser.RULE_function_name,
@ -114,37 +35,5 @@ export const defaultRules: Record<string, number[]> = {
PostgreSqlParser.RULE_target_el, PostgreSqlParser.RULE_target_el,
-PostgreSqlParser.RULE_attr_name, -PostgreSqlParser.RULE_attr_name,
PostgreSqlParser.RULE_collabel PostgreSqlParser.RULE_collabel
],
table_name_colid: [
PostgreSqlParser.RULE_table_name,
PostgreSqlParser.RULE_colid
],
table_name_attr: [
PostgreSqlParser.RULE_table_name,
PostgreSqlParser.RULE_attr_name,
],
view_name_colid: [
PostgreSqlParser.RULE_view_name,
PostgreSqlParser.RULE_colid
],
view_name_attr: [
PostgreSqlParser.RULE_view_name,
PostgreSqlParser.RULE_attr_name,
],
function_colid: [
PostgreSqlParser.RULE_function_with_argtypes,
PostgreSqlParser.RULE_colid
],
function_attr: [
PostgreSqlParser.RULE_function_with_argtypes,
PostgreSqlParser.RULE_attr_name,
],
column_name_colid: [
PostgreSqlParser.RULE_column_name,
PostgreSqlParser.RULE_colid
],
column_name_attr: [
PostgreSqlParser.RULE_column_name,
PostgreSqlParser.RULE_attr_name,
] ]
} }

View File

@ -85,7 +85,7 @@ export class SQLVisitor extends AbstractParseTreeVisitor<void> implements Postgr
if (!beginStmt.relatedEntities[rule]) beginStmt.relatedEntities[rule] = [] if (!beginStmt.relatedEntities[rule]) beginStmt.relatedEntities[rule] = []
beginStmt.relatedEntities[rule].push(result) beginStmt.relatedEntities[rule].push(result)
} }
if (beginStmt && withCaret(ctx)) this.result.nerestCaretEntityList.push(result) if (withCaret(ctx)) this.result.nerestCaretEntityList.push(result)
this.entityStack.push(result) this.entityStack.push(result)
isHitRule = true isHitRule = true
} }