Skip to content

Commit

Permalink
Merge pull request #1324 from kuzudb/expression-raw-name
Browse files Browse the repository at this point in the history
Replace getRawName with toString
  • Loading branch information
andyfengHKU committed Feb 26, 2023
2 parents b618ea4 + a0a8ed9 commit f8cbe3e
Show file tree
Hide file tree
Showing 43 changed files with 304 additions and 179 deletions.
1 change: 1 addition & 0 deletions src/binder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_subdirectory(query)
add_library(kuzu_binder
OBJECT
binder.cpp
bound_statement_result.cpp
expression_binder.cpp
query_normalizer.cpp)

Expand Down
13 changes: 6 additions & 7 deletions src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ static void validateNodeRelConnectivity(const Catalog& catalog_, const RelExpres
}
}
}
throw BinderException("Nodes " + srcNode.getRawName() + " and " + dstNode.getRawName() +
" are not connected through rel " + rel.getRawName() + ".");
throw BinderException("Nodes " + srcNode.toString() + " and " + dstNode.toString() +
" are not connected through rel " + rel.toString() + ".");
}

static std::vector<std::pair<std::string, std::vector<Property>>> getPropertyNameAndSchemasPairs(
Expand Down Expand Up @@ -126,10 +126,9 @@ void Binder::bindQueryRel(const RelPattern& relPattern,
}
// bind variable length
auto [lowerBound, upperBound] = bindVariableLengthRelBound(relPattern);
auto queryRel = make_shared<RelExpression>(
getUniqueExpressionName(parsedName), tableIDs, srcNode, dstNode, lowerBound, upperBound);
auto queryRel = make_shared<RelExpression>(getUniqueExpressionName(parsedName), parsedName,
tableIDs, srcNode, dstNode, lowerBound, upperBound);
queryRel->setAlias(parsedName);
queryRel->setRawName(parsedName);
validateNodeRelConnectivity(catalog, *queryRel, *srcNode, *dstNode);
// resolve properties associate with rel table
std::vector<RelTableSchema*> relTableSchemas;
Expand Down Expand Up @@ -205,9 +204,9 @@ std::shared_ptr<NodeExpression> Binder::bindQueryNode(
std::shared_ptr<NodeExpression> Binder::createQueryNode(const NodePattern& nodePattern) {
auto parsedName = nodePattern.getVariableName();
auto tableIDs = bindNodeTableIDs(nodePattern.getTableNames());
auto queryNode = make_shared<NodeExpression>(getUniqueExpressionName(parsedName), tableIDs);
auto queryNode =
make_shared<NodeExpression>(getUniqueExpressionName(parsedName), parsedName, tableIDs);
queryNode->setAlias(parsedName);
queryNode->setRawName(parsedName);
queryNode->setInternalIDProperty(expressionBinder.createInternalNodeIDExpression(*queryNode));
// resolve properties associate with node table
std::vector<NodeTableSchema*> nodeTableSchemas;
Expand Down
4 changes: 2 additions & 2 deletions src/binder/bind/bind_projection_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ expression_vector Binder::bindOrderByExpressions(
for (auto& expression : orderByExpressions) {
auto boundExpression = expressionBinder.bindExpression(*expression);
if (boundExpression->dataType.typeID == NODE || boundExpression->dataType.typeID == REL) {
throw BinderException("Cannot order by " + boundExpression->getRawName() +
throw BinderException("Cannot order by " + boundExpression->toString() +
". Order by node or rel is not supported.");
}
boundOrderByExpressions.push_back(std::move(boundExpression));
Expand All @@ -151,7 +151,7 @@ uint64_t Binder::bindSkipLimitExpression(const ParsedExpression& expression) {
void Binder::addExpressionsToScope(const expression_vector& projectionExpressions) {
for (auto& expression : projectionExpressions) {
// In RETURN clause, if expression is not aliased, its input name will serve its alias.
auto alias = expression->hasAlias() ? expression->getAlias() : expression->getRawName();
auto alias = expression->hasAlias() ? expression->getAlias() : expression->toString();
variablesInScope.insert({alias, expression});
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/binder/bind/bind_updating_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ std::unique_ptr<BoundUpdatingClause> Binder::bindCreateClause(
auto queryGraph = queryGraphCollection->getQueryGraph(i);
for (auto j = 0u; j < queryGraph->getNumQueryNodes(); ++j) {
auto node = queryGraph->getQueryNode(j);
if (!prevVariablesInScope.contains(node->getRawName())) {
if (!prevVariablesInScope.contains(node->toString())) {
boundCreateClause->addCreateNode(bindCreateNode(node, *propertyCollection));
}
}
for (auto j = 0u; j < queryGraph->getNumQueryRels(); ++j) {
auto rel = queryGraph->getQueryRel(j);
if (!prevVariablesInScope.contains(rel->getRawName())) {
if (!prevVariablesInScope.contains(rel->toString())) {
boundCreateClause->addCreateRel(bindCreateRel(rel, *propertyCollection));
}
}
Expand All @@ -58,7 +58,7 @@ std::unique_ptr<BoundCreateNode> Binder::bindCreateNode(
std::shared_ptr<NodeExpression> node, const PropertyKeyValCollection& collection) {
if (node->isMultiLabeled()) {
throw BinderException(
"Create node " + node->getRawName() + " with multiple node labels is not supported.");
"Create node " + node->toString() + " with multiple node labels is not supported.");
}
auto nodeTableID = node->getSingleTableID();
auto nodeTableSchema = catalog.getReadOnlyVersion()->getNodeTableSchema(nodeTableID);
Expand All @@ -73,7 +73,7 @@ std::unique_ptr<BoundCreateNode> Binder::bindCreateNode(
setItems.emplace_back(key, val);
}
if (primaryKeyExpression == nullptr) {
throw BinderException("Create node " + node->getRawName() + " expects primary key " +
throw BinderException("Create node " + node->toString() + " expects primary key " +
primaryKey.name + " as input.");
}
return std::make_unique<BoundCreateNode>(
Expand All @@ -84,7 +84,7 @@ std::unique_ptr<BoundCreateRel> Binder::bindCreateRel(
std::shared_ptr<RelExpression> rel, const PropertyKeyValCollection& collection) {
if (rel->isMultiLabeled() || rel->isBoundByMultiLabeledNode()) {
throw BinderException(
"Create rel " + rel->getRawName() +
"Create rel " + rel->toString() +
" with multiple rel labels or bound by multiple node labels is not supported.");
}
auto relTableID = rel->getSingleTableID();
Expand Down Expand Up @@ -133,7 +133,7 @@ std::unique_ptr<BoundUpdatingClause> Binder::bindSetClause(const UpdatingClause&
std::unique_ptr<BoundSetNodeProperty> Binder::bindSetNodeProperty(
std::shared_ptr<NodeExpression> node, std::pair<ParsedExpression*, ParsedExpression*> setItem) {
if (node->isMultiLabeled()) {
throw BinderException("Set property of node " + node->getRawName() +
throw BinderException("Set property of node " + node->toString() +
" with multiple node labels is not supported.");
}
return std::make_unique<BoundSetNodeProperty>(std::move(node), bindSetItem(setItem));
Expand All @@ -142,7 +142,7 @@ std::unique_ptr<BoundSetNodeProperty> Binder::bindSetNodeProperty(
std::unique_ptr<BoundSetRelProperty> Binder::bindSetRelProperty(
std::shared_ptr<RelExpression> rel, std::pair<ParsedExpression*, ParsedExpression*> setItem) {
if (rel->isMultiLabeled() || rel->isBoundByMultiLabeledNode()) {
throw BinderException("Set property of rel " + rel->getRawName() +
throw BinderException("Set property of rel " + rel->toString() +
" with multiple rel labels or bound by multiple node labels "
"is not supported.");
}
Expand Down Expand Up @@ -183,7 +183,7 @@ std::unique_ptr<BoundDeleteNode> Binder::bindDeleteNode(
const std::shared_ptr<NodeExpression>& node) {
if (node->isMultiLabeled()) {
throw BinderException(
"Delete node " + node->getRawName() + " with multiple node labels is not supported.");
"Delete node " + node->toString() + " with multiple node labels is not supported.");
}
auto nodeTableID = node->getSingleTableID();
auto nodeTableSchema = catalog.getReadOnlyVersion()->getNodeTableSchema(nodeTableID);
Expand All @@ -195,7 +195,7 @@ std::unique_ptr<BoundDeleteNode> Binder::bindDeleteNode(
std::shared_ptr<RelExpression> Binder::bindDeleteRel(std::shared_ptr<RelExpression> rel) {
if (rel->isMultiLabeled() || rel->isBoundByMultiLabeledNode()) {
throw BinderException(
"Delete rel " + rel->getRawName() +
"Delete rel " + rel->toString() +
" with multiple rel labels or bound by multiple node labels is not supported.");
}
return rel;
Expand Down
2 changes: 1 addition & 1 deletion src/binder/bind_expression/bind_boolean_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std::shared_ptr<Expression> ExpressionBinder::bindBooleanExpression(
function::VectorBooleanOperations::bindSelectFunction(expressionType, childrenAfterCast);
auto uniqueExpressionName =
ScalarFunctionExpression::getUniqueName(functionName, childrenAfterCast);
return make_shared<ScalarFunctionExpression>(expressionType, DataType(BOOL),
return make_shared<ScalarFunctionExpression>(functionName, expressionType, DataType(BOOL),
std::move(childrenAfterCast), std::move(execFunc), std::move(selectFunc),
uniqueExpressionName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/binder/bind_expression/bind_comparison_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::shared_ptr<Expression> ExpressionBinder::bindComparisonExpression(
}
auto uniqueExpressionName =
ScalarFunctionExpression::getUniqueName(function->name, childrenAfterCast);
return make_shared<ScalarFunctionExpression>(expressionType,
return make_shared<ScalarFunctionExpression>(functionName, expressionType,
common::DataType(function->returnTypeID), std::move(childrenAfterCast), function->execFunc,
function->selectFunc, uniqueExpressionName);
}
Expand Down
15 changes: 8 additions & 7 deletions src/binder/bind_expression/bind_function_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ std::shared_ptr<Expression> ExpressionBinder::bindScalarFunctionExpression(
}
auto uniqueExpressionName =
ScalarFunctionExpression::getUniqueName(function->name, childrenAfterCast);
return make_shared<ScalarFunctionExpression>(FUNCTION, returnType, std::move(childrenAfterCast),
function->execFunc, function->selectFunc, uniqueExpressionName);
return make_shared<ScalarFunctionExpression>(functionName, FUNCTION, returnType,
std::move(childrenAfterCast), function->execFunc, function->selectFunc,
uniqueExpressionName);
}

std::shared_ptr<Expression> ExpressionBinder::bindAggregateFunctionExpression(
Expand Down Expand Up @@ -91,7 +92,7 @@ std::shared_ptr<Expression> ExpressionBinder::bindAggregateFunctionExpression(
} else {
returnType = DataType(function->returnTypeID);
}
return make_shared<AggregateFunctionExpression>(returnType, std::move(children),
return make_shared<AggregateFunctionExpression>(functionName, returnType, std::move(children),
function->aggregateFunction->clone(), uniqueExpressionName);
}

Expand Down Expand Up @@ -186,8 +187,8 @@ std::shared_ptr<Expression> ExpressionBinder::bindNodeLabelFunction(const Expres
children.push_back(createLiteralExpression(std::move(labelsValue)));
auto execFunc = function::LabelVectorOperation::execFunction;
auto uniqueExpressionName = ScalarFunctionExpression::getUniqueName(LABEL_FUNC_NAME, children);
return make_shared<ScalarFunctionExpression>(
FUNCTION, DataType(STRING), std::move(children), execFunc, nullptr, uniqueExpressionName);
return make_shared<ScalarFunctionExpression>(LABEL_FUNC_NAME, FUNCTION, DataType(STRING),
std::move(children), execFunc, nullptr, uniqueExpressionName);
}

std::shared_ptr<Expression> ExpressionBinder::bindRelLabelFunction(const Expression& expression) {
Expand All @@ -206,8 +207,8 @@ std::shared_ptr<Expression> ExpressionBinder::bindRelLabelFunction(const Express
children.push_back(createLiteralExpression(std::move(labelsValue)));
auto execFunc = function::LabelVectorOperation::execFunction;
auto uniqueExpressionName = ScalarFunctionExpression::getUniqueName(LABEL_FUNC_NAME, children);
return make_shared<ScalarFunctionExpression>(
FUNCTION, DataType(STRING), std::move(children), execFunc, nullptr, uniqueExpressionName);
return make_shared<ScalarFunctionExpression>(LABEL_FUNC_NAME, FUNCTION, DataType(STRING),
std::move(children), execFunc, nullptr, uniqueExpressionName);
}

} // namespace binder
Expand Down
5 changes: 3 additions & 2 deletions src/binder/bind_expression/bind_null_operator_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ std::shared_ptr<Expression> ExpressionBinder::bindNullOperatorExpression(
auto execFunc = function::VectorNullOperations::bindExecFunction(expressionType, children);
auto selectFunc = function::VectorNullOperations::bindSelectFunction(expressionType, children);
auto uniqueExpressionName = ScalarFunctionExpression::getUniqueName(functionName, children);
return make_shared<ScalarFunctionExpression>(expressionType, common::DataType(common::BOOL),
std::move(children), std::move(execFunc), std::move(selectFunc), uniqueExpressionName);
return make_shared<ScalarFunctionExpression>(functionName, expressionType,
common::DataType(common::BOOL), std::move(children), std::move(execFunc),
std::move(selectFunc), uniqueExpressionName);
}

} // namespace binder
Expand Down
8 changes: 4 additions & 4 deletions src/binder/bind_expression/bind_property_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::shared_ptr<Expression> ExpressionBinder::bindNodePropertyExpression(
auto& nodeOrRel = (NodeOrRelExpression&)expression;
if (!nodeOrRel.hasPropertyExpression(propertyName)) {
throw BinderException(
"Cannot find property " + propertyName + " for " + expression.getRawName() + ".");
"Cannot find property " + propertyName + " for " + expression.toString() + ".");
}
return nodeOrRel.getPropertyExpression(propertyName);
}
Expand Down Expand Up @@ -63,11 +63,11 @@ std::shared_ptr<Expression> ExpressionBinder::bindRelPropertyExpression(
auto& rel = (RelExpression&)expression;
if (rel.isVariableLength()) {
throw BinderException(
"Cannot read property of variable length rel " + rel.getRawName() + ".");
"Cannot read property of variable length rel " + rel.toString() + ".");
}
if (!rel.hasPropertyExpression(propertyName)) {
throw BinderException(
"Cannot find property " + propertyName + " for " + expression.getRawName() + ".");
"Cannot find property " + propertyName + " for " + expression.toString() + ".");
}
return rel.getPropertyExpression(propertyName);
}
Expand All @@ -77,7 +77,7 @@ std::unique_ptr<Expression> ExpressionBinder::createPropertyExpression(
assert(!properties.empty());
auto anchorProperty = properties[0];
validatePropertiesWithSameDataType(
properties, anchorProperty.dataType, anchorProperty.name, nodeOrRel.getRawName());
properties, anchorProperty.dataType, anchorProperty.name, nodeOrRel.toString());
return make_unique<PropertyExpression>(anchorProperty.dataType, anchorProperty.name, nodeOrRel,
populatePropertyIDPerTable(properties), isPrimaryKey);
}
Expand Down
7 changes: 4 additions & 3 deletions src/binder/bind_expression/bind_subquery_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ std::shared_ptr<Expression> ExpressionBinder::bindExistentialSubqueryExpression(
auto& subqueryExpression = (ParsedSubqueryExpression&)parsedExpression;
auto prevVariablesInScope = binder->enterSubquery();
auto [queryGraph, _] = binder->bindGraphPattern(subqueryExpression.getPatternElements());
auto name = binder->getUniqueExpressionName(parsedExpression.getRawName());
auto boundSubqueryExpression =
make_shared<ExistentialSubqueryExpression>(std::move(queryGraph), std::move(name));
auto rawName = parsedExpression.getRawName();
auto uniqueName = binder->getUniqueExpressionName(rawName);
auto boundSubqueryExpression = make_shared<ExistentialSubqueryExpression>(
std::move(queryGraph), std::move(uniqueName), std::move(rawName));
if (subqueryExpression.hasWhereClause()) {
boundSubqueryExpression->setWhereExpression(
binder->bindWhereExpression(*subqueryExpression.getWhereClause()));
Expand Down
8 changes: 4 additions & 4 deletions src/binder/binder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "binder/binder.h"

#include "binder/expression/variable_expression.h"

using namespace kuzu::common;
using namespace kuzu::parser;
using namespace kuzu::catalog;
Expand Down Expand Up @@ -67,8 +69,7 @@ std::shared_ptr<Expression> Binder::createVariable(
throw BinderException("Variable " + name + " already exists.");
}
auto uniqueName = getUniqueExpressionName(name);
auto variable = make_shared<Expression>(ExpressionType::VARIABLE, dataType, uniqueName);
variable->setRawName(name);
auto variable = make_shared<VariableExpression>(dataType, uniqueName, name);
variable->setAlias(name);
variablesInScope.insert({name, variable});
return variable;
Expand All @@ -83,8 +84,7 @@ void Binder::validateFirstMatchIsNotOptional(const SingleQuery& singleQuery) {
void Binder::validateProjectionColumnNamesAreUnique(const expression_vector& expressions) {
auto existColumnNames = std::unordered_set<std::string>();
for (auto& expression : expressions) {
auto columnName =
expression->hasAlias() ? expression->getAlias() : expression->getRawName();
auto columnName = expression->hasAlias() ? expression->getAlias() : expression->toString();
if (existColumnNames.contains(columnName)) {
throw BinderException(
"Multiple result column with the same name " + columnName + " are not supported.");
Expand Down
28 changes: 28 additions & 0 deletions src/binder/bound_statement_result.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "binder/bound_statement_result.h"

#include "binder/expression/literal_expression.h"

namespace kuzu {
namespace binder {

std::unique_ptr<BoundStatementResult> BoundStatementResult::createSingleStringColumnResult() {
auto result = std::make_unique<BoundStatementResult>();
auto columnName = std::string("outputMsg");
auto value = std::make_unique<common::Value>(columnName);
auto stringColumn = std::make_shared<LiteralExpression>(std::move(value), columnName);
result->addColumn(stringColumn, expression_vector{stringColumn});
return result;
}

expression_vector BoundStatementResult::getExpressionsToCollect() {
expression_vector result;
for (auto& expressionsToCollect : expressionsToCollectPerColumn) {
for (auto& expression : expressionsToCollect) {
result.push_back(expression);
}
}
return result;
}

} // namespace binder
} // namespace kuzu
3 changes: 2 additions & 1 deletion src/binder/expression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ add_library(
OBJECT
case_expression.cpp
existential_subquery_expression.cpp
expression.cpp)
expression.cpp
function_expression.cpp)

set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:kuzu_binder_expression>
Expand Down
10 changes: 10 additions & 0 deletions src/binder/expression/case_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ expression_vector CaseExpression::getChildren() const {
return result;
}

std::string CaseExpression::toString() const {
std::string result = "CASE ";
for (auto& caseAlternative : caseAlternatives) {
result += "WHEN " + caseAlternative->whenExpression->toString() + " THEN " +
caseAlternative->thenExpression->toString();
}
result += " ELSE " + elseExpression->toString();
return result;
}

} // namespace binder
} // namespace kuzu
4 changes: 2 additions & 2 deletions src/binder/expression/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ std::string ExpressionUtil::toString(const expression_vector& expressions) {
if (expressions.empty()) {
return std::string{};
}
auto result = expressions[0]->getRawName();
auto result = expressions[0]->toString();
for (auto i = 1u; i < expressions.size(); ++i) {
result += "," + expressions[i]->getRawName();
result += "," + expressions[i]->toString();
}
return result;
}
Expand Down
Loading

0 comments on commit f8cbe3e

Please sign in to comment.