Files
five/hbrtl/sqlexpr.go
CharlesKWON dd270d5d9d perf: RTL Go-native migration — 27 optimizations, DML up to 70-90x
Systematic pass through PRG hot paths, promoting them to Go RTL while
preserving Harbour/FiveSql2 semantics. Full log in
docs/RTL-Go-Native-Migration.md.

Bench (bench_sql) vs 2026-04-08 baseline
 - B1  SELECT *             2,192 → 114   µs   (19x)
 - B6  INNER JOIN           9,291 → 233   µs   (40x)
 - B7  CTE simple           8,037 → 129   µs   (62x)
 - B9  ROW_NUMBER           3,705 → 265   µs   (14x)
 - B10 RANK PARTITION       4,748 → 309   µs   (15x)
 - B12 INSERT (WA cache)    4,319 →  63   µs   (69x)
 - B13 UPDATE (WA cache)    6,144 →  68   µs   (90x)
 - B15 CTE+WIN+JOIN        18,395 → 1,873 µs   (10x)

Infrastructure
 - HbHash O(1) Index preserving insertion order (Harbour KEEPORDER)
 - HbDeepClone Go RTL (scalar-sharing, immutable hash keys)
 - MEMRDD auto-imported via gengo; all Five programs get mem:name driver
 - SQL plan + pcode caches (s_hPlanCache, s_hDmlPcodeCache)
 - Opt-in SqlWACacheEnable — dbUseArea/Close/Commit batched for DML

SQL engine
 - FiveSql2 lexer ported to Go (byte FSM) with combined automatic
   template parameterization (literals → ?, concat queries share plan)
 - Go RTL: SqlDistinct, SqlGroupRows, SqlWindowPartitions,
   SqlWindowSortPartition, SqlWindowAssignRank, SqlComputeAggSimple,
   SqlBulkInsert, SqlBulkUpdate, SqlExprHasAgg, SqlEvalHaving
 - CTE / subquery / driving-table materialize paths use MEMRDD
 - SqlCoerce/SqlCmp/SqlIsTrue helpers moved from PRG to Go
 - SqlBulkUpdate defers Flush when WA cache active (APFS fsync was
   dominant B13 cost — 1.6ms/call → gone)

Correctness fixes uncovered during migration
 - ASort default path now sorts dates/logicals/timestamps (was no-op)
 - ORDER BY default NULL placement matches PRG SqlRowCompare across
   Go fast path; explicit NULLS FIRST/LAST honored by both paths
 - SqlBulkUpdate respects EXCLUSIVE vs SHARED mode record locks
 - SqlCmp/SqlCmpEq normalize NumInt vs Double (caught by test 6b)

Verification
 - go test ./...              ALL PASS
 - FiveSql2 test_sql1999      43/43
 - tests/compat_harbour       56/56 (+5 new: ASort dates/logicals,
                              AScan int cross-type)
 - Regression test test_null_order.prg for ORDER BY NULL ordering

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 20:20:14 +09:00

138 lines
3.7 KiB
Go

// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
// All rights reserved.
// Go-native FiveSql2 expression helpers.
// Port of the tight, interpreter-heavy recursive walkers from
// _FiveSql2/src/TSqlExpr.prg into straight Go — the PRG versions
// are bottleneck-prone because every recursion pays the full VM
// frame setup cost, and SqlExprHasAgg is invoked per result
// column per query.
package hbrtl
import (
"five/hbrt"
)
// FiveSql2 AST node kinds — must mirror _FiveSql2/src/FiveSqlDef.ch.
// Nodes are stored as Five arrays { nKind, xVal, xLeft, xRight, xExtra }
// (1-based in PRG, 0-based here).
const (
ndLit = 1
ndCol = 2
ndFn = 3
ndBin = 4
ndUni = 5
ndCase = 6
ndSub = 7
ndPar = 9
ndNil = 10
ndWindow = 12
)
// aggFuncSet mirrors the AGG_FUNCTIONS macro in FiveSqlDef.ch. Names
// are stored in canonical upper case; the PRG parser upper-cases
// function identifiers at parse time so no ToUpper is needed on the
// hot path. If that invariant ever changes, upper-case here.
var aggFuncSet = map[string]struct{}{
"COUNT": {},
"SUM": {},
"AVG": {},
"MIN": {},
"MAX": {},
"GROUP_CONCAT": {},
"STRING_AGG": {},
"LISTAGG": {},
"JSON_ARRAYAGG": {},
"JSON_OBJECTAGG": {},
"XMLAGG": {},
"ANY_VALUE": {},
"BOOL_AND": {},
"BOOL_OR": {},
}
// sqlExprHasAggWalk is the actual recursion shared by the RTL entry
// point. Returns true if the tree rooted at v contains a direct
// aggregate call. Matches TSqlExpr.prg:SqlExprHasAgg — walks into
// ND_BIN children, ND_UNI child, ND_FN args, ND_CASE WHEN/THEN pairs
// and ELSE; does not descend into ND_WINDOW or ND_SUB (those carry
// their own aggregation scope).
func sqlExprHasAggWalk(v hbrt.Value) bool {
if v.IsNil() {
return false
}
arr := v.AsArray()
if arr == nil || len(arr.Items) < 2 {
return false
}
kind := int(arr.Items[0].AsNumInt())
switch kind {
case ndFn:
name := arr.Items[1].AsString()
if _, ok := aggFuncSet[name]; ok {
return true
}
// Scalar function — descend into args for nested aggregates.
if len(arr.Items) >= 3 && arr.Items[2].IsArray() {
for _, a := range arr.Items[2].AsArray().Items {
if sqlExprHasAggWalk(a) {
return true
}
}
}
return false
case ndBin:
if len(arr.Items) < 4 {
return false
}
return sqlExprHasAggWalk(arr.Items[2]) || sqlExprHasAggWalk(arr.Items[3])
case ndUni:
if len(arr.Items) < 3 {
return false
}
return sqlExprHasAggWalk(arr.Items[2])
case ndCase:
// arr.Items[1] is the WHEN/THEN pair array,
// arr.Items[2] is the ELSE branch (may be NIL).
if arr.Items[1].IsArray() {
for _, pair := range arr.Items[1].AsArray().Items {
pa := pair.AsArray()
if pa == nil || len(pa.Items) < 2 {
continue
}
if sqlExprHasAggWalk(pa.Items[0]) || sqlExprHasAggWalk(pa.Items[1]) {
return true
}
}
}
if len(arr.Items) >= 3 && !arr.Items[2].IsNil() {
return sqlExprHasAggWalk(arr.Items[2])
}
return false
}
return false
}
// SqlExprHasAgg(xExpr) → lHasAgg
//
// Returns .T. if the AST tree contains an aggregate function call.
// Drop-in replacement for the PRG SqlExprHasAgg function — same
// output for every input, just without the interpreter per-frame
// cost on deep expression trees.
func SqlExprHasAgg(t *hbrt.Thread) {
t.Frame(1, 0)
defer t.EndProc()
t.RetBool(sqlExprHasAggWalk(t.Local(1)))
}
// Silence "declared and not used" for constants that exist solely to
// document FiveSqlDef.ch layout — keeping them in source form helps
// future walker additions (ND_SUB for subquery flattening, ND_WINDOW
// for window-over-aggregate detection).
var _ = [...]int{ndLit, ndCol, ndSub, ndPar, ndNil, ndWindow}