1.4.0 • Published 2 months ago

sqomplexity v1.4.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

SQompLexity

Build Status NPM Version NPM Downloads Install Size

   _____   ____                            _                  _  _          
  / ____| / __ \                          | |                (_)| |         
 | (___  | |  | |  ___   _ __ ___   _ __  | |      ___ __  __ _ | |_  _   _ 
  \___ \ | |  | | / _ \ | '_ ` _ \ | '_ \ | |     / _ \\ \/ /| || __|| | | |
  ____) || |__| || (_) || | | | | || |_) || |____|  __/ >  < | || |_ | |_| |
 |_____/  \___\_\ \___/ |_| |_| |_|| .__/ |______|\___|/_/\_\|_| \__| \__, |
                                   | |                                 __/ |
     Calculate complexity scores   |_|   for SQL queries              |___/ 
     

SQompLexity is a metric that assigns a complexity score to SELECT queries. It is specifically tailored to work with MySQL queries, but other dialects of SQL will likely work as well. It needs no knowledge of the database schema and quantifies each query in a vacuum.

Installation

npm i sqomplexity

Demo

https://bert-w.github.io/sqomplexity/

Usage

Execution in Node

import { Sqomplexity } from 'sqomplexity';

See examples/node.js for a full example.

Execution in a browser

Use the precompiled dist/sqomplexity-browser.js file:

<script src="dist/sqomplexity-browser.js"></script>
<script>
    const command = window.$sqomplexity(...);
</script>

See examples/browser.html for a full example.

Execution as a Stand-alone application

Use the precompiled dist/sqomplexity.js file.

Options:

node dist/sqomplexity.js --help

Arguments:
  queries                  one or multiple SQL queries (space separated or quoted)

Options:
  -V, --version            output the version number
  -f, --files              assumes the given arguments/queries are filepaths, and it will read the contents from them.
                           Every file is expected to contain 1 query; if not, their complexity is summed
  -b, --base64             assumes the given arguments/queries are base64 encoded
  -s, --score              output only the complexity score. -1 will be returned if an error occurs
  -w, --weights <weights>  takes a path to a json file that defines a custom set of weights
  -a, --all                returns all data including the AST
  -p, --pretty-print       output JSON with indentation and newlines (default: false)
  -h, --help               display help for command

See examples/standalone.sh for various examples.

Defining a complexity metric

The scoring of an SQL query is based on 2 major components, being:

Data complexity (see prefix D in the table below), also called Computational complexity, which takes into account elements like the amount of rows that a query operates on (relatively speaking), the computation paths a query may take, and the usage of table indexes (indices). All of these determine the computational cost of a certain component.

Cognitive complexity (see prefix C in the table below), which describes the mental effort and the concepts a person must understand in order to parse the query. This includes components like understanding of First-order logic, understanding of grouping, filtering and sorting (common SQL concepts), and Domain knowledge like the context of the query compared to its database schema.

Complexity indicators

CodeExplanation
Indexing behavior
D1-ANo possibility to affect the chosen index
D1-BLow possibility to affect the chosen index
D1-CHigh possibility to affect the chosen index
Running time
D2-A$O(0)$ (negligible) running time w.r.t. the number of rows
D2-B$O(1)$ (constant) running time w.r.t. the number of rows
D2-C$O(\log n)$ (logarithmic) running time w.r.t. the number of rows
D2-D$O(n)$ (linear) running time w.r.t. the number of rows
D2-E$O(n \log n)$ (linearithmic) running time w.r.t. the number of rows
D2-F$O(x)$ (highly variable) running time w.r.t. the number of rows
Relational algebra
C1Requires understanding of projection (selection of columns)
C2Requires understanding of selection (e.g. boolean logic like (in)equalities and comparisons)
C3Requires understanding of composition (multiple tables, column relations, set theory)
C4Requires understanding of grouping
C5Requires understanding of aggregation
Programming
C6Requires understanding of data types (e.g. integers, decimals, booleans, dates, times)
C7Requires understanding of variable scopes
C8Requires understanding of nesting
Usage
C9-AOne parameter
C9-BLow amount of parameters
C9-CHigh amount of parameters
C10Requires understanding of the database schema
C11Requires understanding of the RDBMS toolset (e.g. function support and differences)

What follows is the assignment of each of these indicators to components of an SQL query. The table below shows the result of this process. The combination and presence of these indicators are combined into a final weighting for each component, namely Low, Medium or High.

Complexity scoring

ComponentData ComplexityByCognitive ComplexityBy
Clause:SELECTLowD1-A, D2-DLowC1, C6, C9-B, C10
Clause:FROMMediumD1-B, D2-DLowC3, C7, C9-A, C10
Clause:JOINMediumD1-C, D2-FMediumC2, C3, C7, C9-B, C10
Clause:WHEREHighD1-C, D2-C/DMediumC2, C6, C9-B, C10
Clause:GROUP BYHighD1-C, D2-D/EHighC2, C4, C5, C9-B, C10
Clause:HAVINGMediumD1-A, D2-DHighC2, C4, C5, C9-C, C10
Clause:ORDER BYLowD1-C, D2-D/EMediumC6, C9-B, C10
Clause:LIMITLowD1-A, D2-BLowC9-A
Clause:OFFSETLowD1-A, D2-BLowC9-A
Expression:TableMediumD1-B, D2-AMediumC9-A, C10
Expression:ColumnMediumD1-B, D2-AMediumC6, C9-A, C10
Expression:StringLowD1-A, D2-ALowC6, C9-A
Expression:NumberLowD1-A, D2-ALowC6, C9-A
Expression:NullLowD1-A, D2-ALowC6, C9-A
Expression:StarLowD1-A, D2-ALowC1, C9-A
Expression:UnaryLowD1-A, D2-AMediumC2, C6, C9-A
Expression:BinaryLowD1-A, D2-AMediumC2, C6, C9-B
Expression:FunctionHighD1-B, D2-DMediumC6, C9-A, C11
Expression:ListLowD1-C, D2-ALowC6, C9-C
Expression:Agg-FunctionHighD1-B, D2-FHighC4, C5, C9-A, C10, C11
OperatorLowD1-C, D2-AMediumC2, C6, C9-B
Emergent:CycleMediumD1-B, D2-FHighC2, C3, C9-C, C10
Emergent:Mixed-StyleNoneD1-A, D2-AMediumC9-C
Emergent:SubqueryHighD1-C, D2-FHighC1, C2, C3, C7, C8, C9-C, C10
Emergent:VarietyNoneD1-A, D2-AMediumC9-C

Calculation

Each query that passes through SQompLexity is parsed into an Abstract Syntax Tree (AST), which provides the backbone of the algorithm that sums up the weights. Each query is traversed fully (including subqueries), and the scores are summed to result in a final SQompLexity score for any given SQL query.

The numerical weights for each of groups are like so:

CategoryNumerical Score
Data Complexity50%
Cognitive Complexity50%
Low1.0
Medium1.25
High1.5

The equal contribution of both Data Complexity and Cognitive Complexity is arbitrary, and research could still be done to develop a distribution that more fairly approaches a general sense of complexity.

Similarly, the weights of Low, Medium and High are set to some sensible defaults. It is necessary though for all weights to be greater than or equal to 1, since multiplication may take place during the algorithm.

Project Origin

This is a product of my master's thesis on complexity progression and correlations on Stack Overflow. For this study, I have developed an SQL complexity metric to be used on question and answer data from Stack Overflow.

1.4.0

2 months ago

1.3.1

3 months ago

1.2.1

3 months ago

1.1.1

3 months ago

1.1.0

3 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago