SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
Sergei Petrunia
MariaDB devroom
FOSDEM 2021
JSON Support in MariaDB
News, non-news, and the bigger picture
FOSDEM 2021
MariaDB devroom
Sergei Petrunia
MariaDB developer
2
JSON Path
Non-news
3
JSON Path
●
A lot of JSON functions accept “JSON Path” expressions
– locate element(s) in JSON document
●
Path language in MariaDB wasn’t documented
– It’s more than “foo.bar.baz”
●
Caution: there are many different JSON Path definitions on the
web
– SQL uses SQL:2016, “SQL/JSON Path language”
4
SQL/JSON Path language
●
mode is lax (the default) or strict
●
$ is the context element (root by default)
●
followed by several steps.
path: [mode] $ [step]*
5
Object member selection step
●
In strict mode, the context must be an object, it must have a
member with the specified name.
●
lax mode “ignores” missing elements, “unwraps” 1-element arrays
.name
.*
{
"name": "MariaDB"
"version": "10.5"
}
$.name → "MariaDB"
$.* → "MariaDB" "10.5"
●
Select a member
●
Select all members
– produces a sequence of values
6
Array element selection step
●
Produces a sequence of elements
●
Strict mode: indexes must be within bounds
[N]
[*]
[
10, "abc", {"x":0}
]
$[0] → 10
$[last] → {"x":0}
●
Select array elements
– one
– range
– last element
– list of ^^
– all elements
[N to M]
[N1,N2,...]
[last]
$[*] →10 "abc" {"x":0}
7
Filters
●
Predicate can have
– AND/OR formulas (&&, ||)
– comparisons
– arithmetics
– some functions
– parameters passed from outside
[
{
"item":"Jeans",
"color":"blue"
},
{
"item":"Laptop",
"color":"black"
}
]
●
Filters elements in sequence
?(predicate)
$[*]?(@.color=="black").item
→ "Laptop"
8
MariaDB and MySQL
●
Support only lax mode
– Not fully compliant: MySQL BUG#102233, MDEV-24573.
●
Object member selection is supported
●
Array selection: [N] is supported
– MySQL also supports [last] and [N to M]
●
Filters are not supported
– expressions, arithmetics, functions, passing variables
9
Recursive search extension
●
Task: find “foo” anywhere in the JSON document
●
SQL/JSON Path doesn’t allow this
●
Extension: wildcard search step
●
Select all (direct and indirect) children:
step: **
$**.price
●
Example: find “price” anywhere:
●
PostgreSQL also supports this, the syntax is .**
10
Usage example: Optimizer trace
●
Optimizer Trace is JSON, log of query optimizer’s actions
– Deeply-nested
– “Recursive”, as SQL allows nesting of subqueries/CTEs/etc
select
JSON_DETAILED(JSON_EXTRACT(trace, '$**.rows_estimation'))
from
information_schema.optimizer_trace;
●
Filters would be helpful: $**.rows_estimation?(@table=="tbl1")
11
JSON Path summary
●
Language for pointing to node(s) in JSON document
– SQL:2016, “SQL/JSON Path language”
●
MariaDB and MySQL implement a subset
– lax mode only
– no support for filters (BAD)
– array index – only [N]
●
MySQL also allows [last] and [M to N]
●
MySQL and MariaDB have recursive-search extension (GOOD)
12
JSON Path in other databases
●
PostgreSQL seems have the most compliant implementation
– Supports filtering, strict/lax modes, etc.
●
Other databases support different and [very] restrictive subsets.
13
JSON_TABLE
News
14
JSON_TABLE
●
JSON_TABLE converts JSON input to a table
– It is a “table function”
– to be used in the FROM clause
●
Introduced in SQL:2016
●
Supported by Oracle DB, MySQL 8
●
Under development in MariaDB
– Trying to get it into 10.6
15
JSON_TABLE by example
●
Start with a JSON
document:
select *
from
JSON_TABLE(@json_doc,
'$[*]' columns(name varchar(32) path '$.name',
price int path '$.price')
) as T
●
Use JSON_TABLE to produce tabular output:
set @json_doc='
[
{"name": "Laptop", "price": 1200},
{"name": "Jeans", "price": 60}
]';
16
JSON_TABLE by example
●
Start with a JSON
document:
set @json_doc='
[
{"name": "Laptop", "price": 1200},
{"name": "Jeans", "price": 60}
]';
select *
from
JSON_TABLE(@json_doc,
'$[*]' columns(name varchar(32) path '$.name',
price int path '$.price')
) as T
●
Use JSON_TABLE to produce tabular output:
+--------+-------+
| name | price |
+--------+-------+
| Laptop | 1200 |
| Jeans | 60 |
+--------+-------+
17
JSON_TABLE syntax
select *
from
JSON_TABLE(@json_doc,
'$[*]' columns(name varchar(32) path '$.name',
price int path '$.price')
) as T
Source document
18
JSON_TABLE syntax
select *
from
JSON_TABLE(@json_doc,
'$[*]' columns(name varchar(32) path '$.name',
price int path '$.price')
) as T
Source document
Path to nodes
to examine
19
JSON_TABLE syntax
select *
from
JSON_TABLE(@json_doc,
'$[*]' columns(name varchar(32) path '$.name',
price int path '$.price')
) as T
Source document
Path to nodes
to examine
Column definitions
20
JSON_TABLE syntax
select *
from
JSON_TABLE(@json_doc,
'$[*]' columns(name varchar(32) path '$.name',
price int path '$.price')
) as T
Source document
Path to nodes
to examine
Column definitions
Column name Path where to get the value
from. The context item is the
node being examined
21
Nested paths
set @json_doc='
[
{"name": "Laptop", "colors": ["black", "white", "red"] },
{"name": "T-Shirt", "colors": ["yellow", "blue"] }
]';
select *
from
JSON_TABLE(@json_doc,
'$[*]'
columns(name varchar(32) path '$.name',
nested path '$.colors[*]'
columns (
color varchar(32) path '$'
)
)
) as T
22
Nested paths
set @json_doc='
[
{"name": "Laptop", "colors": ["black", "white", "red"] },
{"name": "T-Shirt", "colors": ["yellow", "blue"] }
]';
select *
from
JSON_TABLE(@json_doc,
'$[*]'
columns(name varchar(32) path '$.name',
nested path '$.colors[*]'
columns (
color varchar(32) path '$'
)
)
) as T
+---------+--------+
| name | color |
+---------+--------+
| Laptop | black |
| Laptop | white |
| Laptop | red |
| T-Shirt | yellow |
| T-Shirt | blue |
+---------+--------+
23
Multiple nested paths
●
NESTED PATH can be nested
●
Can have “sibling” NESTED PATHs
– The standard allows to specify how to unnest (“the PLAN clause”)
– The default way is “outer join” like:
{
"name": "T-Shirt",
"colors": ["yellow", "blue"],
"sizes": ["Small", "Medium", "Large"]
}
+---------+--------+--------+
| name | color | size |
+---------+--------+--------+
| T-Shirt | yellow | NULL |
| T-Shirt | blue | NULL |
| T-Shirt | NULL | Small |
| T-Shirt | NULL | Medium |
| T-Shirt | NULL | Large |
+---------+--------+--------+
– MySQL (and soon MariaDB) only support “outer join”-like unnesting
●
“no PLAN clause support”.
24
Error handling
columns(column_name type path '$path' [action on empty]
[action on error])
Handling missing values and/or conversion errors
action: NULL
default 'string'
error
● on empty is used when JSON element is missing
● on error is used on datatype conversion error or non-scalar JSON.
●
Both MariaDB and MySQL support this
25
JSON_TABLE and joins
26
JSON_TABLE and joins
[
{"name": "Laptop", "price": 1200},
{"name": "Jeans", "price": 60}
]
[
{"name": "T-Shirt", "price": 10},
{"name": "Headphones", "price": 100}
]
1
2
orders
27
JSON_TABLE and joins
select
orders.order_id,
order_items.name,
order_items.price
from
orders,
JSON_TABLE(orders.items_json,
'$[*]' columns(name varchar(32) path '$.name',
price int path '$.price')
) as order_items
+----------+------------+-------+
| order_id | name | price |
+----------+------------+-------+
| 1 | Laptop | 1200 |
| 1 | Jeans | 60 |
| 2 | T-Shirt | 10 |
| 2 | Headphones | 100 |
+----------+------------+-------+
[
{"name": "Laptop", "price": 1200},
{"name": "Jeans", "price": 60}
]
[
{"name": "T-Shirt", "price": 10},
{"name": "Headphones", "price": 100}
]
1
2
orders
28
JSON_TABLE and joins
●
JSON_TABLE’s argument can refer to other tables
●
LATERAL-like semantics: contents of JSON_TABLE(...) depends on
the parameter
●
Allows to do “normalization” for contents of columns with JSON data
29
JSON_TABLE Summary
●
A table function to convert JSON data to relational form
●
Introduced in SQL:2016
– The standard specifies a lot of features
●
MySQL 8 implements a subset
– PLAN clause is not supported
●
MariaDB: MDEV-17399, under development
– Will implement a subset very similar to MySQL
30
JSON_TABLE in other databases
●
Quoting Markus Winand, https://modern-sql.com/slides/ModernSQL-
2019-05-30.pdf:
●
PostgreSQL have JSON_TABLE under development, too.
31
The takeaways
●
SQL:2016 introduced JSON support
●
MySQL 8 has implemented a subset of it
– The subset is reasonably good
– There are some extensions
●
MariaDB is catching up
– including the extensions
32
The low-hanging fruits
●
JSON Path: [last], [N to M]
●
JSON Path: filtering support
●
Improved JSON_DETAILED function
– It’s a JSON pretty-printer
– Used in development with JSON
– It works, but is fairly dumb
●
All are great to start contributing
– Contact us if interested.
33
Thanks!
Q & A

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Postgres rules
Postgres rulesPostgres rules
Postgres rules
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 
Using JSON with MariaDB and MySQL
Using JSON with MariaDB and MySQLUsing JSON with MariaDB and MySQL
Using JSON with MariaDB and MySQL
 
