← 返回主平台

概述

计算规则(compute)允许你使用公式为每行数据生成新字段。它支持:

  • 基础数学运算(+、-、*、/、%)
  • 数字函数(SUM、AVG、MAX、MIN、ROUND 等)
  • 日期函数(TODAY、DATE_ADD、DATE_DIFF、DATE_FORMAT 等)
  • 字符串函数(STR_REPLACE、STR_SLICE、STR_LEFT 等)
  • 条件函数(IF)
  • Python UDF 脚本执行

基本语法

配置结构

{
  "type": "compute",
  "targetField": "新字段名",      // 结果将存储在这个字段
  "formula": "计算公式"           // 支持函数和运算符
}

简单示例

假设有一行数据:

{
  "price": 100,
  "quantity": 5,
  "discount": 20
}

1️⃣ 简单算术:

{
  "type": "compute",
  "targetField": "total",
  "formula": "price * quantity - discount"
}

// 结果:total = 480

2️⃣ 使用函数:

{
  "type": "compute",
  "targetField": "summary",
  "formula": "SUM(price, quantity) + discount"
}

// 结果:summary = 125

数字函数

基础聚合

函数 作用 示例
SUM(a, b, c...) 求和 SUM(10, 20, 30) → 60
AVG(a, b, c...) 平均值 AVG(10, 20, 30) → 20
MAX(a, b, c...) 最大值 MAX(10, 20, 30) → 30
MIN(a, b, c...) 最小值 MIN(10, 20, 30) → 10

数学运算

函数 作用 示例
ABS(n) 绝对值 ABS(-12.345) → 12.345
ROUND(n, digits) 四舍五入 ROUND(12.345, 2) → 12.35
FLOOR(n, digits) 向下舍入 FLOOR(12.987, 2) → 12.98
CEIL(n, digits) 向上舍入 CEIL(12.341, 2) → 12.35
SQRT(n) 平方根 SQRT(9) → 3
POW(base, exp) 幂运算 POW(2, 3) → 8
💡 提示:数字函数支持混合运算。例如 ROUND(AVG(a, b, c), 2) 可以先计算平均值再四舍五入。

日期函数

TODAY() - 获取当前日期

{
  "type": "compute",
  "targetField": "today_date",
  "formula": "TODAY()"
}

// 结果:today_date = "2024-03-23"

DATE_ADD() - 日期加减

语法:DATE_ADD(date, n, unit)

单位 示例 结果
"day" DATE_ADD("1999-06-15", 10, "day") "1999-06-25"
"month" DATE_ADD("1999-06-15", 3, "month") "1999-09-15"
"year" DATE_ADD("1999-06-15", 18, "year") "2017-06-15"
"week" DATE_ADD("1999-06-15", 2, "week") "1999-06-29"

负数表示减法:

DATE_ADD("1999-06-15", -5, "day")  // → "1999-06-10"

DATE_DIFF() - 日期差值

语法:DATE_DIFF(date1, date2, unit)

{
  "type": "compute",
  "targetField": "age_days",
  "formula": "DATE_DIFF('2024-03-23', dob, 'day')"
}

// 示例:
// dob = "2000-01-01" → age_days = 8928

DATE_FORMAT() - 日期格式转换

语法:DATE_FORMAT(date, format_string)

DATE_FORMAT("2024-03-07", "DD/MM/YYYY")  // → "07/03/2024"
DATE_FORMAT("2024-03-07", "YYYY-MM-DD")  // → "2024-03-07"
DATE_FORMAT("2024-03-07", "YY")          // → "24"

DATE_PART() - 提取日期部分

语法:DATE_PART(date, part)

DATE_PART("2024-03-15", "year")     // → 2024
DATE_PART("2024-03-15", "month")    // → 3
DATE_PART("2024-03-15", "day")      // → 15
DATE_PART("2024-03-15", "quarter")  // → 1
DATE_PART("2024-03-15", "weekday")  // → 5 (Friday)
⚠️ 注意:日期格式应为 YYYY-MM-DD(ISO 8601)。如果日期无效,函数会返回原始值。

字符串函数

STR_REPLACE() - 字符串替换

STR_REPLACE("a-b-c", "-", "_")  // → "a_b_c"
STR_REPLACE("hello", "x", "y")  // → "hello"(无匹配)
STR_REPLACE("helloworld", "world", "")  // → "hello"(删除)

