JSON_PATH_QUERY_FIRST (Lakehouse v1)
指定したJSON値に対してJSONパスが返す最初のJSON項目を取得します。
Analyze構文
Section titled “Analyze構文”func.json_path_query_first(<variant>, <path_name>)Analyzeの例
Section titled “Analyzeの例”table.name, func.json_path_query_first(table.details, '$.features.*').alias('first_feature')
┌────────────┬───────────────┐│ name │ first_feature │├────────────┼───────────────┤│ Laptop │ "16GB" ││ Laptop │ "16GB" ││ Smartphone │ "4GB" ││ Smartphone │ "4GB" ││ Headphones │ "20h" ││ Headphones │ "20h" │└────────────┴───────────────┘JSON_PATH_QUERY_FIRST(<variant>, '<path_name>')VARIANT
テーブルの作成とサンプルデータの挿入
CREATE TABLE products ( name VARCHAR, details VARIANT);
INSERT INTO products (name, details)VALUES ('Laptop', '{"brand": "Dell", "colors": ["Black", "Silver"], "price": 1200, "features": {"ram": "16GB", "storage": "512GB"}}'), ('Smartphone', '{"brand": "Apple", "colors": ["White", "Black"], "price": 999, "features": {"ram": "4GB", "storage": "128GB"}}'), ('Headphones', '{"brand": "Sony", "colors": ["Black", "Blue", "Red"], "price": 150, "features": {"battery": "20h", "bluetooth": "5.0"}}');クエリのデモ:製品詳細から最初の特徴を抽出する
SELECT name, JSON_PATH_QUERY(details, '$.features.*') AS all_features, JSON_PATH_QUERY_FIRST(details, '$.features.*') AS first_featureFROM products;結果
┌────────────┬──────────────┬───────────────┐│ name │ all_features │ first_feature │├────────────┼──────────────┼───────────────┤│ Laptop │ "16GB" │ "16GB" ││ Laptop │ "512GB" │ "16GB" ││ Smartphone │ "4GB" │ "4GB" ││ Smartphone │ "128GB" │ "4GB" ││ Headphones │ "20h" │ "20h" ││ Headphones │ "5.0" │ "20h" │└────────────┴──────────────┴───────────────┘