Using spark data frame for sql
Using spark data frame for sqlUsing spark data frame for sql
Using spark data frame for sql
 
The Ring programming language version 1.8 book - Part 50 of 202
The Ring programming language version 1.8 book - Part 50 of 202The Ring programming language version 1.8 book - Part 50 of 202
The Ring programming language version 1.8 book - Part 50 of 202
 
BGOUG15: JSON support in MySQL 5.7
BGOUG15: JSON support in MySQL 5.7BGOUG15: JSON support in MySQL 5.7
BGOUG15: JSON support in MySQL 5.7
 
Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)
 
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
 
MySQL 5.7 NF – JSON Datatype 활용
MySQL 5.7 NF – JSON Datatype 활용MySQL 5.7 NF – JSON Datatype 활용
MySQL 5.7 NF – JSON Datatype 활용
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Php forum2015 tomas_final
Php forum2015 tomas_finalPhp forum2015 tomas_final
Php forum2015 tomas_final
 
Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1
 
MongoDB Aggregation
MongoDB Aggregation MongoDB Aggregation
MongoDB Aggregation
 
Indexing and Performance Tuning
Indexing and Performance TuningIndexing and Performance Tuning
Indexing and Performance Tuning
 
SICP_2.5 일반화된 연산시스템
SICP_2.5 일반화된 연산시스템SICP_2.5 일반화된 연산시스템
SICP_2.5 일반화된 연산시스템
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
CR17 - Designing a database like an archaeologist
CR17 - Designing a database like an archaeologistCR17 - Designing a database like an archaeologist
CR17 - Designing a database like an archaeologist
 
Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access
 
