[improvement][headless] Fix interval time sql error(#1717) (#1786)

This commit is contained in:
jipeli
2024-10-11 18:53:49 +08:00
committed by GitHub
parent 4408bf4325
commit b6e4577784

View File

@@ -1,14 +1,16 @@
package com.tencent.supersonic.common.calcite;
import com.google.common.base.Preconditions;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlIntervalLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.sql.*;
import org.apache.calcite.sql.fun.SqlMonotonicBinaryOperator;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.validate.SqlConformance;
import org.checkerframework.checker.nullness.qual.Nullable;
/** customize the SqlDialect */
/**
* customize the SqlDialect
*/
public class SemanticSqlDialect extends SqlDialect {
private static final SqlConformance tagTdwSqlConformance = new SemanticSqlConformance();
@@ -85,4 +87,36 @@ public class SemanticSqlDialect extends SqlDialect {
public boolean supportsNestedAggregations() {
return false;
}
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
if (modifyIntervalTime(call, writer, leftPrec, rightPrec)) {
return;
}
super.unparseCall(writer, call, leftPrec, rightPrec);
}
private Boolean modifyIntervalTime(SqlCall call, SqlWriter writer, int leftPrec,
int rightPrec) {
SqlOperator operator = call.getOperator();
if (operator instanceof SqlMonotonicBinaryOperator
&& call.getKind().equals(SqlKind.TIMES)) {
if (call.getOperandList() != null && call.getOperandList().size() == 2
&& call.getOperandList().get(1) instanceof SqlIntervalLiteral) {
SqlIntervalLiteral intervalOperand =
(SqlIntervalLiteral) call.getOperandList().get(1);
SqlIntervalLiteral.IntervalValue interval =
(SqlIntervalLiteral.IntervalValue) intervalOperand.getValue();
call.setOperand(1, SqlNumericLiteral.createExactNumeric(interval.toString(),
SqlParserPos.ZERO));
writer.keyword(SqlKind.INTERVAL.name());
call.unparse(writer, leftPrec, rightPrec);
unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
RelDataTypeSystem.DEFAULT);
return true;
}
}
return false;
}
}