-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support insert values #1067
Support insert values #1067
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 org.apache.doris.analysis; | ||
|
||
import org.apache.doris.common.AnalysisException; | ||
import org.apache.doris.thrift.TExprNode; | ||
|
||
// | ||
public class DefaultValueExpr extends Expr { | ||
@Override | ||
protected void analyzeImpl(Analyzer analyzer) throws AnalysisException { | ||
} | ||
|
||
@Override | ||
protected String toSqlImpl() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected void toThrift(TExprNode msg) { | ||
|
||
} | ||
|
||
@Override | ||
public Expr clone() { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,8 @@ public class InsertStmt extends DdlStmt { | |
private DataSink dataSink; | ||
private DataPartition dataPartition; | ||
|
||
List<Column> targetColumns = Lists.newArrayList(); | ||
|
||
public InsertStmt(InsertTarget target, List<String> cols, InsertSource source, List<String> hints) { | ||
this.tblName = target.getTblName(); | ||
List<String> tmpPartitions = target.getPartitions(); | ||
|
@@ -318,12 +320,8 @@ private void analyzeTargetTable(Analyzer analyzer) throws AnalysisException { | |
} | ||
} | ||
|
||
private void checkColumnCoverage(Set<String> mentionedCols, List<Column> baseColumns, List<Expr> selectList) | ||
private void checkColumnCoverage(Set<String> mentionedCols, List<Column> baseColumns) | ||
throws AnalysisException { | ||
// check if size of select item equal with columns mentioned in statement | ||
if (mentionedCols.size() != selectList.size()) { | ||
ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_VALUE_COUNT); | ||
} | ||
|
||
// check columns of target table | ||
for (Column col : baseColumns) { | ||
|
@@ -337,14 +335,8 @@ private void checkColumnCoverage(Set<String> mentionedCols, List<Column> baseCol | |
} | ||
|
||
public void analyzeSubquery(Analyzer analyzer) throws UserException { | ||
queryStmt.setFromInsert(true); | ||
// parse query statement | ||
queryStmt.analyze(analyzer); | ||
List<Expr> selectList = Expr.cloneList(queryStmt.getBaseTblResultExprs()); | ||
|
||
// Analyze columns mentioned in the statement. | ||
Set<String> mentionedColumns = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER); | ||
List<Column> targetColumns = Lists.newArrayList(); | ||
if (targetColumnNames == null) { | ||
for (Column col : targetTable.getBaseSchema()) { | ||
mentionedColumns.add(col.getName()); | ||
|
@@ -363,10 +355,69 @@ public void analyzeSubquery(Analyzer analyzer) throws UserException { | |
} | ||
} | ||
|
||
// parse query statement | ||
queryStmt.setFromInsert(true); | ||
queryStmt.analyze(analyzer); | ||
|
||
// check if size of select item equal with columns mentioned in statement | ||
if (mentionedColumns.size() != queryStmt.getResultExprs().size()) { | ||
ErrorReport.reportAnalysisException(ErrorCode.ERR_WRONG_VALUE_COUNT); | ||
} | ||
|
||
// Check if all columns mentioned is enough | ||
checkColumnCoverage(mentionedColumns, targetTable.getBaseSchema(), selectList); | ||
checkColumnCoverage(mentionedColumns, targetTable.getBaseSchema()) ; | ||
if (queryStmt instanceof SelectStmt && ((SelectStmt) queryStmt).getTableRefs().isEmpty()) { | ||
SelectStmt selectStmt = (SelectStmt) queryStmt; | ||
if (selectStmt.getValueList() != null) { | ||
List<ArrayList<Expr>> rows = selectStmt.getValueList().getRows(); | ||
for (int rowIdx = 0; rowIdx < rows.size(); ++rowIdx) { | ||
analyzeRow(analyzer, targetColumns, rows.get(rowIdx), rowIdx + 1); | ||
} | ||
for (int i = 0; i < selectStmt.getResultExprs().size(); ++i) { | ||
selectStmt.getResultExprs().set(i, selectStmt.getValueList().getFirstRow().get(i)); | ||
selectStmt.getBaseTblResultExprs().set(i, selectStmt.getValueList().getFirstRow().get(i)); | ||
} | ||
} else { | ||
analyzeRow(analyzer, targetColumns, selectStmt.getResultExprs(), 1); | ||
} | ||
isStreaming = true; | ||
} else { | ||
for (int i = 0; i < targetColumns.size(); ++i) { | ||
Column column = targetColumns.get(i); | ||
if (column.getType().isHllType()) { | ||
Expr expr = queryStmt.getResultExprs().get(i); | ||
checkHllCompatibility(column, expr); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void analyzeRow(Analyzer analyzer, List<Column> targetColumns, ArrayList<Expr> row, int rowIdx) | ||
throws AnalysisException { | ||
// 1. check number of fields if equal with first row | ||
if (row.size() != targetColumns.size()) { | ||
throw new AnalysisException("Column count doesn't match value count at row " + rowIdx); | ||
} | ||
for (int i = 0; i < row.size(); ++i) { | ||
Expr expr = row.get(i); | ||
Column col = targetColumns.get(i); | ||
|
||
// TargeTable's hll column must be hll_hash's result | ||
if (col.getType().equals(Type.HLL)) { | ||
checkHllCompatibility(col, expr); | ||
} | ||
|
||
if (expr instanceof DefaultValueExpr) { | ||
if (targetColumns.get(i).getDefaultValue() == null) { | ||
throw new AnalysisException("Column has no default value, column=" + targetColumns.get(i).getName()); | ||
} | ||
expr = new StringLiteral(targetColumns.get(i).getDefaultValue()); | ||
} | ||
|
||
expr.analyze(analyzer); | ||
|
||
prepareExpressions(targetColumns, selectList, analyzer); | ||
row.set(i, checkTypeCompatibility(col, expr)); | ||
} | ||
} | ||
|
||
private void analyzePlanHints(Analyzer analyzer) throws AnalysisException { | ||
|
@@ -397,40 +448,37 @@ private void analyzePlanHints(Analyzer analyzer) throws AnalysisException { | |
} | ||
} | ||
} | ||
|
||
private Expr checkTypeCompatibility(Column col, Expr expr) throws AnalysisException { | ||
// TargeTable's hll column must be hll_hash's result | ||
if (col.getType().equals(Type.HLL)) { | ||
final String hllMismatchLog = "Column's type is HLL," | ||
+ " SelectList must contains HLL or hll_hash function's result, column=" + col.getName(); | ||
if (expr instanceof SlotRef) { | ||
final SlotRef slot = (SlotRef) expr; | ||
if (!slot.getType().equals(Type.HLL)) { | ||
throw new AnalysisException(hllMismatchLog); | ||
} | ||
} else if (expr instanceof FunctionCallExpr) { | ||
final FunctionCallExpr functionExpr = (FunctionCallExpr) expr; | ||
if (!functionExpr.getFnName().getFunction().equalsIgnoreCase("hll_hash")) { | ||
throw new AnalysisException(hllMismatchLog); | ||
} | ||
} else { | ||
private void checkHllCompatibility(Column col, Expr expr) throws AnalysisException { | ||
final String hllMismatchLog = "Column's type is HLL," | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why hll's compatibility check is s separated from normal compatibility check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because HLL column will replace with SlotRef in Planner with a |
||
+ " SelectList must contains HLL or hll_hash function's result, column=" + col.getName(); | ||
if (expr instanceof SlotRef) { | ||
final SlotRef slot = (SlotRef) expr; | ||
if (!slot.getType().equals(Type.HLL)) { | ||
throw new AnalysisException(hllMismatchLog); | ||
} | ||
|
||
} else if (expr instanceof FunctionCallExpr) { | ||
final FunctionCallExpr functionExpr = (FunctionCallExpr) expr; | ||
if (!functionExpr.getFnName().getFunction().equalsIgnoreCase("hll_hash")) { | ||
throw new AnalysisException(hllMismatchLog); | ||
} | ||
} else { | ||
throw new AnalysisException(hllMismatchLog); | ||
} | ||
} | ||
|
||
if (col.getDataType().equals(expr.getType())) { | ||
private Expr checkTypeCompatibility(Column col, Expr expr) throws AnalysisException { | ||
if (col.getDataType().equals(expr.getType().getPrimitiveType())) { | ||
return expr; | ||
} | ||
return expr.castTo(col.getType()); | ||
} | ||
|
||
private void prepareExpressions(List<Column> targetCols, List<Expr> selectList, Analyzer analyzer) | ||
throws UserException { | ||
public void prepareExpressions() throws UserException { | ||
List<Expr> selectList = Expr.cloneList(queryStmt.getBaseTblResultExprs()); | ||
// check type compatibility | ||
int numCols = targetCols.size(); | ||
int numCols = targetColumns.size(); | ||
for (int i = 0; i < numCols; ++i) { | ||
Column col = targetCols.get(i); | ||
Column col = targetColumns.get(i); | ||
Expr expr = checkTypeCompatibility(col, selectList.get(i)); | ||
selectList.set(i, expr); | ||
exprByName.put(col.getName(), expr); | ||
|
@@ -498,5 +546,6 @@ public void reset() { | |
exprByName.clear(); | ||
dataSink = null; | ||
dataPartition = null; | ||
targetColumns.clear(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The length has been checked in checkColumnConverage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two checks are not the same. this checks if the number of query statement's result is equal with number of columns user assigned. And
checkColumnConverage
means to check if user assigned columns can covert the columns of target table.