NoSQL для PostgreSQL: Jsquery — язык запросов
NoSQL для PostgreSQL: Jsquery — язык запросовNoSQL для PostgreSQL: Jsquery — язык запросов
NoSQL для PostgreSQL: Jsquery — язык запросов
 

Ähnlich wie JSON Support in MariaDB: News, non-news and the bigger picture

Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !
Alexander Korotkov
 
MongoDB Advanced Topics
MongoDB Advanced TopicsMongoDB Advanced Topics
MongoDB Advanced Topics
César Rodas
 
JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)
Faysal Shaarani (MBA)
 

Ähnlich wie JSON Support in MariaDB: News, non-news and the bigger picture (20)

Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !
 
Postgre(No)SQL - A JSON journey
Postgre(No)SQL - A JSON journeyPostgre(No)SQL - A JSON journey
Postgre(No)SQL - A JSON journey
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory Database
 
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
 
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
 
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
 
UKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseUKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the Database
 
Greenplum 6 Changes
Greenplum 6 ChangesGreenplum 6 Changes
Greenplum 6 Changes
 
Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesWebscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
 
XML introduction and Uses of XML in JSBSim
XML introduction and Uses of XML in JSBSimXML introduction and Uses of XML in JSBSim
XML introduction and Uses of XML in JSBSim
 
