Files
five/_FiveSql2/test/test_null_order.prg
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

141 lines
4.3 KiB
Plaintext

// Regression test for NULL ordering in ORDER BY.
// Both the Go fast path (SqlOrderBy) and the PRG fallback
// (SqlRowCompare) must produce the same order and honor explicit
// NULLS FIRST/LAST from SQL:2003.
//
// Default (no NULLS spec): NULLs sort as the largest value — last in
// ASC, first in DESC. Matches PostgreSQL default and the legacy PRG
// SqlRowCompare behavior that predates the Go port.
STATIC s_nPass := 0
STATIC s_nFail := 0
STATIC s_nTotal := 0
PROCEDURE Main()
ErrorBlock( {|e| QOut( "TRAP: " + e:description + " " + e:operation ), Break(e) } )
? "================================================================"
? " FiveSql NULL Ordering Test"
? "================================================================"
?
SetupData()
// Diagnostic: show what score column actually contains after UPDATE,
// then what ORDER BY score ASC produces.
LOCAL aR := five_SQL( "SELECT name, score FROM nullord" )
LOCAL i, aRows
? "Raw rows:"
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
aRows := aR[ 2 ]
FOR i := 1 TO Len( aRows )
? " name=" + AllTrim( aRows[ i ][ 1 ] ), "score=" + ;
iif( aRows[ i ][ 2 ] == NIL, "NIL", LTrim( Str( aRows[ i ][ 2 ] ) ) )
NEXT
ENDIF
?
aR := five_SQL( "SELECT name, score FROM nullord ORDER BY score" )
? "ORDER BY score ASC:"
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
aRows := aR[ 2 ]
FOR i := 1 TO Len( aRows )
? " name=" + AllTrim( aRows[ i ][ 1 ] ), "score=" + ;
iif( aRows[ i ][ 2 ] == NIL, "NIL", LTrim( Str( aRows[ i ][ 2 ] ) ) )
NEXT
ENDIF
?
TestDefaultAsc()
TestDefaultDesc()
TestNullsFirstAsc()
TestNullsLastDesc()
?
? "================================================================"
? " Results:", LTrim(Str(s_nPass)), "/", LTrim(Str(s_nTotal)), "passed"
? "================================================================"
CleanupData()
RETURN
STATIC PROCEDURE SetupData()
LOCAL aFields
aFields := { ;
{"NAME", "C", 20, 0}, ;
{"SCORE", "N", 6, 0} }
dbCreate( "nullord", aFields )
USE "nullord" NEW EXCLUSIVE
APPEND BLANK ; REPLACE NAME WITH "A", SCORE WITH 10
APPEND BLANK ; REPLACE NAME WITH "B", SCORE WITH 30
APPEND BLANK ; REPLACE NAME WITH "C"
APPEND BLANK ; REPLACE NAME WITH "D", SCORE WITH 20
APPEND BLANK ; REPLACE NAME WITH "E"
CLOSE ALL
// Null out C and E via UPDATE — SCORE column goes from numeric 0 to NIL.
five_SQL( "UPDATE nullord SET score = NULL WHERE name = 'C'" )
five_SQL( "UPDATE nullord SET score = NULL WHERE name = 'E'" )
RETURN
STATIC PROCEDURE CleanupData()
CLOSE ALL
FErase( "nullord.dbf" )
RETURN
STATIC FUNCTION NameOrder( aResult )
LOCAL cOut := "", i, aRows
IF ValType( aResult ) == "A" .AND. Len( aResult ) >= 2
aRows := aResult[ 2 ]
FOR i := 1 TO Len( aRows )
cOut += AllTrim( aRows[ i ][ 1 ] )
NEXT
ENDIF
RETURN cOut
STATIC PROCEDURE Check( cLabel, cGot, cWant )
s_nTotal++
IF cGot == cWant
s_nPass++
? " PASS:", cLabel, "→", cGot
ELSE
s_nFail++
? " FAIL:", cLabel, "→ got", cGot, "want", cWant
ENDIF
RETURN
// Induce NULL via NULLIF(score, 0) — DBF N fields can't store NULL, so
// we turn the sentinel 0 into NULL at projection time. C and E have
// score=0 → projected s = NULL.
STATIC PROCEDURE TestDefaultAsc()
LOCAL aR := five_SQL( "SELECT name, NULLIF(score, 0) AS s FROM nullord ORDER BY s" )
// Default ASC: NULLs sort as largest → non-NULLs first (A=10, D=20, B=30), then NULLs (C, E)
Check( "default ASC (NULLs last)", NameOrder( aR ), "ADBCE" )
RETURN
STATIC PROCEDURE TestDefaultDesc()
LOCAL aR := five_SQL( "SELECT name, NULLIF(score, 0) AS s FROM nullord ORDER BY s DESC" )
// Default DESC: NULLs sort as largest → NULLs first (C, E), then descending (B=30, D=20, A=10)
Check( "default DESC (NULLs first)", NameOrder( aR ), "CEBDA" )
RETURN
STATIC PROCEDURE TestNullsFirstAsc()
LOCAL aR := five_SQL( "SELECT name, NULLIF(score, 0) AS s FROM nullord ORDER BY s ASC NULLS FIRST" )
Check( "ASC NULLS FIRST", NameOrder( aR ), "CEADB" )
RETURN
STATIC PROCEDURE TestNullsLastDesc()
LOCAL aR := five_SQL( "SELECT name, NULLIF(score, 0) AS s FROM nullord ORDER BY s DESC NULLS LAST" )
Check( "DESC NULLS LAST", NameOrder( aR ), "BDACE" )
RETURN