sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.37k stars 405 forks source link

[FORMATTING] Full text search distance formats `<->` incorrectly #770

Closed huypham50 closed 2 months ago

huypham50 commented 2 months ago

Input data

Which SQL and options did you provide as input?

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, name TEXT, vec vector(3));

INSERT INTO test (name, vec) VALUES ('test1', '[1,2,3]');
INSERT INTO test (name, vec) VALUES ('test2', '[4,5,6]');
INSERT INTO test (name, vec) VALUES ('test3', '[7,8,9]');

SELECT name, vec, vec <-> '[3,1,2]' AS distance FROM test;

Expected Output

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, name TEXT, vec vector (3));

INSERT INTO
  test (name, vec)
VALUES
  ('test1', '[1,2,3]');

INSERT INTO
  test (name, vec)
VALUES
  ('test2', '[4,5,6]');

INSERT INTO
  test (name, vec)
VALUES
  ('test3', '[7,8,9]');

SELECT
  name,
  vec,
  vec <-> '[3,1,2]' AS distance
FROM
  test;

Actual Output

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, name TEXT, vec vector (3));

INSERT INTO
  test (name, vec)
VALUES
  ('test1', '[1,2,3]');

INSERT INTO
  test (name, vec)
VALUES
  ('test2', '[4,5,6]');

INSERT INTO
  test (name, vec)
VALUES
  ('test3', '[7,8,9]');

SELECT
  name,
  vec,
  vec < - > '[3,1,2]' AS distance
FROM
  test;

^ Notice the spaces < - >

I also think it should be vector(3) instead of vector (3) as well

Usage

nene commented 2 months ago

Simply specify PostgreSQL as the dialect that you're formatting. The <-> operator is not part of standard SQL.