sqlpage / SQLPage

Fast SQL-only data application builder. Automatically build a UI on top of SQL queries.
https://sql-page.com
MIT License
1.66k stars 99 forks source link

Error with json_table in mariadb. #658

Closed xylle closed 2 weeks ago

xylle commented 1 month ago

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

lovasoa commented 4 weeks ago

Thank you for reporting this, @xylle !

I proposed a fix for this in the sql parser we are using: https://github.com/apache/datafusion-sqlparser-rs/pull/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 commented 4 weeks ago

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

lovasoa commented 3 weeks ago

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

lovasoa commented 2 weeks ago

I just updated the SQL parser. This is fixed :)