GithubHelp home page GithubHelp logo

Format Merge about sql-formatter HOT 7 CLOSED

numeralnathan avatar numeralnathan commented on May 20, 2024
Format Merge

from sql-formatter.

Comments (7)

manticore-projects avatar manticore-projects commented on May 20, 2024 1

Hi,

for my own curiosity I ran it against my own manticore JSQLFormatter.
I think your statement is INVALID as you are missing the BRACKETS around the ON-Expression. Adding the brackets would format the MERGE statement correctly:

-- EXTERNAL
MERGE INTO DW_STG_USER.ACCOUNT_DIM target
    USING ( SELECT COMMON_NAME m_commonName
                , ORIGIN m_origin
                , USAGE_TYPE m_usageType
                , CATEGORY m_category
            FROM MY_TABLE
            WHERE USAGE_TYPE = :value ) source
        ON ( source.m_usageType = target.USAGE_TYPE ) 
WHEN NOT MATCHED THEN 
    INSERT ( target.COMMON_NAME
                , target.ORIGIN
                , target.USAGE_TYPE
                , target.CATEGORY ) 
    VALUES ( source.m_commonName
                , source.m_origin
                , source.m_usageType
                , source.m_category ) 
WHEN MATCHED THEN 
    UPDATE SET target.COMMON_NAME = source.m_commonName
                    , target.ORIGIN = source.m_origin
                    , target.USAGE_TYPE = source.m_usageType
                    , target.CATEGORY = source.m_category
    WHERE ( ( source.m_commonName <> target.COMMON_NAME )
                    OR ( source.m_origin <> target.ORIGIN )
                    OR ( source.m_usageType <> target.USAGE_TYPE )
                    OR ( source.m_category <> target.CATEGORY ) )
;

from sql-formatter.

manticore-projects avatar manticore-projects commented on May 20, 2024 1

from sql-formatter.

vertical-blank avatar vertical-blank commented on May 20, 2024

This seems to be fixed on current version.
Thanks @manticore-projects !

from sql-formatter.

manticore-projects avatar manticore-projects commented on May 20, 2024

Simple statements look better now, but there are still a lot of issues with complex ones, e. g.

-- MERGE 4
MERGE INTO cfe.instrument_import_measure imp
    USING ( WITH x AS (
                    SELECT  a.id_instrument
                            , a.id_currency
                            , a.id_instrument_type
                            , b.id_portfolio
                            , c.attribute_value product_code
                            , t.valid_date
                            , t.yield
                    FROM cfe.instrument a
                        INNER JOIN cfe.impairment b
                            ON a.id_instrument = b.id_instrument
                        LEFT JOIN cfe.instrument_attribute c
                            ON a.id_instrument = c.id_instrument
                                AND c.id_attribute = 'product'
                        INNER JOIN cfe.ext_yield t
                            ON ( a.id_currency = t.id_currency )
                                AND ( a.id_instrument_type LIKE t.id_instrument_type )
                                AND ( b.id_portfolio LIKE t.id_portfolio
                                        OR ( b.id_portfolio IS NULL
                                                AND t.id_portfolio = '%' ) )
                                AND ( c.attribute_value LIKE t.product_code
                                        OR ( c.attribute_value IS NULL
                                                AND t.product_code = '%' ) ) )
SELECT /*+ PARALLEL */ *
            FROM x x1
            WHERE x1.valid_date = ( SELECT max
                                    FROM x
                                    WHERE id_instrument = x1.id_instrument
                                        AND valid_date <= to_date ) ) s
        ON ( imp.id_instrument = s.id_instrument
                    AND imp.measure = 'YIELD_PP' )
WHEN MATCHED THEN
    UPDATE SET  imp.value = s.yield
;

You can have a look at https://github.com/manticore-projects/jsqlformatter and also benchmark it against the interactive DEMO. Maybe the provided samples and test cases will be helpful.

from sql-formatter.

manticore-projects avatar manticore-projects commented on May 20, 2024

Btw, how do you plan to deal with comments? Example

-- BOTH CLAUSES PRESENT 'with a string' AND "a field"
MERGE /*+ PARALLEL */ INTO test1 /*the target table*/ a
    USING all_objects      /*the source table*/
        ON ( /*joins in()!*/ a.object_id = b.object_id )
-- INSERT CLAUSE 
WHEN /*comments between keywords!*/ NOT MATCHED THEN
    INSERT ( object_id     /*ID Column*/
                , status   /*Status Column*/ )
    VALUES ( b.object_id
                , b.status )
/* UPDATE CLAUSE
WITH A WHERE CONDITION */ 
WHEN MATCHED THEN          /* Lets rock */
    UPDATE SET  a.status = '/*this is no comment!*/ and -- this ain''t either'
    WHERE   b."--status" != 'VALID'
;

Had a really hard time to figure that out.
So maybe we should just join our efforts and work together? One 90% solution would be better than 2 different 75% solution in my opinion.

from sql-formatter.

vertical-blank avatar vertical-blank commented on May 20, 2024

That's a good idea, but I think the advantage of this library is having no dependencies, as described in readme.
And about formatting, currently, my priority is to reproduce the behavior of original library, https://github.com/zeroturnaround/sql-formatter.
But, there are a few bugs or issues in the original version, so I'm now wondering if I should improve it independently...

from sql-formatter.

vertical-blank avatar vertical-blank commented on May 20, 2024

I almost completely agree with you, there is no doubt that JSQLFormatter is more reliable at formatting.
But, why I ported this is the original is very simple implementation by using REGEX.

And now, there is an issue about using external SQL parser created by maintainer of original.
sql-formatter-org/sql-formatter#117
As mentioned in this, to make pluggable the use of external parser might be the better solution, I think.

Thank you very much too.

from sql-formatter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.