GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — How to convert Statement to Expression · Issue #1764 · JSQLParser/JSqlParser

How to convert Statement to Expression #1764

New issue
Copy link
New issue
Copy link
Closed
#1754
Closed
How to convert Statement to Expression#1764
#1754
Copy link

Description

@kevin2000
kevin2000
opened on Apr 14, 2023
Issue body actions

the target sql is "select * from table2 where id in (SELECT id FROM table1 WHERE find_in_set(100, ancestors))"

the follow code will throw exception that Statement cant convert to Expression
InExpression expression = new InExpression();
expression.setLeftExpression(new Column(deptColumn));
Statement subSelect = CCJSqlParserUtil.parse("SELECT id FROM table1 WHERE find_in_set(100, ancestors)");
expression.setRightExpression((Expression) subSelect);

Activity

  1. manticore-projects commented on Apr 14, 2023

    @manticore-projects
    manticore-projects
    on Apr 14, 2023
    Contributor
    More actions

    Greetings.

    Yes, your observation is correct because SubSelect is an Expression, but not a Statement.
    So you have to options:

    1. Build a new SubSelect using the Java API.
    2. Parse the SubSelect as an Expression:
    SubSelect subSelect = (SubSelect) CCJSqlParserUtil.parseExpression("SELECT id FROM table1 WHERE find_in_set(100, ancestors)");
    InExpression expression = new InExpression();
    expression.setLeftExpression(new Column(deptColumn));
    expression.setRightExpression( subSelect );
  2. manticore-projects commented on Apr 14, 2023

    @manticore-projects
    manticore-projects
    on Apr 14, 2023
    Contributor
    More actions

    It's just pure coincidence, that I have provided PR #1754 which changes exactly that behaviour and makes the former SubSelect a normal Statement (and Expression).

    So when you checkout this PR #1754, the following will work:

        @Test
        void testSubJoinParsing() throws JSQLParserException {
            String sqlStr = "(SELECT id FROM table1 WHERE find_in_set(100, ancestors))";
            Select select = (Select)  CCJSqlParserUtil.parse(sqlStr);
    
            InExpression inExpression = new InExpression();
            inExpression.setLeftExpression(new Column("id"));
            inExpression.setRightExpression( select );
    
            Assertions.assertEquals("id IN (SELECT id FROM table1 WHERE find_in_set(100, ancestors))", inExpression.toString());
        }
  3. kevin2000
    closed this as completedon Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.