Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with json_table in mariadb. #658

Open
xylle opened this issue Oct 30, 2024 · 3 comments
Open

Error with json_table in mariadb. #658

xylle opened this issue Oct 30, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@xylle
Copy link

xylle commented Oct 30, 2024

Introduction

Error with json_table in mariadb.

To Reproduce

Query work correctly with phpmyadmin. The SQL code comes from the page : https://sql.datapage.app/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide

SELECT jt.*
FROM JSON_TABLE(
  '["Alice", "Bob", "Charlie"]',
  '$[*]' COLUMNS(
    row_num FOR ORDINALITY,
    name VARCHAR(50) PATH '$'
  )
) AS jt;

Actual behavior

Not working.

[2024-10-30T15:31:47.813Z ERROR sqlpage::render] SQL error: Parsing failed: SQLPage couldn't understand the SQL file. Please check for syntax errors:

      '$[*]' COLUMNS(
        row_num FOR ORDINALITY,
                   ⬆️
        name VARCHAR(50) PATH '$'


    Caused by:
        0: sql parser error: Expected: PATH, found: ORDINALITY at Line: 40, Column: 17
        1: SQLPage could not parse and prepare this SQL statement

Screenshots

Screenshot 2024-10-30 at 16-33-01 Serveurs 2

Expected behavior

row_num name
1 Alice
2 Bob
3 Charlie

Version information

  • OS : Windows 10
  • Database : Mariadb
  • SQLPage Version : 0.29.0
@lovasoa
Copy link
Collaborator

lovasoa commented Oct 30, 2024

Thank you for reporting this, @xylle !

I proposed a fix for this in the sql parser we are using: apache/datafusion-sqlparser-rs#1493
We are now just waiting for it to be merged and for a new version of the parser to be released.

In the meantime, you can use a window function instead to count row numbers:

SELECT 
  jt.*,
  row_number() OVER () AS row_num
FROM JSON_TABLE(
  '["Alice", "Bob", "Charlie"]',
  '$[*]' COLUMNS(
    name VARCHAR(50) PATH '$')
) AS jt

@xylle
Copy link
Author

xylle commented Oct 31, 2024

I was trying to understand how JSON works. SQL and JSON are obscure to me.
Thanks

@lovasoa
Copy link
Collaborator

lovasoa commented Nov 6, 2024

The fix was merged in the sql library. It's coming soon to sqlpage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants