Introduction to Jsoniq
Total Page:16
File Type:pdf, Size:1020Kb
JSONiq 0.1 Introduction to JSONiq The SQL of NoSQL Ghislain Fourny Introduction to JSONiq JSONiq 0.1 Introduction to JSONiq The SQL of NoSQL Edition 0 Author Ghislain Fourny JSONiq is a query and processing language specifically designed for the popular JSON data mod- el. The main ideas behind JSONiq are based on lessons learned in more than 30 years of relation- al query systems and more than 15 years of experience with designing and implementing query lan- guages for semi-structured data. As a result, JSONiq is an expressive and highly optimizable lan- guage to query and update any kind of JSONiq store or resource. It enables developers to leverage the same productive high-level language across a variety of NoSQL products. This book gives a com- plete introduction to the JSONiq language. It does so by giving examples for all types of expressions and functions. Those examples can be immediately used because they work standalone, which allows the interested reader to start diving into the language. 1. Introduction 1 1.1. NoSQL - Why Are Relational Databases Not Good Enough? .......................................... 1 1.2. Why JSONiq? .............................................................................................................. 1 1.3. How to Run the Queries in This Book? ......................................................................... 2 1.4. Acknowledgements ....................................................................................................... 2 I. JSON and the JSONiq Data Model 5 2. The JSON Syntax 7 2.1. JSON Strings ....................................................................................................... 7 2.2. JSON Numbers .................................................................................................... 7 2.3. JSON Booleans ................................................................................................... 8 2.4. JSON Null ........................................................................................................... 8 2.5. JSON Objects ...................................................................................................... 8 3. The JSONiq Data Model 11 3.1. JSONiq Values: Items and Sequences ................................................................ 11 3.2. Objects .............................................................................................................. 13 3.3. Arrays ................................................................................................................ 13 3.4. Atomics .............................................................................................................. 13 4. The JSONiq Type System 15 4.1. Item Types ......................................................................................................... 16 4.1.1. Atomic Types .......................................................................................... 16 4.1.2. JSON Item Types : Object Types and Array Types .................................... 17 4.1.3. The Most General Item Type. ................................................................... 18 4.2. Sequence Types ................................................................................................ 18 II. Construction of Items and JSON Navigation 21 5. Construction of Items 23 5.1. Atomic Literals ................................................................................................... 23 5.1.1. String Literals .......................................................................................... 23 5.1.2. Number Literals. ...................................................................................... 23 5.1.3. Boolean and Null Literals ......................................................................... 24 5.2. Object Constructors ............................................................................................ 24 5.3. Array Constructors ............................................................................................. 25 5.4. Composing Constructors ..................................................................................... 26 6. Collections 29 6.1. Collections Used Throughout This Book .............................................................. 29 7. JSON Navigation 31 7.1. Object Navigation ............................................................................................... 31 7.2. Array Unboxing .................................................................................................. 33 7.3. Sequence Filtering ............................................................................................. 34 7.4. Array Navigation ................................................................................................. 35 III. JSONiq Expressions 37 8. Basic Operations 39 8.1. Construction of Sequences ................................................................................. 39 8.1.1. Comma Operator ..................................................................................... 39 8.1.2. Range Operator ....................................................................................... 39 8.2. Parenthesized Expressions ................................................................................. 40 8.3. Arithmetics ......................................................................................................... 40 iii Introduction to JSONiq 8.4. String Concatenation .......................................................................................... 41 8.5. Comparison ........................................................................................................ 41 8.6. Empty Sequence Behavior .................................................................................. 42 8.7. Logic ................................................................................................................. 42 8.7.1. Propositional Logic .................................................................................. 43 8.7.2. First-Order Logic (Quantified Variables) ..................................................... 44 8.8. Builtin Functions ................................................................................................. 44 9. Control Flow Expressions 47 9.1. Conditional Expressions ...................................................................................... 47 9.2. Switch expressions ............................................................................................. 48 9.3. Try-Catch expressions ........................................................................................ 49 10. FLWOR Expressions 51 10.1. Variables .......................................................................................................... 51 10.2. For Clauses ..................................................................................................... 51 10.3. Where Clauses ................................................................................................. 56 10.4. Order Clauses .................................................................................................. 57 10.5. Group Clauses ................................................................................................. 60 10.6. Let Clauses ...................................................................................................... 63 10.7. Count Clauses .................................................................................................. 65 10.8. Map Operator ................................................................................................... 65 10.9. Composing FLWOR Expressions ....................................................................... 66 10.10. Ordered and Unordered Expressions ............................................................... 67 11. Expressions Dealing with Types 69 11.1. Instance-of Expressions .................................................................................... 69 11.2. Treat Expressions ............................................................................................. 69 11.3. Castable Expressions ....................................................................................... 70 11.4. Cast Expressions ............................................................................................. 70 11.5. Typeswitch Expressions .................................................................................... 71 IV. Prolog, Modules and Functions 73 12. Prologs 75 12.1. Setters. ............................................................................................................ 75 12.1.1. Default Ordering Mode ........................................................................... 75 12.1.2. Default Ordering Behaviour for Empty Sequences .................................... 76 12.1.3. Default Decimal Format .......................................................................... 76 12.2. Namespaces .................................................................................................... 76 12.3. Global Variables ............................................................................................... 77 12.4. User-Defined Functions ..................................................................................... 78 13. Modules 81 14. Function Library 83 V. Advanced Notes 89 15. Errors 91 16. Equality vs. Identity 93 17. Sequences vs. Arrays 95 18. Null vs. Empty Sequence 99 19. Reference 103 iv Chapter