STR_SLICE() - 字符串截取

STR_SLICE("Hello World", 6, 11)  // → "World"
STR_SLICE("Hello World", 6)      // → "World"
STR_SLICE("Hello World", -5)     // → "World"(从末尾开始)

STR_LEFT() / STR_RIGHT() - 取左/右部分

STR_LEFT("ABCDEF", 3)   // → "ABC"
STR_RIGHT("ABCDEF", 3)  // → "DEF"

STR_PAD() - 字符串填充

STR_PAD("42", 5, "0", "left")   // → "00042"
STR_PAD("42", 5, "0", "right")  // → "42000"

STR_SPLIT() - 字符串分割

STR_SPLIT("a,b,c", ",", 0)  // → "a"(第0个元素)
STR_SPLIT("a,b,c", ",", 1)  // → "b"(第1个元素)

FIND() - 查找位置

FIND("hello world", "world")  // → 6(首次出现位置)

条件函数

IF() - 条件判断

语法:IF(condition, true_value, false_value)

{
  "type": "compute",
  "targetField": "category",
  "formula": "IF(salary > 10000, 'high', 'low')"
}

// 示例:
// salary = 12000 → category = "high"
// salary = 8000 → category = "low"

支持的比较运算符:

  • == - 等于
  • != - 不等于
  • > - 大于
  • < - 小于
  • >= - 大于等于
  • <= - 小于等于

嵌套条件:

IF(age < 18, 'minor', IF(age < 65, 'adult', 'senior'))

实战示例

例 1:计算订单总金额

{
  "type": "compute",
  "targetField": "order_total",
  "formula": "price * quantity * (1 - discount_rate / 100)"
}

// 数据:price=100, quantity=2, discount_rate=10
// 结果:order_total = 180

例 2:计算员工年龄

{
  "type": "compute",
  "targetField": "age",
  "formula": "DATE_DIFF(TODAY(), dob, 'year')"
}

// 数据:dob="1990-05-15"
// 结果:age = 33(或34,取决于当前日期)

例 3:提取和规范化手机号

{
  "type": "compute",
  "targetField": "phone_clean",
  "formula": "STR_REPLACE(STR_REPLACE(phone, '-', ''), ' ', '')"
}

// 数据:phone="123-4567-8900"
// 结果:phone_clean = "1234567890"

例 4:按收入分级

{
  "type": "compute",
  "targetField": "income_level",
  "formula": "IF(annual_income < 50000, 'entry', IF(annual_income < 100000, 'mid', 'senior'))"
}

// 根据年收入分级为:entry、mid、senior

例 5:链式计算

在实际使用中,你可以链式应用多个计算规则:

// 第一步:计算小计
{
  "type": "compute",
  "targetField": "subtotal",
  "formula": "price * quantity"
}

// 第二步:计算税费
{
  "type": "compute",
  "targetField": "tax",
  "formula": "subtotal * 0.1"
}

// 第三步:计算最终总额
{
  "type": "compute",
  "targetField": "total",
  "formula": "subtotal + tax"
}

常见错误与排查

❌ 错误 1:变量名拼写错误

// 错误:使用了不存在的字段
"formula": "prices * quantity"  // 应该是 "price"

解决:检查数据中的字段名是否正确

❌ 错误 2:日期格式错误

// 错误:日期格式不是 YYYY-MM-DD
"formula": "DATE_DIFF(TODAY(), '2024/03/23', 'day')"

解决:确保日期格式为 YYYY-MM-DD

❌ 错误 3:忘记引号

// 错误:字符串没有引号
"formula": "STR_REPLACE(name, foo, bar)"

// 正确:
"formula": "STR_REPLACE(name, 'foo', 'bar')"

❌ 错误 4:除以零

// 危险:可能导致错误
"formula": "total / count"  // count 可能为 0

// 更安全:
"formula": "IF(count == 0, 0, total / count)"

性能提示

  • ✅ 在大数据集上,计算规则执行速度很快(毫秒级)
  • ✅ 避免在计算中使用过于复杂的嵌套条件
  • ⚠️ 如果计算逻辑太复杂,考虑拆分为多条规则
  • ⚠️ 对超大字符串的操作(几MB)可能较慢
💡 下一步:掌握了计算规则后,继续学习聚合规则来统计分析数据。