Skip to content

Commit

Permalink
Merge pull request #1549 from kuzudb/physical-type
Browse files Browse the repository at this point in the history
Physical type
  • Loading branch information
acquamarin committed May 19, 2023
2 parents e49d980 + 7959699 commit 4aae989
Show file tree
Hide file tree
Showing 181 changed files with 3,042 additions and 2,687 deletions.
7 changes: 4 additions & 3 deletions src/binder/bind/bind_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ CSVReaderConfig Binder::bindParsingOptions(
auto boundCopyOptionExpression = expressionBinder.bindExpression(*copyOptionExpression);
assert(boundCopyOptionExpression->expressionType = LITERAL);
if (copyOptionName == "HEADER") {
if (boundCopyOptionExpression->dataType.typeID != BOOL) {
if (boundCopyOptionExpression->dataType.getLogicalTypeID() != LogicalTypeID::BOOL) {
throw BinderException(
"The value type of parsing csv option " + copyOptionName + " must be boolean.");
}
csvReaderConfig.hasHeader =
((LiteralExpression&)(*boundCopyOptionExpression)).value->getValue<bool>();
} else if (boundCopyOptionExpression->dataType.typeID == STRING &&
} else if (boundCopyOptionExpression->dataType.getLogicalTypeID() ==
LogicalTypeID::STRING &&
isValidStringParsingOption) {
if (boundCopyOptionExpression->dataType.typeID != STRING) {
if (boundCopyOptionExpression->dataType.getLogicalTypeID() != LogicalTypeID::STRING) {
throw BinderException(
"The value type of parsing csv option " + copyOptionName + " must be string.");
}
Expand Down
38 changes: 20 additions & 18 deletions src/binder/bind/bind_ddl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ std::unique_ptr<BoundStatement> Binder::bindCreateNodeTableClause(
auto primaryKeyIdx = bindPrimaryKey(
createNodeTableClause.getPKColName(), createNodeTableClause.getPropertyNameDataTypes());
for (auto i = 0u; i < boundProperties.size(); ++i) {
if (boundProperties[i].dataType.typeID == SERIAL && primaryKeyIdx != i) {
if (boundProperties[i].dataType.getLogicalTypeID() == LogicalTypeID::SERIAL &&
primaryKeyIdx != i) {
throw BinderException("Serial property in node table must be the primary key.");
}
}
Expand All @@ -49,7 +50,7 @@ std::unique_ptr<BoundStatement> Binder::bindCreateRelTableClause(
}
auto boundProperties = bindProperties(createRelClause.getPropertyNameDataTypes());
for (auto& boundProperty : boundProperties) {
if (boundProperty.dataType.typeID == SERIAL) {
if (boundProperty.dataType.getLogicalTypeID() == LogicalTypeID::SERIAL) {
throw BinderException("Serial property is not supported in rel table.");
}
}
Expand Down Expand Up @@ -93,7 +94,7 @@ std::unique_ptr<BoundStatement> Binder::bindAddPropertyClause(const parser::Stat
if (catalogContent->getTableSchema(tableID)->containProperty(addProperty.getPropertyName())) {
throw BinderException("Property: " + addProperty.getPropertyName() + " already exists.");
}
if (dataType.typeID == SERIAL) {
if (dataType.getLogicalTypeID() == LogicalTypeID::SERIAL) {
throw BinderException("Serial property in node table must be the primary key.");
}
auto defaultVal = ExpressionBinder::implicitCastIfNecessary(
Expand Down Expand Up @@ -173,10 +174,10 @@ uint32_t Binder::bindPrimaryKey(const std::string& pkColName,
auto primaryKey = propertyNameDataTypes[primaryKeyIdx];
StringUtils::toUpper(primaryKey.second);
// We only support INT64, STRING and SERIAL column as the primary key.
switch (Types::dataTypeFromString(primaryKey.second).typeID) {
case common::INT64:
case common::STRING:
case common::SERIAL:
switch (LogicalTypeUtils::dataTypeFromString(primaryKey.second).getLogicalTypeID()) {
case common::LogicalTypeID::INT64:
case common::LogicalTypeID::STRING:
case common::LogicalTypeID::SERIAL:
break;
default:
throw BinderException(
Expand All @@ -195,30 +196,31 @@ property_id_t Binder::bindPropertyName(TableSchema* tableSchema, const std::stri
tableSchema->tableName + " table doesn't have property: " + propertyName + ".");
}

DataType Binder::bindDataType(const std::string& dataType) {
auto boundType = Types::dataTypeFromString(dataType);
if (boundType.typeID == common::FIXED_LIST) {
auto validNumericTypes = common::DataType::getNumericalTypeIDs();
auto fixedListTypeInfo = reinterpret_cast<FixedListTypeInfo*>(boundType.getExtraTypeInfo());
LogicalType Binder::bindDataType(const std::string& dataType) {
auto boundType = LogicalTypeUtils::dataTypeFromString(dataType);
if (boundType.getLogicalTypeID() == common::LogicalTypeID::FIXED_LIST) {
auto validNumericTypes = common::LogicalType::getNumericalLogicalTypeIDs();
auto childType = common::FixedListType::getChildType(&boundType);
auto numElementsInList = common::FixedListType::getNumElementsInList(&boundType);
if (find(validNumericTypes.begin(), validNumericTypes.end(),
boundType.getChildType()->typeID) == validNumericTypes.end()) {
childType->getLogicalTypeID()) == validNumericTypes.end()) {
throw common::BinderException(
"The child type of a fixed list must be a numeric type. Given: " +
common::Types::dataTypeToString(*boundType.getChildType()) + ".");
common::LogicalTypeUtils::dataTypeToString(*childType) + ".");
}
if (fixedListTypeInfo->getFixedNumElementsInList() == 0) {
if (numElementsInList == 0) {
// Note: the parser already guarantees that the number of elements is a non-negative
// number. However, we still need to check whether the number of elements is 0.
throw common::BinderException(
"The number of elements in a fixed list must be greater than 0. Given: " +
std::to_string(fixedListTypeInfo->getFixedNumElementsInList()) + ".");
std::to_string(numElementsInList) + ".");
}
auto numElementsPerPage = storage::PageUtils::getNumElementsInAPage(
Types::getDataTypeSize(boundType), true /* hasNull */);
storage::StorageUtils::getDataTypeSize(boundType), true /* hasNull */);
if (numElementsPerPage == 0) {
throw common::BinderException(
StringUtils::string_format("Cannot store a fixed list of size {} in a page.",
Types::getDataTypeSize(boundType)));
storage::StorageUtils::getDataTypeSize(boundType)));
}
}
return boundType;
Expand Down
19 changes: 11 additions & 8 deletions src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void Binder::bindQueryRel(const RelPattern& relPattern,
auto parsedName = relPattern.getVariableName();
if (variablesInScope.contains(parsedName)) {
auto prevVariable = variablesInScope.at(parsedName);
ExpressionBinder::validateExpectedDataType(*prevVariable, REL);
ExpressionBinder::validateExpectedDataType(*prevVariable, LogicalTypeID::REL);
throw BinderException("Bind relationship " + parsedName +
" to relationship with same name is not supported.");
}
Expand All @@ -131,8 +131,11 @@ void Binder::bindQueryRel(const RelPattern& relPattern,
// bind variable length
auto [lowerBound, upperBound] = bindVariableLengthRelBound(relPattern);
auto isVariableLength = !(lowerBound == 1 && upperBound == 1);
auto dataType = isVariableLength ? common::DataType(std::make_unique<DataType>(INTERNAL_ID)) :
common::DataType(common::REL);
auto dataType = isVariableLength ?
common::LogicalType(LogicalTypeID::VAR_LIST,
std::make_unique<VarListTypeInfo>(
std::make_unique<LogicalType>(LogicalTypeID::INTERNAL_ID))) :
common::LogicalType(common::LogicalTypeID::REL);
auto queryRel = make_shared<RelExpression>(std::move(dataType),
getUniqueExpressionName(parsedName), parsedName, tableIDs, srcNode, dstNode,
relPattern.getDirection() != BOTH, relPattern.getRelType(), lowerBound, upperBound);
Expand Down Expand Up @@ -186,7 +189,7 @@ std::shared_ptr<NodeExpression> Binder::bindQueryNode(
std::shared_ptr<NodeExpression> queryNode;
if (variablesInScope.contains(parsedName)) { // bind to node in scope
auto prevVariable = variablesInScope.at(parsedName);
ExpressionBinder::validateExpectedDataType(*prevVariable, NODE);
ExpressionBinder::validateExpectedDataType(*prevVariable, LogicalTypeID::NODE);
queryNode = static_pointer_cast<NodeExpression>(prevVariable);
// E.g. MATCH (a:person) MATCH (a:organisation)
// We bind to single node a with both labels
Expand Down Expand Up @@ -236,10 +239,10 @@ std::shared_ptr<NodeExpression> Binder::createQueryNode(const NodePattern& nodeP
}

std::vector<table_id_t> Binder::bindTableIDs(
const std::vector<std::string>& tableNames, DataTypeID nodeOrRelType) {
const std::vector<std::string>& tableNames, LogicalTypeID nodeOrRelType) {
std::unordered_set<table_id_t> tableIDs;
switch (nodeOrRelType) {
case NODE: {
case LogicalTypeID::NODE: {
if (tableNames.empty()) {
for (auto tableID : catalog.getReadOnlyVersion()->getNodeTableIDs()) {
tableIDs.insert(tableID);
Expand All @@ -251,7 +254,7 @@ std::vector<table_id_t> Binder::bindTableIDs(
}

} break;
case REL: {
case LogicalTypeID::REL: {
if (tableNames.empty()) {
for (auto tableID : catalog.getReadOnlyVersion()->getRelTableIDs()) {
tableIDs.insert(tableID);
Expand All @@ -263,7 +266,7 @@ std::vector<table_id_t> Binder::bindTableIDs(
} break;
default:
throw NotImplementedException(
"bindTableIDs(" + Types::dataTypeToString(nodeOrRelType) + ").");
"bindTableIDs(" + LogicalTypeUtils::dataTypeToString(nodeOrRelType) + ").");
}
auto result = std::vector<table_id_t>{tableIDs.begin(), tableIDs.end()};
std::sort(result.begin(), result.end());
Expand Down
17 changes: 10 additions & 7 deletions src/binder/bind/bind_projection_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ std::unique_ptr<BoundReturnClause> Binder::bindReturnClause(const ReturnClause&
auto statementResult = std::make_unique<BoundStatementResult>();
for (auto& expression : boundProjectionExpressions) {
auto dataType = expression->getDataType();
if (dataType.typeID == common::NODE || dataType.typeID == common::REL) {
if (dataType.getLogicalTypeID() == common::LogicalTypeID::NODE ||
dataType.getLogicalTypeID() == common::LogicalTypeID::REL) {
statementResult->addColumn(expression, rewriteNodeOrRelExpression(*expression));
} else {
statementResult->addColumn(expression, expression_vector{expression});
Expand Down Expand Up @@ -67,10 +68,10 @@ expression_vector Binder::bindProjectionExpressions(
}

expression_vector Binder::rewriteNodeOrRelExpression(const Expression& expression) {
if (expression.dataType.typeID == common::NODE) {
if (expression.dataType.getLogicalTypeID() == common::LogicalTypeID::NODE) {
return rewriteNodeExpression(expression);
} else {
assert(expression.dataType.typeID == common::REL);
assert(expression.dataType.getLogicalTypeID() == common::LogicalTypeID::REL);
return rewriteRelExpression(expression);
}
}
Expand Down Expand Up @@ -138,7 +139,8 @@ expression_vector Binder::bindOrderByExpressions(
expression_vector boundOrderByExpressions;
for (auto& expression : orderByExpressions) {
auto boundExpression = expressionBinder.bindExpression(*expression);
if (boundExpression->dataType.typeID == NODE || boundExpression->dataType.typeID == REL) {
if (boundExpression->dataType.getLogicalTypeID() == LogicalTypeID::NODE ||
boundExpression->dataType.getLogicalTypeID() == LogicalTypeID::REL) {
throw BinderException("Cannot order by " + boundExpression->toString() +
". Order by node or rel is not supported.");
}
Expand All @@ -153,7 +155,8 @@ uint64_t Binder::bindSkipLimitExpression(const ParsedExpression& expression) {
// We currently do not support the number of rows to skip/limit written as an expression (eg.
// SKIP 3 + 2 is not supported).
if (expression.getExpressionType() != LITERAL ||
((LiteralExpression&)(*boundExpression)).getDataType().typeID != INT64) {
((LiteralExpression&)(*boundExpression)).getDataType().getLogicalTypeID() !=
LogicalTypeID::INT64) {
throw BinderException("The number of rows to skip/limit must be a non-negative integer.");
}
return ((LiteralExpression&)(*boundExpression)).value->getValue<int64_t>();
Expand All @@ -169,8 +172,8 @@ void Binder::addExpressionsToScope(const expression_vector& projectionExpression

void Binder::resolveAnyDataTypeWithDefaultType(const expression_vector& expressions) {
for (auto& expression : expressions) {
if (expression->dataType.typeID == ANY) {
ExpressionBinder::implicitCastIfNecessary(expression, STRING);
if (expression->dataType.getLogicalTypeID() == LogicalTypeID::ANY) {
ExpressionBinder::implicitCastIfNecessary(expression, LogicalTypeID::STRING);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/binder/bind/bind_reading_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ std::unique_ptr<BoundReadingClause> Binder::bindMatchClause(const ReadingClause&
std::unique_ptr<BoundReadingClause> Binder::bindUnwindClause(const ReadingClause& readingClause) {
auto& unwindClause = (UnwindClause&)readingClause;
auto boundExpression = expressionBinder.bindExpression(*unwindClause.getExpression());
boundExpression = ExpressionBinder::implicitCastIfNecessary(boundExpression, VAR_LIST);
auto aliasExpression =
createVariable(unwindClause.getAlias(), *boundExpression->dataType.getChildType());
boundExpression =
ExpressionBinder::implicitCastIfNecessary(boundExpression, LogicalTypeID::VAR_LIST);
auto aliasExpression = createVariable(
unwindClause.getAlias(), *common::VarListType::getChildType(&boundExpression->dataType));
return make_unique<BoundUnwindClause>(std::move(boundExpression), std::move(aliasExpression));
}

Expand Down
12 changes: 6 additions & 6 deletions src/binder/bind/bind_updating_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ std::unique_ptr<BoundUpdatingClause> Binder::bindSetClause(const UpdatingClause&
for (auto i = 0u; i < setClause.getNumSetItems(); ++i) {
auto setItem = setClause.getSetItem(i);
auto nodeOrRel = expressionBinder.bindExpression(*setItem.first->getChild(0));
switch (nodeOrRel->dataType.typeID) {
case DataTypeID::NODE: {
switch (nodeOrRel->dataType.getLogicalTypeID()) {
case LogicalTypeID::NODE: {
auto node = static_pointer_cast<NodeExpression>(nodeOrRel);
boundSetClause->addSetNodeProperty(bindSetNodeProperty(node, setItem));
} break;
case DataTypeID::REL: {
case LogicalTypeID::REL: {
auto rel = static_pointer_cast<RelExpression>(nodeOrRel);
boundSetClause->addSetRelProperty(bindSetRelProperty(rel, setItem));
} break;
Expand Down Expand Up @@ -162,12 +162,12 @@ std::unique_ptr<BoundUpdatingClause> Binder::bindDeleteClause(
auto boundDeleteClause = std::make_unique<BoundDeleteClause>();
for (auto i = 0u; i < deleteClause.getNumExpressions(); ++i) {
auto nodeOrRel = expressionBinder.bindExpression(*deleteClause.getExpression(i));
switch (nodeOrRel->dataType.typeID) {
case DataTypeID::NODE: {
switch (nodeOrRel->dataType.getLogicalTypeID()) {
case LogicalTypeID::NODE: {
auto deleteNode = bindDeleteNode(static_pointer_cast<NodeExpression>(nodeOrRel));
boundDeleteClause->addDeleteNode(std::move(deleteNode));
} break;
case DataTypeID::REL: {
case LogicalTypeID::REL: {
auto deleteRel = bindDeleteRel(static_pointer_cast<RelExpression>(nodeOrRel));
boundDeleteClause->addDeleteRel(std::move(deleteRel));
} break;
Expand Down
4 changes: 2 additions & 2 deletions src/binder/bind_expression/bind_boolean_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ std::shared_ptr<Expression> ExpressionBinder::bindBooleanExpression(
ExpressionType expressionType, const expression_vector& children) {
expression_vector childrenAfterCast;
for (auto& child : children) {
childrenAfterCast.push_back(implicitCastIfNecessary(child, BOOL));
childrenAfterCast.push_back(implicitCastIfNecessary(child, LogicalTypeID::BOOL));
}
auto functionName = expressionTypeToString(expressionType);
auto execFunc =
function::VectorBooleanOperations::bindExecFunction(expressionType, childrenAfterCast);
auto selectFunc =
function::VectorBooleanOperations::bindSelectFunction(expressionType, childrenAfterCast);
auto bindData = std::make_unique<function::FunctionBindData>(DataType(BOOL));
auto bindData = std::make_unique<function::FunctionBindData>(LogicalType(LogicalTypeID::BOOL));
auto uniqueExpressionName =
ScalarFunctionExpression::getUniqueName(functionName, childrenAfterCast);
return make_shared<ScalarFunctionExpression>(functionName, expressionType, std::move(bindData),
Expand Down
2 changes: 1 addition & 1 deletion src/binder/bind_expression/bind_case_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ std::shared_ptr<Expression> ExpressionBinder::bindCaseExpression(
for (auto i = 0u; i < parsedCaseExpression.getNumCaseAlternative(); ++i) {
auto caseAlternative = parsedCaseExpression.getCaseAlternative(i);
auto boundWhen = bindExpression(*caseAlternative->whenExpression);
boundWhen = implicitCastIfNecessary(boundWhen, common::BOOL);
boundWhen = implicitCastIfNecessary(boundWhen, common::LogicalTypeID::BOOL);
auto boundThen = bindExpression(*caseAlternative->thenExpression);
boundThen = implicitCastIfNecessary(boundThen, outDataType);
boundCaseExpression->addCaseAlternative(boundWhen, boundThen);
Expand Down
4 changes: 2 additions & 2 deletions src/binder/bind_expression/bind_comparison_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ std::shared_ptr<Expression> ExpressionBinder::bindComparisonExpression(
common::ExpressionType expressionType, const expression_vector& children) {
auto builtInFunctions = binder->catalog.getBuiltInScalarFunctions();
auto functionName = expressionTypeToString(expressionType);
std::vector<common::DataType> childrenTypes;
std::vector<common::LogicalType> childrenTypes;
for (auto& child : children) {
childrenTypes.push_back(child->dataType);
}
Expand All @@ -32,7 +32,7 @@ std::shared_ptr<Expression> ExpressionBinder::bindComparisonExpression(
implicitCastIfNecessary(children[i], function->parameterTypeIDs[i]));
}
auto bindData =
std::make_unique<function::FunctionBindData>(common::DataType(function->returnTypeID));
std::make_unique<function::FunctionBindData>(common::LogicalType(function->returnTypeID));
auto uniqueExpressionName =
ScalarFunctionExpression::getUniqueName(function->name, childrenAfterCast);
return make_shared<ScalarFunctionExpression>(functionName, expressionType, std::move(bindData),
Expand Down
Loading

0 comments on commit 4aae989

Please sign in to comment.