Skip to content

Support constant folding for insert values - #17850

Open
Caideyipi wants to merge 13 commits into
masterfrom
insert-values-constant-folding
Open

Caideyipi wants to merge 13 commits into
masterfrom
insert-values-constant-folding

Conversation

@Caideyipi

Copy link
Copy Markdown
Collaborator

Summary

  • defer table-model INSERT VALUES materialization until analysis so values can reuse IrExpressionInterpreter constant folding
  • throw a readable SemanticException when an insert expression still is not constant after folding
  • add relational session coverage for folded INSERT VALUES expressions

Verification

  • mvn -pl iotdb-core/datanode spotless:check
  • mvn -pl integration-test spotless:check -P with-integration-tests
  • mvn -pl iotdb-core/datanode -DskipTests compile (fails in pre-existing generated aggregation sources: missing ComparatorChain / getModeMapSizeThreshold)

@Caideyipi
Caideyipi force-pushed the insert-values-constant-folding branch from 96ed42c to 88803de Compare June 5, 2026 04:08
@codecov

codecov Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 54.72313% with 139 lines in your changes missing coverage. Please review.
✅ Project coverage is 46.84%. Comparing base (f028736) to head (7decea7).
⚠️ Report is 74 commits behind head on master.

Files with missing lines Patch % Lines
...an/relational/planner/IrExpressionInterpreter.java 17.11% 92 Missing ⚠️
...ryengine/plan/relational/sql/ast/InsertValues.java 84.10% 24 Missing ⚠️
.../queryengine/plan/relational/sql/util/AstUtil.java 52.94% 16 Missing ⚠️
...nner/plan/node/write/RelationalInsertRowsNode.java 0.00% 5 Missing ⚠️
...ne/plan/relational/analyzer/StatementAnalyzer.java 33.33% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #17850      +/-   ##
============================================
+ Coverage     46.42%   46.84%   +0.41%     
  Complexity      713      713              
============================================
  Files          5537     5538       +1     
  Lines        404332   404728     +396     
  Branches      52852    52917      +65     
============================================
+ Hits         187717   189582    +1865     
+ Misses       216615   215146    -1469     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Caideyipi
Caideyipi marked this pull request as draft June 9, 2026 07:23
Caideyipi added 3 commits June 9, 2026 16:29
…ant-folding

# Conflicts:
#	iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
@Caideyipi
Caideyipi marked this pull request as ready for review July 3, 2026 03:47
@sonarqubecloud

Copy link
Copy Markdown

Retain uniform insert targets and semantic errors for unbound parameters in the constant-folding path. Align time-expression integration tests and localize insert error messages.
Retain constant-folded timestamp assertions alongside the FIELD schema validation setup and preserve both locales' insert and LOAD messages.

Validated English and Chinese full-reactor test compilation, formatting, 3 InsertValuesTest cases, and 4 targeted TableSimpleIT methods.

if (set != null) {
return set.contains(value);
return set.contains(normalizeArithmeticValue(value, type(node.getValue())));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found and fixed in 939d153bdf9: folded INT32 arithmetic can produce Integer while an INT32 literal can produce Long, so raw Set.contains made 1 + 1 IN (2, 3) false. The IN cache now normalizes same-type values to the SQL native container, and mixed SQL types use numeric comparison. testFoldedNumericInPredicates covers both paths.


private Boolean evaluateNumericComparison(
ComparisonExpression.Operator operator, Number left, Number right) {
if (Double.isNaN(left.doubleValue()) || Double.isNaN(right.doubleValue())) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found and fixed in 939d153bdf9: Double.compare orders NaN above finite values and treats NaN as equal to itself, so constant folding incorrectly made 1.0 < NaN, NaN >= 1.0, NaN = NaN, and the derived <> predicate true. The interpreter now returns false whenever either operand is NaN, matching CompareBinaryColumnTransformer. testFoldedNaNComparisons and the focused IT cover these cases.

@Caideyipi

Copy link
Copy Markdown
Collaborator Author

Self-review passed.

I reviewed the full PR diff and fixed two correctness issues in 939d153bdf9:

  • IN constant folding now normalizes numeric containers and uses numeric comparison for mixed SQL numeric types, so folded arithmetic matches literal values.
  • Numeric comparisons now reject NaN operands, including <>, matching CompareBinaryColumnTransformer.

Verification:

  • spotless:apply -P with-integration-tests: passed.
  • English source compile/install path with the constrained 2 GB retry: passed; InsertValuesTest: 5/5.
  • Chinese full-reactor test-compile -P with-zh-locale: passed.
  • Direct Failsafe execution: IoTDBInsertTableIT 3/3 and IoTDBSessionRelationalIT 2/2 passed.
  • The standard verify -pl integration-test -am path was blocked by the unrelated pre-existing DropPipeProcedureTest test-compilation error; the direct Failsafe run completed the five requested methods successfully.
  • The earlier PR Sonar job failed inside the scanner with the NOPLoggerFactory/LoggerContext incompatibility, so that CI check is not claimed as passing.

@sonarqubecloud

sonarqubecloud Bot commented Nov 6, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant