Skip to content

Commit 0d66343

Browse files
committed
Preserve source text for float literals in CAST expressions
- Add Source field to ast.Literal to store original source text - Set Source field when parsing decimal float literals - Use Source field in formatExprAsString for CAST operator syntax - This preserves "0.0" instead of outputting "0" for zero floats Fixes iceberg_bucket and other tests with float casting.
1 parent e0490f2 commit 0d66343

File tree

12 files changed

+14
-77
lines changed

12 files changed

+14
-77
lines changed

ast/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ type Literal struct {
10791079
Position token.Position `json:"-"`
10801080
Type LiteralType `json:"type"`
10811081
Value interface{} `json:"value"`
1082+
Source string `json:"source,omitempty"` // Original source text (for preserving 0.0 vs 0)
10821083
Negative bool `json:"negative,omitempty"` // True if literal was explicitly negative (for -0)
10831084
}
10841085

internal/explain/format.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ func formatExprAsString(expr ast.Expression) string {
374374
}
375375
return fmt.Sprintf("%d", e.Value)
376376
case ast.LiteralFloat:
377+
// Use Source field if available to preserve original representation (e.g., "0.0")
378+
if e.Source != "" {
379+
return e.Source
380+
}
377381
if e.Negative {
378382
switch v := e.Value.(type) {
379383
case float64:

parser/expression.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ func (p *Parser) parseNumber() ast.Expression {
751751
} else {
752752
lit.Type = ast.LiteralFloat
753753
lit.Value = f
754+
lit.Source = value // Preserve original source text (e.g., "0.0" vs "0")
754755
}
755756
} else if isHexFloat {
756757
// Parse hex float (Go doesn't support this directly, approximate)
@@ -860,6 +861,7 @@ func (p *Parser) parseUnaryMinus() ast.Expression {
860861
f, _ := strconv.ParseFloat(numVal, 64)
861862
lit.Type = ast.LiteralFloat
862863
lit.Value = f
864+
lit.Source = numVal // Preserve original source text
863865
} else {
864866
i, _ := strconv.ParseInt(numVal, 10, 64)
865867
lit.Value = i
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt11": true,
4-
"stmt12": true,
5-
"stmt13": true,
6-
"stmt14": true,
7-
"stmt15": true,
8-
"stmt27": true,
9-
"stmt28": true,
10-
"stmt29": true,
11-
"stmt30": true,
12-
"stmt31": true
13-
}
14-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt18": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"explain_todo":{"stmt10":true,"stmt11":true,"stmt9":true}}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt14": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
{
22
"explain_todo": {
3-
"stmt10": true,
43
"stmt27": true,
54
"stmt30": true,
65
"stmt31": true,
76
"stmt32": true,
87
"stmt33": true,
98
"stmt34": true,
10-
"stmt35": true,
11-
"stmt4": true,
12-
"stmt5": true,
13-
"stmt6": true,
14-
"stmt7": true,
15-
"stmt8": true,
16-
"stmt9": true
9+
"stmt35": true
1710
}
1811
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt24": true,
4-
"stmt25": true,
5-
"stmt26": true,
6-
"stmt31": true,
7-
"stmt32": true,
8-
"stmt33": true
3+
"stmt26": true
94
}
105
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
{
22
"explain_todo": {
3-
"stmt38": true,
4-
"stmt39": true,
5-
"stmt40": true,
6-
"stmt41": true,
73
"stmt42": true,
8-
"stmt43": true,
9-
"stmt52": true,
10-
"stmt53": true,
11-
"stmt54": true,
12-
"stmt55": true
4+
"stmt43": true
135
}
146
}

0 commit comments

Comments
 (0)