MongoDB Advanced Topics
MongoDB Advanced TopicsMongoDB Advanced Topics
MongoDB Advanced Topics
 
JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)
 
PostgreSQL 9.4 JSON Types and Operators
PostgreSQL 9.4 JSON Types and OperatorsPostgreSQL 9.4 JSON Types and Operators
PostgreSQL 9.4 JSON Types and Operators
 
MongoDB a document store that won't let you down.
MongoDB a document store that won't let you down.MongoDB a document store that won't let you down.
MongoDB a document store that won't let you down.
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database Jones
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 

Mehr von Sergey Petrunya

Mehr von Sergey Petrunya (20)

New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12
 
MariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixesMariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixes
 
Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8
 
Optimizer Trace Walkthrough
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace Walkthrough
 
Optimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databases
 
MariaDB 10.4 - что нового
MariaDB 10.4 - что новогоMariaDB 10.4 - что нового
MariaDB 10.4 - что нового
 
Query Optimizer in MariaDB 10.4
Query Optimizer in MariaDB 10.4Query Optimizer in MariaDB 10.4
Query Optimizer in MariaDB 10.4
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmark
 
MyRocks in MariaDB | M18
MyRocks in MariaDB | M18MyRocks in MariaDB | M18
MyRocks in MariaDB | M18
 
New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3
 
MyRocks in MariaDB
MyRocks in MariaDBMyRocks in MariaDB
MyRocks in MariaDB
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQL
 
Say Hello to MyRocks
Say Hello to MyRocksSay Hello to MyRocks
Say Hello to MyRocks
 
Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2
 
MyRocks in MariaDB: why and how
MyRocks in MariaDB: why and howMyRocks in MariaDB: why and how
MyRocks in MariaDB: why and how
 
Эволюция репликации в MySQL и MariaDB
Эволюция репликации в MySQL и MariaDBЭволюция репликации в MySQL и MariaDB
Эволюция репликации в MySQL и MariaDB
 
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
Common Table Expressions in MariaDB 10.2 (Percona Live Amsterdam 2016)
 
MariaDB 10.1 - что нового.
MariaDB 10.1 - что нового.MariaDB 10.1 - что нового.
MariaDB 10.1 - что нового.
 
Window functions in MariaDB 10.2
Window functions in MariaDB 10.2Window functions in MariaDB 10.2
Window functions in MariaDB 10.2
 
MyRocks: табличный движок для MySQL на основе RocksDB
MyRocks: табличный движок для MySQL на основе RocksDBMyRocks: табличный движок для MySQL на основе RocksDB
MyRocks: табличный движок для MySQL на основе RocksDB
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 

