From bbab0387fbf61646aa0e4209c37983946ffb66ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tito=20Grin=C3=A9?= Date: Fri, 11 Nov 2022 20:58:12 +0200 Subject: [PATCH 1/4] Added further support for SingleStoreDB type-cast and path operators. --- .../singlestoredb/singlestoredb.formatter.ts | 3 +- test/singlestoredb.test.ts | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/languages/singlestoredb/singlestoredb.formatter.ts b/src/languages/singlestoredb/singlestoredb.formatter.ts index 447251e3e..87f6daef4 100644 --- a/src/languages/singlestoredb/singlestoredb.formatter.ts +++ b/src/languages/singlestoredb/singlestoredb.formatter.ts @@ -254,10 +254,11 @@ export const singlestoredb: DialectOptions = { { quote: '``', prefixes: ['@'], requirePrefix: true }, ], lineCommentTypes: ['--', '#'], - operators: [':=', '&', '|', '^', '~', '<<', '>>', '<=>', '&&', '||'], + operators: [':=', '&', '|', '^', '~', '<<', '>>', '<=>', '&&', '||', '::', '::$', '::%', ':>', '!:>'], postProcess, }, formatOptions: { + alwaysDenseOperators: ['::', '::$', '::%'], onelineClauses, }, }; diff --git a/test/singlestoredb.test.ts b/test/singlestoredb.test.ts index 5907f981a..8e8da9c01 100644 --- a/test/singlestoredb.test.ts +++ b/test/singlestoredb.test.ts @@ -9,6 +9,7 @@ import supportsCreateTable from './features/createTable.js'; import supportsCreateView from './features/createView.js'; import supportsAlterTable from './features/alterTable.js'; import supportsStrings from './features/strings.js'; +import dedent from 'dedent-js'; describe('SingleStoreDbFormatter', () => { const language = 'singlestoredb'; @@ -45,4 +46,67 @@ describe('SingleStoreDbFormatter', () => { modify: true, renameTo: true, }); + // it('allows operations in BETWEEN ranges', () => { + // expect(format('SELECT SUM(extendedprice * discount) AS revenue FROM item WHERE discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01')).toBe(dedent` + // SELECT + // SUM(extendedprice * discount) AS revenue + // FROM + // item + // WHERE + // discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01 + // `); + // }); + describe(`formats traversal of semi structured data`, () => { + it(`formats '.' path-operator without spaces`, () => { + expect(format(`SELECT TO_JSON(foo.*) AS foo_json FROM foo`)).toBe(dedent` + SELECT + TO_JSON(foo.*) AS foo_json + FROM + foo + `); + }); + it(`formats '::' path-operator without spaces`, () => { + expect(format(`SELECT * FROM foo WHERE json_foo::bar = 'foobar'`)).toBe(dedent` + SELECT + * + FROM + foo + WHERE + json_foo::bar = 'foobar' + `); + }); + it(`formats '::$' conversion path-operator without spaces`, () => { + expect(format(`SELECT * FROM foo WHERE json_foo::$bar = 'foobar'`)).toBe(dedent` + SELECT + * + FROM + foo + WHERE + json_foo::$bar = 'foobar' + `); + }); + it(`formats '::%' conversion path-operator without spaces`, () => { + expect(format(`SELECT * FROM foo WHERE json_foo::%bar = 'foobar'`)).toBe(dedent` + SELECT + * + FROM + foo + WHERE + json_foo::%bar = 'foobar' + `); + }); + }); + describe(`formats custom type-cast operators`, () => { + it(`formats ':>' type-cast operator`, () => { + expect(format(`SELECT 1 :> DOUBLE AS foo`)).toBe(dedent` + SELECT + 1 :> DOUBLE AS foo + `); + expect(format(`SELECT 1 !:> DOUBLE AS foo`)).toBe(dedent` + SELECT + 1 !:> DOUBLE AS foo + `); + }); + }); + }); From 86becd41570a3d4a047805c840f5216731bca783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tito=20Grin=C3=A9?= Date: Mon, 21 Nov 2022 17:38:52 +0200 Subject: [PATCH 2/4] Ran prettier formatter. --- .../singlestoredb/singlestoredb.formatter.ts | 18 +++++++++++++++++- test/singlestoredb.test.ts | 14 ++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/languages/singlestoredb/singlestoredb.formatter.ts b/src/languages/singlestoredb/singlestoredb.formatter.ts index 87f6daef4..1188f96e2 100644 --- a/src/languages/singlestoredb/singlestoredb.formatter.ts +++ b/src/languages/singlestoredb/singlestoredb.formatter.ts @@ -254,7 +254,23 @@ export const singlestoredb: DialectOptions = { { quote: '``', prefixes: ['@'], requirePrefix: true }, ], lineCommentTypes: ['--', '#'], - operators: [':=', '&', '|', '^', '~', '<<', '>>', '<=>', '&&', '||', '::', '::$', '::%', ':>', '!:>'], + operators: [ + ':=', + '&', + '|', + '^', + '~', + '<<', + '>>', + '<=>', + '&&', + '||', + '::', + '::$', + '::%', + ':>', + '!:>', + ], postProcess, }, formatOptions: { diff --git a/test/singlestoredb.test.ts b/test/singlestoredb.test.ts index 8e8da9c01..69608581d 100644 --- a/test/singlestoredb.test.ts +++ b/test/singlestoredb.test.ts @@ -1,3 +1,4 @@ +import dedent from 'dedent-js'; import { format as originalFormat, FormatFn } from '../src/sqlFormatter.js'; import behavesLikeMariaDbFormatter from './behavesLikeMariaDbFormatter.js'; @@ -9,7 +10,7 @@ import supportsCreateTable from './features/createTable.js'; import supportsCreateView from './features/createView.js'; import supportsAlterTable from './features/alterTable.js'; import supportsStrings from './features/strings.js'; -import dedent from 'dedent-js'; +import supportsBetween from './features/between.js'; describe('SingleStoreDbFormatter', () => { const language = 'singlestoredb'; @@ -46,16 +47,6 @@ describe('SingleStoreDbFormatter', () => { modify: true, renameTo: true, }); - // it('allows operations in BETWEEN ranges', () => { - // expect(format('SELECT SUM(extendedprice * discount) AS revenue FROM item WHERE discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01')).toBe(dedent` - // SELECT - // SUM(extendedprice * discount) AS revenue - // FROM - // item - // WHERE - // discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01 - // `); - // }); describe(`formats traversal of semi structured data`, () => { it(`formats '.' path-operator without spaces`, () => { expect(format(`SELECT TO_JSON(foo.*) AS foo_json FROM foo`)).toBe(dedent` @@ -108,5 +99,4 @@ describe('SingleStoreDbFormatter', () => { `); }); }); - }); From f9ee4e6b1183980a0a6e27a3e78a41a019a7499b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tito=20Grin=C3=A9?= Date: Mon, 21 Nov 2022 18:01:44 +0200 Subject: [PATCH 3/4] Added BETWEEN tests to singlestoredb> --- test/singlestoredb.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/singlestoredb.test.ts b/test/singlestoredb.test.ts index 69608581d..988452634 100644 --- a/test/singlestoredb.test.ts +++ b/test/singlestoredb.test.ts @@ -41,6 +41,7 @@ describe('SingleStoreDbFormatter', () => { supportsLimiting(format, { limit: true, offset: true }); supportsCreateTable(format, { ifNotExists: true }); supportsCreateView(format); + supportsBetween(format); supportsAlterTable(format, { addColumn: true, dropColumn: true, From 62c53611707c279c4a91f3a509feacf36daa369a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tito=20Grin=C3=A9?= <36516031+TitoGrine@users.noreply.github.com> Date: Mon, 21 Nov 2022 21:10:10 +0000 Subject: [PATCH 4/4] Fixed minor issue with new tests --- test/singlestoredb.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/singlestoredb.test.ts b/test/singlestoredb.test.ts index 988452634..4c25d2625 100644 --- a/test/singlestoredb.test.ts +++ b/test/singlestoredb.test.ts @@ -94,6 +94,8 @@ describe('SingleStoreDbFormatter', () => { SELECT 1 :> DOUBLE AS foo `); + }); + it(`formats '!:>' type-cast operator`, () => { expect(format(`SELECT 1 !:> DOUBLE AS foo`)).toBe(dedent` SELECT 1 !:> DOUBLE AS foo