Skip to content

Commit

Permalink
[feature][dingo-executor]Support trace for insert, select limit and
Browse files Browse the repository at this point in the history
order by
  • Loading branch information
zhaoyuan2024 authored and guojn1 committed Oct 9, 2024
1 parent 0d00dc9 commit e2f258a
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 11 deletions.
1 change: 1 addition & 0 deletions dingo-calcite/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ data: {

statementParserMethods: [
"SqlTraceSelect"
"SqlTraceInsert"
"SqlDescTable"
"SqlTruncate"
"SqlGrant"
Expand Down
99 changes: 98 additions & 1 deletion dingo-calcite/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,55 @@ SqlNode OrderByLimitOpt(SqlNode e) :
}
}

/** Reads optional "ORDER BY", "LIMIT", "OFFSET", "FETCH" following a query,
* {@code e}. If any of them are present, adds them to the query;
* otherwise returns the query unchanged.
* Throws if they are present and {@code e} is not a query. */
SqlNode OrderByLimitTraceOpt(SqlNode e) :
{
final SqlNodeList orderBy;
final Span s = Span.of();
SqlNode[] offsetFetch = {null, null};
ExportOptions exportOptions = null;
boolean forUpdate=false;
}
{
(
// use the syntactic type of the expression we just parsed
// to decide whether ORDER BY makes sense
orderBy = OrderBy(e.isA(SqlKind.QUERY))
| { orderBy = null; }
)
[
LimitClause(s, offsetFetch)
[ OffsetClause(s, offsetFetch) ]
|
OffsetClause(s, offsetFetch)
[
LimitClause(s, offsetFetch) {
if (!this.conformance.isOffsetLimitAllowed()) {
throw SqlUtil.newContextException(s.end(this),
RESOURCE.offsetLimitNotAllowed());
}
}
|
FetchClause(offsetFetch)
]
|
FetchClause(offsetFetch)
]
[ exportOptions = exportOptions() ]
[ <FOR><UPDATE> { forUpdate = true;} ]
{
if (orderBy != null || offsetFetch[0] != null || offsetFetch[1] != null) {
return new io.dingodb.calcite.grammar.dql.SqlOrderBy(getPos(), e,
Util.first(orderBy, SqlNodeList.EMPTY),
offsetFetch[0], offsetFetch[1], true);
}
return e;
}
}

/**
* Parses an OFFSET clause in an ORDER BY expression.
*/
Expand Down Expand Up @@ -1395,8 +1444,10 @@ SqlNode SqlTraceSelect() :
final SqlNode having;
final SqlNodeList windowDecls;
final List<SqlNode> hints = new ArrayList<SqlNode>();
final SqlNodeList orderBy;
final Span s;
boolean forUpdate = false;
SqlNode[] offsetFetch = {null, null};
ExportOptions exportOptions = null;
}
{
Expand Down Expand Up @@ -1428,6 +1479,8 @@ SqlNode SqlTraceSelect() :
( groupBy = GroupBy() | { groupBy = null; } )
( having = Having() | { having = null; } )
( windowDecls = Window() | { windowDecls = null; } )
( orderBy = OrderBy(true) | { orderBy = null; } )
[ LimitClause(s, offsetFetch) ]
[ exportOptions = exportOptions() ]
[ <FOR> <UPDATE> { forUpdate=true; }]
|
Expand All @@ -1436,13 +1489,14 @@ SqlNode SqlTraceSelect() :
where = null;
groupBy = null;
having = null;
orderBy = null;
windowDecls = null;
}
)
{
return new SqlSelect(s.end(this), keywordList,
new SqlNodeList(selectList, Span.of(selectList).pos()),
fromClause, where, groupBy, having, windowDecls, null, null, null,
fromClause, where, groupBy, having, windowDecls, orderBy, null, null,
new SqlNodeList(hints, getPos()),
exportOptions, true);
}
Expand Down Expand Up @@ -1873,6 +1927,49 @@ SqlNode SqlInsert() :
}
}

/**
* Parses a leaf insert into expression.
*/
SqlNode SqlTraceInsert() :
{
final List<SqlLiteral> keywords = new ArrayList<SqlLiteral>();
final SqlNodeList keywordList;
final SqlIdentifier tableName;
SqlNode tableRef;
SqlNode source;
final SqlNodeList columnList;
final Span s;
final Pair<SqlNodeList, SqlNodeList> p;
}
{

<TRACE><INSERT> { s = span(); }
SqlInsertKeywords(keywords) {
keywordList = new SqlNodeList(keywords, s.addAll(keywords).pos());
}
<INTO> tableName = CompoundTableIdentifier()
( tableRef = TableHints(tableName) | { tableRef = tableName; } )
[ LOOKAHEAD(5) tableRef = ExtendTable(tableRef) ]
(
LOOKAHEAD(2)
p = ParenthesizedCompoundIdentifierList() {
if (p.right.size() > 0) {
tableRef = extend(tableRef, p.right);
}
if (p.left.size() > 0) {
columnList = p.left;
} else {
columnList = null;
}
}
| { columnList = null; }
)
source = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY) {
return new io.dingodb.calcite.grammar.dml.SqlInsert(s.end(source), keywordList, tableRef, source,
columnList, true);
}
}