JSON Support in MariaDB: News, non-news and the bigger picture

  • 1. Sergei Petrunia MariaDB devroom FOSDEM 2021 JSON Support in MariaDB News, non-news, and the bigger picture FOSDEM 2021 MariaDB devroom Sergei Petrunia MariaDB developer
  • 3. 3 JSON Path ● A lot of JSON functions accept “JSON Path” expressions – locate element(s) in JSON document ● Path language in MariaDB wasn’t documented – It’s more than “foo.bar.baz” ● Caution: there are many different JSON Path definitions on the web – SQL uses SQL:2016, “SQL/JSON Path language”
  • 4. 4 SQL/JSON Path language ● mode is lax (the default) or strict ● $ is the context element (root by default) ● followed by several steps. path: [mode] $ [step]*
  • 5. 5 Object member selection step ● In strict mode, the context must be an object, it must have a member with the specified name. ● lax mode “ignores” missing elements, “unwraps” 1-element arrays .name .* { "name": "MariaDB" "version": "10.5" } $.name → "MariaDB" $.* → "MariaDB" "10.5" ● Select a member ● Select all members – produces a sequence of values
  • 6. 6 Array element selection step ● Produces a sequence of elements ● Strict mode: indexes must be within bounds [N] [*] [ 10, "abc", {"x":0} ] $[0] → 10 $[last] → {"x":0} ● Select array elements – one – range – last element – list of ^^ – all elements [N to M] [N1,N2,...] [last] $[*] →10 "abc" {"x":0}
  • 7. 7 Filters ● Predicate can have – AND/OR formulas (&&, ||) – comparisons – arithmetics – some functions – parameters passed from outside [ { "item":"Jeans", "color":"blue" }, { "item":"Laptop", "color":"black" } ] ● Filters elements in sequence ?(predicate) $[*]?(@.color=="black").item → "Laptop"
  • 8. 8 MariaDB and MySQL ● Support only lax mode – Not fully compliant: MySQL BUG#102233, MDEV-24573. ● Object member selection is supported ● Array selection: [N] is supported – MySQL also supports [last] and [N to M] ● Filters are not supported – expressions, arithmetics, functions, passing variables
  • 9. 9 Recursive search extension ● Task: find “foo” anywhere in the JSON document ● SQL/JSON Path doesn’t allow this ● Extension: wildcard search step ● Select all (direct and indirect) children: step: ** $**.price ● Example: find “price” anywhere: ● PostgreSQL also supports this, the syntax is .**
  • 10. 10 Usage example: Optimizer trace ● Optimizer Trace is JSON, log of query optimizer’s actions – Deeply-nested – “Recursive”, as SQL allows nesting of subqueries/CTEs/etc select JSON_DETAILED(JSON_EXTRACT(trace, '$**.rows_estimation')) from information_schema.optimizer_trace; ● Filters would be helpful: $**.rows_estimation?(@table=="tbl1")
  • 11. 11 JSON Path summary ● Language for pointing to node(s) in JSON document – SQL:2016, “SQL/JSON Path language” ● MariaDB and MySQL implement a subset – lax mode only – no support for filters (BAD) – array index – only [N] ● MySQL also allows [last] and [M to N] ● MySQL and MariaDB have recursive-search extension (GOOD)
  • 12. 12 JSON Path in other databases ● PostgreSQL seems have the most compliant implementation – Supports filtering, strict/lax modes, etc. ● Other databases support different and [very] restrictive subsets.
  • 14. 14 JSON_TABLE ● JSON_TABLE converts JSON input to a table – It is a “table function” – to be used in the FROM clause ● Introduced in SQL:2016 ● Supported by Oracle DB, MySQL 8 ● Under development in MariaDB – Trying to get it into 10.6
  • 15. 15 JSON_TABLE by example ● Start with a JSON document: select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', price int path '$.price') ) as T ● Use JSON_TABLE to produce tabular output: set @json_doc=' [ {"name": "Laptop", "price": 1200}, {"name": "Jeans", "price": 60} ]';
  • 16. 16 JSON_TABLE by example ● Start with a JSON document: set @json_doc=' [ {"name": "Laptop", "price": 1200}, {"name": "Jeans", "price": 60} ]'; select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', price int path '$.price') ) as T ● Use JSON_TABLE to produce tabular output: +--------+-------+ | name | price | +--------+-------+ | Laptop | 1200 | | Jeans | 60 | +--------+-------+
  • 17. 17 JSON_TABLE syntax select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', price int path '$.price') ) as T Source document
  • 18. 18 JSON_TABLE syntax select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', price int path '$.price') ) as T Source document Path to nodes to examine
  • 19. 19 JSON_TABLE syntax select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', price int path '$.price') ) as T Source document Path to nodes to examine Column definitions
  • 20. 20 JSON_TABLE syntax select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', price int path '$.price') ) as T Source document Path to nodes to examine Column definitions Column name Path where to get the value from. The context item is the node being examined
  • 21. 21 Nested paths set @json_doc=' [ {"name": "Laptop", "colors": ["black", "white", "red"] }, {"name": "T-Shirt", "colors": ["yellow", "blue"] } ]'; select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', nested path '$.colors[*]' columns ( color varchar(32) path '$' ) ) ) as T
  • 22. 22 Nested paths set @json_doc=' [ {"name": "Laptop", "colors": ["black", "white", "red"] }, {"name": "T-Shirt", "colors": ["yellow", "blue"] } ]'; select * from JSON_TABLE(@json_doc, '$[*]' columns(name varchar(32) path '$.name', nested path '$.colors[*]' columns ( color varchar(32) path '$' ) ) ) as T +---------+--------+ | name | color | +---------+--------+ | Laptop | black | | Laptop | white | | Laptop | red | | T-Shirt | yellow | | T-Shirt | blue | +---------+--------+
  • 23. 23 Multiple nested paths ● NESTED PATH can be nested ● Can have “sibling” NESTED PATHs – The standard allows to specify how to unnest (“the PLAN clause”) – The default way is “outer join” like: { "name": "T-Shirt", "colors": ["yellow", "blue"], "sizes": ["Small", "Medium", "Large"] } +---------+--------+--------+ | name | color | size | +---------+--------+--------+ | T-Shirt | yellow | NULL | | T-Shirt | blue | NULL | | T-Shirt | NULL | Small | | T-Shirt | NULL | Medium | | T-Shirt | NULL | Large | +---------+--------+--------+ – MySQL (and soon MariaDB) only support “outer join”-like unnesting ● “no PLAN clause support”.
  • 24. 24 Error handling columns(column_name type path '$path' [action on empty] [action on error]) Handling missing values and/or conversion errors action: NULL default 'string' error ● on empty is used when JSON element is missing ● on error is used on datatype conversion error or non-scalar JSON. ● Both MariaDB and MySQL support this
  • 26. 26 JSON_TABLE and joins [ {"name": "Laptop", "price": 1200}, {"name": "Jeans", "price": 60} ] [ {"name": "T-Shirt", "price": 10}, {"name": "Headphones", "price": 100} ] 1 2 orders
  • 27. 27 JSON_TABLE and joins select orders.order_id, order_items.name, order_items.price from orders, JSON_TABLE(orders.items_json, '$[*]' columns(name varchar(32) path '$.name', price int path '$.price') ) as order_items +----------+------------+-------+ | order_id | name | price | +----------+------------+-------+ | 1 | Laptop | 1200 | | 1 | Jeans | 60 | | 2 | T-Shirt | 10 | | 2 | Headphones | 100 | +----------+------------+-------+ [ {"name": "Laptop", "price": 1200}, {"name": "Jeans", "price": 60} ] [ {"name": "T-Shirt", "price": 10}, {"name": "Headphones", "price": 100} ] 1 2 orders
  • 28. 28 JSON_TABLE and joins ● JSON_TABLE’s argument can refer to other tables ● LATERAL-like semantics: contents of JSON_TABLE(...) depends on the parameter ● Allows to do “normalization” for contents of columns with JSON data
  • 29. 29 JSON_TABLE Summary ● A table function to convert JSON data to relational form ● Introduced in SQL:2016 – The standard specifies a lot of features ● MySQL 8 implements a subset – PLAN clause is not supported ● MariaDB: MDEV-17399, under development – Will implement a subset very similar to MySQL
  • 30. 30 JSON_TABLE in other databases ● Quoting Markus Winand, https://modern-sql.com/slides/ModernSQL- 2019-05-30.pdf: ● PostgreSQL have JSON_TABLE under development, too.
  • 31. 31 The takeaways ● SQL:2016 introduced JSON support ● MySQL 8 has implemented a subset of it – The subset is reasonably good – There are some extensions ● MariaDB is catching up – including the extensions
  • 32. 32 The low-hanging fruits ● JSON Path: [last], [N to M] ● JSON Path: filtering support ● Improved JSON_DETAILED function – It’s a JSON pretty-printer – Used in development with JSON – It works, but is fairly dumb ● All are great to start contributing – Contact us if interested.