Skip to main content

Query Client

This is a simple client to query.

example

初始化客户端及元数据

from query_client import QueryClient

qc = QueryClient()

tables = qc.get_meta()
# print(tables) 获取全部可查表,及其coloumns, 元数据
## 目前只有两个相关表注册
can_table = tables["can_detail"]
pcd_table = tables["pcd_label_desc"]

fetch

python
from sqlalchemy.sql.expression import select, text, and_
from query_client import QueryClient
import pandas as pd

qc = QueryClient()
tables = qc.get_meta()
mytable = tables["pcd_label_desc"]

# 1. fetch_by_stmt 初始化 statement, select/where/group_by
stmt = select(mytable.c.dataset, mytable.c.batch_path, text("array_distinct(categories) scenes"), text("cardinality(filter(categories, x -> x IN ('car'))) as target_cnt"))
r = qc.fetch_by_stmt(stmt)
df = pd.DataFrame(r)

# 2. fetch_by_sql
query = "select * from pcd_label_desc limit 10"
r = qc.fetch_by_sql(query)
df = pd.DataFrame(r)

具体示例

  1. 城区场景的筛选
    • 定义单帧指标(各种目标数)
    • group by 序列
  2. fetch_by_sql 实现方式
    • cte 语法:
      • t 表查询单帧的去重场景、不同类型的目标数指标,dataset 及batch 维度
      • 利用 t 表的dataset, batch_path 维度聚合指标
    • code example
      query = """
      WITH t AS (
          SELECT
              dataset,
              batch_path,
              array_distinct(categories) scenes,
              cardinality(
                  filter(
                      categories,
                      x -> x IN (
                          'car',
                          'van',
                          'truck',
                          'mini_truck',
                          'special_truck',
                          'truck',
                          'cyclist',
                          'bicycle',
                          'pedestrian'
                      )
                  )
              ) AS target_cnt,
              cardinality(
                  filter(
                      categories,
                      x -> x IN (
                          'truck',
                          'mini_truck',
                          'special_truck'
                      )
                  )
              ) truck_cnt,
              cardinality(filter(categories, x -> x IN ('car', 'van'))) carvan_cnt,
              cardinality(filter(categories, x -> x = 'pedestrian')) pedestrian_cnt,
              cardinality(
                  filter(categories, x -> x IN ('cyclist', 'bicycle'))
              ) cyclist_cnt
          FROM
              pcd_label_desc
          WHERE
              delivery_date = '20230310'
              AND any_match(
                  road_condition,
                  x -> x = 'highway_road'
              )
      )
      SELECT
          dataset,
          batch_path,
          array_distinct(FLATTEN(ARRAY_AGG(scenes))) scenes,
          SUM(target_cnt) target_cnt,
          SUM(carvan_cnt) carvan_cnt,
          SUM(pedestrian_cnt) pedestrian_cnt,
          SUM(truck_cnt) truck_cnt,
          SUM(cyclist_cnt) cyclist_cnt
      FROM
          t
      GROUP BY
          1,
          2
      ORDER BY
          target_cnt DESC
      """
      
      r = qc.fetch_by_sql(query)
      df = pd.DataFrame(r)
      
  3. fetch_by_stmt 实现方式
    mytable = tables["pcd_label_desc"]
    
    stmt = select(
            mytable.c.dataset, 
            mytable.c.batch_path, # 指定table聚合的维度
    	text("cardinality(filter(categories, x -> x IN ('car', 'van'))) carvan_cnt"), # 定义函数变换指标
            # ... goon
            )
            .where(and_(
                mytable.c.delivery_date == "20230310",
                text("any_match(road_condition, x -> x = 'city_road')") # 过滤城区场景
    	))
    
    # 指标cte 表
    cte = qc.with_cte(stmt)
    
    # 继续聚合
    stmt2 = select(
        cte.c.dataset, 
        cte.c.batch_path,
        text("SUM(carvan_cnt) carvan_cnt")
        ).group_by(cte.c.dataset, cte.c.batch_path)
    
    # 查询结果
    r = qc.fetch_by_stmt(stmt2)
    df = pd.DataFrame(r)
    

Release files for query-client 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for query-client 0.0.1
File Size Uploaded
query_client-0.0.1.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for query-client 0.0.1
File Interpreter ABI Platform
query_client-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / query_client-0.0.1.tar.gz

Download URL query_client-0.0.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6ce95267438135c0b9beb432d6edf9048cd438bfc7fc43593a4e5101f8e1976e
BLAKE2b-256 checksum
How to use checksums
649a67799e79775e66eae44842696ca8c76e79004df258938ed36e47df7203b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release files / query_client-0.0.1-py3-none-any.whl

Download URL query_client-0.0.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
40fba790a25f607f771e106b4cd8217514bc5b437c1335d2ad454c35cef04598
BLAKE2b-256 checksum
How to use checksums
b95c96233baf16fb013d409525c9f2a422d283dc80f614f5bac17a667226e3d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page