/*
* Abstract production:
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2021 DataCanvas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.dingodb.calcite.grammar.dml;

import lombok.Getter;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNumericLiteral;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* add trace
*/
public class SqlInsert extends org.apache.calcite.sql.SqlInsert {

@Getter
private boolean trace;

public SqlInsert(SqlParserPos pos,
SqlNodeList keywords,
SqlNode targetTable,
SqlNode source,
@Nullable SqlNodeList columnList,
@Nullable boolean trace) {
super(pos, keywords, targetTable, source, columnList);
this.trace = trace;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 DataCanvas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.dingodb.calcite.grammar.dql;

import lombok.Getter;
import lombok.Setter;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.checkerframework.checker.nullness.qual.Nullable;

@Getter
@Setter
public class SqlOrderBy extends org.apache.calcite.sql.SqlOrderBy {
private boolean trace;

public SqlOrderBy(SqlParserPos pos,
SqlNode query,
SqlNodeList orderList,
@Nullable SqlNode offset,
@Nullable SqlNode fetch,
@Nullable boolean trace) {
super(pos, query, orderList, offset, fetch);
this.trace = trace;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2021 DataCanvas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.dingodb.calcite.grammar.dql;

import lombok.Getter;
import lombok.Setter;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.UUID;

@Getter
public class SqlTraceSelect extends SqlSelect {

@Setter
ExportOptions exportOptions;

boolean trace;

public SqlTraceSelect(SqlParserPos pos,
@Nullable SqlNodeList keywordList,
SqlNodeList selectList,
@Nullable SqlNode from,
@Nullable SqlNode where,
@Nullable SqlNodeList groupBy,
@Nullable SqlNode having,
@Nullable SqlNodeList windowDecls,
@Nullable SqlNodeList orderBy,
@Nullable SqlNode offset,
@Nullable SqlNode fetch,
@Nullable SqlNodeList hints,
ExportOptions exportOptions,
boolean trace) {
super(pos, keywordList, selectList, from, where, groupBy, having, windowDecls, orderBy, offset, fetch, hints);
this.exportOptions = exportOptions;
this.trace = trace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ public void traceTree(byte[] prefix, List<Object[]> rowList) {
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Object[] val = new Object[3];
Object[] val = new Object[4];
val[0] = prefixStr + "runStmt";
val[1] = DateTimeUtils.timeFormat(new Time(start));
val[2] = String.valueOf(duration);
val[3] = Long.valueOf(this.getCount());
rowList.add(val);

if (profile != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ public void traceTree(byte[] prefix, List<Object[]> rowList) {
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Object[] val = new Object[3];
Object[] val = new Object[4];
val[0] = termStr + "compile";
val[1] = DateTimeUtils.timeFormat(new Time(start));
val[2] = String.valueOf(duration);
val[3] = Long.valueOf(this.getCount());
rowList.add(val);

byte[] prefix1 = new byte[prefix.length + 2];
Expand All @@ -85,22 +86,25 @@ public void traceTree(byte[] prefix, List<Object[]> rowList) {
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Object[] parseVal = new Object[3];
Object[] parseVal = new Object[4];
parseVal[0] = termStr + "parse";
parseVal[1] = DateTimeUtils.timeFormat(new Time(start));
parseVal[2] = String.valueOf(parse);
parseVal[3] = Long.valueOf(this.getCount());
rowList.add(parseVal);

Object[] validateVal = new Object[3];
Object[] validateVal = new Object[4];
validateVal[0] = termStr + "validate";
validateVal[1] = DateTimeUtils.timeFormat(new Time(parseTime));
validateVal[2] = String.valueOf(validate);
validateVal[3] = Long.valueOf(this.getCount());
rowList.add(validateVal);

Object[] optimizeVal = new Object[3];
Object[] optimizeVal = new Object[4];
optimizeVal[0] = termStr + "optimize";
optimizeVal[1] = DateTimeUtils.timeFormat(new Time(validateTime));
optimizeVal[2] = String.valueOf(optimize);
optimizeVal[3] = Long.valueOf(this.getCount());
rowList.add(optimizeVal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void traceTree(Profile profile, byte[] prefix, List<Object[]> rowList) {
&& !"source".equals(profile.type)
&& !"root".equals(profile.type) && profile.getEnd() > 0 && profile.getStart() > 0
) {
Object[] val = new Object[3];
Object[] val = new Object[4];
val[0] = node + profile.type;
if (profile instanceof SourceProfile) {
SourceProfile sourceProfile = (SourceProfile) profile;
Expand All @@ -118,6 +118,7 @@ public void traceTree(Profile profile, byte[] prefix, List<Object[]> rowList) {
}
val[1] = DateTimeUtils.timeFormat(new Time(profile.start));
val[2] = String.valueOf(profile.getDuration());
val[3] = Long.valueOf(this.getCount());
rowList.add(val);
skip = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ public List<Object[]> traceTree(List<Object[]> rowList) {
}
if (execProfile != null) {
if (planEndTime > 0) {
Object[] val = new Object[3];
Object[] val = new Object[4];
val[0] = termStr + "schedule job";
val[1] = DateTimeUtils.timeFormat(new Time(execProfile.getStart()));
val[2] = String.valueOf(execProfile.start - planEndTime);
val[3] = Long.valueOf(this.getCount());
rowList.add(val);
}
execProfile.traceTree(prefix, rowList);
Expand Down
Loading

0 comments on commit e2f258a

Please sign in to comment.