0

我必须通过计算构成 sql 的特定关键字、子查询、派生表、函数等的出现次数来确定 sql 的复杂程度(简单/中等/复杂等)。此外,我必须在语法上验证 sql。

我在网上搜索,发现 Perl 有 2 个已命名的类SQL::StatementSQL::Parser可以利用它们来实现相同的目的。但是,我发现这些类有几个限制(例如CASE WHEN不支持的构造等)。

话虽如此,用 Lex/Yacc 或 Flex/Bison 构建自定义简洁的 sql 解析器会更好吗?哪种方法会更好更快?

请分享您对此的看法。另外,任何人都可以将我指向任何讨论相同内容的在线资源。

谢谢

4

1 回答 1

2

Teradata has many non ANSI features and you're considering re-implementing the parser for it.

Instead use the database server and put an 'explain' in front of your statements and process the result.

explain select * from dbc.dbcinfo;

  1) First, we lock a distinct DBC."pseudo table" for read on a RowHash
     to prevent global deadlock for DBC.DBCInfoTbl.
  2) Next, we lock DBC.DBCInfoTbl in view dbcinfo for read.
  3) We do an all-AMPs RETRIEVE step from DBC.DBCInfoTbl in view
     dbcinfo by way of an all-rows scan with no residual conditions
     into Spool 1 (group_amps), which is built locally on the AMPs.
     The size of Spool 1 is estimated with low confidence to be 432
     rows (2,374,272 bytes).  The estimated time for this step is 0.01
     seconds.
  4) Finally, we send out an END TRANSACTION step to all AMPs involved
     in processing the request.
  -> The contents of Spool 1 are sent back to the user as the result of
     statement 1.  The total estimated time is 0.01 seconds.

This will also validate your SQL.

于 2016-03-29T23:30:08.763 回答