npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sql-fingerprint

v1.0.2

Published

Convert queries into fingerprints.

Downloads

3

Readme

sql-fingerprint

Converts a SQL into a fingerprint, the abstracted form of a query, which makes it possible to classify similar queries.

Actually, this module is a JavaScript port of pt-fingerprint. Therefore is primarily intended to use for MySQL queries.

Description

The fingerprint is the abstracted form of a query. What "abstracting query" is string conversion such that, for instance, replace values to ?, collapse whitespaces and so on.

Let's say that there are such queries:

SELECT * FROM t WHERE i = 1 ORDER BY a, b ASC, d DESC, e ASC;

select *         from t       where i = 1
 order by a, b ASC, d DESC, e asc;

A fingerprint of both of these will be below:

select * from t where i = ? order by a, b, d desc, e;

Usage

CLI

npm install -g sql-fingerprint

fingerprint --query="your query"

Module

npm install sql-fingerprint
import fingerprint from 'sql-fingerprint';

console.log(fingerprint('SELECT * FROM users WHERE id = 1', false, false));

BigQuery UDF

The initial motivation I wrote this was that classifies similar queries stored in my dataset of BigQuery. BigQuery supports user-defined functions written in JavaScript. Therefore the approach of pt-fingerprint, which is generate fingerprint by a set of RegExps, is more fit than other approaches such as parsing SQL, building AST and then interpreting it. RegExps approach seems a little bit harder to maintain but easier to use as UDF because of the relatively simple source code.

CREATE TEMP FUNCTION fingerprint(sql STRING, matchMD5Checksum BOOL, matchEmbeddedNumbers BOOL)
RETURNS STRING
LANGUAGE js AS r"""

  // copy and paste fingerprint function here from fingerprint.js
  // ...

  return query;
""";

SELECT
  fingerprint(textPayload, true, true) fp,
  count(*) as num,
  max(query) as raw_query_sample
FROM
  `your_table`
WHERE
  DATE(timestamp, "Asia/Tokyo") = "2022-05-22"
group by
  fp
order by
  num desc

See also: Standard SQL user-defined functions  |  BigQuery  |  Google Cloud

Options

  • matchMD5Checksum
    • Replace md5 string to single ?
-- original
SELECT * FROM db.fbc5e685a5d3d45aa1d0347fdb7c4d35_temp where id=1
-- fingerprint with matchMD5Checksum=true
select * from db.?_temp where id=?
-- fingerprint with matchMD5Checksum=false (default)
select * from db.fbc?_temp where id=?
  • matchEmbeddedNumbers
    • Preserve numbers within words. Useful for the case like that table name contains a number
-- original
SELECT * FROM prices.rt_5min WHERE id = 1
-- fingerprint with matchEmbeddedNumbers=true
select * from prices.rt_5min where id = ?
-- fingerprint with matchEmbeddedNumbers=false (default)
select * from prices.rt_?min where id = ?